secureswitools/swisistools/source/common/util.cpp
changeset 60 245df5276b97
parent 0 ba25891c3a9e
child 76 f36d4ce8961e
equal deleted inserted replaced
53:ae54820ef82c 60:245df5276b97
     1 /*
     1 /*
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    22  @released
    22  @released
    23 */
    23 */
    24 
    24 
    25 #include "util.h"
    25 #include "util.h"
    26 #include "symbiantypes.h"
    26 #include "symbiantypes.h"
    27 #include <windows.h>
       
    28 #include <fstream.h>
    27 #include <fstream.h>
    29 #include <iostream>
    28 #include <iostream>
    30 #include <sstream>
    29 #include <sstream>
    31 // openssl
    30 // openssl
    32 #include <openssl/bio.h>
    31 #include <openssl/bio.h>
    33 #include <openssl/evp.h>
    32 #include <openssl/evp.h>
    34 #include <openssl/buffer.h>
    33 #include <openssl/buffer.h>
       
    34 
       
    35 #ifdef _WIN32
       
    36 #include <windows.h>
       
    37 #endif // _WIN32
       
    38 
       
    39 
       
    40 #include "utf8_wrapper.h"
    35 
    41 
    36 static const TUint32 CrcTab32[256] =
    42 static const TUint32 CrcTab32[256] =
    37      {
    43      {
    38      0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
    44      0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
    39      0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
    45      0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
    98      0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
   104      0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
    99      0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
   105      0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
   100      0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
   106      0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
   101      0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
   107      0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
   102      };
   108      };
       
   109 
       
   110 namespace Util
       
   111 {
       
   112 /**
       
   113  * Converts wide char (unicode) string to multibyte string
       
   114  * This interface is provided so that we can have different implementation 
       
   115  * in windows and linux machine.
       
   116  * @param aSource 		string to be converted
       
   117  * @param aSourceLen	Source len. If this is -1 then it will calculate the length of the source.
       
   118  * @param aTarget		target location.
       
   119  * @param aTargetLen	Space in the target location.
       
   120  * @param aCodePage		Code page number (currently supported in windows only)
       
   121  * @return Number of bytes that make up the converted part of multibyte sequence. 
       
   122  * 			If aTarget is NULL then the function will return the size needed to store
       
   123  * 			the complete conversion of the source string.
       
   124  */
       
   125 int ConvertWideCharToMultiByte(const wchar_t* aSource, int aSourceLen, char* aTarget, int aTargetLen, TUint32 aCodePage = 0);
       
   126 /**
       
   127  * Converts multibyte string to wide char (unicode)
       
   128  * This interface is provided so that we can have different implementation 
       
   129  * in windows and linux machine.
       
   130  * @param aSource 		string to be converted
       
   131  * @param aSourceLen	Source len. If this is -1 then it will calculate the length of the source.
       
   132  * @param aTarget		target location.
       
   133  * @param aTargetLen	Space in the target location.
       
   134  * @param aCodePage		Code page number (currently supported in windows only)
       
   135  * @return Number of bytes that make up the converted part of widechar sequence. 
       
   136  * 			If aTarget is NULL then the function will return the size needed to store
       
   137  * 			the complete conversion of the source string.
       
   138  */
       
   139 int ConvertMultiByteToWideChar(const char* aSource, int aSourceLen, wchar_t* aTarget, int aTargetLen, TUint32 aCodePage = 0);
       
   140 }; // namespace Util
   103  
   141  
   104 
   142 
   105 DllExport std::string Util::wstring2string (const std::wstring& aWide)
   143 #ifdef __linux__ 
   106 	{
   144 int Util::ConvertWideCharToMultiByte(const wchar_t* aSource, int /*aSourceLen*/, char* aTarget, int aTargetLen, TUint32 /*aCodePage*/)
   107 	int max = WideCharToMultiByte(CP_OEMCP,0,aWide.c_str(),aWide.length(),0,0,0,0);
   145 	{
   108 	std::string reply;
   146 	int retValue = wcstombs(aTarget, aSource, aTargetLen);
   109 	if (max > 0 )
   147 	if (-1 == retValue)
   110 		{
   148 		{
   111 		char* buffer = new char [max];
   149 		return 0;
   112 		try
   150 		}
   113 			{
   151 	return retValue;
   114 			WideCharToMultiByte(CP_OEMCP,0,aWide.c_str(),aWide.length(),buffer,max,0,0);
   152 	}
   115 			reply = std::string (buffer, max);
   153 
   116 			}
   154 int Util::ConvertMultiByteToWideChar(const char* aSource, int /*aSourceLen*/, wchar_t* aTarget, int aTargetLen, TUint32 /*aCodePage*/)
   117 		catch (...)
   155 	{
   118 			{
   156 	int retValue = mbstowcs(aTarget, aSource, aTargetLen);
   119 			}
   157 	if (-1 == retValue)
   120 		delete [] buffer;
   158 		{
   121 		}
   159 		return 0;
   122 	return reply;
   160 		}
   123 	}
   161 	return retValue;
   124 
   162 	}
   125 std::wstring Util::string2wstring (const std::string& aNarrow)
   163 
   126 	{
   164 #else
   127 	int max = MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),0,0);
   165 
       
   166 int Util::ConvertWideCharToMultiByte(const wchar_t* aSource, int aSourceLen, char* aTarget, int aTargetLen, TUint32 aCodePage)
       
   167 	{
       
   168 	if(0 == aCodePage)
       
   169 		{
       
   170 		aCodePage = CP_OEMCP;
       
   171 		}
       
   172 	return WideCharToMultiByte( aCodePage, 0, aSource, aSourceLen, aTarget, aTargetLen, NULL, NULL);
       
   173 	}
       
   174 
       
   175 int Util::ConvertMultiByteToWideChar(const char* aSource, int aSourceLen, wchar_t* aTarget, int aTargetLen, TUint32 aCodePage)
       
   176 	{
       
   177 	if(0 == aCodePage)
       
   178 		{
       
   179 		aCodePage = CP_OEMCP;
       
   180 		}
       
   181 	return MultiByteToWideChar( aCodePage, 0, aSource, aSourceLen, aTarget, aTargetLen);
       
   182 	}
       
   183 
       
   184 #endif // __linux__
       
   185 
       
   186 std::wstring Util::string2wstring (const char* aNarrow)
       
   187 	{
       
   188 	std::string narrowStr(aNarrow);
       
   189 	int max = ConvertMultiByteToWideChar(aNarrow, strlen(aNarrow),0,0);
   128 	std::wstring reply;
   190 	std::wstring reply;
   129 	if (max > 0 )
   191 	if (max > 0 )
   130 		{
   192 		{
   131 		wchar_t* buffer = new wchar_t [max];
   193 		wchar_t* buffer = new wchar_t [max];
   132 		try
   194 		try
   133 			{
   195 			{
   134 			MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),buffer,max);
   196 			ConvertMultiByteToWideChar(aNarrow, strlen(aNarrow),buffer,max);
   135 			reply = std::wstring (buffer, max);
   197 			reply = std::wstring (buffer, max);
   136 			}
   198 			}
   137 		catch (...)
   199 		catch (...)
   138 			{
   200 			{
   139 			}
   201 			}
   140 		delete [] buffer;
   202 		delete [] buffer;
   141 		}
   203 		}
   142 	return reply;
   204 	return reply;
   143 	}
   205 	}
   144 
   206 
   145 std::wstring Util::string2wstring (const char* aNarrow)
       
   146 	{
       
   147 	std::string narrowStr(aNarrow);
       
   148 	int max = MultiByteToWideChar(CP_OEMCP,0,narrowStr.c_str(),narrowStr.length(),0,0);
       
   149 	std::wstring reply;
       
   150 	if (max > 0 )
       
   151 		{
       
   152 		wchar_t* buffer = new wchar_t [max];
       
   153 		try
       
   154 			{
       
   155 			MultiByteToWideChar(CP_OEMCP,0,narrowStr.c_str(),narrowStr.length(),buffer,max);
       
   156 			reply = std::wstring (buffer, max);
       
   157 			}
       
   158 		catch (...)
       
   159 			{
       
   160 			}
       
   161 		delete [] buffer;
       
   162 		}
       
   163 	return reply;
       
   164 	}
       
   165 
   207 
   166 DllExport int Util::WideCharToInteger(const wchar_t* aWideChar)
   208 DllExport int Util::WideCharToInteger(const wchar_t* aWideChar)
   167 	{
   209 	{
   168 	unsigned long int value=0;
   210 	unsigned long int value=0;
   169 	std::wstringstream str(aWideChar);
   211 	std::wstringstream str(aWideChar);
   171 	return value;
   213 	return value;
   172 	}
   214 	}
   173 
   215 
   174 TInt64 Util::WideCharToInt64(const wchar_t* aWideChar)
   216 TInt64 Util::WideCharToInt64(const wchar_t* aWideChar)
   175 	{
   217 	{
   176 	__int64 i64 = 0;
   218 	TInt64 value=0;
   177 	swscanf(aWideChar, L"%I64d", &i64);
   219 	std::wstringstream str(aWideChar);
   178 	return i64;
   220 	str >> value;
       
   221 	return value;
   179 	}
   222 	}
   180 
   223 
   181 DllExport const std::wstring Util::IntegerToWideString(int aInt)
   224 DllExport const std::wstring Util::IntegerToWideString(int aInt)
   182 	{
   225 	{
   183 	std::wstringstream wstream;
   226 	std::wstringstream wstream;
   202 	return encodedString;
   245 	return encodedString;
   203 	}
   246 	}
   204 
   247 
   205 std::string Util::Base64Decode( const std::string& aEncodedData )
   248 std::string Util::Base64Decode( const std::string& aEncodedData )
   206 	{
   249 	{
   207 	BIO *mem = BIO_new(BIO_s_mem());
   250 	unsigned char* pIn = aEncodedData.c_str();
   208 	BIO_write(mem, aEncodedData.c_str(), aEncodedData.length());
   251 	int inLen = aEncodedData.length();
   209 
   252 	unsigned char* pOut = new unsigned char[inLen];
   210 	BIO* b64 = BIO_new(BIO_f_base64());
   253 
   211 	mem = BIO_push(b64, mem);
   254 	int outLen;
   212 	int inlen = 0;
   255 	std::string aDecodeData;
   213 	char inbuf[40]={0};
   256 		
   214 
   257     // create a memory buffer containing base64 encoded data
   215 	inlen = BIO_read(b64, inbuf, aEncodedData.length());
   258     BIO* bmem = BIO_new_mem_buf((void*)pIn, inLen);
   216     BIO_write(mem, inbuf, inlen);
   259 
   217 	std::string decodedData = inbuf;
   260     // push a Base64 filter so that reading from buffer decodes it
   218 	
   261     BIO *bioCmd = BIO_new(BIO_f_base64());
   219 	BIO_free_all(mem);
   262     // we don't want newlines
   220 	return decodedData;
   263     BIO_set_flags(bioCmd, BIO_FLAGS_BASE64_NO_NL);
   221 	}
   264     bmem = BIO_push(bioCmd, bmem);
   222 
   265 
       
   266     int finalLen = BIO_read(bmem, (void*)pOut, outLen);
       
   267 
       
   268 	aDecodeData.assign(pOut, pOut+finalLen);
       
   269 	delete pOut;
       
   270     BIO_free_all(bmem);
       
   271 
       
   272 	return aDecodeData;
       
   273 	}
   223 
   274 
   224 TUint32 Util::Crc32(const void* aPtr, TInt aLength)
   275 TUint32 Util::Crc32(const void* aPtr, TInt aLength)
   225 	{
   276 	{
   226 	const TUint8* p = (const TUint8*)aPtr;
   277 	const TUint8* p = (const TUint8*)aPtr;
   227 	const TUint8* q = p + aLength;
   278 	const TUint8* q = p + aLength;
   228 	TUint32 crc = 0;
   279 	TUint32 crc = 0;
   229 	while (p < q)
   280 	while (p < q)
   230 	 crc = (crc >> 8) ^ CrcTab32[(crc ^ *p++) & 0xff];
   281 	 crc = (crc >> 8) ^ CrcTab32[(crc ^ *p++) & 0xff];
   231 	return crc;
   282 	return crc;
   232 	}
   283 	}
   233 	
   284 
       
   285 
       
   286 
       
   287