libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
bytearray.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
11#include <cstddef>
12#include <cstring>
13#include <array>
15#include <promeki/core/string.h>
16#include <promeki/core/error.h>
18
20
30template <size_t NumBytes> class ByteArray {
32 public:
34 using DataType = std::array<uint8_t, NumBytes>;
35
37 ByteArray() : d{} {}
38
40 ByteArray(const DataType &val) : d(val) {}
41
43 ByteArray(const DataType &&val) : d(std::move(val)) {}
44
46 explicit ByteArray(const uint8_t *src) {
47 std::memcpy(d.data(), src, NumBytes);
48 }
49
52
54 constexpr size_t size() const { return NumBytes; }
55
57 uint8_t &operator[](size_t index) { return d[index]; }
58
60 const uint8_t &operator[](size_t index) const { return d[index]; }
61
63 uint8_t *data() { return d.data(); }
64
66 const uint8_t *data() const { return d.data(); }
67
69 bool isZero() const {
70 for(size_t i = 0; i < NumBytes; i++) {
71 if(d[i] != 0) return false;
72 }
73 return true;
74 }
75
77 friend bool operator==(const ByteArray &lhs, const ByteArray &rhs) {
78 return lhs.d == rhs.d;
79 }
80
82 friend bool operator!=(const ByteArray &lhs, const ByteArray &rhs) {
83 return lhs.d != rhs.d;
84 }
85
91 static const char digits[] = "0123456789abcdef";
92 std::string ret;
93 ret.reserve(NumBytes * 2);
94 for(size_t i = 0; i < NumBytes; i++) {
95 ret += digits[d[i] >> 4];
96 ret += digits[d[i] & 0x0F];
97 }
98 return ret;
99 }
100
107 static ByteArray fromHexString(const char *str, Error *err = nullptr) {
109 if(str == nullptr) {
110 if(err != nullptr) *err = Error::Invalid;
111 return ret;
112 }
113 for(size_t i = 0; i < NumBytes; i++) {
114 int hi = hexCharToVal(str[i * 2]);
115 int lo = hexCharToVal(str[i * 2 + 1]);
116 if(hi < 0 || lo < 0) {
117 if(err != nullptr) *err = Error::Invalid;
118 return ByteArray();
119 }
120 ret.d[i] = static_cast<uint8_t>((hi << 4) | lo);
121 }
122 if(err != nullptr) *err = Error::Ok;
123 return ret;
124 }
125
132 static ByteArray fromHexString(const String &str, Error *err = nullptr) {
133 return fromHexString(str.cstr(), err);
134 }
135
136 private:
137 DataType d;
138
139 static int hexCharToVal(char c) {
140 if(c >= '0' && c <= '9') return c - '0';
141 if(c >= 'a' && c <= 'f') return c - 'a' + 10;
142 if(c >= 'A' && c <= 'F') return c - 'A' + 10;
143 return -1;
144 }
145};
146
Fixed-size byte array with hex string conversion.
Definition bytearray.h:30
ByteArray(const uint8_t *src)
Constructs from a raw byte pointer. Copies NumBytes from src.
Definition bytearray.h:46
ByteArray()
Default constructor. Value-initializes all bytes to zero.
Definition bytearray.h:37
constexpr size_t size() const
Returns the number of bytes.
Definition bytearray.h:54
uint8_t * data()
Returns a mutable pointer to the underlying data.
Definition bytearray.h:63
const uint8_t * data() const
Returns a const pointer to the underlying data.
Definition bytearray.h:66
String toHexString() const
Converts the byte array to a lowercase hexadecimal string.
Definition bytearray.h:90
friend bool operator!=(const ByteArray &lhs, const ByteArray &rhs)
Returns true if the arrays differ.
Definition bytearray.h:82
const uint8_t & operator[](size_t index) const
Returns a const reference to the byte at index.
Definition bytearray.h:60
bool isZero() const
Returns true if all bytes are zero.
Definition bytearray.h:69
~ByteArray()
Destructor.
Definition bytearray.h:51
uint8_t & operator[](size_t index)
Returns a mutable reference to the byte at index.
Definition bytearray.h:57
static ByteArray fromHexString(const char *str, Error *err=nullptr)
Constructs a ByteArray from a hexadecimal C string.
Definition bytearray.h:107
ByteArray(const DataType &val)
Constructs from a std::array.
Definition bytearray.h:40
static ByteArray fromHexString(const String &str, Error *err=nullptr)
Constructs a ByteArray from a hexadecimal String.
Definition bytearray.h:132
friend bool operator==(const ByteArray &lhs, const ByteArray &rhs)
Returns true if both arrays contain identical bytes.
Definition bytearray.h:77
ByteArray(const DataType &&val)
Move-constructs from a std::array.
Definition bytearray.h:43
Lightweight error code wrapper for the promeki library.
Definition error.h:39
@ Ok
No error.
Definition error.h:51
@ Invalid
Invalid value or argument (EINVAL).
Definition error.h:66
Dynamic array container wrapping std::vector.
Definition list.h:40
void reserve(size_t newCapacity)
Pre-allocates storage for at least newCapacity elements.
Definition list.h:314
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
const char * cstr() const
Returns a null-terminated C string pointer.
Definition string.h:289
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
#define PROMEKI_SHARED_FINAL(TYPE)
Macro for non-polymorphic native shared objects.
Definition sharedptr.h:88