sal/rtl/random.cxx | 163 +++++++++++++++++------------------------------- sal/rtl/rtl_process.cxx | 13 ++- sal/rtl/uri.cxx | 140 ++++++++++++++++++++++++++++++++--------- sal/rtl/uuid.cxx | 73 +++++++++++---------- 4 files changed, 216 insertions(+), 173 deletions(-)
New commits: commit 28b382b7b0a32417e0aedd4ae415a69e479fe60b Author: Chris Sherlock <[email protected]> Date: Sun Jul 23 12:24:29 2017 +1000 rtl: cleanup equality conditions in uuid.cxx Change-Id: I8918cd97f9ab89f0a2f7f95cd59b706ca5a55e2b diff --git a/sal/rtl/uuid.cxx b/sal/rtl/uuid.cxx index b3197d14a117..9ffbff7ff2cc 100644 --- a/sal/rtl/uuid.cxx +++ b/sal/rtl/uuid.cxx @@ -65,45 +65,49 @@ struct UUID sal_uInt8 node[6]; }; -static void write_v3( sal_uInt8 *pUuid ) +static void write_v3( sal_uInt8 *pUuid ) { UUID uuid; // copy to avoid alignment problems - memcpy( &uuid , pUuid , 16 ); + memcpy(&uuid, pUuid, 16); - SWAP_NETWORK_TO_INT32( uuid.time_low ); - SWAP_NETWORK_TO_INT16( uuid.time_mid ); - SWAP_NETWORK_TO_INT16( uuid.time_hi_and_version ); + SWAP_NETWORK_TO_INT32(uuid.time_low); + SWAP_NETWORK_TO_INT16(uuid.time_mid); + SWAP_NETWORK_TO_INT16(uuid.time_hi_and_version); /* put in the variant and version bits */ - uuid.time_hi_and_version &= 0x0FFF; - uuid.time_hi_and_version |= (3 << 12); + uuid.time_hi_and_version &= 0x0FFF; + uuid.time_hi_and_version |= (3 << 12); uuid.clock_seq_hi_and_reserved &= 0x3F; uuid.clock_seq_hi_and_reserved |= 0x80; - SWAP_INT32_TO_NETWORK( uuid.time_low ); - SWAP_INT16_TO_NETWORK( uuid.time_mid ); - SWAP_INT16_TO_NETWORK( uuid.time_hi_and_version ); + SWAP_INT32_TO_NETWORK(uuid.time_low); + SWAP_INT16_TO_NETWORK(uuid.time_mid); + SWAP_INT16_TO_NETWORK(uuid.time_hi_and_version); - memcpy( pUuid , &uuid , 16 ); + memcpy(pUuid, &uuid, 16); } -extern "C" void SAL_CALL rtl_createUuid( sal_uInt8 *pTargetUUID , - SAL_UNUSED_PARAMETER const sal_uInt8 *, - SAL_UNUSED_PARAMETER sal_Bool ) +extern "C" void SAL_CALL rtl_createUuid(sal_uInt8 *pTargetUUID , + SAL_UNUSED_PARAMETER const sal_uInt8 *, + SAL_UNUSED_PARAMETER sal_Bool) { { osl::MutexGuard g(osl::Mutex::getGlobalMutex()); static rtlRandomPool pool = nullptr; - if (pool == nullptr) { + if (!pool) + { pool = rtl_random_createPool(); - if (pool == nullptr) { + if (!pool) + { abort(); // only possible way to signal failure here (rtl_createUuid // being part of a fixed C API) } } - if (rtl_random_getBytes(pool, pTargetUUID, 16) != rtl_Random_E_None) { + + if (rtl_random_getBytes(pool, pTargetUUID, 16) != rtl_Random_E_None) + { abort(); // only possible way to signal failure here (rtl_createUuid // being part of a fixed C API) @@ -116,36 +120,36 @@ extern "C" void SAL_CALL rtl_createUuid( sal_uInt8 *pTargetUUID , pTargetUUID[8] |= 0x80; } -extern "C" void SAL_CALL rtl_createNamedUuid( sal_uInt8 *pTargetUUID, - const sal_uInt8 *pNameSpaceUUID, - const rtl_String *pName ) +extern "C" void SAL_CALL rtl_createNamedUuid(sal_uInt8 *pTargetUUID, + const sal_uInt8 *pNameSpaceUUID, + const rtl_String *pName ) { - rtlDigest digest = rtl_digest_createMD5 (); + rtlDigest digest = rtl_digest_createMD5(); - rtl_digest_updateMD5( digest, pNameSpaceUUID , 16 ); - rtl_digest_updateMD5( digest, pName->buffer , pName->length ); + rtl_digest_updateMD5(digest, pNameSpaceUUID, 16); + rtl_digest_updateMD5(digest, pName->buffer, pName->length); - rtl_digest_getMD5( digest, pTargetUUID , 16 ); - rtl_digest_destroyMD5 (digest); + rtl_digest_getMD5(digest, pTargetUUID, 16); + rtl_digest_destroyMD5(digest); write_v3(pTargetUUID); } -extern "C" sal_Int32 SAL_CALL rtl_compareUuid( const sal_uInt8 *pUUID1 , const sal_uInt8 *pUUID2 ) +extern "C" sal_Int32 SAL_CALL rtl_compareUuid(const sal_uInt8 *pUUID1, const sal_uInt8 *pUUID2) { int i; UUID u1; UUID u2; - memcpy( &u1 , pUUID1 , 16 ); - memcpy( &u2 , pUUID2 , 16 ); + memcpy(&u1, pUUID1, 16 ); + memcpy(&u2, pUUID2, 16 ); - SWAP_NETWORK_TO_INT32( u1.time_low ); - SWAP_NETWORK_TO_INT16( u1.time_mid ); - SWAP_NETWORK_TO_INT16( u1.time_hi_and_version ); + SWAP_NETWORK_TO_INT32(u1.time_low); + SWAP_NETWORK_TO_INT16(u1.time_mid); + SWAP_NETWORK_TO_INT16(u1.time_hi_and_version); - SWAP_NETWORK_TO_INT32( u2.time_low ); - SWAP_NETWORK_TO_INT16( u2.time_mid ); - SWAP_NETWORK_TO_INT16( u2.time_hi_and_version ); + SWAP_NETWORK_TO_INT32(u2.time_low); + SWAP_NETWORK_TO_INT16(u2.time_mid); + SWAP_NETWORK_TO_INT16(u2.time_hi_and_version); #define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1; CHECK(u1.time_low, u2.time_low); @@ -161,7 +165,6 @@ extern "C" sal_Int32 SAL_CALL rtl_compareUuid( const sal_uInt8 *pUUID1 , const s return 1; } return 0; - } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ commit 80cdd90793c6b8b027f7f5366b03041b1ae5e0e5 Author: Chris Sherlock <[email protected]> Date: Sun Jul 23 12:20:38 2017 +1000 rtl: cleanup uri.cxx Change-Id: Ic9ddcaa7c699830216e157bd9dfc09d30b50b3e6 diff --git a/sal/rtl/uri.cxx b/sal/rtl/uri.cxx index b886e2153795..257a0a27abac 100644 --- a/sal/rtl/uri.cxx +++ b/sal/rtl/uri.cxx @@ -67,14 +67,12 @@ enum EscapeType EscapeOctet }; -/* Read any of the following: +/** Read any of the following: - - sequence of escape sequences representing character from eCharset, - translated to single UCS4 character; or - - - pair of UTF-16 surrogates, translated to single UCS4 character; or - - _ single UTF-16 character, extended to UCS4 character. + @li sequence of escape sequences representing character from eCharset, + translated to single UCS4 character; or + @li pair of UTF-16 surrogates, translated to single UCS4 character; or + @li single UTF-16 character, extended to UCS4 character. */ sal_uInt32 readUcs4(sal_Unicode const ** pBegin, sal_Unicode const * pEnd, bool bEncoded, rtl_TextEncoding eCharset, @@ -90,7 +88,9 @@ sal_uInt32 readUcs4(sal_Unicode const ** pBegin, sal_Unicode const * pEnd, *pBegin += 2; nChar = static_cast< sal_uInt32 >(nWeight1 << 4 | nWeight2); if (nChar <= 0x7F) + { *pType = EscapeChar; + } else if (eCharset == RTL_TEXTENCODING_UTF8) { if (nChar >= 0xC0 && nChar <= 0xF4) @@ -116,8 +116,10 @@ sal_uInt32 readUcs4(sal_Unicode const ** pBegin, sal_Unicode const * pEnd, nShift = 12; nMin = 0x10000; } + sal_Unicode const * p = *pBegin; bool bUTF8 = true; + for (; nShift >= 0; nShift -= 6) { if (pEnd - p < 3 || p[0] != cEscapePrefix @@ -149,6 +151,7 @@ sal_uInt32 readUcs4(sal_Unicode const ** pBegin, sal_Unicode const * pEnd, rtl_TextToUnicodeConverter aConverter = rtl_createTextToUnicodeConverter(eCharset); sal_Unicode const * p = *pBegin; + for (;;) { sal_Unicode aDst[2]; @@ -161,17 +164,21 @@ sal_uInt32 readUcs4(sal_Unicode const ** pBegin, sal_Unicode const * pEnd, | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR), &nInfo, &nConverted); + if (nInfo == 0) { assert( nConverted == sal::static_int_cast< sal_uInt32 >( aBuf.getLength())); + rtl_destroyTextToUnicodeConverter(aConverter); *pBegin = p; *pType = EscapeChar; + assert( nDstSize == 1 || (nDstSize == 2 && rtl::isHighSurrogate(aDst[0]) && rtl::isLowSurrogate(aDst[1]))); + return nDstSize == 1 ? aDst[0] : rtl::combineSurrogates(aDst[0], aDst[1]); } @@ -211,10 +218,12 @@ sal_uInt32 readUcs4(sal_Unicode const ** pBegin, sal_Unicode const * pEnd, void writeUcs4(rtl_uString ** pBuffer, sal_Int32 * pCapacity, sal_uInt32 nUtf32) { assert(rtl::isUnicodeCodePoint(nUtf32)); - if (nUtf32 <= 0xFFFF) { - writeUnicode( - pBuffer, pCapacity, static_cast< sal_Unicode >(nUtf32)); - } else { + if (nUtf32 <= 0xFFFF) + { + writeUnicode(pBuffer, pCapacity, static_cast< sal_Unicode >(nUtf32)); + } + else + { nUtf32 -= 0x10000; writeUnicode( pBuffer, pCapacity, @@ -243,9 +252,12 @@ bool writeEscapeChar(rtl_uString ** pBuffer, sal_Int32 * pCapacity, sal_uInt32 nUtf32, rtl_TextEncoding eCharset, bool bStrict) { assert(rtl::isUnicodeCodePoint(nUtf32)); - if (eCharset == RTL_TEXTENCODING_UTF8) { + if (eCharset == RTL_TEXTENCODING_UTF8) + { if (nUtf32 < 0x80) + { writeEscapeOctet(pBuffer, pCapacity, nUtf32); + } else if (nUtf32 < 0x800) { writeEscapeOctet(pBuffer, pCapacity, nUtf32 >> 6 | 0xC0); @@ -264,7 +276,9 @@ bool writeEscapeChar(rtl_uString ** pBuffer, sal_Int32 * pCapacity, writeEscapeOctet(pBuffer, pCapacity, (nUtf32 >> 6 & 0x3F) | 0x80); writeEscapeOctet(pBuffer, pCapacity, (nUtf32 & 0x3F) | 0x80); } - } else { + } + else + { rtl_UnicodeToTextConverter aConverter = rtl_createUnicodeToTextConverter(eCharset); sal_Unicode aSrc[2]; @@ -282,6 +296,7 @@ bool writeEscapeChar(rtl_uString ** pBuffer, sal_Int32 * pCapacity, ((nUtf32 - 0x10000) & 0x3FF) | 0xDC00); nSrcSize = 2; } + sal_Char aDst[32]; // FIXME random value sal_uInt32 nInfo; sal_Size nConverted; @@ -293,16 +308,23 @@ bool writeEscapeChar(rtl_uString ** pBuffer, sal_Int32 * pCapacity, &nInfo, &nConverted); assert((nInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL) == 0); rtl_destroyUnicodeToTextConverter(aConverter); - if (nInfo == 0) { + + if (nInfo == 0) + { assert(nConverted == nSrcSize); // bad rtl_convertUnicodeToText + for (sal_Size i = 0; i < nDstSize; ++i) + { writeEscapeOctet(pBuffer, pCapacity, static_cast< unsigned char >(aDst[i])); // FIXME all octets are escaped, even if there is no need - } else { - if (bStrict) { - return false; } + } + else + { + if (bStrict) + return false; + writeUcs4(pBuffer, pCapacity, nUtf32); } } @@ -355,6 +377,7 @@ void parseUriRef(rtl_uString const * pUriRef, Components * pComponents) pPos = p; break; } + if (!rtl::isAsciiAlphanumeric(*p) && *p != '+' && *p != '-' && *p != '.') { @@ -368,20 +391,29 @@ void parseUriRef(rtl_uString const * pUriRef, Components * pComponents) pComponents->aAuthority.pBegin = pPos; pPos += 2; while (pPos != pEnd && *pPos != '/' && *pPos != '?' && *pPos != '#') + { ++pPos; + } + pComponents->aAuthority.pEnd = pPos; } pComponents->aPath.pBegin = pPos; while (pPos != pEnd && *pPos != '?' && * pPos != '#') + { ++pPos; + } + pComponents->aPath.pEnd = pPos; if (pPos != pEnd && *pPos == '?') { pComponents->aQuery.pBegin = pPos++; while (pPos != pEnd && * pPos != '#') + { ++pPos; + } + pComponents->aQuery.pEnd = pPos; } @@ -397,13 +429,17 @@ void appendPath( rtl::OUStringBuffer & buffer, sal_Int32 bufferStart, bool precedingSlash, sal_Unicode const * pathBegin, sal_Unicode const * pathEnd) { - while (precedingSlash || pathBegin != pathEnd) { + while (precedingSlash || pathBegin != pathEnd) + { sal_Unicode const * p = pathBegin; - while (p != pathEnd && *p != '/') { + while (p != pathEnd && *p != '/') + { ++p; } + std::size_t n = p - pathBegin; - if (n == 1 && pathBegin[0] == '.') { + if (n == 1 && pathBegin[0] == '.') + { // input begins with "." -> remove from input (and done): // i.e., !precedingSlash -> !precedingSlash // input begins with "./" -> remove from input: @@ -413,7 +449,9 @@ void appendPath( // i.e., precedingSlash -> precedingSlash // input begins with "/./" -> replace with "/" in input: // i.e., precedingSlash -> precedingSlash - } else if (n == 2 && pathBegin[0] == '.' && pathBegin[1] == '.') { + } + else if (n == 2 && pathBegin[0] == '.' && pathBegin[1] == '.') + { // input begins with ".." -> remove from input (and done): // i.e., !precedingSlash -> !precedingSlash // input begins with "../" -> remove from input @@ -424,7 +462,8 @@ void appendPath( // input begins with "/../" -> replace with "/" in input, and shrink // output: // i.e., precedingSlash -> precedingSlash - if (precedingSlash) { + if (precedingSlash) + { buffer.truncate( bufferStart + std::max<sal_Int32>( @@ -433,10 +472,12 @@ void appendPath( buffer.getLength() - bufferStart, '/'), 0)); } - } else { - if (precedingSlash) { + } + else + { + if (precedingSlash) buffer.append('/'); - } + buffer.append(pathBegin, n); precedingSlash = p != pathEnd; } @@ -578,6 +619,7 @@ sal_Bool const * SAL_CALL rtl_getUriCharClass(rtl_UriCharClass eCharClass) true, true, true, true, true, true, true, true, // hijklmno true, true, true, true, true, true, true, true, // pqrstuvw true, true, true, false, false, false, true, false}}; // xyz{|}~ + assert( (eCharClass >= 0 && (sal::static_int_cast< std::size_t >(eCharClass) @@ -596,6 +638,7 @@ void SAL_CALL rtl_uriEncode(rtl_uString * pText, sal_Bool const * pCharClass, sal_Unicode const * pEnd = p + pText->length; sal_Int32 nCapacity = pText->length; rtl_uString_new_WithLength(pResult, nCapacity); + while (p < pEnd) { EscapeType eType; @@ -605,12 +648,15 @@ void SAL_CALL rtl_uriEncode(rtl_uString * pText, sal_Bool const * pCharClass, || eMechanism == rtl_UriEncodeCheckEscapes || eMechanism == rtl_UriEncodeStrictKeepEscapes), eCharset, &eType); + switch (eType) { case EscapeNo: if (isValid(pCharClass, nUtf32)) // implies nUtf32 <= 0x7F + { writeUnicode(pResult, &nCapacity, static_cast< sal_Unicode >(nUtf32)); + } else if (!writeEscapeChar( pResult, &nCapacity, nUtf32, eCharset, (eMechanism == rtl_UriEncodeStrict @@ -624,8 +670,10 @@ void SAL_CALL rtl_uriEncode(rtl_uString * pText, sal_Bool const * pCharClass, case EscapeChar: if (eMechanism == rtl_UriEncodeCheckEscapes && isValid(pCharClass, nUtf32)) // implies nUtf32 <= 0x7F + { writeUnicode(pResult, &nCapacity, static_cast< sal_Unicode >(nUtf32)); + } else if (!writeEscapeChar( pResult, &nCapacity, nUtf32, eCharset, (eMechanism == rtl_UriEncodeStrict @@ -641,7 +689,7 @@ void SAL_CALL rtl_uriEncode(rtl_uString * pText, sal_Bool const * pCharClass, break; } } - *pResult = rtl_uStringBuffer_makeStringAndClear( pResult, &nCapacity ); + *pResult = rtl_uStringBuffer_makeStringAndClear(pResult, &nCapacity); } void SAL_CALL rtl_uriDecode(rtl_uString * pText, @@ -664,6 +712,7 @@ void SAL_CALL rtl_uriDecode(rtl_uString * pText, sal_Unicode const * pEnd = p + pText->length; sal_Int32 nCapacity = pText->length; rtl_uString_new_WithLength(pResult, nCapacity); + while (p < pEnd) { EscapeType eType; @@ -677,12 +726,14 @@ void SAL_CALL rtl_uriDecode(rtl_uString * pText, break; } SAL_FALLTHROUGH; + case EscapeNo: writeUcs4(pResult, &nCapacity, nUtf32); break; case EscapeOctet: - if (eMechanism == rtl_UriDecodeStrict) { + if (eMechanism == rtl_UriDecodeStrict) + { rtl_uString_new(pResult); return; } @@ -690,6 +741,7 @@ void SAL_CALL rtl_uriDecode(rtl_uString * pText, break; } } + *pResult = rtl_uStringBuffer_makeStringAndClear( pResult, &nCapacity ); } break; @@ -707,19 +759,27 @@ sal_Bool SAL_CALL rtl_uriConvertRelToAbs(rtl_uString * pBaseUriRef, rtl::OUStringBuffer aBuffer; Components aRelComponents; parseUriRef(pRelUriRef, &aRelComponents); + if (aRelComponents.aScheme.isPresent()) { aBuffer.append(aRelComponents.aScheme.pBegin, aRelComponents.aScheme.getLength()); + if (aRelComponents.aAuthority.isPresent()) + { aBuffer.append(aRelComponents.aAuthority.pBegin, aRelComponents.aAuthority.getLength()); + } + appendPath( aBuffer, aBuffer.getLength(), false, aRelComponents.aPath.pBegin, aRelComponents.aPath.pEnd); + if (aRelComponents.aQuery.isPresent()) + { aBuffer.append(aRelComponents.aQuery.pBegin, aRelComponents.aQuery.getLength()); + } } else { @@ -735,6 +795,7 @@ sal_Bool SAL_CALL rtl_uriConvertRelToAbs(rtl_uString * pBaseUriRef, .pData)); return false; } + aBuffer.append(aBaseComponents.aScheme.pBegin, aBaseComponents.aScheme.getLength()); if (aRelComponents.aAuthority.isPresent()) @@ -744,63 +805,86 @@ sal_Bool SAL_CALL rtl_uriConvertRelToAbs(rtl_uString * pBaseUriRef, appendPath( aBuffer, aBuffer.getLength(), false, aRelComponents.aPath.pBegin, aRelComponents.aPath.pEnd); + if (aRelComponents.aQuery.isPresent()) + { aBuffer.append(aRelComponents.aQuery.pBegin, aRelComponents.aQuery.getLength()); + } } else { if (aBaseComponents.aAuthority.isPresent()) + { aBuffer.append(aBaseComponents.aAuthority.pBegin, aBaseComponents.aAuthority.getLength()); + } + if (aRelComponents.aPath.pBegin == aRelComponents.aPath.pEnd) { aBuffer.append(aBaseComponents.aPath.pBegin, aBaseComponents.aPath.getLength()); if (aRelComponents.aQuery.isPresent()) + { aBuffer.append(aRelComponents.aQuery.pBegin, aRelComponents.aQuery.getLength()); + } else if (aBaseComponents.aQuery.isPresent()) + { aBuffer.append(aBaseComponents.aQuery.pBegin, aBaseComponents.aQuery.getLength()); + } } else { if (aRelComponents.aPath.pBegin != aRelComponents.aPath.pEnd && *aRelComponents.aPath.pBegin == '/') + { appendPath( aBuffer, aBuffer.getLength(), false, aRelComponents.aPath.pBegin, aRelComponents.aPath.pEnd); + } else if (aBaseComponents.aAuthority.isPresent() && aBaseComponents.aPath.pBegin == aBaseComponents.aPath.pEnd) + { appendPath( aBuffer, aBuffer.getLength(), true, aRelComponents.aPath.pBegin, aRelComponents.aPath.pEnd); + } else { sal_Int32 n = aBuffer.getLength(); sal_Int32 i = rtl_ustr_lastIndexOfChar_WithLength( aBaseComponents.aPath.pBegin, aBaseComponents.aPath.getLength(), '/'); - if (i >= 0) { + + if (i >= 0) + { appendPath( aBuffer, n, false, aBaseComponents.aPath.pBegin, aBaseComponents.aPath.pBegin + i); } + appendPath( aBuffer, n, i >= 0, aRelComponents.aPath.pBegin, aRelComponents.aPath.pEnd); } + if (aRelComponents.aQuery.isPresent()) + { aBuffer.append(aRelComponents.aQuery.pBegin, aRelComponents.aQuery.getLength()); + } } } } if (aRelComponents.aFragment.isPresent()) + { aBuffer.append(aRelComponents.aFragment.pBegin, aRelComponents.aFragment.getLength()); + } + rtl_uString_assign(pResult, aBuffer.makeStringAndClear().pData); return true; } commit 6128d6277142c703ec9c938c22f395de6b66f3f7 Author: Chris Sherlock <[email protected]> Date: Sun Jul 23 12:06:10 2017 +1000 rtl: cleanup rtl_process.cxx Change-Id: I8640da0d5f44d69b9b628ac2076aec50b8e62ceb diff --git a/sal/rtl/rtl_process.cxx b/sal/rtl/rtl_process.cxx index a391dc8e7453..4904ff224a7b 100644 --- a/sal/rtl/rtl_process.cxx +++ b/sal/rtl/rtl_process.cxx @@ -26,9 +26,11 @@ #include "rtl/uuid.h" #include "sal/types.h" -namespace { +namespace +{ -class Id { +class Id +{ public: Id() { rtl_createUuid(uuid_, nullptr, false); } @@ -36,7 +38,7 @@ public: Id& operator=(const Id&) = delete; void copy(sal_uInt8 * target) const - { std::memcpy(target, uuid_, UUID_SIZE); } + { std::memcpy(target, uuid_, UUID_SIZE); } private: enum { UUID_SIZE = 16 }; @@ -46,9 +48,10 @@ private: struct theId: public rtl::Static< Id, theId > {}; -} +} // end namespace -void rtl_getGlobalProcessId(sal_uInt8 * pTargetUUID) { +void rtl_getGlobalProcessId(sal_uInt8 * pTargetUUID) +{ theId::get().copy(pTargetUUID); } commit 16204ebb6862e3ada703001c048089ca931060d1 Author: Chris Sherlock <[email protected]> Date: Sun Jul 23 12:04:12 2017 +1000 rtl: remove comments, cleanup equality conditions in random.cxx Change-Id: I915aafe5a0df39b19e1f5bdc701cb9175dabb5ed diff --git a/sal/rtl/random.cxx b/sal/rtl/random.cxx index 360ade3b7873..96978ffdfbc7 100644 --- a/sal/rtl/random.cxx +++ b/sal/rtl/random.cxx @@ -25,11 +25,7 @@ #include <rtl/digest.h> #include <rtl/random.h> #include "oslrandom.h" -/*======================================================================== - * - * rtlRandom internals. - * - *======================================================================*/ + #define RTL_RANDOM_RNG_1(a) ((a) * 16807L) #define RTL_RANDOM_RNG_2(a) ((a) * 65539L) @@ -45,8 +41,6 @@ if ((z) < 0) (z) += 30307L; \ } -/** RandomData_Impl. - */ struct RandomData_Impl { sal_Int16 m_nX; @@ -54,12 +48,8 @@ struct RandomData_Impl sal_Int16 m_nZ; }; -/** data. - */ static double data (RandomData_Impl *pImpl); -/** RandomPool_Impl. - */ #define RTL_RANDOM_DIGEST rtl_Digest_AlgorithmMD5 #define RTL_RANDOM_SIZE_DIGEST RTL_DIGEST_LENGTH_MD5 #define RTL_RANDOM_SIZE_POOL 1023 @@ -74,25 +64,15 @@ struct RandomPool_Impl sal_uInt32 m_nCount; }; -/** initPool. - */ -static bool initPool ( - RandomPool_Impl *pImpl); +static bool initPool(RandomPool_Impl *pImpl); -/** seedPool. - */ -static void seedPool ( +static void seedPool( RandomPool_Impl *pImpl, const sal_uInt8 *pBuffer, sal_Size nBufLen); -/** readPool. - */ -static void readPool ( +static void readPool( RandomPool_Impl *pImpl, sal_uInt8 *pBuffer, sal_Size nBufLen); -/* - * data. - */ -static double data (RandomData_Impl *pImpl) +static double data(RandomData_Impl *pImpl) { double random; @@ -105,18 +85,15 @@ static double data (RandomData_Impl *pImpl) return random; } -/* - * initPool. - */ -static bool initPool (RandomPool_Impl *pImpl) +static bool initPool(RandomPool_Impl *pImpl) { - pImpl->m_hDigest = rtl_digest_create (RTL_RANDOM_DIGEST); + pImpl->m_hDigest = rtl_digest_create(RTL_RANDOM_DIGEST); if (pImpl->m_hDigest) { oslThreadIdentifier tid; - TimeValue tv; - RandomData_Impl rd; - double seed; + TimeValue tv; + RandomData_Impl rd; + double seed; /* The use of uninitialized stack variables as a way to * enhance the entropy of the random pool triggers @@ -131,36 +108,33 @@ static bool initPool (RandomPool_Impl *pImpl) tid = osl::Thread::getCurrentIdentifier(); tid = RTL_RANDOM_RNG_2(RTL_RANDOM_RNG_1(tid)); - seedPool (pImpl, reinterpret_cast<sal_uInt8*>(&tid), sizeof(tid)); + seedPool (pImpl, reinterpret_cast< sal_uInt8* >(&tid), sizeof(tid)); osl_getSystemTime (&tv); tv.Seconds = RTL_RANDOM_RNG_2(tv.Seconds); tv.Nanosec = RTL_RANDOM_RNG_2(tv.Nanosec); - seedPool (pImpl, reinterpret_cast<sal_uInt8*>(&tv), sizeof(tv)); + seedPool (pImpl, reinterpret_cast< sal_uInt8* >(&tv), sizeof(tv)); - rd.m_nX = (sal_Int16)(((tid >> 1) << 1) + 1); + rd.m_nX = (sal_Int16)(((tid >> 1) << 1) + 1); rd.m_nY = (sal_Int16)(((tv.Seconds >> 1) << 1) + 1); rd.m_nZ = (sal_Int16)(((tv.Nanosec >> 1) << 1) + 1); - seedPool (pImpl, reinterpret_cast<sal_uInt8*>(&rd), sizeof(rd)); + seedPool (pImpl, reinterpret_cast< sal_uInt8* >(&rd), sizeof(rd)); while (pImpl->m_nData < RTL_RANDOM_SIZE_POOL) { seed = data (&rd); - seedPool (pImpl, reinterpret_cast<sal_uInt8*>(&seed), sizeof(seed)); + seedPool (pImpl, reinterpret_cast< sal_uInt8* >(&seed), sizeof(seed)); } return true; } return false; } -/* - * seedPool. - */ -static void seedPool ( +static void seedPool( RandomPool_Impl *pImpl, const sal_uInt8 *pBuffer, sal_Size nBufLen) { sal_Size i; - sal_sSize j, k; + sal_sSize j, k; for (i = 0; i < nBufLen; i += RTL_RANDOM_SIZE_DIGEST) { @@ -168,27 +142,27 @@ static void seedPool ( if (j > RTL_RANDOM_SIZE_DIGEST) j = RTL_RANDOM_SIZE_DIGEST; - rtl_digest_update ( + rtl_digest_update( pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST); k = (pImpl->m_nIndex + j) - RTL_RANDOM_SIZE_POOL; if (k > 0) { - rtl_digest_update ( + rtl_digest_update( pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j - k); - rtl_digest_update ( + rtl_digest_update( pImpl->m_hDigest, &(pImpl->m_pData[0]), k); } else { - rtl_digest_update ( + rtl_digest_update( pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j); } - rtl_digest_update (pImpl->m_hDigest, pBuffer, j); + rtl_digest_update(pImpl->m_hDigest, pBuffer, j); pBuffer += j; - rtl_digest_get ( + rtl_digest_get( pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST); for (k = 0; k < j; k++) { @@ -205,9 +179,6 @@ static void seedPool ( pImpl->m_nData = pImpl->m_nIndex; } -/* - * readPool. - */ static void readPool ( RandomPool_Impl *pImpl, sal_uInt8 *pBuffer, sal_Size nBufLen) { @@ -220,7 +191,7 @@ static void readPool ( j = RTL_RANDOM_SIZE_DIGEST/2; nBufLen -= j; - rtl_digest_update ( + rtl_digest_update( pImpl->m_hDigest, &(pImpl->m_pDigest[RTL_RANDOM_SIZE_DIGEST/2]), RTL_RANDOM_SIZE_DIGEST/2); @@ -228,121 +199,103 @@ static void readPool ( k = (pImpl->m_nIndex + j) - pImpl->m_nData; if (k > 0) { - rtl_digest_update ( + rtl_digest_update( pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j - k); - rtl_digest_update ( + rtl_digest_update( pImpl->m_hDigest, &(pImpl->m_pData[0]), k); } else { - rtl_digest_update ( + rtl_digest_update( pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j); } - rtl_digest_get ( + rtl_digest_get( pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST); for (k = 0; k < j; k++) { - if (pImpl->m_nIndex >= pImpl->m_nData) pImpl->m_nIndex = 0; + if (pImpl->m_nIndex >= pImpl->m_nData) + pImpl->m_nIndex = 0; + pImpl->m_pData[pImpl->m_nIndex++] ^= pImpl->m_pDigest[k]; *pBuffer++ = pImpl->m_pDigest[k + RTL_RANDOM_SIZE_DIGEST/2]; } } pImpl->m_nCount++; - rtl_digest_update ( + rtl_digest_update( pImpl->m_hDigest, &(pImpl->m_nCount), sizeof(pImpl->m_nCount)); - rtl_digest_update ( + rtl_digest_update( pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST); - rtl_digest_get ( + rtl_digest_get( pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST); } -/*======================================================================== - * - * rtlRandom implementation. - * - *======================================================================*/ -/* - * rtl_random_createPool. - */ rtlRandomPool SAL_CALL rtl_random_createPool() SAL_THROW_EXTERN_C() { RandomPool_Impl *pImpl = nullptr; char sanity[4]; /* try to get system random number, if it fail fall back on own pool */ - pImpl = static_cast<RandomPool_Impl*>(rtl_allocateZeroMemory (sizeof(RandomPool_Impl))); + pImpl = static_cast< RandomPool_Impl* >(rtl_allocateZeroMemory(sizeof(RandomPool_Impl))); if (pImpl) { - if(!osl_get_system_random_data(sanity, 4)) + if (!osl_get_system_random_data(sanity, 4)) { - if (!initPool (pImpl)) + if (!initPool(pImpl)) { - rtl_freeZeroMemory (pImpl, sizeof(RandomPool_Impl)); + rtl_freeZeroMemory(pImpl, sizeof(RandomPool_Impl)); pImpl = nullptr; } } } - return static_cast<rtlRandomPool>(pImpl); + return static_cast< rtlRandomPool >(pImpl); } -/* - * rtl_random_destroyPool. - */ -void SAL_CALL rtl_random_destroyPool (rtlRandomPool Pool) SAL_THROW_EXTERN_C() +void SAL_CALL rtl_random_destroyPool(rtlRandomPool Pool) SAL_THROW_EXTERN_C() { - RandomPool_Impl *pImpl = static_cast<RandomPool_Impl *>(Pool); + RandomPool_Impl *pImpl = static_cast< RandomPool_Impl* >(Pool); if (pImpl) { - if(pImpl->m_hDigest) - { - rtl_digest_destroy (pImpl->m_hDigest); - } - rtl_freeZeroMemory (pImpl, sizeof (RandomPool_Impl)); + if (pImpl->m_hDigest) + rtl_digest_destroy(pImpl->m_hDigest); + + rtl_freeZeroMemory (pImpl, sizeof(RandomPool_Impl)); } } -/* - * rtl_random_addBytes. - */ -rtlRandomError SAL_CALL rtl_random_addBytes ( +rtlRandomError SAL_CALL rtl_random_addBytes( rtlRandomPool Pool, const void *Buffer, sal_Size Bytes) SAL_THROW_EXTERN_C() { - RandomPool_Impl *pImpl = static_cast<RandomPool_Impl *>(Pool); - const sal_uInt8 *pBuffer = static_cast<const sal_uInt8 *>(Buffer); + RandomPool_Impl *pImpl = static_cast< RandomPool_Impl* >(Pool); + const sal_uInt8 *pBuffer = static_cast< const sal_uInt8* >(Buffer); - if ((pImpl == nullptr) || (pBuffer == nullptr)) + if (!pImpl || !pBuffer) return rtl_Random_E_Argument; - if(pImpl->m_hDigest) - { + + if (pImpl->m_hDigest) seedPool (pImpl, pBuffer, Bytes); - } + return rtl_Random_E_None; } -/* - * rtl_random_getBytes. - */ rtlRandomError SAL_CALL rtl_random_getBytes ( rtlRandomPool Pool, void *Buffer, sal_Size Bytes) SAL_THROW_EXTERN_C() { - RandomPool_Impl *pImpl = static_cast<RandomPool_Impl *>(Pool); - sal_uInt8 *pBuffer = static_cast<sal_uInt8 *>(Buffer); + RandomPool_Impl *pImpl = static_cast< RandomPool_Impl* >(Pool); + sal_uInt8 *pBuffer = static_cast< sal_uInt8* >(Buffer); - if ((pImpl == nullptr) || (pBuffer == nullptr)) + if (!pImpl || !pBuffer) return rtl_Random_E_Argument; - if(pImpl->m_hDigest || !osl_get_system_random_data(static_cast<char*>(Buffer), Bytes)) + if (pImpl->m_hDigest || !osl_get_system_random_data(static_cast< char* >(Buffer), Bytes)) { - if(!pImpl->m_hDigest) + if (!pImpl->m_hDigest) { if (!initPool (pImpl)) - { return rtl_Random_E_Unknown; - } } - readPool (pImpl, pBuffer, Bytes); + readPool(pImpl, pBuffer, Bytes); } return rtl_Random_E_None; } _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
