diff -r ae54820ef82c -r 245df5276b97 secureswitools/swisistools/source/common/util.cpp --- a/secureswitools/swisistools/source/common/util.cpp Tue Jul 06 14:23:31 2010 +0300 +++ b/secureswitools/swisistools/source/common/util.cpp Wed Aug 18 09:55:45 2010 +0300 @@ -1,5 +1,5 @@ /* -* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). +* Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). * All rights reserved. * This component and the accompanying materials are made available * under the terms of the License "Eclipse Public License v1.0" @@ -24,7 +24,6 @@ #include "util.h" #include "symbiantypes.h" -#include #include #include #include @@ -33,6 +32,13 @@ #include #include +#ifdef _WIN32 +#include +#endif // _WIN32 + + +#include "utf8_wrapper.h" + static const TUint32 CrcTab32[256] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, @@ -100,38 +106,94 @@ 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; + +namespace Util +{ +/** + * Converts wide char (unicode) string to multibyte string + * This interface is provided so that we can have different implementation + * in windows and linux machine. + * @param aSource string to be converted + * @param aSourceLen Source len. If this is -1 then it will calculate the length of the source. + * @param aTarget target location. + * @param aTargetLen Space in the target location. + * @param aCodePage Code page number (currently supported in windows only) + * @return Number of bytes that make up the converted part of multibyte sequence. + * If aTarget is NULL then the function will return the size needed to store + * the complete conversion of the source string. + */ +int ConvertWideCharToMultiByte(const wchar_t* aSource, int aSourceLen, char* aTarget, int aTargetLen, TUint32 aCodePage = 0); +/** + * Converts multibyte string to wide char (unicode) + * This interface is provided so that we can have different implementation + * in windows and linux machine. + * @param aSource string to be converted + * @param aSourceLen Source len. If this is -1 then it will calculate the length of the source. + * @param aTarget target location. + * @param aTargetLen Space in the target location. + * @param aCodePage Code page number (currently supported in windows only) + * @return Number of bytes that make up the converted part of widechar sequence. + * If aTarget is NULL then the function will return the size needed to store + * the complete conversion of the source string. + */ +int ConvertMultiByteToWideChar(const char* aSource, int aSourceLen, wchar_t* aTarget, int aTargetLen, TUint32 aCodePage = 0); +}; // namespace Util -DllExport std::string Util::wstring2string (const std::wstring& aWide) +#ifdef __linux__ +int Util::ConvertWideCharToMultiByte(const wchar_t* aSource, int /*aSourceLen*/, char* aTarget, int aTargetLen, TUint32 /*aCodePage*/) { - int max = WideCharToMultiByte(CP_OEMCP,0,aWide.c_str(),aWide.length(),0,0,0,0); - std::string reply; - if (max > 0 ) + int retValue = wcstombs(aTarget, aSource, aTargetLen); + if (-1 == retValue) { - char* buffer = new char [max]; - try - { - WideCharToMultiByte(CP_OEMCP,0,aWide.c_str(),aWide.length(),buffer,max,0,0); - reply = std::string (buffer, max); - } - catch (...) - { - } - delete [] buffer; + return 0; } - return reply; + return retValue; + } + +int Util::ConvertMultiByteToWideChar(const char* aSource, int /*aSourceLen*/, wchar_t* aTarget, int aTargetLen, TUint32 /*aCodePage*/) + { + int retValue = mbstowcs(aTarget, aSource, aTargetLen); + if (-1 == retValue) + { + return 0; + } + return retValue; } -std::wstring Util::string2wstring (const std::string& aNarrow) +#else + +int Util::ConvertWideCharToMultiByte(const wchar_t* aSource, int aSourceLen, char* aTarget, int aTargetLen, TUint32 aCodePage) + { + if(0 == aCodePage) + { + aCodePage = CP_OEMCP; + } + return WideCharToMultiByte( aCodePage, 0, aSource, aSourceLen, aTarget, aTargetLen, NULL, NULL); + } + +int Util::ConvertMultiByteToWideChar(const char* aSource, int aSourceLen, wchar_t* aTarget, int aTargetLen, TUint32 aCodePage) { - int max = MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),0,0); + if(0 == aCodePage) + { + aCodePage = CP_OEMCP; + } + return MultiByteToWideChar( aCodePage, 0, aSource, aSourceLen, aTarget, aTargetLen); + } + +#endif // __linux__ + +std::wstring Util::string2wstring (const char* aNarrow) + { + std::string narrowStr(aNarrow); + int max = ConvertMultiByteToWideChar(aNarrow, strlen(aNarrow),0,0); std::wstring reply; if (max > 0 ) { wchar_t* buffer = new wchar_t [max]; try { - MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),buffer,max); + ConvertMultiByteToWideChar(aNarrow, strlen(aNarrow),buffer,max); reply = std::wstring (buffer, max); } catch (...) @@ -142,26 +204,6 @@ return reply; } -std::wstring Util::string2wstring (const char* aNarrow) - { - std::string narrowStr(aNarrow); - int max = MultiByteToWideChar(CP_OEMCP,0,narrowStr.c_str(),narrowStr.length(),0,0); - std::wstring reply; - if (max > 0 ) - { - wchar_t* buffer = new wchar_t [max]; - try - { - MultiByteToWideChar(CP_OEMCP,0,narrowStr.c_str(),narrowStr.length(),buffer,max); - reply = std::wstring (buffer, max); - } - catch (...) - { - } - delete [] buffer; - } - return reply; - } DllExport int Util::WideCharToInteger(const wchar_t* aWideChar) { @@ -173,9 +215,10 @@ TInt64 Util::WideCharToInt64(const wchar_t* aWideChar) { - __int64 i64 = 0; - swscanf(aWideChar, L"%I64d", &i64); - return i64; + TInt64 value=0; + std::wstringstream str(aWideChar); + str >> value; + return value; } DllExport const std::wstring Util::IntegerToWideString(int aInt) @@ -204,23 +247,31 @@ std::string Util::Base64Decode( const std::string& aEncodedData ) { - BIO *mem = BIO_new(BIO_s_mem()); - BIO_write(mem, aEncodedData.c_str(), aEncodedData.length()); + unsigned char* pIn = aEncodedData.c_str(); + int inLen = aEncodedData.length(); + unsigned char* pOut = new unsigned char[inLen]; - BIO* b64 = BIO_new(BIO_f_base64()); - mem = BIO_push(b64, mem); - int inlen = 0; - char inbuf[40]={0}; + int outLen; + std::string aDecodeData; + + // create a memory buffer containing base64 encoded data + BIO* bmem = BIO_new_mem_buf((void*)pIn, inLen); - inlen = BIO_read(b64, inbuf, aEncodedData.length()); - BIO_write(mem, inbuf, inlen); - std::string decodedData = inbuf; - - BIO_free_all(mem); - return decodedData; + // push a Base64 filter so that reading from buffer decodes it + BIO *bioCmd = BIO_new(BIO_f_base64()); + // we don't want newlines + BIO_set_flags(bioCmd, BIO_FLAGS_BASE64_NO_NL); + bmem = BIO_push(bioCmd, bmem); + + int finalLen = BIO_read(bmem, (void*)pOut, outLen); + + aDecodeData.assign(pOut, pOut+finalLen); + delete pOut; + BIO_free_all(bmem); + + return aDecodeData; } - TUint32 Util::Crc32(const void* aPtr, TInt aLength) { const TUint8* p = (const TUint8*)aPtr; @@ -230,4 +281,7 @@ crc = (crc >> 8) ^ CrcTab32[(crc ^ *p++) & 0xff]; return crc; } - \ No newline at end of file + + + +