From ab7e0b08fa8c31cf1d468ab4b3c660e2b1836811 Mon Sep 17 00:00:00 2001 From: Fishbone Date: Sun, 2 Jun 2013 16:19:21 +0000 Subject: Added WhatsApp-protocol git-svn-id: http://svn.miranda-ng.org/main/trunk@4861 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp | 155 ++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp (limited to 'protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp') diff --git a/protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp b/protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp new file mode 100644 index 0000000000..44354a4e3d --- /dev/null +++ b/protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp @@ -0,0 +1,155 @@ +/* + * ByteArray.cpp + * + * Created on: 26/06/2012 + * Author: Antonio + */ +#include "stdafx.h" +#include "ByteArray.h" +#include "WAException.h" +#include +#include +#include "utilities.h" + +ByteArrayOutputStream::ByteArrayOutputStream(int size) { + this->buf = new std::vector(); + this->buf->reserve(size); + this->position = 0; +} + +void ByteArrayOutputStream::setLength(size_t length) { + this->buf->resize(length); +} + +size_t ByteArrayOutputStream::getLength() { + return this->buf->size(); +} + +size_t ByteArrayOutputStream::getCapacity() { + return this->buf->capacity(); +} + +size_t ByteArrayOutputStream::getPosition() { + return this->position; +} + +void ByteArrayOutputStream::setPosition(size_t count) { + this->position = count; +} + + +std::vector* ByteArrayOutputStream::toByteArray() { + std::vector* array = new std::vector(this->buf->size()); + for (size_t i = 0; i < this->buf->size(); i++) + (*array)[i] = (*this->buf)[i]; + return array; +} + +std::vector* ByteArrayOutputStream::getBuffer() { + return this->buf; +} + +void ByteArrayOutputStream::write(int i) { + if (this->position == this->buf->size()) + this->buf->push_back((unsigned char) i); + else + (*this->buf)[this->position] = (unsigned char) i; + this->position = this->position + 1; +} + +void ByteArrayOutputStream::write(unsigned char* b, size_t len) { + if (len == 0) + return; + + for (size_t i = 0; i < len; i++) + write(b[i]); +} + +void ByteArrayOutputStream::write(const std::string& s) { + for (size_t i = 0; i < s.size(); i++) + write((unsigned char) s[i]); +} + + +ByteArrayOutputStream::~ByteArrayOutputStream() { + delete this->buf; +} + + +ByteArrayInputStream::ByteArrayInputStream(std::vector* buf, size_t off, size_t length ) { + this->buf = buf; + this->pos = off; + this->count = std::min(off + length, buf->size()); +} + +ByteArrayInputStream::ByteArrayInputStream(std::vector* buf) { + this->buf = buf; + this->pos = 0; + this->count = buf->size(); +} + +int ByteArrayInputStream::read() { + return (pos < count) ? ((*this->buf)[pos++]) : -1; +} + +int ByteArrayInputStream::read(std::vector& b, size_t off, size_t len) { + if (len > (b.size() - off)) { + throw new WAException("Index out of bounds"); + } else if (len == 0) { + return 0; + } + + int c = read(); + if (c == -1) { + return -1; + } + b[off] = (unsigned char) c; + + size_t i = 1; + try { + for (; i < len ; i++) { + c = read(); + if (c == -1) { + break; + } + b[off + i] = (unsigned char) c; + } + } catch (std::exception& ee) { + } + return i; +} + +ByteArrayInputStream::~ByteArrayInputStream() { +} + +void ByteArrayInputStream::print() { + std::cout << "["; + 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]) << " "; + } + std::cout << "]" << std::endl; +} + +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])) + " "; + } + _LOGDATA("%s", numbers.c_str()); + _LOGDATA("]"); +} + + + + + + -- cgit v1.2.3