summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/WhatsAPI++/ByteArray.cpp
blob: f379a2061aea88c55afbd8c34ca1dce3ca9a3977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * ByteArray.cpp
 *
 *  Created on: 26/06/2012
 *      Author: Antonio
 */

#include "ByteArray.h"
#include "WAException.h"
#include <iostream>
#include <algorithm>
#include "utilities.h"

ByteArrayOutputStream::ByteArrayOutputStream(int size) {
	this->buf = new std::vector<unsigned char>();
	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<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() {
	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<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)  {
	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<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) {
		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& ) {
	}
	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("]");
}