From b40015c18a9acd7272d850df4389de98009a91f4 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 24 Jun 2013 15:40:31 +0000 Subject: memory leak fix inside bytesToHex git-svn-id: http://svn.miranda-ng.org/main/trunk@5122 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp | 2 - protocols/WhatsApp/src/WhatsAPI++/WALogin.cpp | 6 +- protocols/WhatsApp/src/WhatsAPI++/utilities.cpp | 33 +++------- protocols/WhatsApp/src/WhatsAPI++/utilities.h | 70 ++++++++++++---------- protocols/WhatsApp/src/proto.cpp | 7 ++- protocols/WhatsApp/src/utils.cpp | 10 +++- 6 files changed, 61 insertions(+), 67 deletions(-) (limited to 'protocols/WhatsApp') diff --git a/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp b/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp index d5440d16f8..49f02697c7 100644 --- a/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp +++ b/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp @@ -11,8 +11,6 @@ #include #include "utilities.h" -std::string base64_encode(void*, size_t); - const char* WAConnection::dictionary[] = { "", "", diff --git a/protocols/WhatsApp/src/WhatsAPI++/WALogin.cpp b/protocols/WhatsApp/src/WhatsAPI++/WALogin.cpp index 37d5fe0378..af21776b83 100644 --- a/protocols/WhatsApp/src/WhatsAPI++/WALogin.cpp +++ b/protocols/WhatsApp/src/WhatsAPI++/WALogin.cpp @@ -91,12 +91,12 @@ std::string WALogin::getResponse(const std::string& challenge) { std::string digest_uri = "xmpp/" + this->domain; std::vector* A1 = bos.toByteArray(); std::string A2 = "AUTHENTICATE:" + digest_uri; - std::string KD((char*) bytesToHex(utils::md5string(&A1->front(), (int)A1->size(), md5_buffer), SIZEOF(md5_buffer)), SIZEOF(md5_buffer) * 2); - KD += + ":" + nonce + ":" + nc + ":" + cnonce + ":auth:" + std::string((char*) bytesToHex(utils::md5string(A2, md5_buffer), SIZEOF(md5_buffer)), SIZEOF(md5_buffer)*2); + std::string KD = bytesToHex(utils::md5string(&A1->front(), (int)A1->size(), md5_buffer), SIZEOF(md5_buffer)); + KD += + ":" + nonce + ":" + nc + ":" + cnonce + ":auth:" + bytesToHex(utils::md5string(A2, md5_buffer), SIZEOF(md5_buffer)); _LOGDATA("KD = %s", KD.c_str()); - std::string response((char*) bytesToHex(utils::md5string(KD, md5_buffer), SIZEOF(md5_buffer)), SIZEOF(md5_buffer)*2); + std::string response = bytesToHex(utils::md5string(KD, md5_buffer), SIZEOF(md5_buffer)); _LOGDATA("response = %s", response.c_str()); diff --git a/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp b/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp index 90d03dbdaa..7b7af160e7 100644 --- a/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp +++ b/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp @@ -1,5 +1,4 @@ #include "utilities.h" -//#include "ApplicationData.h" #include #include #include @@ -11,9 +10,6 @@ #include #include -// #TODO Remove Miranda dependency -#include "../common.h" - namespace Utilities{ const static char digits[] = { @@ -84,7 +80,7 @@ std::string processIdentity(const std::string& id){ std::string buffer_str = reverseString(id); unsigned char digest[16]; - utils::md5string(buffer_str, digest); + md5_string(buffer_str, digest); buffer_str.clear(); for(int i =0; i < 16; i++){ @@ -132,7 +128,7 @@ std::string str(int64_t i, int radix ) { } int64_t randLong() { - std::srand(time(NULL)); + std::srand((unsigned)time(NULL)); int64_t r = (int64_t) ((char) (std::rand() % 256)); for (int i = 0; i < 7 ; i++) @@ -195,18 +191,14 @@ long long parseLongLong(const std::string& str) { return val; } -unsigned char* bytesToHex(unsigned char* bytes, int length) { - unsigned char* ret = new unsigned char[length*2]; +string bytesToHex(unsigned char* bytes, int length) { + string ret(length*2, ' '); + string::iterator p = ret.begin(); int i = 0; for (int c = 0; c < length; c++) { int ub = bytes[c]; - - if (ub < 0) - ub += 256; - ret[i] = forDigit(ub >> 4); - i++; - ret[i] = forDigit(ub % 16); - i++; + *p++ = forDigit(ub >> 4); + *p++ = forDigit(ub % 16); } return ret; @@ -218,15 +210,6 @@ unsigned char forDigit(int b) { return (unsigned char) (97 + b - 10); } -string md5String(const string& data) { - unsigned char md5_buffer[16+1]; - md5_buffer[16] = '\0'; - utils::md5string(data, md5_buffer); - std::string result((char*) Utilities::bytesToHex(md5_buffer, 16), 16*2); - - return result; -} - bool saveStringToFile(const string& data, const string& filePath) { std::ofstream out(filePath.c_str()); if (out.fail()) return false; @@ -261,7 +244,7 @@ bool saveBytesToFile(const string& data, const string& filePath) { vector* loadFileToBytes(const string& path) { vector* bytes = NULL; std::ifstream in(path.c_str(), ios::in | ios::binary | ios::ate); - long size = in.tellg(); + size_t size = in.tellg(); if (in.fail()) return NULL; in.seekg(0, ios::beg); diff --git a/protocols/WhatsApp/src/WhatsAPI++/utilities.h b/protocols/WhatsApp/src/WhatsAPI++/utilities.h index 1c071471bd..fec1b29862 100644 --- a/protocols/WhatsApp/src/WhatsAPI++/utilities.h +++ b/protocols/WhatsApp/src/WhatsAPI++/utilities.h @@ -43,40 +43,44 @@ #define _LOGDATA(format, ...) if (_DEBUGENABLED) Utilities::logData(format, ##__VA_ARGS__) using namespace std; + +// these functions must be declared somewhere in the same linking module +std::string base64_encode(void*, size_t); +void md5_string(const std::string& data, unsigned char digest[16]); + namespace Utilities{ - extern void configureLogging(const char* ident); - extern void closeLog(); - extern string getCountryCode(); - extern string getMcc(); - extern string getMnc(); - extern string reverseString(const string& str); - extern string processIdentity(const std::string& password); - extern int64_t randLong(); - extern int64_t absLong(int64_t num); - extern string str(int64_t number, int radix); - extern std::string itoa(int value, unsigned int base); - extern std::string intToStr(int i); - extern std::string doubleToStr(double d); - extern long long parseLongLong(const std::string& str); - extern time_t parseBBDate(const string& s); - extern void logData(const char *msg, ...); - extern long long getCurrentTimeMillis(); - extern unsigned char* bytesToHex(unsigned char* bytes, int length); - extern unsigned char forDigit(int b); - extern string md5String(const string& data); - extern bool saveStringToFile(const string& data, const string& filePath); - extern bool saveBytesToFile(const string& data, const string& filePath); - extern bool saveBytesToFile(const std::vector& data, const string& filePath); - extern string removeWaDomainFromJid(const string& jid); - extern string getNameFromPath(const std::string& path); - extern vector* loadFileToBytes(const string& path); - extern bool fileExists(const std::string& path); - extern std::vector* getChallengeData(const std::string& file); - extern bool saveChallengeData(const std::vector& data, const std::string& file); - extern std::string utf8_to_utf16(const std::string& utf8); - extern std::string string_format(const std::string fmt, ...); - extern std::string string_format(const std::string fmt, va_list ap); - extern std::string string_format(const char* fmt, va_list ap); + void configureLogging(const char* ident); + void closeLog(); + string getCountryCode(); + string getMcc(); + string getMnc(); + string reverseString(const string& str); + string processIdentity(const std::string& password); + int64_t randLong(); + int64_t absLong(int64_t num); + string str(int64_t number, int radix); + std::string itoa(int value, unsigned int base); + std::string intToStr(int i); + std::string doubleToStr(double d); + long long parseLongLong(const std::string& str); + time_t parseBBDate(const string& s); + void logData(const char *msg, ...); + long long getCurrentTimeMillis(); + std::string bytesToHex(unsigned char* bytes, int length); + unsigned char forDigit(int b); + bool saveStringToFile(const string& data, const string& filePath); + bool saveBytesToFile(const string& data, const string& filePath); + bool saveBytesToFile(const std::vector& data, const string& filePath); + string removeWaDomainFromJid(const string& jid); + string getNameFromPath(const std::string& path); + vector* loadFileToBytes(const string& path); + bool fileExists(const std::string& path); + std::vector* getChallengeData(const std::string& file); + bool saveChallengeData(const std::vector& data, const std::string& file); + std::string utf8_to_utf16(const std::string& utf8); + std::string string_format(const std::string fmt, ...); + std::string string_format(const std::string fmt, va_list ap); + std::string string_format(const char* fmt, va_list ap); } #endif diff --git a/protocols/WhatsApp/src/proto.cpp b/protocols/WhatsApp/src/proto.cpp index a1da570953..de7835cd86 100644 --- a/protocols/WhatsApp/src/proto.cpp +++ b/protocols/WhatsApp/src/proto.cpp @@ -212,15 +212,16 @@ string WhatsAppProto::Register(int state, string cc, string number, string code) BYTE idxBuf[16]; utils::md5string(tm.str(), idxBuf); idx = std::string((const char*) idxBuf, 16); - db_set_s(0, m_szModuleName,WHATSAPP_KEY_IDX, idx.c_str()); + db_set_s(0, m_szModuleName, WHATSAPP_KEY_IDX, idx.c_str()); } string url; if (state == REG_STATE_REQ_CODE) { - string token(Utilities::md5String(std::string(ACCOUNT_TOKEN_PREFIX1) + ACCOUNT_TOKEN_PREFIX2 + number)); + unsigned char digest[16]; + utils::md5string(std::string(ACCOUNT_TOKEN_PREFIX1) + ACCOUNT_TOKEN_PREFIX2 + number, digest); url = string(ACCOUNT_URL_CODEREQUESTV2); - url += "?lc=US&lg=en&mcc=000&mnc=000&method=sms&token="+ token; + url += "?lc=US&lg=en&mcc=000&mnc=000&method=sms&token=" + Utilities::bytesToHex(digest, 16); } else if (state == REG_STATE_REG_CODE) { diff --git a/protocols/WhatsApp/src/utils.cpp b/protocols/WhatsApp/src/utils.cpp index b78ef7f3b5..9559fb3372 100644 --- a/protocols/WhatsApp/src/utils.cpp +++ b/protocols/WhatsApp/src/utils.cpp @@ -113,7 +113,15 @@ BYTE* utils::md5string(const BYTE *data, int size, BYTE *digest) return digest; } +///////////////////////////////////////////////////////////////////////////////////////// +// external stubs for WhatsAPI++ + std::string base64_encode(void* pData, size_t len) { return (char*)ptrA( mir_base64_encode((BYTE*)pData, (unsigned)len)); -} \ No newline at end of file +} + +void md5_string(const std::string& data, BYTE digest[16]) +{ + utils::md5string(data, digest); +} -- cgit v1.2.3