secureswitools/swisistools/source/common/util.cpp
branchRCL_3
changeset 62 5cc91383ab1e
parent 0 ba25891c3a9e
child 65 7333d7932ef7
equal deleted inserted replaced
61:cd189dac02f7 62:5cc91383ab1e
     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 #ifdef _WIN32
       
    35 #include <windows.h>
       
    36 #endif // _WIN32
       
    37 #include "utf8.h"
    35 
    38 
    36 static const TUint32 CrcTab32[256] =
    39 static const TUint32 CrcTab32[256] =
    37      {
    40      {
    38      0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
    41      0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
    39      0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
    42      0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
    98      0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
   101      0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
    99      0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
   102      0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
   100      0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
   103      0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
   101      0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
   104      0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
   102      };
   105      };
       
   106 
       
   107 namespace Util
       
   108 {
       
   109 /**
       
   110  * Converts wide char (unicode) string to multibyte string
       
   111  * This interface is provided so that we can have different implementation 
       
   112  * in windows and linux machine.
       
   113  * @param aSource 		string to be converted
       
   114  * @param aSourceLen	Source len. If this is -1 then it will calculate the length of the source.
       
   115  * @param aTarget		target location.
       
   116  * @param aTargetLen	Space in the target location.
       
   117  * @param aCodePage		Code page number (currently supported in windows only)
       
   118  * @return Number of bytes that make up the converted part of multibyte sequence. 
       
   119  * 			If aTarget is NULL then the function will return the size needed to store
       
   120  * 			the complete conversion of the source string.
       
   121  */
       
   122 int ConvertWideCharToMultiByte(const wchar_t* aSource, int aSourceLen, char* aTarget, int aTargetLen, TUint32 aCodePage = 0);
       
   123 /**
       
   124  * Converts multibyte string to wide char (unicode)
       
   125  * This interface is provided so that we can have different implementation 
       
   126  * in windows and linux machine.
       
   127  * @param aSource 		string to be converted
       
   128  * @param aSourceLen	Source len. If this is -1 then it will calculate the length of the source.
       
   129  * @param aTarget		target location.
       
   130  * @param aTargetLen	Space in the target location.
       
   131  * @param aCodePage		Code page number (currently supported in windows only)
       
   132  * @return Number of bytes that make up the converted part of widechar sequence. 
       
   133  * 			If aTarget is NULL then the function will return the size needed to store
       
   134  * 			the complete conversion of the source string.
       
   135  */
       
   136 int ConvertMultiByteToWideChar(const char* aSource, int aSourceLen, wchar_t* aTarget, int aTargetLen, TUint32 aCodePage = 0);
       
   137 }; // namespace Util
   103  
   138  
   104 
   139 
   105 DllExport std::string Util::wstring2string (const std::wstring& aWide)
   140 #ifdef __linux__ 
   106 	{
   141 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);
   142 	{
   108 	std::string reply;
   143 	int retValue = wcstombs(aTarget, aSource, aTargetLen);
   109 	if (max > 0 )
   144 	if (-1 == retValue)
   110 		{
   145 		{
   111 		char* buffer = new char [max];
   146 		return 0;
   112 		try
   147 		}
   113 			{
   148 	return retValue;
   114 			WideCharToMultiByte(CP_OEMCP,0,aWide.c_str(),aWide.length(),buffer,max,0,0);
   149 	}
   115 			reply = std::string (buffer, max);
   150 
   116 			}
   151 int Util::ConvertMultiByteToWideChar(const char* aSource, int /*aSourceLen*/, wchar_t* aTarget, int aTargetLen, TUint32 /*aCodePage*/)
   117 		catch (...)
   152 	{
   118 			{
   153 	int retValue = mbstowcs(aTarget, aSource, aTargetLen);
   119 			}
   154 	if (-1 == retValue)
   120 		delete [] buffer;
   155 		{
   121 		}
   156 		return 0;
   122 	return reply;
   157 		}
   123 	}
   158 	return retValue;
   124 
   159 	}
   125 std::wstring Util::string2wstring (const std::string& aNarrow)
   160 
   126 	{
   161 #else
   127 	int max = MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),0,0);
   162 
       
   163 int Util::ConvertWideCharToMultiByte(const wchar_t* aSource, int aSourceLen, char* aTarget, int aTargetLen, TUint32 aCodePage)
       
   164 	{
       
   165 	if(0 == aCodePage)
       
   166 		{
       
   167 		aCodePage = CP_OEMCP;
       
   168 		}
       
   169 	return WideCharToMultiByte( aCodePage, 0, aSource, aSourceLen, aTarget, aTargetLen, NULL, NULL);
       
   170 	}
       
   171 
       
   172 int Util::ConvertMultiByteToWideChar(const char* aSource, int aSourceLen, wchar_t* aTarget, int aTargetLen, TUint32 aCodePage)
       
   173 	{
       
   174 	if(0 == aCodePage)
       
   175 		{
       
   176 		aCodePage = CP_OEMCP;
       
   177 		}
       
   178 	return MultiByteToWideChar( aCodePage, 0, aSource, aSourceLen, aTarget, aTargetLen);
       
   179 	}
       
   180 
       
   181 #endif // __linux__
       
   182 
       
   183 std::wstring Util::string2wstring (const char* aNarrow)
       
   184 	{
       
   185 	std::string narrowStr(aNarrow);
       
   186 	int max = ConvertMultiByteToWideChar(aNarrow, strlen(aNarrow),0,0);
   128 	std::wstring reply;
   187 	std::wstring reply;
   129 	if (max > 0 )
   188 	if (max > 0 )
   130 		{
   189 		{
   131 		wchar_t* buffer = new wchar_t [max];
   190 		wchar_t* buffer = new wchar_t [max];
   132 		try
   191 		try
   133 			{
   192 			{
   134 			MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),buffer,max);
   193 			ConvertMultiByteToWideChar(aNarrow, strlen(aNarrow),buffer,max);
   135 			reply = std::wstring (buffer, max);
   194 			reply = std::wstring (buffer, max);
   136 			}
   195 			}
   137 		catch (...)
   196 		catch (...)
   138 			{
   197 			{
   139 			}
   198 			}
   140 		delete [] buffer;
   199 		delete [] buffer;
   141 		}
   200 		}
   142 	return reply;
   201 	return reply;
   143 	}
   202 	}
   144 
   203 
   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 
   204 
   166 DllExport int Util::WideCharToInteger(const wchar_t* aWideChar)
   205 DllExport int Util::WideCharToInteger(const wchar_t* aWideChar)
   167 	{
   206 	{
   168 	unsigned long int value=0;
   207 	unsigned long int value=0;
   169 	std::wstringstream str(aWideChar);
   208 	std::wstringstream str(aWideChar);
   171 	return value;
   210 	return value;
   172 	}
   211 	}
   173 
   212 
   174 TInt64 Util::WideCharToInt64(const wchar_t* aWideChar)
   213 TInt64 Util::WideCharToInt64(const wchar_t* aWideChar)
   175 	{
   214 	{
   176 	__int64 i64 = 0;
   215 	TInt64 i64 = 0;
   177 	swscanf(aWideChar, L"%I64d", &i64);
   216 	swscanf(aWideChar, L"%I64d", &i64);
   178 	return i64;
   217 	return i64;
   179 	}
   218 	}
   180 
   219 
   181 DllExport const std::wstring Util::IntegerToWideString(int aInt)
   220 DllExport const std::wstring Util::IntegerToWideString(int aInt)
   228 	TUint32 crc = 0;
   267 	TUint32 crc = 0;
   229 	while (p < q)
   268 	while (p < q)
   230 	 crc = (crc >> 8) ^ CrcTab32[(crc ^ *p++) & 0xff];
   269 	 crc = (crc >> 8) ^ CrcTab32[(crc ^ *p++) & 0xff];
   231 	return crc;
   270 	return crc;
   232 	}
   271 	}
   233