secureswitools/swisistools/source/common/util.cpp
changeset 50 c6e8afe0ba85
parent 0 ba25891c3a9e
child 55 ac7f90a6ff4c
equal deleted inserted replaced
46:bb1748e0dd9b 50:c6e8afe0ba85
     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 
       
   143 #ifdef __linux__ 
       
   144 int Util::ConvertWideCharToMultiByte(const wchar_t* aSource, int /*aSourceLen*/, char* aTarget, int aTargetLen, TUint32 /*aCodePage*/)
       
   145 	{
       
   146 	int retValue = wcstombs(aTarget, aSource, aTargetLen);
       
   147 	if (-1 == retValue)
       
   148 		{
       
   149 		return 0;
       
   150 		}
       
   151 	return retValue;
       
   152 	}
       
   153 
       
   154 int Util::ConvertMultiByteToWideChar(const char* aSource, int /*aSourceLen*/, wchar_t* aTarget, int aTargetLen, TUint32 /*aCodePage*/)
       
   155 	{
       
   156 	int retValue = mbstowcs(aTarget, aSource, aTargetLen);
       
   157 	if (-1 == retValue)
       
   158 		{
       
   159 		return 0;
       
   160 		}
       
   161 	return retValue;
       
   162 	}
       
   163 
       
   164 #else
       
   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 
       
   187 #ifndef __linux__
       
   188 /*
   105 DllExport std::string Util::wstring2string (const std::wstring& aWide)
   189 DllExport std::string Util::wstring2string (const std::wstring& aWide)
   106 	{
   190 	{
   107 	int max = WideCharToMultiByte(CP_OEMCP,0,aWide.c_str(),aWide.length(),0,0,0,0);
   191 	int max = ConvertWideCharToMultiByte(aWide.c_str(),aWide.length(),0,0);
   108 	std::string reply;
   192 	std::string reply;
   109 	if (max > 0 )
   193 	if (max > 0 )
   110 		{
   194 		{
   111 		char* buffer = new char [max];
   195 		char* buffer = new char [max];
   112 		try
   196 		try
   113 			{
   197 			{
   114 			WideCharToMultiByte(CP_OEMCP,0,aWide.c_str(),aWide.length(),buffer,max,0,0);
   198 			ConvertWideCharToMultiByte(aWide.c_str(),aWide.length(),buffer,max);
   115 			reply = std::string (buffer, max);
   199 			reply = std::string (buffer, max);
   116 			}
   200 			}
   117 		catch (...)
   201 		catch (...)
   118 			{
   202 			{
   119 			}
   203 			}
   122 	return reply;
   206 	return reply;
   123 	}
   207 	}
   124 
   208 
   125 std::wstring Util::string2wstring (const std::string& aNarrow)
   209 std::wstring Util::string2wstring (const std::string& aNarrow)
   126 	{
   210 	{
   127 	int max = MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),0,0);
   211 	int max = ConvertMultiByteToWideChar(aNarrow.c_str(),aNarrow.length(),0,0);
   128 	std::wstring reply;
   212 	std::wstring reply;
   129 	if (max > 0 )
   213 	if (max > 0 )
   130 		{
   214 		{
   131 		wchar_t* buffer = new wchar_t [max];
   215 		wchar_t* buffer = new wchar_t [max];
   132 		try
   216 		try
   133 			{
   217 			{
   134 			MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),buffer,max);
   218 			ConvertMultiByteToWideChar(aNarrow.c_str(),aNarrow.length(),buffer,max);
   135 			reply = std::wstring (buffer, max);
   219 			reply = std::wstring (buffer, max);
   136 			}
   220 			}
   137 		catch (...)
   221 		catch (...)
   138 			{
   222 			{
   139 			}
   223 			}
   140 		delete [] buffer;
   224 		delete [] buffer;
   141 		}
   225 		}
   142 	return reply;
   226 	return reply;
   143 	}
   227 	}
       
   228 */
       
   229 #endif
   144 
   230 
   145 std::wstring Util::string2wstring (const char* aNarrow)
   231 std::wstring Util::string2wstring (const char* aNarrow)
   146 	{
   232 	{
   147 	std::string narrowStr(aNarrow);
   233 	std::string narrowStr(aNarrow);
   148 	int max = MultiByteToWideChar(CP_OEMCP,0,narrowStr.c_str(),narrowStr.length(),0,0);
   234 	int max = ConvertMultiByteToWideChar(aNarrow, strlen(aNarrow),0,0);
   149 	std::wstring reply;
   235 	std::wstring reply;
   150 	if (max > 0 )
   236 	if (max > 0 )
   151 		{
   237 		{
   152 		wchar_t* buffer = new wchar_t [max];
   238 		wchar_t* buffer = new wchar_t [max];
   153 		try
   239 		try
   154 			{
   240 			{
   155 			MultiByteToWideChar(CP_OEMCP,0,narrowStr.c_str(),narrowStr.length(),buffer,max);
   241 			ConvertMultiByteToWideChar(aNarrow, strlen(aNarrow),buffer,max);
   156 			reply = std::wstring (buffer, max);
   242 			reply = std::wstring (buffer, max);
   157 			}
   243 			}
   158 		catch (...)
   244 		catch (...)
   159 			{
   245 			{
   160 			}
   246 			}
   161 		delete [] buffer;
   247 		delete [] buffer;
   162 		}
   248 		}
   163 	return reply;
   249 	return reply;
   164 	}
   250 	}
       
   251 
   165 
   252 
   166 DllExport int Util::WideCharToInteger(const wchar_t* aWideChar)
   253 DllExport int Util::WideCharToInteger(const wchar_t* aWideChar)
   167 	{
   254 	{
   168 	unsigned long int value=0;
   255 	unsigned long int value=0;
   169 	std::wstringstream str(aWideChar);
   256 	std::wstringstream str(aWideChar);
   171 	return value;
   258 	return value;
   172 	}
   259 	}
   173 
   260 
   174 TInt64 Util::WideCharToInt64(const wchar_t* aWideChar)
   261 TInt64 Util::WideCharToInt64(const wchar_t* aWideChar)
   175 	{
   262 	{
   176 	__int64 i64 = 0;
   263 	TInt64 value=0;
   177 	swscanf(aWideChar, L"%I64d", &i64);
   264 	std::wstringstream str(aWideChar);
   178 	return i64;
   265 	str >> value;
       
   266 	return value;
   179 	}
   267 	}
   180 
   268 
   181 DllExport const std::wstring Util::IntegerToWideString(int aInt)
   269 DllExport const std::wstring Util::IntegerToWideString(int aInt)
   182 	{
   270 	{
   183 	std::wstringstream wstream;
   271 	std::wstringstream wstream;
   228 	TUint32 crc = 0;
   316 	TUint32 crc = 0;
   229 	while (p < q)
   317 	while (p < q)
   230 	 crc = (crc >> 8) ^ CrcTab32[(crc ^ *p++) & 0xff];
   318 	 crc = (crc >> 8) ^ CrcTab32[(crc ^ *p++) & 0xff];
   231 	return crc;
   319 	return crc;
   232 	}
   320 	}
   233 	
   321 
       
   322 
       
   323 
       
   324