25#ifndef JAU_OCTETS_HPP_
26#define JAU_OCTETS_HPP_
32#include <initializer_list>
45#ifdef JAU_TRACE_OCTETS
46 #define JAU_TRACE_OCTETS_PRINT(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); fflush(stderr); }
48 #define JAU_TRACE_OCTETS_PRINT(...)
86 if(
nullptr == data_ && 0 < size_ ) {
92 constexpr uint8_t *
data() noexcept {
return _data; }
105 _size, _data, size_, data_);
124 : _size( len ), _data(
const_cast<uint8_t *
>(source) ),
125 _byte_order( byte_order_val )
137 : _size( 0 ), _data(
nullptr ),
149 if( i+count > _size ) {
155 return i+count <= _size;
239 return std::string( (
const char*)(_data+i) );
243 return std::string( (
const char*)(_data+i) );
249 return std::string( (
const char*)(_data+i), length );
272 constexpr uint8_t
const *
get_ptr() const noexcept {
return _data; }
282 return _size == rhs._size && 0 == std::memcmp(_data, rhs._data, _size);
285 return !(*
this == rhs);
325 data()[i] =
static_cast<uint8_t
>(v);
328 data()[i] =
static_cast<uint8_t
>(v);
400 std::memcpy(
data() + i, v.get_ptr(), v.size());
405 std::memcpy(
data() + i, v.get_ptr() + v_off,
size);
409 std::memcpy(
data() + i, v.get_ptr() + v_off,
size);
414 std::memcpy(
data() + i, source, byte_count);
417 std::memcpy(
data() + i, source, byte_count);
422 std::memmove(
data() + i, source, byte_count);
425 std::memmove(
data() + i, source, byte_count);
430 std::memset(
data() + i, c, byte_count);
433 std::memset(
data() + i, c, byte_count);
449 const nsize_t size1 = v.size() + ( includeEOS ? 1 : 0 );
452 std::memcpy(
data() + i, v.c_str(),
size);
453 if(
size < size1 && includeEOS ) {
458 const nsize_t size1 = v.size() + ( includeEOS ? 1 : 0 );
460 std::memcpy(
data() + i, v.c_str(),
size);
461 if(
size < size1 && includeEOS ) {
511 : _parent(buffer_), _offset(offset_), _size(size_)
513 if( offset_+_size > buffer_.
size() ) {
540 return _parent.
get_ptr(_offset+i);
565 uint8_t * ptr =
data();
566 if(
nullptr != ptr ) {
582 uint8_t * m =
static_cast<uint8_t*
>( std::malloc(
size) );
625 if(
nullptr == source_ ) {
628 std::memcpy(
data(), source_, size_);
645 _capacity( sourcelist.
size() )
647 if( 0 < _capacity ) {
648 std::memcpy(
data(), sourcelist.begin(), _capacity);
664 _capacity( capacity_ )
695 _capacity( source.
size() )
711 _capacity(
std::
max(capacity_, source.
size()) )
722 :
TOctets( o.data(), o.size(), o.byte_order() ),
723 _capacity( o.capacity() )
727 o.setData(
nullptr, 0, o.byte_order());
739 if(
this == &_source ) {
744 _capacity = _source.
size();
757 setData(o.data(), o.size(), o.byte_order());
758 _capacity = o._capacity;
760 o.setData(
nullptr, 0, o.byte_order());
779 _capacity( _source.
size() )
792 if(
static_cast<TROOctets *
>(
this) == &_source ) {
797 _capacity = _source.
size();
810 _capacity( _source.
size() )
825 _capacity = _source.
size();
840 if( newCapacity < newSize ) {
843 if( newCapacity != _capacity ) {
844 if( newSize >
size() ) {
864 if( _capacity < newSize ) {
880 if( newCapacity <
size() ) {
883 if( newCapacity == _capacity ) {
886 uint8_t* data2 = allocData(newCapacity);
893 _capacity = newCapacity;
906 if( _capacity < newSize ) {
923 if( _capacity < newSize ) {
Persistent endian aware octet data, i.e.
POctets & operator=(const TROOctets &_source)
Assignment operator for TROOctets.
POctets(POctets &&o) noexcept
Move constructor.
POctets & operator=(POctets &&o) noexcept
Move assignment operator.
std::string toString() const
POctets(const nsize_t capacity_, const nsize_t size_, const lb_endian_t byte_order)
New buffer (malloc(capacity), free)
POctets(const POctets &source)
Copy constructor.
POctets & operator=(const POctets &_source)
Assignment operator.
POctets(const nsize_t size, const lb_endian_t byte_order)
New buffer (malloc, free)
POctets(const TROOctets &_source)
Makes a persistent POctets by copying the data from TROOctets.
~POctets() noexcept override
POctets & resize(const nsize_t newCapacity, const nsize_t newSize)
Resizes this instance, including its capacity.
POctets & operator=(const TOctetSlice &_source)
Assignment operator for TOctetSlice.
POctets & operator+=(const TOctetSlice &b)
Append and assign operator.
constexpr nsize_t remaining() const noexcept
Returns the remaining octets for put left, i.e.
POctets(const POctets &source, const nsize_t capacity_)
Copy constructor (explicit), allowing to set a higher capacity than source.size() in contrast to the ...
POctets & recapacity(const nsize_t newCapacity)
Changes the capacity.
constexpr nsize_t capacity() const noexcept
Returns the memory capacity, never zero, greater or equal size().
POctets(std::initializer_list< uint8_t > sourcelist, const lb_endian_t byte_order)
Takes ownership (malloc(size) and copy, free) .
POctets & resize(const nsize_t newSize)
Sets a new size for this instance.
POctets(const lb_endian_t byte_order) noexcept
Zero sized POctets instance.
POctets & operator+=(const TROOctets &b)
Append and assign operator.
POctets(const TOctetSlice &_source)
Makes a persistent POctets by copying the data from TOctetSlice.
POctets(const uint8_t *source_, const nsize_t size_, const lb_endian_t byte_order)
Takes ownership (malloc(size) and copy, free) .
Transient endian aware octet data slice, i.e.
std::string toString() const noexcept
uint8_t get_uint8(const nsize_t i) const
constexpr lb_endian_t byte_order() const noexcept
Returns byte order of this octet store.
constexpr nsize_t offset() const noexcept
constexpr nsize_t size() const noexcept
constexpr const TOctets & parent() const noexcept
uint16_t get_uint16(const nsize_t i) const
constexpr uint16_t get_uint16_nc(const nsize_t i) const noexcept
constexpr uint8_t const * get_ptr_nc(const nsize_t i) const noexcept
TOctetSlice(const TOctets &buffer_, const nsize_t offset_, const nsize_t size_)
Creates a view of a given TOctet with the specified offset_ and size_.
uint8_t const * get_ptr(const nsize_t i) const
constexpr uint8_t get_uint8_nc(const nsize_t i) const noexcept
Transient endian aware octet data, i.e.
constexpr void put_uint64_nc(const nsize_t i, const uint64_t &v) noexcept
void put_uint128(const nsize_t i, const uint128dp_t &v)
void put_octets_nc(const nsize_t i, const TROOctets &v, const nsize_t v_off, const nsize_t v_len) noexcept
void memset(const nsize_t i, const uint8_t c, const nsize_t byte_count)
void put_uint64(const nsize_t i, const uint64_t &v)
TOctets(TOctets &&o) noexcept=default
TOctets(uint8_t *source, const nsize_t len, const lb_endian_t byte_order) noexcept
Transient passthrough r/w memory, w/o ownership .
void memmove_nc(const nsize_t i, const uint8_t *source, const nsize_t byte_count) noexcept
std::string toString() const noexcept
TOctets & operator=(const TOctets &o) noexcept=default
constexpr void put_uint32_nc(const nsize_t i, const uint32_t v) noexcept
void put_eui48(const nsize_t i, const EUI48 &v)
void put_octets_nc(const nsize_t i, const TROOctets &v) noexcept
void put_uint192(const nsize_t i, const uint192dp_t &v)
void bzero_nc(const nsize_t i, const nsize_t byte_count) noexcept
void put_bytes_nc(const nsize_t i, const uint8_t *source, const nsize_t byte_count) noexcept
void put_string(const nsize_t i, const std::string &v, const nsize_t max_len, const bool includeEOS)
void put_octets(const nsize_t i, const TROOctets &v)
void bzero(const nsize_t i, const nsize_t byte_count)
TOctets(const TOctets &o) noexcept=default
void put_uint256(const nsize_t i, const uint256dp_t &v)
constexpr void put_uint256_nc(const nsize_t i, const uint256dp_t &v) noexcept
TOctets & operator=(TOctets &&o) noexcept=default
void put_eui48_nc(const nsize_t i, const EUI48 &v) noexcept
constexpr void put_uint128_nc(const nsize_t i, const uint128dp_t &v) noexcept
constexpr void put_uint16_nc(const nsize_t i, const uint16_t v) noexcept
~TOctets() noexcept override=default
void put_octets(const nsize_t i, const TROOctets &v, const nsize_t v_off, const nsize_t v_len) noexcept
constexpr void put_uint8_nc(const nsize_t i, const uint8_t v) noexcept
uint8_t * get_wptr() noexcept
void memset_nc(const nsize_t i, const uint8_t c, const nsize_t byte_count) noexcept
void put_uint32(const nsize_t i, const uint32_t v)
void put_string_nc(const nsize_t i, const std::string &v, const nsize_t max_len, const bool includeEOS) noexcept
constexpr void put_int8_nc(const nsize_t i, const int8_t v) noexcept
void memmove(const nsize_t i, const uint8_t *source, const nsize_t byte_count)
uint8_t * get_wptr(const nsize_t i)
void put_uint16(const nsize_t i, const uint16_t v)
void put_uuid(const nsize_t i, const uuid_t &v)
constexpr void put_uint192_nc(const nsize_t i, const uint192dp_t &v) noexcept
void put_bytes(const nsize_t i, const uint8_t *source, const nsize_t byte_count)
void put_uuid_nc(const nsize_t i, const uuid_t &v) noexcept
uint8_t * get_wptr_nc(const nsize_t i) noexcept
void put_int8(const nsize_t i, const int8_t v)
void put_uint8(const nsize_t i, const uint8_t v)
Transient read only and endian aware octet data, i.e.
uint32_t get_uint32(const nsize_t i) const
constexpr uint16_t get_uint16_nc(const nsize_t i) const noexcept
TROOctets & operator=(const TROOctets &o) noexcept=default
std::unique_ptr< const uuid_t > get_uuid(const nsize_t i, const uuid_t::TypeSize tsize) const
uuid128_t get_uuid128_nc(const nsize_t i) const noexcept
constexpr uint128dp_t get_uint128_nc(const nsize_t i) const noexcept
std::string toString() const noexcept
constexpr lb_endian_t byte_order() const noexcept
Returns byte order of this octet store.
uint128dp_t get_uint128(const nsize_t i) const
std::string get_string(const nsize_t i) const
Assumes a null terminated string.
TROOctets(TROOctets &&o) noexcept=default
TROOctets() noexcept
Default constructor with nullptr memory, zero size and lb_endian::native byte order.
TROOctets(const uint8_t *source, const nsize_t len, const lb_endian_t byte_order_val) noexcept
Transient passthrough read-only memory, w/o ownership .
constexpr nsize_t size() const noexcept
Returns the used memory size for read and write operations, may be zero.
constexpr bool is_range_valid(const nsize_t i, const nsize_t count) const noexcept
bool operator==(const TROOctets &rhs) const noexcept
virtual ~TROOctets() noexcept=default
constexpr int8_t get_int8_nc(const nsize_t i) const noexcept
bool operator!=(const TROOctets &rhs) const noexcept
uuid16_t get_uuid16(const nsize_t i) const
uuid16_t get_uuid16_nc(const nsize_t i) const noexcept
static void checkPtr(uint8_t *data_, nsize_t size_) noexcept
Validates the given data_ and size_.
TROOctets(const TROOctets &o) noexcept=default
uint8_t const * get_ptr(const nsize_t i) const
uint16_t get_uint16(const nsize_t i) const
EUI48 get_eui48_nc(const nsize_t i) const noexcept
std::string get_string(const nsize_t i, const nsize_t length) const
Assumes a string with defined length, not necessarily null terminated.
uint8_t get_uint8(const nsize_t i) const
std::string get_string_nc(const nsize_t i) const noexcept
Assumes a null terminated string.
void check_range(const nsize_t i, const nsize_t count, const char *file, int line) const
TROOctets & operator=(TROOctets &&o) noexcept=default
void setData(uint8_t *data_, nsize_t size_, const lb_endian_t byte_order) noexcept
Internally sets the _size and _data fields after validation.
constexpr uint192dp_t get_uint192_nc(const nsize_t i) const noexcept
uint64_t get_uint64(const nsize_t i) const
constexpr uint256dp_t get_uint256_nc(const nsize_t i) const noexcept
int8_t get_int8(const nsize_t i) const
constexpr uint8_t * data() noexcept
uint256dp_t get_uint256(const nsize_t i) const
uint192dp_t get_uint192(const nsize_t i) const
constexpr uint32_t get_uint32_nc(const nsize_t i) const noexcept
constexpr uint64_t get_uint64_nc(const nsize_t i) const noexcept
constexpr uint8_t get_uint8_nc(const nsize_t i) const noexcept
constexpr uint8_t const * get_ptr() const noexcept
constexpr void setSize(nsize_t s) noexcept
uuid128_t get_uuid128(const nsize_t i) const
constexpr uint8_t const * get_ptr_nc(const nsize_t i) const noexcept
EUI48 get_eui48(const nsize_t i) const
static constexpr jau::nsize_t number(const TypeSize rhs) noexcept
virtual jau::nsize_t put(uint8_t *const buffer, lb_endian_t const le_or_be) const noexcept=0
TypeSize
Underlying integer value present octet count.
jau::nsize_t getTypeSizeInt() const noexcept
static std::unique_ptr< uuid_t > create(TypeSize const t, uint8_t const *const buffer, lb_endian_t const le_or_be)
#define ABORT(...)
Use for unconditional ::abort() call with given messages, prefix '[elapsed_time] ABORT @ file:line fu...
constexpr uint192dp_t get_uint192(uint8_t const *buffer) noexcept
See get_uint16() for reference.
constexpr uint128dp_t get_uint128(uint8_t const *buffer) noexcept
See get_uint16() for reference.
constexpr void put_uint256(uint8_t *buffer, const uint256dp_t &v) noexcept
See put_uint16() for reference.
constexpr uint16_t get_uint16(uint8_t const *buffer) noexcept
Returns a uint16_t value from the given byte address using packed_t to resolve a potential memory ali...
constexpr uint32_t get_uint32(uint8_t const *buffer) noexcept
See get_uint16() for reference.
constexpr void put_uint32(uint8_t *buffer, const uint32_t v) noexcept
See put_uint16() for reference.
constexpr uint256dp_t get_uint256(uint8_t const *buffer) noexcept
See get_uint16() for reference.
constexpr void put_uint192(uint8_t *buffer, const uint192dp_t &v) noexcept
See put_uint16() for reference.
lb_endian_t
Simplified reduced endian type only covering little- and big-endian.
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
constexpr void put_uint64(uint8_t *buffer, const uint64_t &v) noexcept
See put_uint16() for reference.
constexpr void put_uint16(uint8_t *buffer, const uint16_t v) noexcept
Put the given uint16_t value into the given byte address using packed_t to resolve a potential memory...
constexpr void put_uint128(uint8_t *buffer, const uint128dp_t &v) noexcept
See put_uint16() for reference.
constexpr uint64_t get_uint64(uint8_t const *buffer) noexcept
See get_uint16() for reference.
constexpr int8_t get_int8(uint8_t const *buffer) noexcept
std::string to_string(const alphabet &v) noexcept
constexpr T min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
uuid128_t get_uuid128(uint8_t const *buffer) noexcept
std::string bytesHexString(const void *data, const nsize_t offset, const nsize_t length, const bool lsbFirst, const bool lowerCase=true) noexcept
Produce a hexadecimal string representation of the given byte values.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
#define JAU_TRACE_OCTETS_PRINT(...)
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
jau::nsize_t put(uint8_t *const sink, const lb_endian_t byte_order) const noexcept
A 128-bit packed uint8_t data array.
A 196-bit packed uint8_t data array.
A 256-bit packed uint8_t data array.