diff options
Diffstat (limited to 'protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.cpp')
-rw-r--r-- | protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.cpp | 111 |
1 files changed, 67 insertions, 44 deletions
diff --git a/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.cpp b/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.cpp index 6936de9a8b..b79bdcfd09 100644 --- a/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.cpp +++ b/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.cpp @@ -10,8 +10,9 @@ #include "utilities.h"
BinTreeNodeWriter::BinTreeNodeWriter(WAConnection* conn, ISocketConnection* connection,
- const char** dictionary, const int dictionarysize, IMutex* mutex) {
- this->mutex = mutex;
+ const char** dictionary, const int dictionarysize, IMutex* mutex)
+{
+ this->mutex = mutex;
this->conn = conn;
this->out = new ByteArrayOutputStream(2048);
this->realOut = connection;
@@ -37,22 +38,19 @@ void BinTreeNodeWriter::processBuffer() {
bool flag = this->conn->outputKey != NULL;
unsigned int num = 0u;
- if (flag)
- {
+ if (flag) {
long num2 = (long)this->out->getLength() + 4L;
this->out->setLength(num2);
this->out->setPosition(num2);
num |= 1u;
}
long num3 = (long)this->out->getLength() - 3L - (long) this->dataBegin;
- if (num3 >= 1048576L)
- {
+ if (num3 >= 1048576L) {
throw WAException("Buffer too large: " + num3, WAException::CORRUPT_STREAM_EX, 0);
}
std::vector<unsigned char>* buffer = this->out->getBuffer();
- if (flag)
- {
+ if (flag) {
int num4 = (int)num3 - 4;
this->conn->outputKey->encodeMessage(buffer->data(), this->dataBegin + 3 + num4, this->dataBegin + 3, num4);
}
@@ -61,15 +59,16 @@ void BinTreeNodeWriter::processBuffer() (*buffer)[this->dataBegin + 2] = (unsigned char)(num3 & 255L);
}
-void BinTreeNodeWriter::streamStart(std::string domain, std::string resource) {
+void BinTreeNodeWriter::streamStart(std::string domain, std::string resource)
+{
this->mutex->lock();
try {
this->out->setPosition(0);
this->out->setLength(0);
- this->out->write(87);
- this->out->write(65);
+ this->out->write('W');
+ this->out->write('A');
this->out->write(1);
- this->out->write(2);
+ this->out->write(5);
std::map<string, string> attributes;
attributes["to"] = domain;
@@ -80,44 +79,53 @@ void BinTreeNodeWriter::streamStart(std::string domain, std::string resource) { this->writeAttributes(&attributes);
this->processBuffer();
this->flushBuffer(true, 0);
- } catch (exception& ex) {
+ }
+ catch (exception& ex) {
this->mutex->unlock();
throw ex;
}
this->mutex->unlock();
}
-void BinTreeNodeWriter::writeListStart(int i) {
+void BinTreeNodeWriter::writeListStart(int i)
+{
if (i == 0) {
this->out->write(0);
- } else if (i < 256) {
+ }
+ else if (i < 256) {
this->out->write(248);
writeInt8(i);
- } else {
+ }
+ else {
this->out->write(249);
writeInt16(i);
}
}
-void BinTreeNodeWriter::writeInt8(int v) {
+void BinTreeNodeWriter::writeInt8(int v)
+{
this->out->write(v & 0xFF);
}
-void BinTreeNodeWriter::writeInt16(int v, ISocketConnection* o) {
+void BinTreeNodeWriter::writeInt16(int v, ISocketConnection* o)
+{
o->write((v & 0xFF00) >> 8);
o->write((v & 0xFF) >> 0);
}
-void BinTreeNodeWriter::writeInt16(int v) {
+void BinTreeNodeWriter::writeInt16(int v)
+{
writeInt16(v, this->out);
}
-void BinTreeNodeWriter::writeInt16(int v, ByteArrayOutputStream* o) {
+void BinTreeNodeWriter::writeInt16(int v, ByteArrayOutputStream* o)
+{
o->write((v & 0xFF00) >> 8);
o->write((v & 0xFF) >> 0);
}
-void BinTreeNodeWriter::writeAttributes(std::map<string, string>* attributes) {
+void BinTreeNodeWriter::writeAttributes(std::map<string, string>* attributes)
+{
if (attributes != NULL) {
std::map<string, string>::iterator ii;
for (ii = attributes->begin(); ii != attributes->end(); ii++) {
@@ -127,14 +135,15 @@ void BinTreeNodeWriter::writeAttributes(std::map<string, string>* attributes) { }
}
-void BinTreeNodeWriter::writeString(const std::string& tag) {
+void BinTreeNodeWriter::writeString(const std::string& tag)
+{
std::map<string, int>::iterator it = this->tokenMap.find(tag);
if (it != this->tokenMap.end())
writeToken(it->second);
else {
size_t atIndex = tag.find('@');
if (atIndex == 0 || atIndex == string::npos)
- writeBytes((unsigned char*) tag.data(), (int)tag.length());
+ writeBytes((unsigned char*)tag.data(), (int)tag.length());
else {
std::string server = tag.substr(atIndex + 1);
std::string user = tag.substr(0, atIndex);
@@ -143,18 +152,21 @@ void BinTreeNodeWriter::writeString(const std::string& tag) { }
}
-void BinTreeNodeWriter::writeJid(std::string* user, const std::string& server) {
+void BinTreeNodeWriter::writeJid(std::string* user, const std::string& server)
+{
this->out->write(250);
if (user != NULL && !user->empty()) {
writeString(*user);
- } else {
+ }
+ else {
writeToken(0);
}
writeString(server);
}
-void BinTreeNodeWriter::writeToken(int intValue) {
+void BinTreeNodeWriter::writeToken(int intValue)
+{
if (intValue < 245)
this->out->write(intValue);
else if (intValue <= 500) {
@@ -163,32 +175,36 @@ void BinTreeNodeWriter::writeToken(int intValue) { }
}
-void BinTreeNodeWriter::writeBytes(unsigned char* bytes, int length) {
+void BinTreeNodeWriter::writeBytes(unsigned char* bytes, int length)
+{
if (length >= 256) {
this->out->write(253);
writeInt24(length);
- } else {
+ }
+ else {
this->out->write(252);
writeInt8(length);
}
this->out->write(bytes, length);
}
-void BinTreeNodeWriter::writeInt24(int v) {
+void BinTreeNodeWriter::writeInt24(int v)
+{
this->out->write((v & 0xFF0000) >> 16);
this->out->write((v & 0xFF00) >> 8);
this->out->write(v & 0xFF);
}
-void BinTreeNodeWriter::writeInternal(ProtocolTreeNode* node) {
+void BinTreeNodeWriter::writeInternal(ProtocolTreeNode* node)
+{
writeListStart(
- 1 + (node->attributes == NULL ? 0 : (int)node->attributes->size() * 2)
- + (node->children == NULL ? 0 : 1)
- + (node->data == NULL ? 0 : 1));
+ 1 + (node->attributes == NULL ? 0 : (int)node->attributes->size() * 2)
+ + (node->children == NULL ? 0 : 1)
+ + (node->data == NULL ? 0 : 1));
writeString(node->tag);
writeAttributes(node->attributes);
if (node->data != NULL) {
- writeBytes((unsigned char*) node->data->data(), (int)node->data->size());
+ writeBytes((unsigned char*)node->data->data(), (int)node->data->size());
}
if (node->children != NULL && !node->children->empty()) {
writeListStart((int)node->children->size());
@@ -203,10 +219,12 @@ void BinTreeNodeWriter::flushBuffer(bool flushNetwork) this->flushBuffer(flushNetwork, this->dataBegin);
}
-void BinTreeNodeWriter::flushBuffer(bool flushNetwork, int startingOffset) {
+void BinTreeNodeWriter::flushBuffer(bool flushNetwork, int startingOffset)
+{
try {
this->processBuffer();
- } catch (WAException& ex) {
+ }
+ catch (WAException& ex) {
this->out->setPosition(0);
this->out->setLength(0);
throw ex;
@@ -216,8 +234,7 @@ void BinTreeNodeWriter::flushBuffer(bool flushNetwork, int startingOffset) { std::vector<unsigned char> buffer(this->out->getBuffer()->begin(), this->out->getBuffer()->end());
int num = (int)(this->out->getLength() - (long)startingOffset);
- if (flushNetwork && ((long)this->out->getCapacity() - this->out->getLength() < 3L || this->out->getLength() > 4096L))
- {
+ if (flushNetwork && ((long)this->out->getCapacity() - this->out->getLength() < 3L || this->out->getLength() > 4096L)) {
delete this->out;
this->out = new ByteArrayOutputStream(4096);
}
@@ -226,24 +243,28 @@ void BinTreeNodeWriter::flushBuffer(bool flushNetwork, int startingOffset) { this->realOut->write(buffer, startingOffset, num);
}
-void BinTreeNodeWriter::streamEnd() {
+void BinTreeNodeWriter::streamEnd()
+{
this->mutex->lock();
try {
writeListStart(1);
this->out->write(2);
flushBuffer(true);
- } catch (exception& ex) {
+ }
+ catch (exception& ex) {
this->mutex->unlock();
throw ex;
}
this->mutex->unlock();
}
-void BinTreeNodeWriter::write(ProtocolTreeNode* node) {
+void BinTreeNodeWriter::write(ProtocolTreeNode* node)
+{
write(node, true);
}
-void BinTreeNodeWriter::write(ProtocolTreeNode* node, bool needsFlush) {
+void BinTreeNodeWriter::write(ProtocolTreeNode* node, bool needsFlush)
+{
this->mutex->lock();
try {
this->writeDummyHeader();
@@ -253,14 +274,16 @@ void BinTreeNodeWriter::write(ProtocolTreeNode* node, bool needsFlush) { writeInternal(node);
}
flushBuffer(needsFlush);
- } catch (exception& ex) {
+ }
+ catch (exception& ex) {
this->mutex->unlock();
throw WAException(ex.what());
}
this->mutex->unlock();
}
-BinTreeNodeWriter::~BinTreeNodeWriter() {
+BinTreeNodeWriter::~BinTreeNodeWriter()
+{
if (this->out != NULL)
delete this->out;
}
|