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