diff options
Diffstat (limited to 'src/dpl/core/src')
-rw-r--r-- | src/dpl/core/src/assert.cpp | 64 | ||||
-rw-r--r-- | src/dpl/core/src/colors.cpp | 74 | ||||
-rw-r--r-- | src/dpl/core/src/errno_string.cpp | 87 | ||||
-rw-r--r-- | src/dpl/core/src/exception.cpp | 34 | ||||
-rw-r--r-- | src/dpl/core/src/string.cpp | 332 |
5 files changed, 284 insertions, 307 deletions
diff --git a/src/dpl/core/src/assert.cpp b/src/dpl/core/src/assert.cpp index e367562..acaa958 100644 --- a/src/dpl/core/src/assert.cpp +++ b/src/dpl/core/src/assert.cpp @@ -28,41 +28,39 @@ namespace CCHECKER { void AssertProc(const char *condition, - const char *file, - int line, - const char *function) + const char *file, + int line, + const char *function) { #define INTERNAL_LOG(message) \ - do \ - { \ - std::ostringstream platformLog; \ - platformLog << message; \ - JournalLog(LOG_DEBUG, \ - platformLog.str().c_str(), \ - __FILE__, __LINE__, __FUNCTION__); \ - } \ - while (0) + do \ + { \ + std::ostringstream platformLog; \ + platformLog << message; \ + JournalLog(LOG_DEBUG, \ + platformLog.str().c_str(), \ + __FILE__, __LINE__, __FUNCTION__); \ + } \ + while (0) + // Try to log failed assertion to log system + Try { + INTERNAL_LOG( + "################################################################################"); + INTERNAL_LOG( + "### cert-checker assertion failed! ###"); + INTERNAL_LOG( + "################################################################################"); + INTERNAL_LOG("### Condition: " << condition); + INTERNAL_LOG("### File: " << file); + INTERNAL_LOG("### Line: " << line); + INTERNAL_LOG("### Function: " << function); + INTERNAL_LOG( + "################################################################################"); + } catch (Exception) { + // Just ignore possible double errors + } - // Try to log failed assertion to log system - Try - { - INTERNAL_LOG( - "################################################################################"); - INTERNAL_LOG( - "### cert-checker assertion failed! ###"); - INTERNAL_LOG( - "################################################################################"); - INTERNAL_LOG("### Condition: " << condition); - INTERNAL_LOG("### File: " << file); - INTERNAL_LOG("### Line: " << line); - INTERNAL_LOG("### Function: " << function); - INTERNAL_LOG( - "################################################################################"); - } catch (Exception) { - // Just ignore possible double errors - } - - // Fail with c-library abort - abort(); + // Fail with c-library abort + abort(); } } // namespace CCHECKER diff --git a/src/dpl/core/src/colors.cpp b/src/dpl/core/src/colors.cpp index 272c06b..1f380b9 100644 --- a/src/dpl/core/src/colors.cpp +++ b/src/dpl/core/src/colors.cpp @@ -27,46 +27,46 @@ namespace CCHECKER { namespace Colors { namespace Text { -const char* BOLD_GREEN_BEGIN = "\033[1;32m"; -const char* BOLD_GREEN_END = "\033[m"; -const char* RED_BEGIN = "\033[0;31m"; -const char* RED_END = "\033[m"; -const char* PURPLE_BEGIN = "\033[0;35m"; -const char* PURPLE_END = "\033[m"; -const char* GREEN_BEGIN = "\033[0;32m"; -const char* GREEN_END = "\033[m"; -const char* CYAN_BEGIN = "\033[0;36m"; -const char* CYAN_END = "\033[m"; -const char* BOLD_RED_BEGIN = "\033[1;31m"; -const char* BOLD_RED_END = "\033[m"; -const char* BOLD_YELLOW_BEGIN = "\033[1;33m"; -const char* BOLD_YELLOW_END = "\033[m"; -const char* BOLD_GOLD_BEGIN = "\033[0;33m"; -const char* BOLD_GOLD_END = "\033[m"; -const char* BOLD_WHITE_BEGIN = "\033[1;37m"; -const char* BOLD_WHITE_END = "\033[m"; -const char* COLOR_END = "\033[m"; +const char *BOLD_GREEN_BEGIN = "\033[1;32m"; +const char *BOLD_GREEN_END = "\033[m"; +const char *RED_BEGIN = "\033[0;31m"; +const char *RED_END = "\033[m"; +const char *PURPLE_BEGIN = "\033[0;35m"; +const char *PURPLE_END = "\033[m"; +const char *GREEN_BEGIN = "\033[0;32m"; +const char *GREEN_END = "\033[m"; +const char *CYAN_BEGIN = "\033[0;36m"; +const char *CYAN_END = "\033[m"; +const char *BOLD_RED_BEGIN = "\033[1;31m"; +const char *BOLD_RED_END = "\033[m"; +const char *BOLD_YELLOW_BEGIN = "\033[1;33m"; +const char *BOLD_YELLOW_END = "\033[m"; +const char *BOLD_GOLD_BEGIN = "\033[0;33m"; +const char *BOLD_GOLD_END = "\033[m"; +const char *BOLD_WHITE_BEGIN = "\033[1;37m"; +const char *BOLD_WHITE_END = "\033[m"; +const char *COLOR_END = "\033[m"; } //namespace Text namespace Html { -const char* BOLD_GREEN_BEGIN = "<font color=\"green\"><b>"; -const char* BOLD_GREEN_END = "</b></font>"; -const char* PURPLE_BEGIN = "<font color=\"purple\"><b>"; -const char* PURPLE_END = "</b></font>"; -const char* RED_BEGIN = "<font color=\"red\"><b>"; -const char* RED_END = "</b></font>"; -const char* GREEN_BEGIN = "<font color=\"green\">"; -const char* GREEN_END = "</font>"; -const char* CYAN_BEGIN = "<font color=\"cyan\">"; -const char* CYAN_END = "</font>"; -const char* BOLD_RED_BEGIN = "<font color=\"red\"><b>"; -const char* BOLD_RED_END = "</b></font>"; -const char* BOLD_YELLOW_BEGIN = "<font color=\"yellow\"><b>"; -const char* BOLD_YELLOW_END = "</b></font>"; -const char* BOLD_GOLD_BEGIN = "<font color=\"gold\"><b>"; -const char* BOLD_GOLD_END = "</b></font>"; -const char* BOLD_WHITE_BEGIN = "<font color=\"white\"><b>"; -const char* BOLD_WHITE_END = "</b></font>"; +const char *BOLD_GREEN_BEGIN = "<font color=\"green\"><b>"; +const char *BOLD_GREEN_END = "</b></font>"; +const char *PURPLE_BEGIN = "<font color=\"purple\"><b>"; +const char *PURPLE_END = "</b></font>"; +const char *RED_BEGIN = "<font color=\"red\"><b>"; +const char *RED_END = "</b></font>"; +const char *GREEN_BEGIN = "<font color=\"green\">"; +const char *GREEN_END = "</font>"; +const char *CYAN_BEGIN = "<font color=\"cyan\">"; +const char *CYAN_END = "</font>"; +const char *BOLD_RED_BEGIN = "<font color=\"red\"><b>"; +const char *BOLD_RED_END = "</b></font>"; +const char *BOLD_YELLOW_BEGIN = "<font color=\"yellow\"><b>"; +const char *BOLD_YELLOW_END = "</b></font>"; +const char *BOLD_GOLD_BEGIN = "<font color=\"gold\"><b>"; +const char *BOLD_GOLD_END = "</b></font>"; +const char *BOLD_WHITE_BEGIN = "<font color=\"white\"><b>"; +const char *BOLD_WHITE_END = "</b></font>"; } //namespace Html } //namespace Colors } //namespace CCHECKER diff --git a/src/dpl/core/src/errno_string.cpp b/src/dpl/core/src/errno_string.cpp index 5b1de37..fa04a45 100644 --- a/src/dpl/core/src/errno_string.cpp +++ b/src/dpl/core/src/errno_string.cpp @@ -34,66 +34,65 @@ #include <cchecker/dpl/scoped_free.h> namespace CCHECKER { -namespace // anonymous -{ +namespace { // anonymous const size_t DEFAULT_ERRNO_STRING_SIZE = 32; } // namespace anonymous std::string GetErrnoString(int error) { - size_t size = DEFAULT_ERRNO_STRING_SIZE; - char *buffer = NULL; - - for (;;) { - // Add one extra characted for end of string null value - char *newBuffer = static_cast<char *>(::realloc(buffer, size + 1)); + size_t size = DEFAULT_ERRNO_STRING_SIZE; + char *buffer = NULL; - if (!newBuffer) { - // Failed to realloc - ::free(buffer); - throw std::bad_alloc(); - } + for (;;) { + // Add one extra characted for end of string null value + char *newBuffer = static_cast<char *>(::realloc(buffer, size + 1)); - // Setup reallocated buffer - buffer = newBuffer; - ::memset(buffer, 0, size + 1); + if (!newBuffer) { + // Failed to realloc + ::free(buffer); + throw std::bad_alloc(); + } - // Try to retrieve error string + // Setup reallocated buffer + buffer = newBuffer; + ::memset(buffer, 0, size + 1); + // Try to retrieve error string #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE - // The XSI-compliant version of strerror_r() is provided if: - int result = ::strerror_r(error, buffer, size); + // The XSI-compliant version of strerror_r() is provided if: + int result = ::strerror_r(error, buffer, size); + + if (result == 0) { + ScopedFree<char> scopedBufferFree(buffer); + return std::string(buffer); + } - if (result == 0) { - ScopedFree<char> scopedBufferFree(buffer); - return std::string(buffer); - } #else - errno = 0; + errno = 0; + // Otherwise, the GNU-specific version is provided. + char *result = ::strerror_r(error, buffer, size); - // Otherwise, the GNU-specific version is provided. - char *result = ::strerror_r(error, buffer, size); + if (result != NULL) { + ScopedFree<char> scopedBufferFree(buffer); + return std::string(result); + } - if (result != NULL) { - ScopedFree<char> scopedBufferFree(buffer); - return std::string(result); - } #endif - // Interpret errors - switch (errno) { - case EINVAL: - // We got an invalid errno value - ::free(buffer); - ThrowMsg(InvalidErrnoValue, "Invalid errno value: " << error); + // Interpret errors + switch (errno) { + case EINVAL: + // We got an invalid errno value + ::free(buffer); + ThrowMsg(InvalidErrnoValue, "Invalid errno value: " << error); - case ERANGE: - // Incease buffer size and retry - size <<= 1; - continue; + case ERANGE: + // Incease buffer size and retry + size <<= 1; + continue; - default: - Assert(0 && "Invalid errno value after call to strerror_r!"); - } - } + default: + Assert(0 && "Invalid errno value after call to strerror_r!"); + } + } } } // namespace CCHECKER diff --git a/src/dpl/core/src/exception.cpp b/src/dpl/core/src/exception.cpp index bee161b..66024fc 100644 --- a/src/dpl/core/src/exception.cpp +++ b/src/dpl/core/src/exception.cpp @@ -26,32 +26,30 @@ #include "common/log.h" namespace CCHECKER { -Exception* Exception::m_lastException = NULL; +Exception *Exception::m_lastException = NULL; unsigned int Exception::m_exceptionCount = 0; void (*Exception::m_terminateHandler)() = NULL; void LogUnhandledException(const std::string &str) { - // Logging to console - printf("%s\n", str.c_str()); - - // Logging to dlog - LogDebug(str); + // Logging to console + printf("%s\n", str.c_str()); + // Logging to dlog + LogDebug(str); } void LogUnhandledException(const std::string &str, - const char *filename, - int line, - const char *function) + const char *filename, + int line, + const char *function) { - // Logging to console - std::ostringstream msg; - msg << "\033[1;5;31m\n=== [" << filename << ":" << line << "] " << - function << " ===\033[m"; - msg << str; - printf("%s\n", msg.str().c_str()); - - // Logging to dlog - LogError(str.c_str() << filename << line << function); + // Logging to console + std::ostringstream msg; + msg << "\033[1;5;31m\n=== [" << filename << ":" << line << "] " << + function << " ===\033[m"; + msg << str; + printf("%s\n", msg.str().c_str()); + // Logging to dlog + LogError(str.c_str() << filename << line << function); } } // namespace CCHECKER diff --git a/src/dpl/core/src/string.cpp b/src/dpl/core/src/string.cpp index 7c8223d..72ade02 100644 --- a/src/dpl/core/src/string.cpp +++ b/src/dpl/core/src/string.cpp @@ -37,215 +37,197 @@ // TODO: Completely move to ICU namespace CCHECKER { -namespace //anonymous -{ -class ASCIIValidator -{ - const std::string& m_TestedString; +namespace { //anonymous +class ASCIIValidator { + const std::string &m_TestedString; - public: - ASCIIValidator(const std::string& aTestedString); +public: + ASCIIValidator(const std::string &aTestedString); - void operator()(signed char aCharacter) const; + void operator()(signed char aCharacter) const; }; -ASCIIValidator::ASCIIValidator(const std::string& aTestedString) : - m_TestedString(aTestedString) +ASCIIValidator::ASCIIValidator(const std::string &aTestedString) : + m_TestedString(aTestedString) {} void ASCIIValidator::operator()(signed char aCharacter) const { - // Check for ASCII data range - if (aCharacter <= 0) { - ThrowMsg( - StringException::InvalidASCIICharacter, - "invalid character code " << static_cast<int>(aCharacter) - << " from string [" << - m_TestedString - << "] passed as ASCII"); - } + // Check for ASCII data range + if (aCharacter <= 0) { + ThrowMsg( + StringException::InvalidASCIICharacter, + "invalid character code " << static_cast<int>(aCharacter) + << " from string [" << + m_TestedString + << "] passed as ASCII"); + } } const iconv_t gc_IconvOperError = reinterpret_cast<iconv_t>(-1); const size_t gc_IconvConvertError = static_cast<size_t>(-1); } // namespace anonymous -String FromUTF8String(const std::string& aIn) +String FromUTF8String(const std::string &aIn) { - if (aIn.empty()) { - return String(); - } - - size_t inbytes = aIn.size(); - - // Default iconv UTF-32 module adds BOM (4 bytes) in from of string - // The worst case is when 8bit UTF-8 char converts to 32bit UTF-32 - // newsize = oldsize * 4 + end + bom - // newsize - bytes for UTF-32 string - // oldsize - letters in UTF-8 string - // end - end character for UTF-32 (\0) - // bom - Unicode header in front of string (0xfeff) - size_t outbytes = sizeof(wchar_t) * (inbytes + 2); - std::vector<wchar_t> output(inbytes + 2, 0); - - size_t outbytesleft = outbytes; - char* inbuf = const_cast<char*>(aIn.c_str()); - - // vector is used to provide buffer for iconv which expects char* buffer - // but during conversion from UTF32 uses internaly wchar_t - char* outbuf = reinterpret_cast<char*>(&output[0]); - - iconv_t iconvHandle = iconv_open("UTF-32", "UTF-8"); - - if (gc_IconvOperError == iconvHandle) { - int error = errno; - - ThrowMsg(StringException::IconvInitErrorUTF8ToUTF32, - "iconv_open failed for " << "UTF-32 <- UTF-8" << - "error: " << GetErrnoString(error)); - } - - size_t iconvRet = iconv(iconvHandle, - &inbuf, - &inbytes, - &outbuf, - &outbytesleft); - - iconv_close(iconvHandle); - - if (gc_IconvConvertError == iconvRet) { - ThrowMsg(StringException::IconvConvertErrorUTF8ToUTF32, - "iconv failed for " << "UTF-32 <- UTF-8" << "error: " - << GetErrnoString()); - } - - // Ignore BOM in front of UTF-32 - return &output[1]; + if (aIn.empty()) { + return String(); + } + + size_t inbytes = aIn.size(); + // Default iconv UTF-32 module adds BOM (4 bytes) in from of string + // The worst case is when 8bit UTF-8 char converts to 32bit UTF-32 + // newsize = oldsize * 4 + end + bom + // newsize - bytes for UTF-32 string + // oldsize - letters in UTF-8 string + // end - end character for UTF-32 (\0) + // bom - Unicode header in front of string (0xfeff) + size_t outbytes = sizeof(wchar_t) * (inbytes + 2); + std::vector<wchar_t> output(inbytes + 2, 0); + size_t outbytesleft = outbytes; + char *inbuf = const_cast<char *>(aIn.c_str()); + // vector is used to provide buffer for iconv which expects char* buffer + // but during conversion from UTF32 uses internaly wchar_t + char *outbuf = reinterpret_cast<char *>(&output[0]); + iconv_t iconvHandle = iconv_open("UTF-32", "UTF-8"); + + if (gc_IconvOperError == iconvHandle) { + int error = errno; + ThrowMsg(StringException::IconvInitErrorUTF8ToUTF32, + "iconv_open failed for " << "UTF-32 <- UTF-8" << + "error: " << GetErrnoString(error)); + } + + size_t iconvRet = iconv(iconvHandle, + &inbuf, + &inbytes, + &outbuf, + &outbytesleft); + iconv_close(iconvHandle); + + if (gc_IconvConvertError == iconvRet) { + ThrowMsg(StringException::IconvConvertErrorUTF8ToUTF32, + "iconv failed for " << "UTF-32 <- UTF-8" << "error: " + << GetErrnoString()); + } + + // Ignore BOM in front of UTF-32 + return &output[1]; } -std::string ToUTF8String(const CCHECKER::String& aIn) +std::string ToUTF8String(const CCHECKER::String &aIn) { - if (aIn.empty()) { - return std::string(); - } - - size_t inbytes = aIn.size() * sizeof(wchar_t); - size_t outbytes = inbytes + sizeof(char); - - // wstring returns wchar_t but iconv expects char* - // iconv internally is processing input as wchar_t - char* inbuf = reinterpret_cast<char*>(const_cast<wchar_t*>(aIn.c_str())); - std::vector<char> output(inbytes, 0); - char* outbuf = &output[0]; - - size_t outbytesleft = outbytes; - - iconv_t iconvHandle = iconv_open("UTF-8", "UTF-32"); - - if (gc_IconvOperError == iconvHandle) { - ThrowMsg(StringException::IconvInitErrorUTF32ToUTF8, - "iconv_open failed for " << "UTF-8 <- UTF-32" - << "error: " << GetErrnoString()); - } - - size_t iconvRet = iconv(iconvHandle, - &inbuf, - &inbytes, - &outbuf, - &outbytesleft); - - iconv_close(iconvHandle); - - if (gc_IconvConvertError == iconvRet) { - ThrowMsg(StringException::IconvConvertErrorUTF32ToUTF8, - "iconv failed for " << "UTF-8 <- UTF-32" - << "error: " << GetErrnoString()); - } - - return &output[0]; + if (aIn.empty()) { + return std::string(); + } + + size_t inbytes = aIn.size() * sizeof(wchar_t); + size_t outbytes = inbytes + sizeof(char); + // wstring returns wchar_t but iconv expects char* + // iconv internally is processing input as wchar_t + char *inbuf = reinterpret_cast<char *>(const_cast<wchar_t *>(aIn.c_str())); + std::vector<char> output(inbytes, 0); + char *outbuf = &output[0]; + size_t outbytesleft = outbytes; + iconv_t iconvHandle = iconv_open("UTF-8", "UTF-32"); + + if (gc_IconvOperError == iconvHandle) { + ThrowMsg(StringException::IconvInitErrorUTF32ToUTF8, + "iconv_open failed for " << "UTF-8 <- UTF-32" + << "error: " << GetErrnoString()); + } + + size_t iconvRet = iconv(iconvHandle, + &inbuf, + &inbytes, + &outbuf, + &outbytesleft); + iconv_close(iconvHandle); + + if (gc_IconvConvertError == iconvRet) { + ThrowMsg(StringException::IconvConvertErrorUTF32ToUTF8, + "iconv failed for " << "UTF-8 <- UTF-32" + << "error: " << GetErrnoString()); + } + + return &output[0]; } -String FromASCIIString(const std::string& aString) +String FromASCIIString(const std::string &aString) { - String output; - - std::for_each(aString.begin(), aString.end(), ASCIIValidator(aString)); - std::copy(aString.begin(), aString.end(), std::back_inserter<String>(output)); - - return output; + String output; + std::for_each(aString.begin(), aString.end(), ASCIIValidator(aString)); + std::copy(aString.begin(), aString.end(), std::back_inserter<String>(output)); + return output; } -String FromUTF32String(const std::wstring& aString) +String FromUTF32String(const std::wstring &aString) { - return String(&aString[0]); + return String(&aString[0]); } static UChar *ConvertToICU(const String &inputString) { - ScopedArray<UChar> outputString; - int32_t size = 0; - int32_t convertedSize = 0; - UErrorCode error = U_ZERO_ERROR; - - // Calculate size of output string - ::u_strFromWCS(NULL, - 0, - &size, - inputString.c_str(), - -1, - &error); - - if (error == U_ZERO_ERROR || - error == U_BUFFER_OVERFLOW_ERROR) - { - // What buffer size is ok ? - LogDebug("ICU: Output buffer size: " << size); - } else { - ThrowMsg(StringException::ICUInvalidCharacterFound, - "ICU: Failed to retrieve output string size. Error: " - << error); - } - - // Allocate proper buffer - outputString.Reset(new UChar[size + 1]); - ::memset(outputString.Get(), 0, sizeof(UChar) * (size + 1)); - - error = U_ZERO_ERROR; - - // Do conversion - ::u_strFromWCS(outputString.Get(), - size + 1, - &convertedSize, - inputString.c_str(), - -1, - &error); - - if (!U_SUCCESS(error)) { - ThrowMsg(StringException::ICUInvalidCharacterFound, - "ICU: Failed to convert string. Error: " << error); - } - - // Done - return outputString.Release(); + ScopedArray<UChar> outputString; + int32_t size = 0; + int32_t convertedSize = 0; + UErrorCode error = U_ZERO_ERROR; + // Calculate size of output string + ::u_strFromWCS(NULL, + 0, + &size, + inputString.c_str(), + -1, + &error); + + if (error == U_ZERO_ERROR || + error == U_BUFFER_OVERFLOW_ERROR) { + // What buffer size is ok ? + LogDebug("ICU: Output buffer size: " << size); + } else { + ThrowMsg(StringException::ICUInvalidCharacterFound, + "ICU: Failed to retrieve output string size. Error: " + << error); + } + + // Allocate proper buffer + outputString.Reset(new UChar[size + 1]); + ::memset(outputString.Get(), 0, sizeof(UChar) * (size + 1)); + error = U_ZERO_ERROR; + // Do conversion + ::u_strFromWCS(outputString.Get(), + size + 1, + &convertedSize, + inputString.c_str(), + -1, + &error); + + if (!U_SUCCESS(error)) { + ThrowMsg(StringException::ICUInvalidCharacterFound, + "ICU: Failed to convert string. Error: " << error); + } + + // Done + return outputString.Release(); } int StringCompare(const String &left, - const String &right, - bool caseInsensitive) + const String &right, + bool caseInsensitive) { - // Convert input strings - ScopedArray<UChar> leftICU(ConvertToICU(left)); - ScopedArray<UChar> rightICU(ConvertToICU(right)); - - if (caseInsensitive) { - return static_cast<int>(u_strcasecmp(leftICU.Get(), rightICU.Get(), 0)); - } else { - return static_cast<int>(u_strcmp(leftICU.Get(), rightICU.Get())); - } + // Convert input strings + ScopedArray<UChar> leftICU(ConvertToICU(left)); + ScopedArray<UChar> rightICU(ConvertToICU(right)); + + if (caseInsensitive) { + return static_cast<int>(u_strcasecmp(leftICU.Get(), rightICU.Get(), 0)); + } else { + return static_cast<int>(u_strcmp(leftICU.Get(), rightICU.Get())); + } } } //namespace CCHECKER -std::ostream& operator<<(std::ostream& aStream, const CCHECKER::String& aString) +std::ostream &operator<<(std::ostream &aStream, const CCHECKER::String &aString) { - return aStream << CCHECKER::ToUTF8String(aString); + return aStream << CCHECKER::ToUTF8String(aString); } |