1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- /*
- * This is the source code of tgnet library v. 1.1
- * It is licensed under GNU GPL v. 2 or later.
- * You should have received a copy of the license in this archive (see LICENSE).
- *
- * Copyright Nikolai Kudashov, 2015-2018.
- */
- #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) {
- if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", len);
- exit(1);
- }
- length = len;
- }
- ByteArray::ByteArray(ByteArray *byteArray) {
- bytes = new uint8_t[byteArray->length];
- if (bytes == nullptr) {
- if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", byteArray->length);
- 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) {
- if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", len);
- 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) {
- if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", len);
- exit(1);
- }
- length = len;
- }
- bool ByteArray::isEqualTo(ByteArray *byteArray) {
- return byteArray->length == length && !memcmp(byteArray->bytes, bytes, length);
- }
|