From 024c7fb409dc9b0c921a41a89a411496a17b0f70 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 1 Apr 2014 14:58:49 +0000 Subject: CryptoPP: code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@8813 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/CryptoPP/src/cpp_svcs.cpp | 114 ++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 59 deletions(-) (limited to 'plugins/CryptoPP/src/cpp_svcs.cpp') diff --git a/plugins/CryptoPP/src/cpp_svcs.cpp b/plugins/CryptoPP/src/cpp_svcs.cpp index 07b4030633..01b95bc2d9 100644 --- a/plugins/CryptoPP/src/cpp_svcs.cpp +++ b/plugins/CryptoPP/src/cpp_svcs.cpp @@ -2,12 +2,11 @@ const unsigned char IV[] = "SIMhell@MIRANDA!"; - // encrypt string using KeyX, return encoded string as ASCII or NULL LPSTR __cdecl cpp_encrypt(pCNTX ptr, LPCSTR szPlainMsg) { ptr->error = ERROR_NONE; - pSIMDATA p = (pSIMDATA) ptr->pdata; + pSIMDATA p = (pSIMDATA)ptr->pdata; BYTE dataflag = 0; size_t slen = strlen(szPlainMsg); @@ -15,8 +14,8 @@ LPSTR __cdecl cpp_encrypt(pCNTX ptr, LPCSTR szPlainMsg) LPSTR szMsg; if (ptr->features & FEATURES_GZIP) { size_t clen; - szMsg = (LPSTR) cpp_gzip((BYTE*)szPlainMsg,slen,clen); - if (clen>=slen) { + szMsg = (LPSTR)cpp_gzip((BYTE*)szPlainMsg, slen, clen); + if (clen >= slen) { free(szMsg); szMsg = _strdup(szPlainMsg); } @@ -29,8 +28,8 @@ LPSTR __cdecl cpp_encrypt(pCNTX ptr, LPCSTR szPlainMsg) string ciphered; - CBC_Mode::Encryption enc(p->KeyX,Tiger::DIGESTSIZE,IV); - StreamTransformationFilter cbcEncryptor(enc,new StringSink(ciphered)); + CBC_Mode::Encryption enc(p->KeyX, Tiger::DIGESTSIZE, IV); + StreamTransformationFilter cbcEncryptor(enc, new StringSink(ciphered)); cbcEncryptor.Put((PBYTE)szMsg, slen); cbcEncryptor.MessageEnd(); @@ -40,14 +39,14 @@ LPSTR __cdecl cpp_encrypt(pCNTX ptr, LPCSTR szPlainMsg) unsigned clen = (unsigned)ciphered.length(); if (ptr->features & FEATURES_CRC32) { BYTE crc32[CRC32::DIGESTSIZE]; - memset(crc32,0,sizeof(crc32)); + memset(crc32, 0, sizeof(crc32)); CRC32().CalculateDigest(crc32, (BYTE*)ciphered.data(), clen); - ciphered.insert(0,(LPSTR)&crc32,CRC32::DIGESTSIZE); - ciphered.insert(0,(LPSTR)&clen,2); + ciphered.insert(0, (LPSTR)&crc32, CRC32::DIGESTSIZE); + ciphered.insert(0, (LPSTR)&clen, 2); } if (ptr->features & FEATURES_GZIP) - ciphered.insert(0,(LPSTR)&dataflag,1); - + ciphered.insert(0, (LPSTR)&dataflag, 1); + clen = (unsigned)ciphered.length(); if (ptr->features & FEATURES_BASE64) replaceStr(ptr->tmp, mir_base64_encode((PBYTE)ciphered.data(), clen)); @@ -65,25 +64,25 @@ LPSTR __cdecl cpp_decrypt(pCNTX ptr, LPCSTR szEncMsg) try { ptr->error = ERROR_SEH; - pSIMDATA p = (pSIMDATA) ptr->pdata; + pSIMDATA p = (pSIMDATA)ptr->pdata; unsigned clen = (unsigned)strlen(szEncMsg); if (ptr->features & FEATURES_BASE64) - ciphered = (LPSTR)mir_base64_decode(szEncMsg,&clen); + ciphered = (LPSTR)mir_base64_decode(szEncMsg, &clen); else - ciphered = base16decode(szEncMsg,&clen); + ciphered = base16decode(szEncMsg, &clen); LPSTR bciphered = ciphered; - BYTE dataflag=0; + BYTE dataflag = 0; if (ptr->features & FEATURES_GZIP) { dataflag = *ciphered; bciphered++; clen--; // cut GZIP flag } if (ptr->features & FEATURES_CRC32) { - int len = *( WORD* )bciphered; - bciphered+=2; clen-=2; // cut CRC32 length + int len = *(WORD*)bciphered; + bciphered += 2; clen -= 2; // cut CRC32 length if ((int)clen - CRC32::DIGESTSIZE < len) { // mesage not full #if defined(_DEBUG) || defined(NETLIB_LOG) @@ -94,11 +93,11 @@ LPSTR __cdecl cpp_decrypt(pCNTX ptr, LPCSTR szEncMsg) } BYTE crc32[CRC32::DIGESTSIZE]; - memset(crc32,0,sizeof(crc32)); + memset(crc32, 0, sizeof(crc32)); - CRC32().CalculateDigest(crc32, (PBYTE)(bciphered+CRC32::DIGESTSIZE), len); + CRC32().CalculateDigest(crc32, (PBYTE)(bciphered + CRC32::DIGESTSIZE), len); - if (memcmp(crc32,bciphered,CRC32::DIGESTSIZE)) { // message is bad crc + if (memcmp(crc32, bciphered, CRC32::DIGESTSIZE)) { // message is bad crc #if defined(_DEBUG) || defined(NETLIB_LOG) Sent_NetLog("cpp_decrypt: error bad_crc"); #endif @@ -111,15 +110,15 @@ LPSTR __cdecl cpp_decrypt(pCNTX ptr, LPCSTR szEncMsg) string unciphered; - CBC_Mode::Decryption dec(p->KeyX,Tiger::DIGESTSIZE,IV); - StreamTransformationFilter cbcDecryptor(dec,new StringSink(unciphered)); + CBC_Mode::Decryption dec(p->KeyX, Tiger::DIGESTSIZE, IV); + StreamTransformationFilter cbcDecryptor(dec, new StringSink(unciphered)); - cbcDecryptor.Put((PBYTE)bciphered,clen); + cbcDecryptor.Put((PBYTE)bciphered, clen); cbcDecryptor.MessageEnd(); if (dataflag & DATA_GZIP) { size_t clen2 = clen; - LPSTR res = (LPSTR)cpp_gunzip((PBYTE)unciphered.data(),unciphered.length(),clen2); + LPSTR res = (LPSTR)cpp_gunzip((PBYTE)unciphered.data(), unciphered.length(), clen2); replaceStr(ptr->tmp, mir_strndup(res, clen2)); free(res); } @@ -143,16 +142,16 @@ LPSTR __cdecl cpp_encodeA(HANDLE context, LPCSTR msg) { pCNTX ptr = get_context_on_id(context); if (!ptr) return NULL; - cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA) ptr->pdata; + cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA)ptr->pdata; if (!p->KeyX) { ptr->error = ERROR_NO_KEYX; return NULL; } LPSTR szNewMsg = NULL; - LPSTR szOldMsg = (LPSTR) msg; + LPSTR szOldMsg = (LPSTR)msg; if (ptr->features & FEATURES_UTF8) { // ansi message: convert to unicode->utf-8 and encrypt. - int slen = (int)strlen(szOldMsg)+1; - LPWSTR wstring = (LPWSTR) alloca(slen*sizeof(WCHAR)); + int slen = (int)strlen(szOldMsg) + 1; + LPWSTR wstring = (LPWSTR)alloca(slen*sizeof(WCHAR)); MultiByteToWideChar(CP_ACP, 0, szOldMsg, -1, wstring, slen*sizeof(WCHAR)); // encrypt szNewMsg = cpp_encrypt(ptr, utf8encode(wstring)); @@ -171,11 +170,11 @@ LPSTR __cdecl cpp_encodeU(HANDLE context, LPCSTR msg) { pCNTX ptr = get_context_on_id(context); if (!ptr) return NULL; - cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA) ptr->pdata; + cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA)ptr->pdata; if (!p->KeyX) { ptr->error = ERROR_NO_KEYX; return NULL; } LPSTR szNewMsg = NULL; - LPSTR szOldMsg = (LPSTR) msg; + LPSTR szOldMsg = (LPSTR)msg; if (ptr->features & FEATURES_UTF8) { // utf8 message: encrypt. @@ -184,8 +183,8 @@ LPSTR __cdecl cpp_encodeU(HANDLE context, LPCSTR msg) else { // utf8 message: convert to ansi and encrypt. LPWSTR wstring = utf8decode(szOldMsg); - int wlen = (int)wcslen(wstring)+1; - LPSTR astring = (LPSTR) alloca(wlen); + int wlen = (int)wcslen(wstring) + 1; + LPSTR astring = (LPSTR)alloca(wlen); WideCharToMultiByte(CP_ACP, 0, (LPWSTR)szOldMsg, -1, astring, wlen, 0, 0); szNewMsg = cpp_encrypt(ptr, astring); } @@ -198,11 +197,11 @@ LPSTR __cdecl cpp_encodeW(HANDLE context, LPWSTR msg) { pCNTX ptr = get_context_on_id(context); if (!ptr) return NULL; - cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA) ptr->pdata; + cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA)ptr->pdata; if (!p->KeyX) { ptr->error = ERROR_NO_KEYX; return NULL; } LPSTR szNewMsg = NULL; - LPSTR szOldMsg = (LPSTR) msg; + LPSTR szOldMsg = (LPSTR)msg; if (ptr->features & FEATURES_UTF8) { // unicode message: convert to utf-8 and encrypt. @@ -210,8 +209,8 @@ LPSTR __cdecl cpp_encodeW(HANDLE context, LPWSTR msg) } else { // unicode message: convert to ansi and encrypt. - int wlen = (int)wcslen((LPWSTR)szOldMsg)+1; - LPSTR astring = (LPSTR) alloca(wlen); + int wlen = (int)wcslen((LPWSTR)szOldMsg) + 1; + LPSTR astring = (LPSTR)alloca(wlen); WideCharToMultiByte(CP_ACP, 0, (LPWSTR)szOldMsg, -1, astring, wlen, 0, 0); szNewMsg = cpp_encrypt(ptr, astring); } @@ -225,7 +224,7 @@ LPSTR __cdecl cpp_decode(HANDLE context, LPCSTR szEncMsg) { pCNTX ptr = get_context_on_id(context); if (!ptr) return NULL; - cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA) ptr->pdata; + cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA)ptr->pdata; if (!p->KeyX) { ptr->error = ERROR_NO_KEYX; return NULL; } LPSTR szNewMsg = NULL; @@ -235,19 +234,19 @@ LPSTR __cdecl cpp_decode(HANDLE context, LPCSTR szEncMsg) if (ptr->features & FEATURES_UTF8) { // utf8 message: convert to unicode -> ansii LPWSTR wstring = utf8decode(szOldMsg); - int wlen = (int)wcslen(wstring)+1; + int wlen = (int)wcslen(wstring) + 1; szNewMsg = (LPSTR)mir_alloc(wlen*(sizeof(WCHAR)+2)); // work.zy@gmail.com WideCharToMultiByte(CP_ACP, 0, wstring, -1, szNewMsg, wlen, 0, 0); - memcpy(szNewMsg+strlen(szNewMsg)+1, wstring, wlen*sizeof(WCHAR)); // work.zy@gmail.com + memcpy(szNewMsg + strlen(szNewMsg) + 1, wstring, wlen*sizeof(WCHAR)); // work.zy@gmail.com } else { // ansi message: convert to unicode - int slen = (int)strlen(szOldMsg)+1; + int slen = (int)strlen(szOldMsg) + 1; szNewMsg = (LPSTR)mir_alloc(slen*(sizeof(WCHAR)+1)); - memcpy(szNewMsg,szOldMsg,slen); - WCHAR* wstring = (LPWSTR) alloca(slen*sizeof(WCHAR)); + memcpy(szNewMsg, szOldMsg, slen); + WCHAR* wstring = (LPWSTR)alloca(slen*sizeof(WCHAR)); MultiByteToWideChar(CP_ACP, 0, szOldMsg, -1, wstring, slen*sizeof(WCHAR)); - memcpy(szNewMsg+slen,wstring,slen*sizeof(WCHAR)); + memcpy(szNewMsg + slen, wstring, slen*sizeof(WCHAR)); } } replaceStr(ptr->tmp, szNewMsg); @@ -259,7 +258,7 @@ LPSTR __cdecl cpp_decodeU(HANDLE context, LPCSTR szEncMsg) { pCNTX ptr = get_context_on_id(context); if (!ptr) return NULL; - cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA) ptr->pdata; + cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA)ptr->pdata; if (!p->KeyX) { ptr->error = ERROR_NO_KEYX; return NULL; } LPSTR szNewMsg = NULL; @@ -272,8 +271,8 @@ LPSTR __cdecl cpp_decodeU(HANDLE context, LPCSTR szEncMsg) } else { // ansi message: convert to utf8 - int slen = (int)strlen(szOldMsg)+1; - LPWSTR wstring = (LPWSTR) alloca(slen*sizeof(WCHAR)); + int slen = (int)strlen(szOldMsg) + 1; + LPWSTR wstring = (LPWSTR)alloca(slen*sizeof(WCHAR)); MultiByteToWideChar(CP_ACP, 0, szOldMsg, -1, wstring, slen*sizeof(WCHAR)); szNewMsg = mir_strdup(utf8encode(wstring)); } @@ -282,16 +281,16 @@ LPSTR __cdecl cpp_decodeU(HANDLE context, LPCSTR szEncMsg) return szNewMsg; } -int __cdecl cpp_encrypt_file(HANDLE context,LPCSTR file_in,LPCSTR file_out) +int __cdecl cpp_encrypt_file(HANDLE context, LPCSTR file_in, LPCSTR file_out) { pCNTX ptr = get_context_on_id(context); if (!ptr) return 0; - cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA) ptr->pdata; + cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA)ptr->pdata; if (!p->KeyX) return 0; - try{ - CBC_Mode::Encryption enc(p->KeyX,Tiger::DIGESTSIZE,IV); - FileSource *f = new FileSource(file_in,true,new StreamTransformationFilter (enc,new FileSink(file_out))); + try { + CBC_Mode::Encryption enc(p->KeyX, Tiger::DIGESTSIZE, IV); + FileSource *f = new FileSource(file_in, true, new StreamTransformationFilter(enc, new FileSink(file_out))); delete f; } catch (...) { @@ -300,16 +299,16 @@ int __cdecl cpp_encrypt_file(HANDLE context,LPCSTR file_in,LPCSTR file_out) return 1; } -int __cdecl cpp_decrypt_file(HANDLE context,LPCSTR file_in,LPCSTR file_out) +int __cdecl cpp_decrypt_file(HANDLE context, LPCSTR file_in, LPCSTR file_out) { pCNTX ptr = get_context_on_id(context); if (!ptr) return 0; - cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA) ptr->pdata; + cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA)ptr->pdata; if (!p->KeyX) return 0; - try{ - CBC_Mode::Decryption dec(p->KeyX,Tiger::DIGESTSIZE,IV); - FileSource *f = new FileSource(file_in,true,new StreamTransformationFilter (dec,new FileSink(file_out))); + try { + CBC_Mode::Decryption dec(p->KeyX, Tiger::DIGESTSIZE, IV); + FileSource *f = new FileSource(file_in, true, new StreamTransformationFilter(dec, new FileSink(file_out))); delete f; } catch (...) { @@ -317,6 +316,3 @@ int __cdecl cpp_decrypt_file(HANDLE context,LPCSTR file_in,LPCSTR file_out) } return 1; } - - -// EOF -- cgit v1.2.3