summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2015-01-25 17:45:10 +0000
committerGeorge Hazan <george.hazan@gmail.com>2015-01-25 17:45:10 +0000
commitdac1f42ef81ac1119430fd294a6b35b0b8cd6837 (patch)
treee3b972fc4bb56a3158e57adc70df6655f6a34dcf /protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp
parent357ae09c7eba86e783583566816285750933beaa (diff)
- class KeyStream extracted to the separate module;
- xml attributes redesigned to produce efficient code; - many small improvements git-svn-id: http://svn.miranda-ng.org/main/trunk@11905 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp')
-rw-r--r--protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp107
1 files changed, 63 insertions, 44 deletions
diff --git a/protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp b/protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp
index d52b10c549..579dd58516 100644
--- a/protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp
+++ b/protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp
@@ -5,59 +5,69 @@
* Author: Antonio
*/
+#include "../common.h" // #TODO Remove Miranda-dependency
+
#include "ByteArray.h"
#include "WAException.h"
-#include <iostream>
-#include <algorithm>
#include "utilities.h"
-ByteArrayOutputStream::ByteArrayOutputStream(int size) {
+ByteArrayOutputStream::ByteArrayOutputStream(int size)
+{
this->buf = new std::vector<unsigned char>();
this->buf->reserve(size);
this->position = 0;
}
-void ByteArrayOutputStream::setLength(size_t length) {
+void ByteArrayOutputStream::setLength(size_t length)
+{
this->buf->resize(length);
}
-size_t ByteArrayOutputStream::getLength() {
+size_t ByteArrayOutputStream::getLength()
+{
return this->buf->size();
}
-size_t ByteArrayOutputStream::getCapacity() {
+size_t ByteArrayOutputStream::getCapacity()
+{
return this->buf->capacity();
}
-size_t ByteArrayOutputStream::getPosition() {
+size_t ByteArrayOutputStream::getPosition()
+{
return this->position;
}
-void ByteArrayOutputStream::setPosition(size_t count) {
+void ByteArrayOutputStream::setPosition(size_t count)
+{
this->position = count;
}
-std::vector<unsigned char>* ByteArrayOutputStream::toByteArray() {
+std::vector<unsigned char>* ByteArrayOutputStream::toByteArray()
+{
std::vector<unsigned char>* array = new std::vector<unsigned char>(this->buf->size());
for (size_t i = 0; i < this->buf->size(); i++)
(*array)[i] = (*this->buf)[i];
return array;
}
-std::vector<unsigned char>* ByteArrayOutputStream::getBuffer() {
+std::vector<unsigned char>* ByteArrayOutputStream::getBuffer()
+{
return this->buf;
}
-void ByteArrayOutputStream::write(int i) {
+void ByteArrayOutputStream::write(int i)
+{
if (this->position == this->buf->size())
- this->buf->push_back((unsigned char) i);
+ this->buf->push_back((unsigned char)i);
else
- (*this->buf)[this->position] = (unsigned char) i;
+ (*this->buf)[this->position] = (unsigned char)i;
this->position = this->position + 1;
}
-void ByteArrayOutputStream::write(unsigned char* b, size_t len) {
+void ByteArrayOutputStream::write(unsigned char* b, size_t len)
+{
if (len == 0)
return;
@@ -65,85 +75,94 @@ void ByteArrayOutputStream::write(unsigned char* b, size_t len) {
write(b[i]);
}
-void ByteArrayOutputStream::write(const std::string& s) {
+void ByteArrayOutputStream::write(const std::string& s)
+{
for (size_t i = 0; i < s.size(); i++)
- write((unsigned char) s[i]);
+ write((unsigned char)s[i]);
}
-ByteArrayOutputStream::~ByteArrayOutputStream() {
+ByteArrayOutputStream::~ByteArrayOutputStream()
+{
delete this->buf;
}
-ByteArrayInputStream::ByteArrayInputStream(std::vector<unsigned char>* buf, size_t off, size_t length ) {
+ByteArrayInputStream::ByteArrayInputStream(std::vector<unsigned char>* buf, size_t off, size_t length)
+{
this->buf = buf;
this->pos = off;
this->count = min(off + length, buf->size());
}
-ByteArrayInputStream::ByteArrayInputStream(std::vector<unsigned char>* buf) {
+ByteArrayInputStream::ByteArrayInputStream(std::vector<unsigned char>* buf)
+{
this->buf = buf;
this->pos = 0;
this->count = buf->size();
}
-int ByteArrayInputStream::read() {
+int ByteArrayInputStream::read()
+{
return (pos < count) ? ((*this->buf)[pos++]) : -1;
}
-int ByteArrayInputStream::read(std::vector<unsigned char>& b, size_t off, size_t len) {
- if (len > (b.size() - off)) {
+int ByteArrayInputStream::read(std::vector<unsigned char>& b, size_t off, size_t len)
+{
+ if (len > (b.size() - off))
throw new WAException("Index out of bounds");
- } else if (len == 0) {
+
+ if (len == 0)
return 0;
- }
int c = read();
- if (c == -1) {
+ if (c == -1)
return -1;
- }
- b[off] = (unsigned char) c;
+
+ b[off] = (unsigned char)c;
size_t i = 1;
try {
- for (; i < len ; i++) {
+ for (; i < len; i++) {
c = read();
- if (c == -1) {
+ if (c == -1)
break;
- }
- b[off + i] = (unsigned char) c;
+
+ b[off + i] = (unsigned char)c;
}
- } catch (std::exception& ) {
+ }
+ catch (std::exception&) {
}
return (int)i;
}
-ByteArrayInputStream::~ByteArrayInputStream() {
-}
+ByteArrayInputStream::~ByteArrayInputStream()
+{}
-void ByteArrayInputStream::print() {
+void ByteArrayInputStream::print()
+{
std::cout << "[";
- for (size_t i = 0; i < this->count; i++) {
+ for (size_t i = 0; i < this->count; i++)
std::cout << (*this->buf)[i] << " ";
- }
+
std::cout << std::endl;
- for (size_t i = 0; i < this->count; i++) {
- std::cout << (int) ((signed char) (*this->buf)[i]) << " ";
- }
+ for (size_t i = 0; i < this->count; i++)
+ std::cout << (int)((signed char)(*this->buf)[i]) << " ";
+
std::cout << "]" << std::endl;
}
-void ByteArrayOutputStream::print() {
+void ByteArrayOutputStream::print()
+{
_LOGDATA("[");
std::string chars(this->buf->begin(), this->buf->end());
_LOGDATA("%s ", chars.c_str());
std::string numbers = "";
- for (size_t i = 0; i < this->buf->size(); i++) {
- numbers += Utilities::intToStr((int) ((signed char) (*this->buf)[i])) + " ";
- }
+ for (size_t i = 0; i < this->buf->size(); i++)
+ numbers += Utilities::intToStr((int)((signed char)(*this->buf)[i])) + " ";
+
_LOGDATA("%s", numbers.c_str());
_LOGDATA("]");
}