From 58ac4567a402d383c3edb0749b9f2fb88b8422d6 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 25 Jan 2015 19:54:43 +0000 Subject: less pointers, less memory problems git-svn-id: http://svn.miranda-ng.org/main/trunk@11906 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- .../WhatsApp/src/WhatsAPI++/BinTreeNodeReader.cpp | 73 +++++++--------------- 1 file changed, 21 insertions(+), 52 deletions(-) (limited to 'protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeReader.cpp') diff --git a/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeReader.cpp b/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeReader.cpp index 16e9238a4f..492dde1027 100644 --- a/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeReader.cpp +++ b/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeReader.cpp @@ -12,45 +12,17 @@ #include "ProtocolTreeNode.h" #include "utilities.h" -static const char *secondary_dict[] = { - "mpeg4", "wmv", "audio/3gpp", "audio/aac", "audio/amr", "audio/mp4", "audio/mpeg", "audio/ogg", "audio/qcelp", "audio/wav", - "audio/webm", "audio/x-caf", "audio/x-ms-wma", "image/gif", "image/jpeg", "image/png", "video/3gpp", "video/avi", "video/mp4", - "video/mpeg", "video/quicktime", "video/x-flv", "video/x-ms-asf", "302", "400", "401", "402", "403", "404", "405", "406", "407", - "409", "410", "500", "501", "503", "504", "abitrate", "acodec", "app_uptime", "asampfmt", "asampfreq", "audio", "clear", "conflict", - "conn_no_nna", "cost", "currency", "duration", "extend", "file", "fps", "g_notify", "g_sound", "gcm", "gone", "google_play", "hash", - "height", "invalid", "jid-malformed", "latitude", "lc", "lg", "live", "location", "log", "longitude", "max_groups", "max_participants", - "max_subject", "mimetype", "mode", "napi_version", "normalize", "orighash", "origin", "passive", "password", "played", - "policy-violation", "pop_mean_time", "pop_plus_minus", "price", "pricing", "redeem", "Replaced by new connection", "resume", - "signature", "size", "sound", "source", "system-shutdown", "username", "vbitrate", "vcard", "vcodec", "video", "width", - "xml-not-well-formed", "checkmarks", "image_max_edge", "image_max_kbytes", "image_quality", "ka", "ka_grow", "ka_shrink", "newmedia", - "library", "caption", "forward", "c0", "c1", "c2", "c3", "clock_skew", "cts", "k0", "k1", "login_rtt", "m_id", "nna_msg_rtt", - "nna_no_off_count", "nna_offline_ratio", "nna_push_rtt", "no_nna_con_count", "off_msg_rtt", "on_msg_rtt", "stat_name", "sts", - "suspect_conn", "lists", "self", "qr", "web", "w:b", "recipient", "w:stats", "forbidden", "aurora.m4r", "bamboo.m4r", "chord.m4r", - "circles.m4r", "complete.m4r", "hello.m4r", "input.m4r", "keys.m4r", "note.m4r", "popcorn.m4r", "pulse.m4r", "synth.m4r", "filehash", - "max_list_recipients", "en-AU", "en-GB", "es-MX", "pt-PT", "zh-Hans", "zh-Hant", "relayelection", "relaylatency", "interruption", - "Apex.m4r", "Beacon.m4r", "Bulletin.m4r", "By The Seaside.m4r", "Chimes.m4r", "Circuit.m4r", "Constellation.m4r", "Cosmic.m4r", - "Crystals.m4r", "Hillside.m4r", "Illuminate.m4r", "Night Owl.m4r", "Opening.m4r", "Playtime.m4r", "Presto.m4r", "Radar.m4r", - "Radiate.m4r", "Ripples.m4r", "Sencha.m4r", "Signal.m4r", "Silk.m4r", "Slow Rise.m4r", "Stargaze.m4r", "Summit.m4r", "Twinkle.m4r", - "Uplift.m4r", "Waves.m4r", "voip", "eligible", "upgrade", "planned", "current", "future", "disable", "expire", "start", "stop", - "accuracy", "speed", "bearing", "recording", "encrypt", "key", "identity", "w:gp2", "admin", "locked", "unlocked", "new", "battery", - "archive", "adm", "plaintext_size", "compressed_size", "delivered", "msg", "pkmsg", "everyone", "v", "transport", "call-id" -}; - -BinTreeNodeReader::BinTreeNodeReader(WAConnection* conn, ISocketConnection* connection, const char** dictionary, const int dictionarysize) +BinTreeNodeReader::BinTreeNodeReader(WAConnection *conn, ISocketConnection *connection) : + buf(BUFFER_SIZE) { this->conn = conn; this->rawIn = connection; - this->tokenMap = dictionary; - this->tokenmapsize = dictionarysize; this->readSize = 1; this->in = NULL; - this->buf = new std::vector(BUFFER_SIZE); } BinTreeNodeReader::~BinTreeNodeReader() { - if (this->buf != NULL) - delete this->buf; if (this->in != NULL) delete this->in; } @@ -88,9 +60,7 @@ ProtocolTreeNode* BinTreeNodeReader::nextTreeInternal() data = new std::vector(s->begin(), s->end()); delete s; } - else { - data = (std::vector*) obj->data; - } + else data = (std::vector*) obj->data; ProtocolTreeNode* ret = new ProtocolTreeNode(*tag, data); ret->attributes = attribs; delete obj; @@ -105,20 +75,21 @@ bool BinTreeNodeReader::isListTag(int b) void BinTreeNodeReader::decodeStream(int flags, int offset, int length) { + unsigned char *pData = (unsigned char*)&buf[0]; + if ((flags & 8) != 0) { if (length < 4) throw WAException("invalid length" + length, WAException::CORRUPT_STREAM_EX, 0); length -= 4; - unsigned char *pData = (unsigned char*)&(*this->buf)[0]; - this->conn->inputKey->decodeMessage(pData, offset + length, 0, length); - this->rawIn->dump(pData + offset, length); + this->conn->inputKey.decodeMessage(pData, offset + length, 0, length); } + this->rawIn->dump(pData + offset, length); if (this->in != NULL) delete this->in; - this->in = new ByteArrayInputStream(this->buf, offset, length); + this->in = new ByteArrayInputStream(&this->buf, offset, length); } std::map* BinTreeNodeReader::readAttributes(int attribCount) @@ -172,17 +143,17 @@ ReadData* BinTreeNodeReader::readString(int token) throw WAException("-1 token in readString", WAException::CORRUPT_STREAM_EX, -1); int bSize; - ReadData* ret = new ReadData(); + ReadData *ret = new ReadData(); - if (token > 2 && token <= this->tokenmapsize) { - if (token != this->tokenmapsize) - ret->data = new std::string(this->tokenMap[token]); + if (token > 2 && token <= WAConnection::DICTIONARY_LEN) { + if (token != WAConnection::DICTIONARY_LEN) + ret->data = new std::string(WAConnection::dictionary[token]); else { token = readInt8(this->in); - if (token >= 0 && token < _countof(secondary_dict)) - ret->data = new std::string(secondary_dict[token]); - else + if (token >= WAConnection::EXTDICTIONARY_LEN) throw WAException("invalid token/length in getToken", WAException::CORRUPT_STREAM_EX, 0); + + ret->data = new std::string(WAConnection::extended_dict[token]); } ret->type = STRING; @@ -305,12 +276,11 @@ void BinTreeNodeReader::getTopLevelStream() int flags = (stanzaSize >> 20); stanzaSize &= 0x0FFFFF; - if (this->buf->size() < (size_t)stanzaSize) { - int newsize = max((int)(this->buf->size() * 3 / 2), stanzaSize); - delete this->buf; - this->buf = new std::vector(newsize); + if (this->buf.size() < (size_t)stanzaSize) { + int newsize = max((int)(this->buf.size() * 3 / 2), stanzaSize); + this->buf.resize(newsize); } - fillArray(*this->buf, stanzaSize, this->rawIn); + fillArray(this->buf, stanzaSize, this->rawIn); this->decodeStream(flags, 0, stanzaSize); } @@ -351,11 +321,10 @@ void BinTreeNodeReader::streamStart() int tag = this->in->read(); int size = readListSize(tag); tag = this->in->read(); - if (tag != 1) { + if (tag != 1) throw WAException("expecting STREAM_START in streamStart", WAException::CORRUPT_STREAM_EX, 0); - } - int attribCount = (size - 2 + size % 2) / 2; + int attribCount = (size - 2 + size % 2) / 2; std::map* attributes = readAttributes(attribCount); delete attributes; } -- cgit v1.2.3