From 8751885ea79df4b666b65bb2b6900617785e0da7 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 4 Jun 2013 07:55:00 +0000 Subject: end of base64* zoo git-svn-id: http://svn.miranda-ng.org/main/trunk@4879 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- src/mir_core/utils.cpp | 25 ++++----- src/modules/netlib/netlib.cpp | 92 --------------------------------- src/modules/netlib/netlibsecurity.cpp | 60 +++++++++------------- src/modules/utils/utils.cpp | 96 ----------------------------------- 4 files changed, 34 insertions(+), 239 deletions(-) (limited to 'src') diff --git a/src/mir_core/utils.cpp b/src/mir_core/utils.cpp index 02593de4fa..be9022188f 100644 --- a/src/mir_core/utils.cpp +++ b/src/mir_core/utils.cpp @@ -220,8 +220,7 @@ MIR_CORE_DLL(char*) mir_base64_encode(const BYTE *input, unsigned inputLen) if (input == NULL) return NULL; - size_t i = 0; - char chr[3], enc[4]; + BYTE chr[3]; static char cb64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; size_t length = inputLen; @@ -230,22 +229,20 @@ MIR_CORE_DLL(char*) mir_base64_encode(const BYTE *input, unsigned inputLen) char *output = (char *)mir_alloc(nLength); char *p = output; - while (i < length) + for (size_t i=0; i < length; ) { chr[0] = input[i++]; chr[1] = input[i++]; chr[2] = input[i++]; - enc[0] = chr[0] >> 2; - enc[1] = ((chr[0] & 0x03) << 4) | (chr[1] >> 4); - enc[2] = ((chr[1] & 0x0F) << 2) | (chr[2] >> 6); - enc[3] = chr[2] & 0x3F; - - *p++ = cb64[enc[0]]; *p++ = cb64[enc[1]]; + *p++ = cb64[ chr[0] >> 2 ]; + *p++ = cb64[ ((chr[0] & 0x03) << 4) | (chr[1] >> 4) ]; + BYTE b2 = ((chr[1] & 0x0F) << 2) | (chr[2] >> 6), + b3 = chr[2] & 0x3F; if (i - 2 >= length) { *p++ = '='; *p++ = '='; } - else if (i - 1 >= length) { *p++ = cb64[enc[2]]; *p++ = '='; } - else { *p++ = cb64[enc[2]]; *p++ = cb64[enc[3]]; } + else if (i - 1 >= length) { *p++ = cb64[b2]; *p++ = '='; } + else { *p++ = cb64[b2]; *p++ = cb64[b3]; } } *p = 0; @@ -281,12 +278,12 @@ MIR_CORE_DLL(void*) mir_base64_decode(const char *input, unsigned *outputLen) return NULL; size_t i = 0; - char chr[3], enc[4]; + BYTE chr[3], enc[4]; size_t length = strlen(input); - size_t nLength = (length / 4) * 3 + 1; + size_t nLength = (length / 4) * 3; - char *output = (char *)mir_alloc(nLength); + char *output = (char *)mir_alloc(nLength+1); char *p = output; while (i < length) diff --git a/src/modules/netlib/netlib.cpp b/src/modules/netlib/netlib.cpp index f3ba8e0e12..b7a3bc3c2d 100644 --- a/src/modules/netlib/netlib.cpp +++ b/src/modules/netlib/netlib.cpp @@ -424,96 +424,6 @@ INT_PTR NetlibHttpUrlEncode(WPARAM, LPARAM lParam) return (INT_PTR)p; } -static const char base64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -INT_PTR NetlibBase64Encode(WPARAM, LPARAM lParam) -{ - NETLIBBASE64 *nlb64 = (NETLIBBASE64*)lParam; - int iIn; - char *pszOut; - PBYTE pbIn; - - if (nlb64 == NULL || nlb64->pszEncoded == NULL || nlb64->pbDecoded == NULL) { - SetLastError(ERROR_INVALID_PARAMETER); - return 0; - } - if (nlb64->cchEncodedcbDecoded)) { - SetLastError(ERROR_BUFFER_OVERFLOW); - return 0; - } - nlb64->cchEncoded = Netlib_GetBase64EncodedBufferSize(nlb64->cbDecoded); - for (iIn = 0, pbIn = nlb64->pbDecoded, pszOut = nlb64->pszEncoded;iIncbDecoded;iIn+=3, pbIn+=3, pszOut+=4) { - pszOut[0] = base64chars[pbIn[0]>>2]; - if (nlb64->cbDecoded-iIn == 1) { - pszOut[1] = base64chars[(pbIn[0]&3)<<4]; - pszOut[2] = '='; - pszOut[3] = '='; - pszOut+=4; - break; - } - pszOut[1] = base64chars[((pbIn[0]&3)<<4)|(pbIn[1]>>4)]; - if (nlb64->cbDecoded-iIn == 2) { - pszOut[2] = base64chars[(pbIn[1]&0xF)<<2]; - pszOut[3] = '='; - pszOut+=4; - break; - } - pszOut[2] = base64chars[((pbIn[1]&0xF)<<2)|(pbIn[2]>>6)]; - pszOut[3] = base64chars[pbIn[2]&0x3F]; - } - pszOut[0] = '\0'; - return 1; -} - -static BYTE Base64CharToInt(char c) -{ - if (c>='A' && c <= 'Z') return c-'A'; - if (c>='a' && c <= 'z') return c-'a'+26; - if (c>='0' && c <= '9') return c-'0'+52; - if (c == '+') return 62; - if (c == '/') return 63; - if (c == '=') return 64; - return 255; -} - -INT_PTR NetlibBase64Decode(WPARAM, LPARAM lParam) -{ - NETLIBBASE64 *nlb64 = (NETLIBBASE64*)lParam; - char *pszIn; - PBYTE pbOut; - BYTE b1, b2, b3, b4; - int iIn; - - if (nlb64 == NULL || nlb64->pszEncoded == NULL || nlb64->pbDecoded == NULL) { - SetLastError(ERROR_INVALID_PARAMETER); - return 0; - } - if (nlb64->cchEncoded&3) { - SetLastError(ERROR_INVALID_DATA); - return 0; - } - if (nlb64->cbDecodedcchEncoded)) { - SetLastError(ERROR_BUFFER_OVERFLOW); - return 0; - } - nlb64->cbDecoded = Netlib_GetBase64DecodedBufferSize(nlb64->cchEncoded); - for (iIn = 0, pszIn = nlb64->pszEncoded, pbOut = nlb64->pbDecoded;iIncchEncoded;iIn+=4, pszIn+=4, pbOut+=3) { - b1 = Base64CharToInt(pszIn[0]); - b2 = Base64CharToInt(pszIn[1]); - b3 = Base64CharToInt(pszIn[2]); - b4 = Base64CharToInt(pszIn[3]); - if (b1 == 255 || b1 == 64 || b2 == 255 || b2 == 64 || b3 == 255 || b4 == 255) { - SetLastError(ERROR_INVALID_DATA); - return 0; - } - pbOut[0] = (b1<<2)|(b2>>4); - if (b3 == 64) {nlb64->cbDecoded-=2; break;} - pbOut[1] = (b2<<4)|(b3>>2); - if (b4 == 64) {nlb64->cbDecoded--; break;} - pbOut[2] = b4|(b3<<6); - } - return 1; -} - void UnloadNetlibModule(void) { if ( !bModuleInitialized) return; @@ -619,8 +529,6 @@ int LoadNetlibModule(void) CreateServiceFunction(MS_NETLIB_SETSTICKYHEADERS, NetlibHttpSetSticky); CreateServiceFunction(MS_NETLIB_GETSOCKET, NetlibGetSocket); CreateServiceFunction(MS_NETLIB_URLENCODE, NetlibHttpUrlEncode); - CreateServiceFunction(MS_NETLIB_BASE64ENCODE, NetlibBase64Encode); - CreateServiceFunction(MS_NETLIB_BASE64DECODE, NetlibBase64Decode); CreateServiceFunction(MS_NETLIB_SENDHTTPREQUEST, NetlibHttpSendRequest); CreateServiceFunction(MS_NETLIB_RECVHTTPHEADERS, NetlibHttpRecvHeaders); CreateServiceFunction(MS_NETLIB_FREEHTTPREQUESTSTRUCT, NetlibHttpFreeRequestStruct); diff --git a/src/modules/netlib/netlibsecurity.cpp b/src/modules/netlib/netlibsecurity.cpp index 33656dc868..2f538a06fd 100644 --- a/src/modules/netlib/netlibsecurity.cpp +++ b/src/modules/netlib/netlibsecurity.cpp @@ -252,14 +252,7 @@ char* CompleteGssapi(HANDLE hSecurity, unsigned char *szChallenge, unsigned chls p += outBuffersDesc.pBuffers[i].cbBuffer; } - NETLIBBASE64 nlb64; - nlb64.cbDecoded = ressz; - nlb64.pbDecoded = response; - nlb64.cchEncoded = Netlib_GetBase64EncodedBufferSize(nlb64.cbDecoded); - nlb64.pszEncoded = (char*)alloca(nlb64.cchEncoded); - if ( !NetlibBase64Encode(0, (LPARAM)&nlb64)) return NULL; - - return mir_strdup(nlb64.pszEncoded); + return mir_base64_encode(response, ressz); } char* NtlmCreateResponseFromChallenge(HANDLE hSecurity, const char *szChallenge, const TCHAR* login, const TCHAR* psw, bool http, unsigned& complete) @@ -269,7 +262,7 @@ char* NtlmCreateResponseFromChallenge(HANDLE hSecurity, const char *szChallenge, SecBuffer outputSecurityToken, inputSecurityToken; TimeStamp tokenExpiration; ULONG contextAttributes; - NETLIBBASE64 nlb64 = { 0 }; + char *szOutputToken; NtlmHandleType* hNtlm = (NtlmHandleType*)hSecurity; @@ -282,30 +275,29 @@ char* NtlmCreateResponseFromChallenge(HANDLE hSecurity, const char *szChallenge, bool hasChallenge = szChallenge != NULL && szChallenge[0] != '\0'; if (hasChallenge) { - nlb64.cchEncoded = lstrlenA(szChallenge); - nlb64.pszEncoded = (char*)szChallenge; - nlb64.cbDecoded = Netlib_GetBase64DecodedBufferSize(nlb64.cchEncoded); - nlb64.pbDecoded = (PBYTE)alloca(nlb64.cbDecoded); - if ( !NetlibBase64Decode(0, (LPARAM)&nlb64)) return NULL; + unsigned tokenLen; + BYTE *token = (BYTE*)mir_base64_decode(szChallenge, &tokenLen); + if (token == NULL) + return NULL; if (isGSSAPI && complete) - return CompleteGssapi(hSecurity, nlb64.pbDecoded, nlb64.cbDecoded); + return CompleteGssapi(hSecurity, token, tokenLen); inputBufferDescriptor.cBuffers = 1; inputBufferDescriptor.pBuffers = &inputSecurityToken; inputBufferDescriptor.ulVersion = SECBUFFER_VERSION; inputSecurityToken.BufferType = SECBUFFER_TOKEN; - inputSecurityToken.cbBuffer = nlb64.cbDecoded; - inputSecurityToken.pvBuffer = nlb64.pbDecoded; + inputSecurityToken.cbBuffer = tokenLen; + inputSecurityToken.pvBuffer = token; // try to decode the domain name from the NTLM challenge if (login != NULL && login[0] != '\0' && !hNtlm->hasDomain) { - NtlmType2packet* pkt = (NtlmType2packet*)nlb64.pbDecoded; + NtlmType2packet* pkt = (NtlmType2packet*)token; if ( !strncmp(pkt->sign, "NTLMSSP", 8) && pkt->type == 2) { - wchar_t* domainName = (wchar_t*)&nlb64.pbDecoded[pkt->targetName.offset]; + wchar_t* domainName = (wchar_t*)&token[pkt->targetName.offset]; int domainLen = pkt->targetName.len; // Negotiate ANSI? if yes, convert the ANSI name to unicode @@ -412,8 +404,7 @@ char* NtlmCreateResponseFromChallenge(HANDLE hSecurity, const char *szChallenge, return NULL; } - nlb64.cbDecoded = outputSecurityToken.cbBuffer; - nlb64.pbDecoded = (PBYTE)outputSecurityToken.pvBuffer; + szOutputToken = mir_base64_encode((PBYTE)outputSecurityToken.pvBuffer, outputSecurityToken.cbBuffer); } else { @@ -425,30 +416,25 @@ char* NtlmCreateResponseFromChallenge(HANDLE hSecurity, const char *szChallenge, size_t authLen = strlen(szLogin) + strlen(szPassw) + 5; char *szAuth = (char*)alloca(authLen); - nlb64.cbDecoded = mir_snprintf(szAuth, authLen, "%s:%s", szLogin, szPassw); - nlb64.pbDecoded = (PBYTE)szAuth; + mir_snprintf(szAuth, authLen, "%s:%s", szLogin, szPassw); + szOutputToken = mir_strdup(szAuth); complete = true; mir_free(szPassw); mir_free(szLogin); } - nlb64.cchEncoded = Netlib_GetBase64EncodedBufferSize(nlb64.cbDecoded); - nlb64.pszEncoded = (char*)alloca(nlb64.cchEncoded); - if ( !NetlibBase64Encode(0, (LPARAM)&nlb64)) return NULL; + if (szOutputToken == NULL) + return NULL; - char* result; - if (http) - { - char* szProvider = mir_t2a(hNtlm->szProvider); - nlb64.cchEncoded += (int)strlen(szProvider) + 10; - result = (char*)mir_alloc(nlb64.cchEncoded); - mir_snprintf(result, nlb64.cchEncoded, "%s %s", szProvider, nlb64.pszEncoded); - mir_free(szProvider); - } - else - result = mir_strdup(nlb64.pszEncoded); + if (!http) + return mir_strdup(szOutputToken); + ptrA szProvider( mir_t2a(hNtlm->szProvider)); + size_t resLen = strlen(szOutputToken) + strlen(szProvider) + 10; + char *result = (char*)mir_alloc(resLen); + mir_snprintf(result, resLen, "%s %s", szProvider, szOutputToken); + mir_free(szOutputToken); return result; } diff --git a/src/modules/utils/utils.cpp b/src/modules/utils/utils.cpp index c6bd60a59b..5d0813c483 100644 --- a/src/modules/utils/utils.cpp +++ b/src/modules/utils/utils.cpp @@ -472,99 +472,6 @@ static INT_PTR GenerateRandom(WPARAM wParam, LPARAM lParam) ///////////////////////////////////////////////////////////////////////////////////////// -static INT_PTR EncodeBase64(WPARAM wParam, LPARAM lParam) -{ - size_t i = 0; - char chr[3], enc[4]; - static char cb64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - const char *input = (const char *)lParam; - size_t length = strlen(input); - size_t nLength = 4 * ((length + 2) / 3) + 1; - - char *output = (char *)mir_alloc(nLength); - char *p = output; - - while (i < length) - { - chr[0] = input[i++]; - chr[1] = input[i++]; - chr[2] = input[i++]; - - enc[0] = chr[0] >> 2; - enc[1] = ((chr[0] & 0x03) << 4) | (chr[1] >> 4); - enc[2] = ((chr[1] & 0x0F) << 2) | (chr[2] >> 6); - enc[3] = chr[2] & 0x3F; - - *p++ = cb64[enc[0]]; *p++ = cb64[enc[1]]; - - if (i - 2 >= length) { *p++ = '='; *p++ = '='; } - else if (i - 1 >= length) { *p++ = cb64[enc[2]]; *p++ = '='; } - else { *p++ = cb64[enc[2]]; *p++ = cb64[enc[3]]; } - } - - *p = 0; - - return (INT_PTR)output; -} - -static int Base64DecodeTable[] = -{ - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, - 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, - 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, - -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, - 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1 -}; - -static INT_PTR DecodeBase64(WPARAM wParam, LPARAM lParam) -{ - size_t i = 0; - char chr[3], enc[4]; - - const char *input = (const char *)lParam; - size_t length = strlen(input); - size_t nLength = (length / 4) * 3 + 1; - - char *output = (char *)mir_alloc(nLength); - char *p = output; - - while (i < length) - { - enc[0] = Base64DecodeTable[input[i++]]; - enc[1] = Base64DecodeTable[input[i++]]; - enc[2] = Base64DecodeTable[input[i++]]; - enc[3] = Base64DecodeTable[input[i++]]; - - if (enc[0] == -1 || enc[1] == -1) break; - - chr[0] = (enc[0] << 2) | (enc[1] >> 4); - chr[1] = ((enc[1] & 15) << 4) | (enc[2] >> 2); - chr[2] = ((enc[2] & 3) << 6) | enc[3]; - - *p++ = chr[0]; - - if (enc[2] != -1) *p++ = chr[1]; - if (enc[3] != -1) *p++ = chr[2]; - } - - *p = 0; - - return (INT_PTR)output; -} - -///////////////////////////////////////////////////////////////////////////////////////// int LoadUtilsModule(void) { bModuleInitialized = TRUE; @@ -579,9 +486,6 @@ int LoadUtilsModule(void) CreateServiceFunction(MS_UTILS_GETRANDOM, GenerateRandom); CreateServiceFunction(MS_SYSTEM_RESTART, RestartMiranda); - CreateServiceFunction(MS_UTILS_ENCODEBASE64, EncodeBase64); - CreateServiceFunction(MS_UTILS_DECODEBASE64, DecodeBase64); - InitOpenUrl(); InitWindowList(); InitHyperlink(); -- cgit v1.2.3