From 1ab65d264fee46914571415551ed78cde14315a6 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 28 Mar 2015 13:35:02 +0000 Subject: - unused code removed; - dynamic memory allocation for JabberSha1() replaced with static git-svn-id: http://svn.miranda-ng.org/main/trunk@12535 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/JabberG/src/jabber.h | 19 +++--- protocols/JabberG/src/jabber_byte.cpp | 52 +++++++-------- protocols/JabberG/src/jabber_iqid.cpp | 9 +-- protocols/JabberG/src/jabber_misc.cpp | 5 +- protocols/JabberG/src/jabber_util.cpp | 121 +--------------------------------- 5 files changed, 44 insertions(+), 162 deletions(-) (limited to 'protocols/JabberG/src') diff --git a/protocols/JabberG/src/jabber.h b/protocols/JabberG/src/jabber.h index f16f4b60aa..2462fbaad6 100644 --- a/protocols/JabberG/src/jabber.h +++ b/protocols/JabberG/src/jabber.h @@ -738,22 +738,21 @@ struct TStringPairs TStringPairsElem* elems; }; +typedef char JabberShaStrBuf[2*MIR_SHA1_HASH_SIZE + 1]; + TCHAR* __stdcall JabberNickFromJID(const TCHAR *jid); TCHAR* JabberPrepareJid(LPCTSTR jid); -char* __stdcall JabberUrlDecode(char* str); -void __stdcall JabberUrlDecodeW(WCHAR* str); -char* __stdcall JabberUrlEncode(const char* str); -char* __stdcall JabberSha1(char* str); +void __stdcall JabberUrlDecodeW(WCHAR *str); +char* __stdcall JabberSha1(const char *str, JabberShaStrBuf buf); TCHAR* __stdcall JabberStrFixLines(const TCHAR *str); -char* __stdcall JabberUnixToDos(const char* str); -WCHAR* __stdcall JabberUnixToDosW(const WCHAR* str); -void __stdcall JabberHttpUrlDecode(TCHAR* str); +char* __stdcall JabberUnixToDos(const char *str); +WCHAR* __stdcall JabberUnixToDosW(const WCHAR *str); +void __stdcall JabberHttpUrlDecode(TCHAR *str); int __stdcall JabberCombineStatus(int status1, int status2); TCHAR* __stdcall JabberErrorStr(int errorCode); -TCHAR* __stdcall JabberErrorMsg(HXML errorNode, int* errorCode = NULL); -void __stdcall JabberUtfToTchar(const char* str, size_t cbLen, LPTSTR& dest); +TCHAR* __stdcall JabberErrorMsg(HXML errorNode, int *errorCode = NULL); time_t __stdcall JabberIsoToUnixTime(const TCHAR *stamp); -TCHAR* __stdcall JabberStripJid(const TCHAR *jid, TCHAR* dest, size_t destLen); +TCHAR* __stdcall JabberStripJid(const TCHAR *jid, TCHAR *dest, size_t destLen); int __stdcall JabberGetPacketID(HXML n); TCHAR* __stdcall JabberId2string(int id); diff --git a/protocols/JabberG/src/jabber_byte.cpp b/protocols/JabberG/src/jabber_byte.cpp index 5082c767ed..0e86f62a08 100644 --- a/protocols/JabberG/src/jabber_byte.cpp +++ b/protocols/JabberG/src/jabber_byte.cpp @@ -316,7 +316,6 @@ int CJabberProto::ByteSendParse(HANDLE hConn, JABBER_BYTE_TRANSFER *jbt, char* b int nMethods; BYTE data[10]; int i; - char* str; switch (jbt->state) { case JBT_INIT: @@ -368,30 +367,29 @@ int CJabberProto::ByteSendParse(HANDLE hConn, JABBER_BYTE_TRANSFER *jbt, char* b mir_free(szInitiatorJid); mir_free(szTargetJid); - char* szAuthString = mir_utf8encodeT(text); + ptrA szAuthString(mir_utf8encodeT(text)); debugLogA("Auth: '%s'", szAuthString); - if ((str = JabberSha1(szAuthString)) != NULL) { - for (i=0; i<40 && buffer[i+5]==str[i]; i++); - mir_free(str); - memset(data, 0, sizeof(data)); - data[1] = (i>=20)?0:2; - data[0] = 5; - data[3] = 1; - Netlib_Send(hConn, (char*)data, 10, 0); + JabberShaStrBuf buf; + JabberSha1(szAuthString, buf); + for (i=0; i<40 && buffer[i+5]==buf[i]; i++); - // wait stream activation - WaitForSingleObject(jbt->hSendEvent, INFINITE); + memset(data, 0, sizeof(data)); + data[1] = (i>=20)?0:2; + data[0] = 5; + data[3] = 1; + Netlib_Send(hConn, (char*)data, 10, 0); - if (jbt->state == JBT_ERROR) - break; + // wait stream activation + WaitForSingleObject(jbt->hSendEvent, INFINITE); - if (i>=20 && (this->*jbt->pfnSend)(hConn, jbt->ft)==TRUE) - jbt->state = JBT_DONE; - else - jbt->state = JBT_ERROR; - } - mir_free(szAuthString); + if (jbt->state == JBT_ERROR) + break; + + if (i>=20 && (this->*jbt->pfnSend)(hConn, jbt->ft)==TRUE) + jbt->state = JBT_DONE; + else + jbt->state = JBT_ERROR; } else jbt->state = JBT_ERROR; @@ -520,9 +518,10 @@ int CJabberProto::ByteSendProxyParse(HANDLE hConn, JABBER_BYTE_TRANSFER *jbt, ch char* szAuthString = mir_utf8encodeT(text); debugLogA("Auth: '%s'", szAuthString); - char* szHash = JabberSha1(szAuthString); - strncpy_s((char*)(data + 5), 40, szHash, _TRUNCATE); - mir_free(szHash); + + JabberShaStrBuf buf; + strncpy_s((char*)(data + 5), 40, JabberSha1(szAuthString, buf), _TRUNCATE); + Netlib_Send(hConn, (char*)data, 47, 0); jbt->state = JBT_CONNECT; mir_free(szAuthString); @@ -713,9 +712,10 @@ int CJabberProto::ByteReceiveParse(HANDLE hConn, JABBER_BYTE_TRANSFER *jbt, char mir_free(szTargetJid); char* szAuthString = mir_utf8encodeT(text); debugLogA("Auth: '%s'", szAuthString); - char* szHash = JabberSha1(szAuthString); - strncpy_s((char*)(data + 5), 40, szHash, _TRUNCATE); - mir_free(szHash); + + JabberShaStrBuf buf; + strncpy_s((char*)(data + 5), 40, JabberSha1(szAuthString, buf), _TRUNCATE); + Netlib_Send(hConn, (char*)data, 47, 0); jbt->state = JBT_CONNECT; mir_free(szAuthString); diff --git a/protocols/JabberG/src/jabber_iqid.cpp b/protocols/JabberG/src/jabber_iqid.cpp index cc0f8d4bd4..d502c21daf 100644 --- a/protocols/JabberG/src/jabber_iqid.cpp +++ b/protocols/JabberG/src/jabber_iqid.cpp @@ -216,14 +216,11 @@ void CJabberProto::OnIqResultGetAuth(HXML iqNode, CJabberIqInfo*) HXML query = iq << XQUERY(_T("jabber:iq:auth")); query << XCHILD(_T("username"), m_ThreadInfo->conn.username); if (xmlGetChild(queryNode, "digest") != NULL && m_ThreadInfo->szStreamId) { - char* str = mir_utf8encodeT(m_ThreadInfo->conn.password); + JabberShaStrBuf buf; + ptrA str(mir_utf8encodeT(m_ThreadInfo->conn.password)); char text[200]; mir_snprintf(text, SIZEOF(text), "%s%s", m_ThreadInfo->szStreamId, str); - mir_free(str); - if ((str = JabberSha1(text)) != NULL) { - query << XCHILD(_T("digest"), _A2T(str)); - mir_free(str); - } + query << XCHILD(_T("digest"), _A2T(JabberSha1(text, buf))); } else if (xmlGetChild(queryNode, "password") != NULL) query << XCHILD(_T("password"), m_ThreadInfo->conn.password); diff --git a/protocols/JabberG/src/jabber_misc.cpp b/protocols/JabberG/src/jabber_misc.cpp index ae21a785a7..976b275a9c 100644 --- a/protocols/JabberG/src/jabber_misc.cpp +++ b/protocols/JabberG/src/jabber_misc.cpp @@ -192,7 +192,8 @@ void CJabberProto::GetAvatarFileName(MCONTACT hContact, TCHAR* pszDest, size_t c const TCHAR* szFileType = ProtoGetAvatarExtension( getByte(hContact, "AvatarType", PA_FORMAT_PNG)); if (hContact != NULL) { - char str[ 256 ]; + char str[256]; + JabberShaStrBuf buf; DBVARIANT dbv; if (!db_get_utf(hContact, m_szModuleName, "jid", &dbv)) { strncpy_s(str, dbv.pszVal, _TRUNCATE); @@ -200,7 +201,7 @@ void CJabberProto::GetAvatarFileName(MCONTACT hContact, TCHAR* pszDest, size_t c db_free(&dbv); } else _i64toa((LONG_PTR)hContact, str, 10); - mir_sntprintf(pszDest + tPathLen, MAX_PATH - tPathLen, _T("%S%s"), ptrA(JabberSha1(str)), szFileType); + mir_sntprintf(pszDest + tPathLen, MAX_PATH - tPathLen, _T("%S%s"), JabberSha1(str, buf), szFileType); } else if (m_ThreadInfo != NULL) { mir_sntprintf(pszDest + tPathLen, MAX_PATH - tPathLen, _T("%s@%S avatar%s"), diff --git a/protocols/JabberG/src/jabber_util.cpp b/protocols/JabberG/src/jabber_util.cpp index 68f7acd2c7..5ca0204854 100644 --- a/protocols/JabberG/src/jabber_util.cpp +++ b/protocols/JabberG/src/jabber_util.cpp @@ -143,40 +143,6 @@ void strdel(char* parBuffer, int len) p[-len] = '\0'; } -char* __stdcall JabberUrlDecode(char *str) -{ - char* p, *q; - - if (str == NULL) - return NULL; - - for (p = q = str; *p != '\0'; p++, q++) { - if (*p == '<') { - // skip CDATA - if (!strncmp(p, ""); - size_t count = tail ? (tail - p) : strlen(p); - memmove(q, p, count); - q += count - 1; - p = (tail ? (tail + 3) : (p + count)) - 1; - } - else *q = *p; - } - else if (*p == '&') { - if (!strncmp(p, "&", 5)) { *q = '&'; p += 4; } - else if (!strncmp(p, "'", 6)) { *q = '\''; p += 5; } - else if (!strncmp(p, ">", 4)) { *q = '>'; p += 3; } - else if (!strncmp(p, "<", 4)) { *q = '<'; p += 3; } - else if (!strncmp(p, """, 6)) { *q = '"'; p += 5; } - else { *q = *p; } - } - else *q = *p; - } - *q = '\0'; - return str; -} - void __stdcall JabberUrlDecodeW(WCHAR *str) { if (str == NULL) @@ -199,97 +165,16 @@ void __stdcall JabberUrlDecodeW(WCHAR *str) *q = '\0'; } -char* __stdcall JabberUrlEncode(const char *str) -{ - char* s, *p, *q; - int c; - - if (str == NULL) - return NULL; - - for (c = 0, p = (char*)str; *p != '\0'; p++) { - switch (*p) { - case '&': c += 5; break; - case '\'': c += 6; break; - case '>': c += 4; break; - case '<': c += 4; break; - case '"': c += 6; break; - default: c++; break; - } - } - if ((s = (char*)mir_alloc(c + 1)) != NULL) { - for (p = (char*)str, q = s; *p != '\0'; p++) { - switch (*p) { - case '&': strcpy(q, "&"); q += 5; break; - case '\'': strcpy(q, "'"); q += 6; break; - case '>': strcpy(q, ">"); q += 4; break; - case '<': strcpy(q, "<"); q += 4; break; - case '"': strcpy(q, """); q += 6; break; - default: - if (*p > 0 && *p < 32) { - switch (*p) { - case '\r': - case '\n': - case '\t': - *q = *p; - break; - default: - *q = '?'; - } - } - else *q = *p; - q++; - break; - } - } - *q = '\0'; - } - - return s; -} - -void __stdcall JabberUtfToTchar(const char *pszValue, size_t cbLen, LPTSTR &dest) -{ - char* pszCopy = NULL; - bool bNeedsFree = false; - __try { - // this code can cause access violation when a stack overflow occurs - pszCopy = (char*)alloca(cbLen + 1); - } - __except (EXCEPTION_EXECUTE_HANDLER) - { - bNeedsFree = true; - pszCopy = (char*)malloc(cbLen + 1); - } - if (pszCopy == NULL) - return; - - memcpy(pszCopy, pszValue, cbLen); - pszCopy[cbLen] = 0; - - JabberUrlDecode(pszCopy); - - mir_utf8decode(pszCopy, &dest); - - if (bNeedsFree) - free(pszCopy); -} - -char* __stdcall JabberSha1(char* str) +char* __stdcall JabberSha1(const char *str, JabberShaStrBuf buf) { - if (str == NULL) - return NULL; - BYTE digest[MIR_SHA1_HASH_SIZE]; mir_sha1_ctx sha; mir_sha1_init(&sha); mir_sha1_append(&sha, (BYTE*)str, (int)strlen(str)); mir_sha1_finish(&sha, digest); - char *result = (char*)mir_alloc(41); - if (result) - bin2hex(digest, sizeof(digest), result); - return result; + bin2hex(digest, sizeof(digest), buf); + return buf; } TCHAR* __stdcall JabberStrFixLines(const TCHAR *str) -- cgit v1.2.3