Telegram-Android/TMessagesProj/jni/tgnet/ByteArray.cpp

72 lines
1.7 KiB
C++
Raw Normal View History

2015-09-24 22:52:02 +02:00
/*
2018-07-30 04:07:02 +02:00
* This is the source code of tgnet library v. 1.1
2015-09-24 22:52:02 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2018-07-30 04:07:02 +02:00
* Copyright Nikolai Kudashov, 2015-2018.
2015-09-24 22:52:02 +02:00
*/
#include <stdlib.h>
#include <memory.h>
#include "ByteArray.h"
#include "FileLog.h"
ByteArray::ByteArray() {
bytes = nullptr;
length = 0;
}
ByteArray::ByteArray(uint32_t len) {
bytes = new uint8_t[len];
if (bytes == nullptr) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", len);
2015-09-24 22:52:02 +02:00
exit(1);
}
length = len;
}
ByteArray::ByteArray(ByteArray *byteArray) {
bytes = new uint8_t[byteArray->length];
if (bytes == nullptr) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", byteArray->length);
2015-09-24 22:52:02 +02:00
exit(1);
}
length = byteArray->length;
memcpy(bytes, byteArray->bytes, length);
}
ByteArray::ByteArray(uint8_t *buffer, uint32_t len) {
bytes = new uint8_t[len];
if (bytes == nullptr) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", len);
2015-09-24 22:52:02 +02:00
exit(1);
}
length = len;
memcpy(bytes, buffer, length);
}
ByteArray::~ByteArray() {
if (bytes != nullptr) {
delete[] bytes;
bytes = nullptr;
}
}
void ByteArray::alloc(uint32_t len) {
if (bytes != nullptr) {
delete[] bytes;
bytes = nullptr;
}
bytes = new uint8_t[len];
if (bytes == nullptr) {
2019-01-23 18:03:33 +01:00
if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", len);
2015-09-24 22:52:02 +02:00
exit(1);
}
length = len;
}
bool ByteArray::isEqualTo(ByteArray *byteArray) {
return byteArray->length == length && !memcmp(byteArray->bytes, bytes, length);
}