diff options
author | George Hazan <george.hazan@gmail.com> | 2014-09-18 21:52:10 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2014-09-18 21:52:10 +0000 |
commit | 6d932bfaf11e4699355fedc45e28b353b8877130 (patch) | |
tree | 78d3f6741cf9762d2c6b97ed1f5bc472e850e15f /protocols/WhatsApp/src/WhatsAPI++/utilities.cpp | |
parent | 4dd774d667df90583315c04696537b6397f6fc02 (diff) |
merge into trunk
git-svn-id: http://svn.miranda-ng.org/main/trunk@10515 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/WhatsApp/src/WhatsAPI++/utilities.cpp')
-rw-r--r-- | protocols/WhatsApp/src/WhatsAPI++/utilities.cpp | 267 |
1 files changed, 120 insertions, 147 deletions
diff --git a/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp b/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp index f2603f9f8d..dc28b86c69 100644 --- a/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp +++ b/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp @@ -13,41 +13,24 @@ #include <fstream>
#include <iomanip>
-namespace Utilities{
-
+namespace Utilities {
+
const static char digits[] = {
- '0' , '1' , '2' , '3' , '4' , '5' ,
- '6' , '7' , '8' , '9' , 'a' , 'b' ,
- 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
- 'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
- 'o' , 'p' , 'q' , 'r' , 's' , 't' ,
- 'u' , 'v' , 'w' , 'x' , 'y' , 'z'
+ '0', '1', '2', '3', '4', '5',
+ '6', '7', '8', '9', 'a', 'b',
+ 'c', 'd', 'e', 'f', 'g', 'h',
+ 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't',
+ 'u', 'v', 'w', 'x', 'y', 'z'
};
-void configureLogging(const char* ident) {
-#ifndef _LOGWIN32
- openlog(ident, 0, LOG_USER);
-#endif
-}
-
-void closeLog() {
-#ifndef _LOGWIN32
- closelog();
-#endif
-}
-
-std::string getCountryCode(){
- return "34";
-}
-
std::string reverseString(const std::string& str)
{
return std::string(str.rbegin(), str.rend());
}
-
-
-std::string itoa(int value, unsigned int base) {
+std::string itoa(int value, unsigned int base)
+{
const char digitMap[] = "0123456789abcdef";
@@ -70,8 +53,8 @@ std::string itoa(int value, unsigned int base) { // Translating number to string with base:
- for (int i = 30; _value && i ; --i) {
- buf = digitMap[ _value % base ] + buf;
+ for (int i = 30; _value && i; --i) {
+ buf = digitMap[_value % base] + buf;
_value /= base;
}
@@ -79,24 +62,26 @@ std::string itoa(int value, unsigned int base) { }
-std::string processIdentity(const std::string& id){
+std::string processIdentity(const std::string& id)
+{
std::string buffer_str = reverseString(id);
- unsigned char digest[16];
- md5_string(buffer_str, digest);
+ unsigned char digest[16];
+ md5_string(buffer_str, digest);
buffer_str.clear();
- for(int i =0; i < 16; i++){
- int tmp = digest[i]+128;
+ for (int i = 0; i < 16; i++) {
+ int tmp = digest[i] + 128;
int f = tmp & 0xff;
- buffer_str = buffer_str.append(itoa(f,16));
+ buffer_str = buffer_str.append(itoa(f, 16));
}
return buffer_str;
}
-void debug(const std::string& msg) {
+void debug(const std::string& msg)
+{
#ifdef _LOGWIN32
cout << "DEBUG: " << msg << endl;
#else
@@ -104,7 +89,8 @@ void debug(const std::string& msg) { #endif
}
-std::string str(int64_t i, int radix ) {
+std::string str(int64_t i, int radix)
+{
if (radix < 2 || radix > 36)
throw WAException("radix must be in 2..36");
char buf[65];
@@ -130,34 +116,39 @@ std::string str(int64_t i, int radix ) { return std::string(aux, charPos, (65 - charPos));
}
-int64_t randLong() {
+int64_t randLong()
+{
std::srand((unsigned)time(NULL));
- int64_t r = (int64_t) ((char) (std::rand() % 256));
+ int64_t r = (int64_t)((char)(std::rand() % 256));
- for (int i = 0; i < 7 ; i++)
- r = (r << 8) + ((char) (std::rand() % 256));
+ for (int i = 0; i < 7; i++)
+ r = (r << 8) + ((char)(std::rand() % 256));
return r;
}
-int64_t absLong(int64_t num) {
- return (num >= 0? num: -num);
+int64_t absLong(int64_t num)
+{
+ return (num >= 0 ? num : -num);
}
-std::string intToStr(int i) {
+std::string intToStr(int i)
+{
std::stringstream convert;
convert << i;
return convert.str();
}
-std::string doubleToStr(double d) {
+std::string doubleToStr(double d)
+{
std::stringstream convert;
convert << d;
return convert.str();
}
-time_t parseBBDate(const string& s) {
+time_t parseBBDate(const string& s)
+{
_LOGDATA("parse DATE %s", s.c_str());
if (s.length() < 17)
return time(NULL);
@@ -165,16 +156,17 @@ time_t parseBBDate(const string& s) { struct tm timeinfo;
timeinfo.tm_year = atoi(s.substr(0, 4).c_str()) - 1900;
timeinfo.tm_mon = atoi(s.substr(4, 2).c_str()) - 1;
- timeinfo.tm_mday = atoi(s.substr(6,2).c_str());
- timeinfo.tm_hour = atoi(s.substr(9,2).c_str());
- timeinfo.tm_min = atoi(s.substr(12,2).c_str());
- timeinfo.tm_sec = atoi(s.substr(15,2).c_str());
+ timeinfo.tm_mday = atoi(s.substr(6, 2).c_str());
+ timeinfo.tm_hour = atoi(s.substr(9, 2).c_str());
+ timeinfo.tm_min = atoi(s.substr(12, 2).c_str());
+ timeinfo.tm_sec = atoi(s.substr(15, 2).c_str());
//return timegm(&timeinfo);
- return mktime(&timeinfo);
+ return mktime(&timeinfo);
}
-void logData(const char *format, ...) {
+void logData(const char *format, ...)
+{
va_list args;
va_start(args, format);
#ifdef _LOGWIN32
@@ -186,7 +178,8 @@ void logData(const char *format, ...) { }
-long long parseLongLong(const std::string& str) {
+long long parseLongLong(const std::string& str)
+{
std::stringstream sstr(str);
long long val;
sstr >> val;
@@ -194,8 +187,9 @@ long long parseLongLong(const std::string& str) { return val;
}
-string bytesToHex(unsigned char* bytes, int length) {
- string ret(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++) {
@@ -207,13 +201,15 @@ string bytesToHex(unsigned char* bytes, int length) { return ret;
}
-unsigned char forDigit(int b) {
+unsigned char forDigit(int b)
+{
if (b < 10)
- return (unsigned char) (48 + b);
- return (unsigned char) (97 + b - 10);
+ return (unsigned char)(48 + b);
+ return (unsigned char)(97 + b - 10);
}
-bool saveStringToFile(const string& data, const string& filePath) {
+bool saveStringToFile(const string& data, const string& filePath)
+{
std::ofstream out(filePath.c_str());
if (out.fail()) return false;
out << data;
@@ -223,10 +219,11 @@ bool saveStringToFile(const string& data, const string& filePath) { return true;
}
-bool saveBytesToFile(const std::vector<unsigned char>& data, const string& filePath) {
+bool saveBytesToFile(const std::vector<unsigned char>& data, const string& filePath)
+{
std::fstream out(filePath.c_str(), ios::out | ios::binary);
if (out.fail()) return false;
- out.write((const char*) &data[0], data.size());
+ out.write((const char*)&data[0], data.size());
if (out.fail()) return false;
out.close();
if (out.fail()) return false;
@@ -234,7 +231,8 @@ bool saveBytesToFile(const std::vector<unsigned char>& data, const string& fileP }
-bool saveBytesToFile(const string& data, const string& filePath) {
+bool saveBytesToFile(const string& data, const string& filePath)
+{
std::fstream out(filePath.c_str(), ios::out | ios::binary);
if (out.fail()) return false;
out.write(data.c_str(), data.length());
@@ -244,7 +242,8 @@ bool saveBytesToFile(const string& data, const string& filePath) { return true;
}
-vector<unsigned char>* loadFileToBytes(const string& path) {
+vector<unsigned char>* loadFileToBytes(const string& path)
+{
vector<unsigned char>* bytes = NULL;
std::ifstream in(path.c_str(), ios::in | ios::binary | ios::ate);
size_t size = in.tellg();
@@ -254,19 +253,21 @@ vector<unsigned char>* loadFileToBytes(const string& path) { char *buffer = new char[size];
in.read(buffer, size);
bytes = new vector<unsigned char>(buffer, buffer + size);
- delete [] buffer;
+ delete[] buffer;
in.close();
if (in.fail()) return NULL;
return bytes;
}
-bool fileExists(const std::string& path) {
+bool fileExists(const std::string& path)
+{
return _access(path.c_str(), 0) == 0;
}
-string removeWaDomainFromJid(const string& jid) {
+string removeWaDomainFromJid(const string& jid)
+{
string result = jid;
size_t index = jid.find("@s.whatsapp.net");
@@ -284,7 +285,8 @@ string removeWaDomainFromJid(const string& jid) { return jid;
}
-string getNameFromPath(const std::string& path) {
+string getNameFromPath(const std::string& path)
+{
size_t i = path.rfind('/');
if (i == string::npos)
i = 0;
@@ -293,11 +295,13 @@ string getNameFromPath(const std::string& path) { return path.substr(i);
}
-vector<unsigned char>* getChallengeData(const std::string& challengeFile) {
+vector<unsigned char>* getChallengeData(const std::string& challengeFile)
+{
return loadFileToBytes(challengeFile);
}
-bool saveChallengeData(const std::vector<unsigned char>& data, const std::string& challengeFile) {
+bool saveChallengeData(const std::vector<unsigned char>& data, const std::string& challengeFile)
+{
return saveBytesToFile(data, challengeFile);
}
@@ -305,42 +309,34 @@ std::string utf8_to_utf16(const std::string& utf8) {
std::vector<unsigned long> unicode;
size_t i = 0;
- while (i < utf8.size())
- {
+ while (i < utf8.size()) {
unsigned long uni;
size_t todo;
bool error = false;
unsigned char ch = utf8[i++];
- if (ch <= 0x7F)
- {
+ if (ch <= 0x7F) {
uni = ch;
todo = 0;
}
- else if (ch <= 0xBF)
- {
+ else if (ch <= 0xBF) {
throw std::logic_error("not a UTF-8 string");
}
- else if (ch <= 0xDF)
- {
- uni = ch&0x1F;
+ else if (ch <= 0xDF) {
+ uni = ch & 0x1F;
todo = 1;
}
- else if (ch <= 0xEF)
- {
- uni = ch&0x0F;
+ else if (ch <= 0xEF) {
+ uni = ch & 0x0F;
todo = 2;
}
- else if (ch <= 0xF7)
- {
- uni = ch&0x07;
+ else if (ch <= 0xF7) {
+ uni = ch & 0x07;
todo = 3;
}
- else
- {
+ else {
throw std::logic_error("not a UTF-8 string");
}
- for (size_t j = 0; j < todo; ++j)
- {
+ for (size_t j = 0; j < todo; ++j) {
if (i == utf8.size())
throw std::logic_error("not a UTF-8 string");
unsigned char ch = utf8[i++];
@@ -356,86 +352,63 @@ std::string utf8_to_utf16(const std::string& utf8) unicode.push_back(uni);
}
std::string utf16;
- for (size_t i = 0; i < unicode.size(); ++i)
- {
+ for (size_t i = 0; i < unicode.size(); ++i) {
unsigned long uni = unicode[i];
if (uni <= 0x7F) {
- utf16 += (char) uni;
+ utf16 += (char)uni;
}
else
- if (uni <= 0xFFFF)
- {
+ if (uni <= 0xFFFF) {
stringstream value;
value << std::setw(4) << std::setfill('0') << Utilities::itoa(uni, 16).c_str();
utf16 += "\\u" + value.str();
- }
- else
- {
- stringstream value1, value2;
- uni -= 0x10000;
- value1 << std::setw(4) << std::setfill('0') << Utilities::itoa(((uni >> 10) + 0xD800), 16);
- utf16 += "\\u" + value1.str();
-
- value2 << std::setw(4) << std::setfill('0') << Utilities::itoa(((uni & 0x3FF) + 0xDC00), 16);
- utf16 += "\\u" + value2.str();
- }
+ }
+ else {
+ stringstream value1, value2;
+ uni -= 0x10000;
+ value1 << std::setw(4) << std::setfill('0') << Utilities::itoa(((uni >> 10) + 0xD800), 16);
+ utf16 += "\\u" + value1.str();
+
+ value2 << std::setw(4) << std::setfill('0') << Utilities::itoa(((uni & 0x3FF) + 0xDC00), 16);
+ utf16 += "\\u" + value2.str();
+ }
}
return utf16;
}
std::string string_format(const char* fmt, va_list ap)
{
- int size = 100;
- std::string str;
- while (1) {
- str.resize(size);
- //va_start(ap, fmt);
- int n = vsnprintf((char *)str.c_str(), size, fmt, ap);
- //va_end(ap);
- if (n > -1 && n < size) {
- str.resize(n);
- return str;
- }
- if (n > -1)
- size = n + 1;
- else
- size *= 2;
- }
- return str;
+ int size = 100;
+ std::string str;
+ while (1) {
+ str.resize(size);
+ //va_start(ap, fmt);
+ int n = vsnprintf((char *)str.c_str(), size, fmt, ap);
+ //va_end(ap);
+ if (n > -1 && n < size) {
+ str.resize(n);
+ return str;
+ }
+ if (n > -1)
+ size = n + 1;
+ else
+ size *= 2;
+ }
+ return str;
}
std::string string_format(const std::string fmt, va_list ap)
{
- return string_format(fmt.c_str(), ap);
- /*
- int size = 100;
- std::string str;
- //va_list ap;
- while (1) {
- str.resize(size);
- //va_start(ap, fmt);
- int n = vsnprintf((char *)str.c_str(), size, fmt.c_str(), ap);
- //va_end(ap);
- if (n > -1 && n < size) {
- str.resize(n);
- return str;
- }
- if (n > -1)
- size = n + 1;
- else
- size *= 2;
- }
- return str;
- */
+ return string_format(fmt.c_str(), ap);
}
std::string string_format(const std::string fmt, ...)
{
- va_list ap;
- va_start(ap, fmt);
- std::string ret = string_format(fmt, ap);
- va_end(ap);
- return ret;
+ va_list ap;
+ va_start(ap, fmt);
+ std::string ret = string_format(fmt, ap);
+ va_end(ap);
+ return ret;
}
}
|