| 6 |
| * Copyright Nikolai Kudashov, 2015-2018. |
| 7 | */ |
| 8 |
| 9 | #include <stdlib.h> |
| 10 | #include <memory.h> |
| 11 | #include "ByteArray.h" |
| 12 | #include "FileLog.h" |
| 13 |
| 14 | ByteArray::ByteArray() { |
| 15 | bytes = nullptr; |
| 16 | length = 0; |
| 17 | } |
| 18 |
| 19 | ByteArray::ByteArray(uint32_t len) { |
| 20 | bytes = new uint8_t[len]; |
| 21 | if (bytes == nullptr) { |
| 22 | if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", len); |
| 23 | exit(1); |
| 24 | } |
| 25 | length = len; |
| 26 | } |
| 27 |
| 28 |
| 29 | ByteArray::ByteArray(ByteArray *byteArray) { |
| 30 | bytes = new uint8_t[byteArray->length]; |
| 31 | if (bytes == nullptr) { |
| 32 | if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", byteArray->length); |
| 33 | exit(1); |
| 34 | } |
| 35 | length = byteArray->length; |
| 36 | memcpy(bytes, byteArray->bytes, length); |
| 37 | } |
| 38 |
| 39 | ByteArray::ByteArray(uint8_t *buffer, uint32_t len) { |
| 40 | bytes = new uint8_t[len]; |
| 41 | if (bytes == nullptr) { |
| 42 | if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", len); |
| 43 | exit(1); |
| 44 | } |
| 45 | length = len; |
| 46 | memcpy(bytes, buffer, length); |
| 47 | } |
| 48 |
| 49 | ByteArray::~ByteArray() { |
| 50 | if (bytes != nullptr) { |
| 51 | delete[] bytes; |
| 52 | bytes = nullptr; |
| 53 | } |
| 54 | } |
| 55 |
| 56 | void ByteArray::alloc(uint32_t len) { |
| 57 | if (bytes != nullptr) { |
| 58 | delete[] bytes; |
| 59 | bytes = nullptr; |
| 60 | } |
| 61 | bytes = new uint8_t[len]; |
| 62 | if (bytes == nullptr) { |
| 63 | if (LOGS_ENABLED) DEBUG_E("unable to allocate byte buffer %u", len); |
| 64 | exit(1); |
| 65 | } |
| 66 | length = len; |
| 67 | } |
| 68 |
| 69 | bool ByteArray::isEqualTo(ByteArray *byteArray) { |
| 70 | return byteArray->length == length && !memcmp(byteArray->bytes, bytes, length); |
| 71 | } |
| 72 |