45 using Filler = std::function<size_t(uint8_t *buf,
size_t size)>;
78 Bytes(
const char *str) :
Bytes((uint8_t *)str, strlen(str) + 1)
91 static_assert(std::is_integral<T>::value,
"only integral types are allowed");
115 mBuffer = value.mBuffer;
152 _copy(rhs.mBuffer, rhs.mSize);
172 mBuffer = rhs.mBuffer;
189 _copy((
const uint8_t *)rhs.c_str(), rhs.length());
212 size_t len = hexStr.length();
219 if (!bytes._reserve(len / 2)) {
222 for (
size_t i = 0; i < len; i += 2) {
223 char buf[3] = {hexStr.charAt(i), hexStr.charAt(i + 1),
'\0'};
224 uint8_t b = strtol(buf,
nullptr, 16);
225 bytes.mBuffer[i / 2] = b;
242 return filler(mBuffer, mSize);
257 if (newSize < mSize) {
270 const uint8_t *
raw()
const {
287 if (mBuffer[mSize - 1] !=
'\0') {
288 if (_reserve(mSize + 1)) {
289 mBuffer[mSize - 1] =
'\0';
307 return mBuffer ? (
const char *)mBuffer :
"";
318 return String(this->
c_str());
330 for (
size_t i = 0; i < mSize; i++) {
332 snprintf(buf,
sizeof(buf),
"%02X", mBuffer[i]);
364 return CRC32(mBuffer, mSize);
373 inline void _init(
void) {
383 void _invalidate(
void) {
400 bool _reserve(
size_t newSize) {
409 auto buffer = (uint8_t *)realloc(mBuffer, newSize);
414 if (newSize > mSize) {
415 memset(buffer + mSize, 0, newSize - mSize);
431 Bytes &_copy(
const uint8_t *data,
size_t size) {
432 if (_reserve(
size)) {
434 memcpy(mBuffer, data,
size);
uint32_t checksum() const
Calculates a checksum of the byte array.
Definition Bytes.h:363
void clean()
Deallocates the internal buffer.
Definition Bytes.h:352
String toString() const
Converts the byte array to an Arduino String.
Definition Bytes.h:317
Bytes & terminate()
Ensures the byte array is null-terminated.
Definition Bytes.h:281
Bytes & operator=(const Bytes &rhs)
Assignment operator.
Definition Bytes.h:149
const uint8_t * raw() const
Gets a const pointer to the raw byte array.
Definition Bytes.h:270
Bytes(const String &value)
Constructor from Arduino String.
Definition Bytes.h:127
const char * c_str() const
Gets the byte array as a C string.
Definition Bytes.h:306
Bytes(const uint8_t *data, size_t size)
Constructor from raw byte array.
Definition Bytes.h:62
Bytes & prune(size_t newSize)
Reduces the size of the buffer.
Definition Bytes.h:256
Bytes & operator=(Bytes &&rhs) noexcept
Move assignment operator.
Definition Bytes.h:169
Bytes(const Bytes &value)
Copy constructor.
Definition Bytes.h:101
Bytes & operator=(const String &rhs)
Assignment from Arduino String.
Definition Bytes.h:187
size_t fill(Filler filler)
Fills the buffer using a provided filler function.
Definition Bytes.h:240
String toHexString() const
Converts the byte array to a hexadecimal string.
Definition Bytes.h:328
std::function< size_t(uint8_t *buf, size_t size)> Filler
Function type for filling byte arrays.
Definition Bytes.h:45
virtual ~Bytes()
Destructor.
Definition Bytes.h:137
static Bytes fromHexString(const String &hexStr)
Creates a Bytes object from a hexadecimal string.
Definition Bytes.h:211
Bytes(Bytes &&value) noexcept
Move constructor.
Definition Bytes.h:114
size_t size() const
Gets the size of the byte array.
Definition Bytes.h:343
Bytes()
Default constructor.
Definition Bytes.h:52
Bytes(T value)
Constructor from integral type.
Definition Bytes.h:90
Bytes(const char *str)
Constructor from C-string.
Definition Bytes.h:78
uint32_t CRC32(const void *data, size_t length, uint32_t crc=0)
Calculates CRC32-C (Castagnoli) checksum for data integrity verification.
Definition Common.h:225
#define UNIOT_LOG_ERROR(...)
Log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:273