secureswitools/swisistools/source/common/util.cpp
branchRCL_3
changeset 66 8b7f4e561641
parent 65 7333d7932ef7
child 81 42552535c1ac
equal deleted inserted replaced
65:7333d7932ef7 66:8b7f4e561641
    29 #include <sstream>
    29 #include <sstream>
    30 // openssl
    30 // openssl
    31 #include <openssl/bio.h>
    31 #include <openssl/bio.h>
    32 #include <openssl/evp.h>
    32 #include <openssl/evp.h>
    33 #include <openssl/buffer.h>
    33 #include <openssl/buffer.h>
    34 
       
    35 #ifdef _WIN32
    34 #ifdef _WIN32
    36 #include <windows.h>
    35 #include <windows.h>
    37 #endif // _WIN32
    36 #endif // _WIN32
    38 
    37 #include "utf8.h"
    39 
       
    40 #include "utf8_wrapper.h"
       
    41 
    38 
    42 static const TUint32 CrcTab32[256] =
    39 static const TUint32 CrcTab32[256] =
    43      {
    40      {
    44      0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
    41      0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
    45      0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
    42      0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
   213 	return value;
   210 	return value;
   214 	}
   211 	}
   215 
   212 
   216 TInt64 Util::WideCharToInt64(const wchar_t* aWideChar)
   213 TInt64 Util::WideCharToInt64(const wchar_t* aWideChar)
   217 	{
   214 	{
   218 	TInt64 value=0;
   215 	TInt64 i64 = 0;
   219 	std::wstringstream str(aWideChar);
   216 	swscanf(aWideChar, L"%I64d", &i64);
   220 	str >> value;
   217 	return i64;
   221 	return value;
       
   222 	}
   218 	}
   223 
   219 
   224 DllExport const std::wstring Util::IntegerToWideString(int aInt)
   220 DllExport const std::wstring Util::IntegerToWideString(int aInt)
   225 	{
   221 	{
   226 	std::wstringstream wstream;
   222 	std::wstringstream wstream;
   245 	return encodedString;
   241 	return encodedString;
   246 	}
   242 	}
   247 
   243 
   248 std::string Util::Base64Decode( const std::string& aEncodedData )
   244 std::string Util::Base64Decode( const std::string& aEncodedData )
   249 	{
   245 	{
   250 	unsigned char* pIn = aEncodedData.c_str();
   246 	BIO *mem = BIO_new(BIO_s_mem());
   251 	int inLen = aEncodedData.length();
   247 	BIO_write(mem, aEncodedData.c_str(), aEncodedData.length());
   252 	unsigned char* pOut = new unsigned char[inLen];
   248 
   253 
   249 	BIO* b64 = BIO_new(BIO_f_base64());
   254 	int outLen;
   250 	mem = BIO_push(b64, mem);
   255 	std::string aDecodeData;
   251 	int inlen = 0;
   256 		
   252 	char inbuf[40]={0};
   257     // create a memory buffer containing base64 encoded data
   253 
   258     BIO* bmem = BIO_new_mem_buf((void*)pIn, inLen);
   254 	inlen = BIO_read(b64, inbuf, aEncodedData.length());
   259 
   255     BIO_write(mem, inbuf, inlen);
   260     // push a Base64 filter so that reading from buffer decodes it
   256 	std::string decodedData = inbuf;
   261     BIO *bioCmd = BIO_new(BIO_f_base64());
   257 	
   262     // we don't want newlines
   258 	BIO_free_all(mem);
   263     BIO_set_flags(bioCmd, BIO_FLAGS_BASE64_NO_NL);
   259 	return decodedData;
   264     bmem = BIO_push(bioCmd, bmem);
   260 	}
   265 
   261 
   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 	}
       
   274 
   262 
   275 TUint32 Util::Crc32(const void* aPtr, TInt aLength)
   263 TUint32 Util::Crc32(const void* aPtr, TInt aLength)
   276 	{
   264 	{
   277 	const TUint8* p = (const TUint8*)aPtr;
   265 	const TUint8* p = (const TUint8*)aPtr;
   278 	const TUint8* q = p + aLength;
   266 	const TUint8* q = p + aLength;
   279 	TUint32 crc = 0;
   267 	TUint32 crc = 0;
   280 	while (p < q)
   268 	while (p < q)
   281 	 crc = (crc >> 8) ^ CrcTab32[(crc ^ *p++) & 0xff];
   269 	 crc = (crc >> 8) ^ CrcTab32[(crc ^ *p++) & 0xff];
   282 	return crc;
   270 	return crc;
   283 	}
   271 	}
   284 
       
   285 
       
   286 
       
   287