jaulib v1.5.0
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
octets.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020-2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef JAU_OCTETS_HPP_
26#define JAU_OCTETS_HPP_
27
28#include <algorithm>
29#include <cstdint>
30#include <cstring>
31#include <initializer_list>
32#include <memory>
33#include <string>
34#include "jau/cpp_lang_util.hpp"
35
36#include <jau/basic_types.hpp>
37#include <jau/debug.hpp>
38#include <jau/io/eui48.hpp>
39#include <jau/secmem.hpp>
40#include <jau/uuid.hpp>
41
42// #define JAU_TRACE_OCTETS 1
43#ifdef JAU_TRACE_OCTETS
44 #define JAU_TRACE_OCTETS_PRINT(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); fflush(stderr); }
45#else
46 #define JAU_TRACE_OCTETS_PRINT(...)
47#endif
48
49namespace jau {
50
51 /** \addtogroup ByteUtils
52 *
53 * @{
54 */
55
56 /**
57 * Transient read only and endian aware octet data, i.e. non persistent passthrough, owned by caller.
58 *
59 * Endian byte order is passed at construction.
60 *
61 * Constructor and assignment operations are `noexcept`. In case invalid arguments are passed, abort() is being called.
62 * This is a design choice based on reusing already existing underlying resources.
63 */
65 {
66 private:
67 /** Used memory size <= capacity, maybe zero. */
68 nsize_t _size;
69 /** Memory pointer, might be nullptr. Actual capacity known by owner, e.g. POctets. */
70 uint8_t * _data;
71 /** byte-order flag, little or big endian */
72 lb_endian_t _byte_order;
73
74 protected:
75 /**
76 * Validates the given data_ and size_.
77 *
78 * Aborts if data_ is nullptr and size_ > 0.
79 *
80 * @param data_ a memory pointer
81 * @param size_ claimed memory size
82 */
83 static inline void checkPtr(uint8_t *data_, nsize_t size_) noexcept {
84 if( nullptr == data_ && 0 < size_ ) {
85 jau_ABORT("TROOctets: nullptr with size %s > 0", std::to_string(size_).c_str());
86 abort(); // never reached
87 }
88 }
89
90 constexpr uint8_t * data() noexcept { return _data; }
91
92 /**
93 * Internally sets the _size and _data fields after validation.
94 *
95 * Aborts if data_ is nullptr and size_ > 0.
96 *
97 * @param data_ a memory pointer
98 * @param size_ used memory size
99 * @param byte_order lb_endian::little or lb_endian::big byte order, one may pass lb_endian::native.
100 */
101 inline void setData(uint8_t *data_, nsize_t size_, const lb_endian_t byte_order) noexcept {
102 JAU_TRACE_OCTETS_PRINT("POctets setData: %zu bytes @ %p -> %zu bytes @ %p",
103 _size, _data, size_, data_);
104 checkPtr(data_, size_);
105 _size = size_;
106 _data = data_;
107 _byte_order = byte_order;
108 }
109 constexpr void setSize(nsize_t s) noexcept { _size = s; }
110
111 public:
112 /**
113 * Transient passthrough read-only memory, w/o ownership ..
114 *
115 * Aborts if source is nullptr and len > 0.
116 *
117 * @param source a non nullptr memory, otherwise throws exception. Actual capacity known by owner.
118 * @param len readable size of the memory, may be zero
119 * @param byte_order lb_endian::little or lb_endian::big byte order, one may pass lb_endian::native.
120 */
121 TROOctets(const uint8_t *source, const nsize_t len, const lb_endian_t byte_order_val ) noexcept
122 : _size( len ), _data( const_cast<uint8_t *>(source) ),
123 _byte_order( byte_order_val )
124 {
125 checkPtr(_data, _size);
126 }
127
128 /**
129 * Default constructor with nullptr memory, zero size and lb_endian::native byte order.
130 *
131 * Conveniently exists to allow instantiation of variables
132 * intended for later assignment.
133 */
134 TROOctets() noexcept
135 : _size( 0 ), _data( nullptr ),
136 _byte_order( lb_endian_t::native )
137 { }
138
139 TROOctets(const TROOctets &o) noexcept = default;
140 TROOctets(TROOctets &&o) noexcept = default;
141 TROOctets& operator=(const TROOctets &o) noexcept = default;
142 TROOctets& operator=(TROOctets &&o) noexcept = default;
143
144 virtual ~TROOctets() noexcept = default;
145
146 inline void check_range(const nsize_t i, const nsize_t count, const char *file, int line) const {
147 if( i+count > _size ) {
148 throw IndexOutOfBoundsError(i, count, _size, file, line);
149 }
150 }
151
152 constexpr bool is_range_valid(const nsize_t i, const nsize_t count) const noexcept {
153 return i+count <= _size;
154 }
155
156 /** Returns byte order of this octet store. */
157 constexpr lb_endian_t byte_order() const noexcept { return _byte_order; }
158
159 /** Returns the used memory size for read and write operations, may be zero. */
160 constexpr nsize_t size() const noexcept { return _size; }
161
162 uint8_t get_uint8(const nsize_t i) const {
164 return _data[i];
165 }
166 constexpr uint8_t get_uint8_nc(const nsize_t i) const noexcept {
167 return _data[i];
168 }
169
170 int8_t get_int8(const nsize_t i) const {
172 return jau::get_int8(_data + i);
173 }
174 constexpr int8_t get_int8_nc(const nsize_t i) const noexcept {
175 return jau::get_int8(_data + i);
176 }
177
178 uint16_t get_uint16(const nsize_t i) const {
180 return jau::get_uint16(_data + i, byte_order());
181 }
182 constexpr uint16_t get_uint16_nc(const nsize_t i) const noexcept {
183 return jau::get_uint16(_data + i, byte_order());
184 }
185
186 uint32_t get_uint32(const nsize_t i) const {
188 return jau::get_uint32(_data + i, byte_order());
189 }
190 constexpr uint32_t get_uint32_nc(const nsize_t i) const noexcept {
191 return jau::get_uint32(_data + i, byte_order());
192 }
193
196 return jau::io::net::EUI48(_data+i, byte_order() );
197 }
198 inline jau::io::net::EUI48 get_eui48_nc(const nsize_t i) const noexcept {
199 return jau::io::net::EUI48(_data+i, byte_order() );
200 }
201
202 uint64_t get_uint64(const nsize_t i) const {
204 return jau::get_uint64(_data + i, byte_order());
205 }
206 constexpr uint64_t get_uint64_nc(const nsize_t i) const noexcept {
207 return jau::get_uint64(_data + i, byte_order());
208 }
209
212 return jau::get_uint128(_data + i, byte_order());
213 }
214 constexpr uint128dp_t get_uint128_nc(const nsize_t i) const noexcept {
215 return jau::get_uint128(_data + i, byte_order());
216 }
217
220 return jau::get_uint192(_data + i, byte_order());
221 }
222 constexpr uint192dp_t get_uint192_nc(const nsize_t i) const noexcept {
223 return jau::get_uint192(_data + i, byte_order());
224 }
225
228 return jau::get_uint256(_data + i, byte_order());
229 }
230 constexpr uint256dp_t get_uint256_nc(const nsize_t i) const noexcept {
231 return jau::get_uint256(_data + i, byte_order());
232 }
233
234 /** Assumes a null terminated string */
235 std::string get_string(const nsize_t i) const {
236 check_range(i, 1, E_FILE_LINE); // minimum size
237 return std::string( (const char*)(_data+i) );
238 }
239 /** Assumes a null terminated string */
240 inline std::string get_string_nc(const nsize_t i) const noexcept {
241 return std::string( (const char*)(_data+i) );
242 }
243
244 /** Assumes a string with defined length, not necessarily null terminated */
245 std::string get_string(const nsize_t i, const nsize_t length) const {
246 check_range(i, length, E_FILE_LINE);
247 return std::string( (const char*)(_data+i), length );
248 }
249
250 uuid16_t get_uuid16(const nsize_t i) const {
251 return uuid16_t(get_uint16(i));
252 }
253 inline uuid16_t get_uuid16_nc(const nsize_t i) const noexcept {
254 return uuid16_t(get_uint16_nc(i));
255 }
256
261 inline uuid128_t get_uuid128_nc(const nsize_t i) const noexcept {
262 return jau::get_uuid128(_data + i, byte_order());
263 }
264
265 std::unique_ptr<const uuid_t> get_uuid(const nsize_t i, const uuid_t::TypeSize tsize) const {
267 return uuid_t::create(tsize, _data + i, byte_order());
268 }
269
270 constexpr uint8_t const * get_ptr() const noexcept { return _data; }
271 uint8_t const * get_ptr(const nsize_t i) const {
273 return _data + i;
274 }
275 constexpr uint8_t const * get_ptr_nc(const nsize_t i) const noexcept {
276 return _data + i;
277 }
278
279 bool operator==(const TROOctets& rhs) const noexcept {
280 return _size == rhs._size && 0 == std::memcmp(_data, rhs._data, _size);
281 }
282 bool operator!=(const TROOctets& rhs) const noexcept {
283 return !(*this == rhs);
284 }
285
286 std::string toString() const noexcept {
287 std::string s;
288 do_noexcept([&]() {
289 s.append("size ")
290 .append(std::to_string(_size))
291 .append(", [").append(to_string( byte_order() )).append(", ").append(to_string( byte_order() )).append("], ro: ");
292 });
293 jau::appendHexString(s, _data, _size);
294 return s;
295 }
296 };
297
298 /**
299 * Transient endian aware octet data, i.e. non persistent passthrough, owned by caller.
300 *
301 * Endian byte order is passed at construction.
302 *
303 * Constructor and assignment operations are `noexcept`. In case invalid arguments are passed, abort() is being called.
304 * This is a design choice based on reusing already existing underlying resources.
305 */
306 class TOctets : public TROOctets
307 {
308 public:
309 /**
310 * Transient passthrough r/w memory, w/o ownership ..
311 *
312 * Aborts if source is nullptr and len > 0.
313 *
314 * @param source transient data source
315 * @param len length of transient data source
316 * @param byte_order lb_endian::little or lb_endian::big byte order, one may pass lb_endian::native.
317 */
318 TOctets(uint8_t *source, const nsize_t len, const lb_endian_t byte_order) noexcept
319 : TROOctets(source, len, byte_order) {}
320
321 TOctets(const TOctets &o) noexcept = default;
322 TOctets(TOctets &&o) noexcept = default;
323 TOctets& operator=(const TOctets &o) noexcept = default;
324 TOctets& operator=(TOctets &&o) noexcept = default;
325
326 ~TOctets() noexcept override = default;
327
328 void put_int8(const nsize_t i, const int8_t v) {
330 data()[i] = static_cast<uint8_t>(v);
331 }
332 constexpr void put_int8_nc(const nsize_t i, const int8_t v) noexcept {
333 data()[i] = static_cast<uint8_t>(v);
334 }
335
336 void put_uint8(const nsize_t i, const uint8_t v) {
338 data()[i] = v;
339 }
340 constexpr void put_uint8_nc(const nsize_t i, const uint8_t v) noexcept {
341 data()[i] = v;
342 }
343
344 void put_uint16(const nsize_t i, const uint16_t v) {
346 jau::put_uint16(data() + i, v, byte_order());
347 }
348 constexpr void put_uint16_nc(const nsize_t i, const uint16_t v) noexcept {
349 jau::put_uint16(data() + i, v, byte_order());
350 }
351
352 void put_uint32(const nsize_t i, const uint32_t v) {
354 jau::put_uint32(data() + i, v, byte_order());
355 }
356 constexpr void put_uint32_nc(const nsize_t i, const uint32_t v) noexcept {
357 jau::put_uint32(data() + i, v, byte_order());
358 }
359
360 void put_eui48(const nsize_t i, const jau::io::net::EUI48 & v) {
361 check_range(i, sizeof(v.b), E_FILE_LINE);
362 v.put(data() + i, byte_order() );
363 }
364 inline void put_eui48_nc(const nsize_t i, const jau::io::net::EUI48 & v) noexcept {
365 v.put(data() + i, byte_order() );
366 }
367
368 void put_uint64(const nsize_t i, const uint64_t & v) {
370 jau::put_uint64(data() + i, v, byte_order());
371 }
372 constexpr void put_uint64_nc(const nsize_t i, const uint64_t & v) noexcept {
373 jau::put_uint64(data() + i, v, byte_order());
374 }
375
376 void put_uint128(const nsize_t i, const uint128dp_t & v) {
378 jau::put_uint128(data() + i, v, byte_order());
379 }
380 constexpr void put_uint128_nc(const nsize_t i, const uint128dp_t & v) noexcept {
381 jau::put_uint128(data() + i, v, byte_order());
382 }
383
384 void put_uint192(const nsize_t i, const uint192dp_t & v) {
386 jau::put_uint192(data() + i, v, byte_order());
387 }
388 constexpr void put_uint192_nc(const nsize_t i, const uint192dp_t & v) noexcept {
389 jau::put_uint192(data() + i, v, byte_order());
390 }
391
392 void put_uint256(const nsize_t i, const uint256dp_t & v) {
394 jau::put_uint256(data() + i, v, byte_order());
395 }
396 constexpr void put_uint256_nc(const nsize_t i, const uint256dp_t & v) noexcept {
397 jau::put_uint256(data() + i, v, byte_order());
398 }
399
400 void put_octets(const nsize_t i, const TROOctets & v) {
402 std::memcpy(data() + i, v.get_ptr(), v.size());
403 }
404 void put_octets_nc(const nsize_t i, const TROOctets & v) noexcept {
405 std::memcpy(data() + i, v.get_ptr(), v.size());
406 }
407 void put_octets(const nsize_t i, const TROOctets & v, const nsize_t v_off, const nsize_t v_len) {
408 const nsize_t size = std::min(v.size()-v_off, v_len);
410 std::memcpy(data() + i, v.get_ptr() + v_off, size);
411 }
412 void put_octets_nc(const nsize_t i, const TROOctets & v, const nsize_t v_off, const nsize_t v_len) noexcept {
413 const nsize_t size = std::min(v.size()-v_off, v_len);
414 std::memcpy(data() + i, v.get_ptr() + v_off, size);
415 }
416
417 void put_bytes(const nsize_t i, const uint8_t *source, const nsize_t byte_count) {
418 check_range(i, byte_count, E_FILE_LINE);
419 std::memcpy(data() + i, source, byte_count);
420 }
421 void put_bytes_nc(const nsize_t i, const uint8_t *source, const nsize_t byte_count) noexcept {
422 std::memcpy(data() + i, source, byte_count);
423 }
424
425 void memmove(const nsize_t i, const uint8_t *source, const nsize_t byte_count) {
426 check_range(i, byte_count, E_FILE_LINE);
427 std::memmove(data() + i, source, byte_count);
428 }
429 void memmove_nc(const nsize_t i, const uint8_t *source, const nsize_t byte_count) noexcept {
430 std::memmove(data() + i, source, byte_count);
431 }
432
433 void memset(const nsize_t i, const uint8_t c, const nsize_t byte_count) {
434 check_range(i, byte_count, E_FILE_LINE);
435 std::memset(data() + i, c, byte_count);
436 }
437 void memset_nc(const nsize_t i, const uint8_t c, const nsize_t byte_count) noexcept {
438 std::memset(data() + i, c, byte_count);
439 }
440 void bzero(const nsize_t i, const nsize_t byte_count) {
441 check_range(i, byte_count, E_FILE_LINE);
442 zero_bytes_sec(data() + i, byte_count);
443 }
444 void bzero_nc(const nsize_t i, const nsize_t byte_count) noexcept {
445 zero_bytes_sec(data() + i, byte_count);
446 }
447 void bzero() noexcept {
448 if( size() > 0 ) {
450 }
451 }
452
453 void put_string(const nsize_t i, const std::string & v, const nsize_t max_len, const bool includeEOS) {
454 const nsize_t size1 = v.size() + ( includeEOS ? 1 : 0 );
455 const nsize_t size = std::min(size1, max_len);
457 std::memcpy(data() + i, v.c_str(), size);
458 if( size < size1 && includeEOS ) {
459 *(data() + i + size - 1) = 0; // ensure EOS
460 }
461 }
462 void put_string_nc(const nsize_t i, const std::string & v, const nsize_t max_len, const bool includeEOS) noexcept {
463 const nsize_t size1 = v.size() + ( includeEOS ? 1 : 0 );
464 const nsize_t size = std::min(size1, max_len);
465 std::memcpy(data() + i, v.c_str(), size);
466 if( size < size1 && includeEOS ) {
467 *(data() + i + size - 1) = 0; // ensure EOS
468 }
469 }
470
471 void put_uuid(const nsize_t i, const uuid_t & v) {
473 v.put(data() + i, byte_order());
474 }
475 void put_uuid_nc(const nsize_t i, const uuid_t & v) noexcept {
476 v.put(data() + i, byte_order());
477 }
478
479 inline uint8_t * get_wptr() noexcept { return data(); }
480
481 uint8_t * get_wptr(const nsize_t i) {
483 return data() + i;
484 }
485 inline uint8_t * get_wptr_nc(const nsize_t i) noexcept {
486 return data() + i;
487 }
488
489 std::string toString() const noexcept {
490 return string_noexcept([&](){ return "size "+std::to_string(size())+", rw: "+toHexString(get_ptr(), size()); });
491 }
492 };
493
494 /**
495 * Transient endian aware octet data slice, i.e. a view of an TOctet.
496 *
497 * Endian byte order is defined by its parent TOctet.
498 */
500 {
501 private:
502 const TOctets & _parent;
503 nsize_t const _offset;
504 nsize_t const _size;
505
506 public:
507 /**
508 * Creates a view of a given TOctet with the specified offset_ and size_.
509 *
510 * @param buffer_ the parent TOctet buffer
511 * @param offset_ offset to the parent TOctet buffer
512 * @param size_ size of this view, starting at offset_
513 * @throws IndexOutOfBoundsException if offset_ + size_ > parent buffer_.size()
514 */
515 TOctetSlice(const TOctets &buffer_, const nsize_t offset_, const nsize_t size_)
516 : _parent(buffer_), _offset(offset_), _size(size_)
517 {
518 if( offset_+_size > buffer_.size() ) {
519 throw IndexOutOfBoundsError(offset_, _size, buffer_.size(), E_FILE_LINE);
520 }
521 }
522
523 /** Returns byte order of this octet store. */
524 constexpr lb_endian_t byte_order() const noexcept { return _parent.byte_order(); }
525
526 constexpr nsize_t size() const noexcept { return _size; }
527 constexpr nsize_t offset() const noexcept { return _offset; }
528 constexpr const TOctets& parent() const noexcept { return _parent; }
529
530 uint8_t get_uint8(const nsize_t i) const {
531 return _parent.get_uint8(_offset+i);
532 }
533 constexpr uint8_t get_uint8_nc(const nsize_t i) const noexcept {
534 return _parent.get_uint8_nc(_offset+i);
535 }
536
537 uint16_t get_uint16(const nsize_t i) const {
538 return _parent.get_uint16(_offset+i);
539 }
540 constexpr uint16_t get_uint16_nc(const nsize_t i) const noexcept {
541 return _parent.get_uint16_nc(_offset+i);
542 }
543
544 uint8_t const * get_ptr(const nsize_t i) const {
545 return _parent.get_ptr(_offset+i);
546 }
547 constexpr uint8_t const * get_ptr_nc(const nsize_t i) const noexcept {
548 return _parent.get_ptr_nc(_offset+i);
549 }
550
551 std::string toString() const noexcept {
552 return string_noexcept([&](){ return "offset "+std::to_string(_offset)+", size "+std::to_string(_size)+": "+toHexString(_parent.get_ptr()+_offset, _size); } );
553 }
554 };
555
556 /**
557 * Persistent endian aware octet data, i.e. owned dynamic heap memory allocation.
558 *
559 * Endian byte order is passed at construction.
560 *
561 * Constructor and assignment operations are **not** completely `noexcept` and may throw exceptions.
562 * This is a design choice based on dynamic resource allocation performed by this class.
563 */
564 class POctets : public TOctets
565 {
566 private:
567 nsize_t _capacity;
568
569 void freeData() {
570 uint8_t * ptr = data();
571 if( nullptr != ptr ) {
572 JAU_TRACE_OCTETS_PRINT("POctets release: %p", ptr);
573 free(ptr);
574 } // else: zero sized POctets w/ nullptr are supported
575 }
576
577 /**
578 * Allocate a memory chunk.
579 * @param size
580 * @return
581 * @throws OutOfMemoryError if running out of memory
582 */
583 [[nodiscard]] static uint8_t * allocData(const nsize_t size) {
584 if( size <= 0 ) {
585 return nullptr;
586 }
587 uint8_t * m = static_cast<uint8_t*>( std::malloc(size) );
588 if( nullptr == m ) {
589 throw OutOfMemoryError("allocData size "+std::to_string(size)+" -> nullptr", E_FILE_LINE);
590 }
591 return m;
592 }
593
594 public:
595 /** Returns the memory capacity, never zero, greater or equal size(). */
596 constexpr nsize_t capacity() const noexcept { return _capacity; }
597
598 /** Returns the remaining octets for put left, i.e. capacity() - size(). */
599 constexpr nsize_t remaining() const noexcept { return _capacity - size(); }
600
601 /**
602 * Zero sized POctets instance.
603 *
604 * Will not throw an OutOfMemoryError exception due to no allocation.
605 *
606 * @param byte_order lb_endian::little or lb_endian::big byte order, one may pass lb_endian::native.
607 */
609 : TOctets(nullptr, 0, byte_order), _capacity(0)
610 {
611 JAU_TRACE_OCTETS_PRINT("POctets ctor0: zero-sized");
612 }
613
614 /**
615 * Takes ownership (malloc(size) and copy, free) ..
616 *
617 * Capacity and size will be of given source size.
618 *
619 * @param source_ source data to be copied into this new instance
620 * @param size_ length of source data
621 * @param byte_order lb_endian::little or lb_endian::big byte order, one may pass lb_endian::native.
622 * @throws IllegalArgumentException if source_ is nullptr and size_ > 0
623 * @throws OutOfMemoryError if allocation fails
624 */
625 POctets(const uint8_t *source_, const nsize_t size_, const lb_endian_t byte_order)
626 : TOctets( allocData(size_), size_, byte_order),
627 _capacity( size_ )
628 {
629 if( 0 < size_ ) {
630 if( nullptr == source_ ) {
631 throw IllegalArgumentError("source nullptr with size "+std::to_string(size_)+" > 0", E_FILE_LINE);
632 }
633 std::memcpy(data(), source_, size_);
634 }
635 JAU_TRACE_OCTETS_PRINT("POctets ctor1: %p", data());
636 }
637
638 /**
639 * Takes ownership (malloc(size) and copy, free) ..
640 *
641 * Capacity and size will be of given source size.
642 *
643 * @param sourcelist source initializer list data to be copied into this new instance with implied size
644 * @param byte_order lb_endian::little or lb_endian::big byte order, one may pass lb_endian::native.
645 * @throws IllegalArgumentException if source_ is nullptr and size_ > 0
646 * @throws OutOfMemoryError if allocation fails
647 */
648 POctets(std::initializer_list<uint8_t> sourcelist, const lb_endian_t byte_order)
649 : TOctets( allocData(sourcelist.size()), sourcelist.size(), byte_order),
650 _capacity( sourcelist.size() )
651 {
652 if( 0 < _capacity ) {
653 std::memcpy(data(), sourcelist.begin(), _capacity);
654 }
655 JAU_TRACE_OCTETS_PRINT("POctets ctor1: %p", data());
656 }
657
658 /**
659 * New buffer (malloc(capacity), free)
660 *
661 * @param capacity_ new capacity
662 * @param size_ new size with size <= capacity
663 * @param byte_order lb_endian::little or lb_endian::big byte order, one may pass lb_endian::native.
664 * @throws IllegalArgumentException if capacity_ < size_
665 * @throws OutOfMemoryError if allocation fails
666 */
667 POctets(const nsize_t capacity_, const nsize_t size_, const lb_endian_t byte_order)
668 : TOctets( allocData( capacity_ ), size_, byte_order ),
669 _capacity( capacity_ )
670 {
671 if( capacity() < size() ) {
672 throw IllegalArgumentError("capacity "+std::to_string(capacity())+" < size "+std::to_string(size()), E_FILE_LINE);
673 }
674 JAU_TRACE_OCTETS_PRINT("POctets ctor2: %p", data());
675 }
676
677 /**
678 * New buffer (malloc, free)
679 *
680 * @param size new size and capacity
681 * @param byte_order lb_endian::little or lb_endian::big byte order, one may pass lb_endian::native.
682 * @throws OutOfMemoryError if allocation fails
683 */
686 {
687 JAU_TRACE_OCTETS_PRINT("POctets ctor3: %p", data());
688 }
689
690 /**
691 * Copy constructor
692 *
693 * Capacity of this new instance will be of source.size() only.
694 *
695 * @param source POctet source to be copied
696 * @throws OutOfMemoryError if allocation fails
697 */
698 POctets(const POctets &source)
699 : TOctets( allocData(source.size()), source.size(), source.byte_order() ),
700 _capacity( source.size() )
701 {
702 std::memcpy(data(), source.get_ptr(), source.size());
703 JAU_TRACE_OCTETS_PRINT("POctets ctor-cpy0: %p -> %p", source.get_ptr(), data());
704 }
705
706 /**
707 * Copy constructor (explicit), allowing to set a higher capacity
708 * than source.size() in contrast to the copy-constructor.
709 *
710 * @param source POctet source to be copied
711 * @param capacity_ used to determine new capacity `std::max(capacity_, source.size())`
712 * @throws OutOfMemoryError if allocation fails
713 */
714 explicit POctets(const POctets &source, const nsize_t capacity_)
715 : TOctets( allocData(source.size()), source.size(), source.byte_order() ),
716 _capacity( std::max(capacity_, source.size()) )
717 {
718 std::memcpy(data(), source.get_ptr(), source.size());
719 JAU_TRACE_OCTETS_PRINT("POctets ctor-cpy-extra1: %p -> %p", source.get_ptr(), data());
720 }
721
722 /**
723 * Move constructor
724 * @param o POctet source to be taken over
725 */
726 POctets(POctets &&o) noexcept
727 : TOctets( o.data(), o.size(), o.byte_order() ),
728 _capacity( o.capacity() )
729 {
730 // moved origin data references
731 // purge origin
732 o.setData(nullptr, 0, o.byte_order());
733 o._capacity = 0;
734 JAU_TRACE_OCTETS_PRINT("POctets ctor-move0: %p", data());
735 }
736
737 /**
738 * Assignment operator
739 * @param _source POctet source to be copied
740 * @return
741 * @throws OutOfMemoryError if allocation fails
742 */
743 POctets& operator=(const POctets &_source) {
744 if( this == &_source ) {
745 return *this;
746 }
747 freeData();
748 setData(allocData(_source.size()), _source.size(), _source.byte_order());
749 _capacity = _source.size();
750 std::memcpy(data(), _source.get_ptr(), _source.size());
751 JAU_TRACE_OCTETS_PRINT("POctets assign0: %p", data());
752 return *this;
753 }
754
755 /**
756 * Move assignment operator
757 * @param o POctet source to be taken over
758 * @return
759 */
760 POctets& operator=(POctets &&o) noexcept {
761 // move origin data references
762 setData(o.data(), o.size(), o.byte_order());
763 _capacity = o._capacity;
764 // purge origin
765 o.setData(nullptr, 0, o.byte_order());
766 o._capacity = 0;
767 JAU_TRACE_OCTETS_PRINT("POctets assign-move0: %p", data());
768 return *this;
769 }
770
771 ~POctets() noexcept override {
772 freeData();
773 setData(nullptr, 0, byte_order());
774 _capacity=0;
775 }
776
777 /**
778 * Makes a persistent POctets by copying the data from TROOctets.
779 * @param _source TROOctets to be copied
780 * @throws OutOfMemoryError if allocation fails
781 */
782 POctets(const TROOctets & _source)
783 : TOctets( allocData(_source.size()), _source.size(), _source.byte_order() ),
784 _capacity( _source.size() )
785 {
786 std::memcpy(data(), _source.get_ptr(), _source.size());
787 JAU_TRACE_OCTETS_PRINT("POctets ctor-cpy1: %p", data());
788 }
789
790 /**
791 * Assignment operator for TROOctets
792 * @param _source TROOctets to be copied
793 * @return
794 * @throws OutOfMemoryError if allocation fails
795 */
796 POctets& operator=(const TROOctets &_source) {
797 if( static_cast<TROOctets *>(this) == &_source ) {
798 return *this;
799 }
800 freeData();
801 setData(allocData(_source.size()), _source.size(), _source.byte_order());
802 _capacity = _source.size();
803 std::memcpy(data(), _source.get_ptr(), _source.size());
804 JAU_TRACE_OCTETS_PRINT("POctets assign1: %p", data());
805 return *this;
806 }
807
808 /**
809 * Makes a persistent POctets by copying the data from TOctetSlice.
810 * @param _source TROOctetSlice to be copied
811 * @throws OutOfMemoryError if allocation fails
812 */
813 POctets(const TOctetSlice & _source)
814 : TOctets( allocData(_source.size()), _source.size(), _source.byte_order() ),
815 _capacity( _source.size() )
816 {
817 std::memcpy(data(), _source.parent().get_ptr() + _source.offset(), _source.size());
818 JAU_TRACE_OCTETS_PRINT("POctets ctor-cpy2: %p", data());
819 }
820
821 /**
822 * Assignment operator for TOctetSlice
823 * @param _source TOctetSlice to be copied
824 * @return
825 * @throws OutOfMemoryError if allocation fails
826 */
827 POctets& operator=(const TOctetSlice &_source) {
828 freeData();
829 setData(allocData(_source.size()), _source.size(), _source.byte_order());
830 _capacity = _source.size();
831 std::memcpy(data(), _source.get_ptr(0), _source.size());
832 JAU_TRACE_OCTETS_PRINT("POctets assign2: %p", data());
833 return *this;
834 }
835
836 /**
837 * Resizes this instance, including its capacity
838 * @param newCapacity new capacity, must be >= newSize
839 * @param newSize new size, must be < newCapacity
840 * @return
841 * @throws OutOfMemoryError if allocation fails
842 * @throws IllegalArgumentException if newCapacity < newSize
843 */
844 POctets & resize(const nsize_t newCapacity, const nsize_t newSize) {
845 if( newCapacity < newSize ) {
846 throw IllegalArgumentError("newCapacity "+std::to_string(newCapacity)+" < newSize "+std::to_string(newSize), E_FILE_LINE);
847 }
848 if( newCapacity != _capacity ) {
849 if( newSize > size() ) {
850 recapacity(newCapacity);
851 setSize(newSize);
852 } else {
853 setSize(newSize);
854 recapacity(newCapacity);
855 }
856 } else {
857 setSize(newSize);
858 }
859 return *this;
860 }
861
862 /**
863 * Sets a new size for this instance.
864 * @param newSize new size, must be <= current capacity()
865 * @return
866 * @throws IllegalArgumentException if newSize > current capacity()
867 */
868 POctets & resize(const nsize_t newSize) {
869 if( _capacity < newSize ) {
870 throw IllegalArgumentError("capacity "+std::to_string(_capacity)+" < newSize "+std::to_string(newSize), E_FILE_LINE);
871 }
872 setSize(newSize);
873 return *this;
874 }
875
876 /**
877 * Changes the capacity.
878 *
879 * @param newCapacity new capacity, must be >= size()
880 * @return
881 * @throws OutOfMemoryError if allocation fails
882 * @throws IllegalArgumentException if newCapacity < size()
883 */
884 POctets & recapacity(const nsize_t newCapacity) {
885 if( newCapacity < size() ) {
886 throw IllegalArgumentError("newCapacity "+std::to_string(newCapacity)+" < size "+std::to_string(size()), E_FILE_LINE);
887 }
888 if( newCapacity == _capacity ) {
889 return *this;
890 }
891 uint8_t* data2 = allocData(newCapacity);
892 if( size() > 0 ) {
893 memcpy(data2, get_ptr(), size());
894 }
895 JAU_TRACE_OCTETS_PRINT("POctets recapacity: %p -> %p", data(), data2);
896 free(data());
897 setData(data2, size(), byte_order());
898 _capacity = newCapacity;
899 return *this;
900 }
901
902 /**
903 * Append and assign operator
904 * @param b
905 * @return
906 * @throws OutOfMemoryError if allocation due to potential recapacity() fails
907 */
909 if( 0 < b.size() ) {
910 const nsize_t newSize = size() + b.size();
911 if( _capacity < newSize ) {
912 recapacity( newSize );
913 }
914 memcpy(data()+size(), b.get_ptr(), b.size());
915 setSize(newSize);
916 }
917 return *this;
918 }
919 /**
920 * Append and assign operator
921 * @param b
922 * @return
923 * @throws OutOfMemoryError if allocation due to potential recapacity() fails
924 */
926 if( 0 < b.size() ) {
927 const nsize_t newSize = size() + b.size();
928 if( _capacity < newSize ) {
929 recapacity( newSize );
930 }
931 memcpy(data()+size(), b.parent().get_ptr()+b.offset(), b.size());
932 setSize(newSize);
933 }
934 return *this;
935 }
936
937 std::string toString() const noexcept {
938 return string_noexcept([&](){ return "size "+std::to_string(size())+", capacity "+std::to_string(capacity())+", "+toHexString(get_ptr(), size()); } );
939 }
940 };
941
942 /**
943 * Persistent endian aware octet data, i.e. owned automatic fixed size memory allocation.
944 *
945 * Endian byte order is passed at construction.
946 *
947 * Constructor and assignment operations are **not** completely `noexcept` and may throw exceptions.
948 * This is a design choice based on dynamic resource allocation performed by this class.
949 */
950 template<jau::nsize_t FixedSize>
951 class AOctets : public TOctets
952 {
953 public:
954 /** Fixed maximum size */
955 constexpr static const jau::nsize_t fixed_size = FixedSize;
956
957 private:
958 uint8_t smem[fixed_size];
959
960 public:
961 /**
962 * Sized AOctets instance.
963 *
964 * @param byte_order lb_endian::little or lb_endian::big byte order, one may pass lb_endian::native.
965 */
968 {
969 JAU_TRACE_OCTETS_PRINT("AOctets ctor0: sized");
970 }
971
972 /**
973 * Takes ownership (malloc(size) and copy, free) ..
974 *
975 * Capacity and size will be of given source size.
976 *
977 * @param source_ source data to be copied into this new instance
978 * @param size_ length of source data
979 * @param byte_order lb_endian::little or lb_endian::big byte order, one may pass lb_endian::native.
980 * @throws IllegalArgumentException if fixed_size < source_size_
981 * @throws IllegalArgumentException if source_ is nullptr and size_ > 0
982 */
983 AOctets(const uint8_t *source_, const nsize_t source_size_, const lb_endian_t byte_order)
984 : TOctets( smem, std::min(fixed_size, source_size_), byte_order)
985 {
986 if( source_size_ > fixed_size ) {
987 throw IllegalArgumentError("source size "+std::to_string(source_size_)+" > capacity "+std::to_string(fixed_size), E_FILE_LINE);
988 } else if( 0 < source_size_ ) {
989 if( nullptr == source_ ) {
990 throw IllegalArgumentError("source nullptr with size "+std::to_string(source_size_)+" > 0", E_FILE_LINE);
991 }
992 std::memcpy(data(), source_, source_size_);
993 }
994 JAU_TRACE_OCTETS_PRINT("AOctets ctor1: %p", data());
995 }
996
997 /**
998 * Takes ownership (malloc(size) and copy, free) ..
999 *
1000 * Capacity and size will be of given source size.
1001 *
1002 * @param sourcelist source initializer list data to be copied into this new instance with implied size
1003 * @param byte_order lb_endian::little or lb_endian::big byte order, one may pass lb_endian::native.
1004 * @throws IllegalArgumentException if fixed_size < source size
1005 */
1006 AOctets(std::initializer_list<uint8_t> sourcelist, const lb_endian_t byte_order)
1007 : TOctets( smem, std::min(fixed_size, sourcelist.size()), byte_order)
1008 {
1009 if( sourcelist.size() > fixed_size ) {
1010 throw IllegalArgumentError("source size "+std::to_string(sourcelist.size())+" > capacity "+std::to_string(fixed_size), E_FILE_LINE);
1011 } else if( 0 < sourcelist.size() ) {
1012 std::memcpy(data(), sourcelist.begin(), sourcelist.size());
1013 }
1014 JAU_TRACE_OCTETS_PRINT("AOctets ctor1: %p", data());
1015 }
1016
1017 /**
1018 * Copy constructor
1019 *
1020 * Capacity of this new instance will be of source.size() only.
1021 *
1022 * @param source POctet source to be copied
1023 * @throws IllegalArgumentException if fixed_size < source size
1024 */
1025 AOctets(const TROOctets &source)
1026 : TOctets( smem, std::min(fixed_size, source.size()), source.byte_order() )
1027 {
1028 if( source.size() > fixed_size ) {
1029 throw IllegalArgumentError("source size "+std::to_string(source.size())+" > capacity "+std::to_string(fixed_size), E_FILE_LINE);
1030 } else if( 0 < source.size() ) {
1031 std::memcpy(data(), source.get_ptr(), source.size());
1032 }
1033 JAU_TRACE_OCTETS_PRINT("AOctets ctor-cpy0: %p -> %p", source.get_ptr(), data());
1034 }
1035
1036 /**
1037 * Assignment operator
1038 * @param _source POctet source to be copied
1039 * @return
1040 * @throws IllegalArgumentException if fixed_size < source size
1041 */
1042 AOctets& operator=(const TROOctets &_source) {
1043 if( this == &_source ) {
1044 return *this;
1045 }
1046 if( _source.size() > fixed_size ) {
1047 throw IllegalArgumentError("source size "+std::to_string(_source.size())+" > capacity "+std::to_string(fixed_size), E_FILE_LINE);
1048 } else if( 0 < _source.size() ) {
1049 std::memcpy(smem, _source.get_ptr(), _source.size());
1050 }
1051 setData(smem, _source.size(), _source.byte_order());
1052 JAU_TRACE_OCTETS_PRINT("AOctets assign0: %p", data());
1053 return *this;
1054 }
1055
1056
1057 ~AOctets() noexcept override {
1058 setData(nullptr, 0, byte_order());
1059 }
1060
1061 /**
1062 * Sets a new size for this instance.
1063 * @param newSize new size, must be <= current capacity()
1064 * @return
1065 * @throws IllegalArgumentException if fixed_size < newSize
1066 */
1067 AOctets & resize(const nsize_t newSize) {
1068 if( fixed_size < newSize ) {
1069 throw IllegalArgumentError("capacity "+std::to_string(fixed_size)+" < newSize "+std::to_string(newSize), E_FILE_LINE);
1070 }
1071 setSize(newSize);
1072 return *this;
1073 }
1074
1075 std::string toString() const noexcept {
1076 return string_noexcept([&](){ return "size "+std::to_string(size())+", fixed_size "+std::to_string(fixed_size)+", "+toHexString(get_ptr(), size()); });
1077 }
1078 };
1079
1080 /**@}*/
1081
1082} /* namespace jau */
1083
1084
1085#endif /* JAU_OCTETS_HPP_ */
AOctets(const TROOctets &source)
Copy constructor.
Definition octets.hpp:1025
static constexpr const jau::nsize_t fixed_size
Fixed maximum size.
Definition octets.hpp:955
~AOctets() noexcept override
Definition octets.hpp:1057
AOctets & operator=(const TROOctets &_source)
Assignment operator.
Definition octets.hpp:1042
AOctets(const lb_endian_t byte_order) noexcept
Sized AOctets instance.
Definition octets.hpp:966
std::string toString() const noexcept
Definition octets.hpp:1075
AOctets & resize(const nsize_t newSize)
Sets a new size for this instance.
Definition octets.hpp:1067
AOctets(const uint8_t *source_, const nsize_t source_size_, const lb_endian_t byte_order)
Takes ownership (malloc(size) and copy, free) .
Definition octets.hpp:983
AOctets(std::initializer_list< uint8_t > sourcelist, const lb_endian_t byte_order)
Takes ownership (malloc(size) and copy, free) .
Definition octets.hpp:1006
POctets & operator=(const TROOctets &_source)
Assignment operator for TROOctets.
Definition octets.hpp:796
POctets(POctets &&o) noexcept
Move constructor.
Definition octets.hpp:726
POctets & operator=(POctets &&o) noexcept
Move assignment operator.
Definition octets.hpp:760
POctets(const nsize_t capacity_, const nsize_t size_, const lb_endian_t byte_order)
New buffer (malloc(capacity), free)
Definition octets.hpp:667
POctets(const POctets &source)
Copy constructor.
Definition octets.hpp:698
POctets & operator=(const POctets &_source)
Assignment operator.
Definition octets.hpp:743
POctets(const nsize_t size, const lb_endian_t byte_order)
New buffer (malloc, free)
Definition octets.hpp:684
POctets(const TROOctets &_source)
Makes a persistent POctets by copying the data from TROOctets.
Definition octets.hpp:782
~POctets() noexcept override
Definition octets.hpp:771
POctets & resize(const nsize_t newCapacity, const nsize_t newSize)
Resizes this instance, including its capacity.
Definition octets.hpp:844
POctets & operator=(const TOctetSlice &_source)
Assignment operator for TOctetSlice.
Definition octets.hpp:827
POctets & operator+=(const TOctetSlice &b)
Append and assign operator.
Definition octets.hpp:925
constexpr nsize_t remaining() const noexcept
Returns the remaining octets for put left, i.e.
Definition octets.hpp:599
POctets(const POctets &source, const nsize_t capacity_)
Copy constructor (explicit), allowing to set a higher capacity than source.size() in contrast to the ...
Definition octets.hpp:714
std::string toString() const noexcept
Definition octets.hpp:937
POctets & recapacity(const nsize_t newCapacity)
Changes the capacity.
Definition octets.hpp:884
constexpr nsize_t capacity() const noexcept
Returns the memory capacity, never zero, greater or equal size().
Definition octets.hpp:596
POctets(std::initializer_list< uint8_t > sourcelist, const lb_endian_t byte_order)
Takes ownership (malloc(size) and copy, free) .
Definition octets.hpp:648
POctets & resize(const nsize_t newSize)
Sets a new size for this instance.
Definition octets.hpp:868
POctets(const lb_endian_t byte_order) noexcept
Zero sized POctets instance.
Definition octets.hpp:608
POctets & operator+=(const TROOctets &b)
Append and assign operator.
Definition octets.hpp:908
POctets(const TOctetSlice &_source)
Makes a persistent POctets by copying the data from TOctetSlice.
Definition octets.hpp:813
POctets(const uint8_t *source_, const nsize_t size_, const lb_endian_t byte_order)
Takes ownership (malloc(size) and copy, free) .
Definition octets.hpp:625
Transient endian aware octet data slice, i.e.
Definition octets.hpp:500
std::string toString() const noexcept
Definition octets.hpp:551
uint8_t get_uint8(const nsize_t i) const
Definition octets.hpp:530
constexpr lb_endian_t byte_order() const noexcept
Returns byte order of this octet store.
Definition octets.hpp:524
constexpr nsize_t offset() const noexcept
Definition octets.hpp:527
constexpr nsize_t size() const noexcept
Definition octets.hpp:526
constexpr const TOctets & parent() const noexcept
Definition octets.hpp:528
uint16_t get_uint16(const nsize_t i) const
Definition octets.hpp:537
constexpr uint16_t get_uint16_nc(const nsize_t i) const noexcept
Definition octets.hpp:540
constexpr uint8_t const * get_ptr_nc(const nsize_t i) const noexcept
Definition octets.hpp:547
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_.
Definition octets.hpp:515
uint8_t const * get_ptr(const nsize_t i) const
Definition octets.hpp:544
constexpr uint8_t get_uint8_nc(const nsize_t i) const noexcept
Definition octets.hpp:533
Transient endian aware octet data, i.e.
Definition octets.hpp:307
constexpr void put_uint64_nc(const nsize_t i, const uint64_t &v) noexcept
Definition octets.hpp:372
void put_uint128(const nsize_t i, const uint128dp_t &v)
Definition octets.hpp:376
void put_octets_nc(const nsize_t i, const TROOctets &v, const nsize_t v_off, const nsize_t v_len) noexcept
Definition octets.hpp:412
void memset(const nsize_t i, const uint8_t c, const nsize_t byte_count)
Definition octets.hpp:433
void put_uint64(const nsize_t i, const uint64_t &v)
Definition octets.hpp:368
TOctets(TOctets &&o) noexcept=default
void put_eui48(const nsize_t i, const jau::io::net::EUI48 &v)
Definition octets.hpp:360
TOctets(uint8_t *source, const nsize_t len, const lb_endian_t byte_order) noexcept
Transient passthrough r/w memory, w/o ownership .
Definition octets.hpp:318
void memmove_nc(const nsize_t i, const uint8_t *source, const nsize_t byte_count) noexcept
Definition octets.hpp:429
void put_eui48_nc(const nsize_t i, const jau::io::net::EUI48 &v) noexcept
Definition octets.hpp:364
std::string toString() const noexcept
Definition octets.hpp:489
TOctets & operator=(const TOctets &o) noexcept=default
constexpr void put_uint32_nc(const nsize_t i, const uint32_t v) noexcept
Definition octets.hpp:356
void put_octets_nc(const nsize_t i, const TROOctets &v) noexcept
Definition octets.hpp:404
void put_uint192(const nsize_t i, const uint192dp_t &v)
Definition octets.hpp:384
void bzero_nc(const nsize_t i, const nsize_t byte_count) noexcept
Definition octets.hpp:444
void put_bytes_nc(const nsize_t i, const uint8_t *source, const nsize_t byte_count) noexcept
Definition octets.hpp:421
void put_string(const nsize_t i, const std::string &v, const nsize_t max_len, const bool includeEOS)
Definition octets.hpp:453
void put_octets(const nsize_t i, const TROOctets &v)
Definition octets.hpp:400
void bzero(const nsize_t i, const nsize_t byte_count)
Definition octets.hpp:440
TOctets(const TOctets &o) noexcept=default
void put_uint256(const nsize_t i, const uint256dp_t &v)
Definition octets.hpp:392
void bzero() noexcept
Definition octets.hpp:447
constexpr void put_uint256_nc(const nsize_t i, const uint256dp_t &v) noexcept
Definition octets.hpp:396
TOctets & operator=(TOctets &&o) noexcept=default
constexpr void put_uint128_nc(const nsize_t i, const uint128dp_t &v) noexcept
Definition octets.hpp:380
constexpr void put_uint16_nc(const nsize_t i, const uint16_t v) noexcept
Definition octets.hpp:348
~TOctets() noexcept override=default
constexpr void put_uint8_nc(const nsize_t i, const uint8_t v) noexcept
Definition octets.hpp:340
uint8_t * get_wptr() noexcept
Definition octets.hpp:479
void memset_nc(const nsize_t i, const uint8_t c, const nsize_t byte_count) noexcept
Definition octets.hpp:437
void put_uint32(const nsize_t i, const uint32_t v)
Definition octets.hpp:352
void put_octets(const nsize_t i, const TROOctets &v, const nsize_t v_off, const nsize_t v_len)
Definition octets.hpp:407
void put_string_nc(const nsize_t i, const std::string &v, const nsize_t max_len, const bool includeEOS) noexcept
Definition octets.hpp:462
constexpr void put_int8_nc(const nsize_t i, const int8_t v) noexcept
Definition octets.hpp:332
void memmove(const nsize_t i, const uint8_t *source, const nsize_t byte_count)
Definition octets.hpp:425
uint8_t * get_wptr(const nsize_t i)
Definition octets.hpp:481
void put_uint16(const nsize_t i, const uint16_t v)
Definition octets.hpp:344
void put_uuid(const nsize_t i, const uuid_t &v)
Definition octets.hpp:471
constexpr void put_uint192_nc(const nsize_t i, const uint192dp_t &v) noexcept
Definition octets.hpp:388
void put_bytes(const nsize_t i, const uint8_t *source, const nsize_t byte_count)
Definition octets.hpp:417
void put_uuid_nc(const nsize_t i, const uuid_t &v) noexcept
Definition octets.hpp:475
uint8_t * get_wptr_nc(const nsize_t i) noexcept
Definition octets.hpp:485
void put_int8(const nsize_t i, const int8_t v)
Definition octets.hpp:328
void put_uint8(const nsize_t i, const uint8_t v)
Definition octets.hpp:336
Transient read only and endian aware octet data, i.e.
Definition octets.hpp:65
uint32_t get_uint32(const nsize_t i) const
Definition octets.hpp:186
constexpr uint16_t get_uint16_nc(const nsize_t i) const noexcept
Definition octets.hpp:182
jau::io::net::EUI48 get_eui48(const nsize_t i) const
Definition octets.hpp:194
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
Definition octets.hpp:265
uuid128_t get_uuid128_nc(const nsize_t i) const noexcept
Definition octets.hpp:261
constexpr uint128dp_t get_uint128_nc(const nsize_t i) const noexcept
Definition octets.hpp:214
std::string toString() const noexcept
Definition octets.hpp:286
jau::io::net::EUI48 get_eui48_nc(const nsize_t i) const noexcept
Definition octets.hpp:198
constexpr lb_endian_t byte_order() const noexcept
Returns byte order of this octet store.
Definition octets.hpp:157
uint128dp_t get_uint128(const nsize_t i) const
Definition octets.hpp:210
std::string get_string(const nsize_t i) const
Assumes a null terminated string.
Definition octets.hpp:235
TROOctets(TROOctets &&o) noexcept=default
TROOctets() noexcept
Default constructor with nullptr memory, zero size and lb_endian::native byte order.
Definition octets.hpp:134
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 .
Definition octets.hpp:121
constexpr nsize_t size() const noexcept
Returns the used memory size for read and write operations, may be zero.
Definition octets.hpp:160
constexpr bool is_range_valid(const nsize_t i, const nsize_t count) const noexcept
Definition octets.hpp:152
bool operator==(const TROOctets &rhs) const noexcept
Definition octets.hpp:279
virtual ~TROOctets() noexcept=default
constexpr int8_t get_int8_nc(const nsize_t i) const noexcept
Definition octets.hpp:174
bool operator!=(const TROOctets &rhs) const noexcept
Definition octets.hpp:282
uuid16_t get_uuid16(const nsize_t i) const
Definition octets.hpp:250
uuid16_t get_uuid16_nc(const nsize_t i) const noexcept
Definition octets.hpp:253
static void checkPtr(uint8_t *data_, nsize_t size_) noexcept
Validates the given data_ and size_.
Definition octets.hpp:83
TROOctets(const TROOctets &o) noexcept=default
uint8_t const * get_ptr(const nsize_t i) const
Definition octets.hpp:271
uint16_t get_uint16(const nsize_t i) const
Definition octets.hpp:178
std::string get_string(const nsize_t i, const nsize_t length) const
Assumes a string with defined length, not necessarily null terminated.
Definition octets.hpp:245
uint8_t get_uint8(const nsize_t i) const
Definition octets.hpp:162
std::string get_string_nc(const nsize_t i) const noexcept
Assumes a null terminated string.
Definition octets.hpp:240
void check_range(const nsize_t i, const nsize_t count, const char *file, int line) const
Definition octets.hpp:146
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.
Definition octets.hpp:101
constexpr uint192dp_t get_uint192_nc(const nsize_t i) const noexcept
Definition octets.hpp:222
uint64_t get_uint64(const nsize_t i) const
Definition octets.hpp:202
constexpr uint256dp_t get_uint256_nc(const nsize_t i) const noexcept
Definition octets.hpp:230
int8_t get_int8(const nsize_t i) const
Definition octets.hpp:170
constexpr uint8_t * data() noexcept
Definition octets.hpp:90
uint256dp_t get_uint256(const nsize_t i) const
Definition octets.hpp:226
uint192dp_t get_uint192(const nsize_t i) const
Definition octets.hpp:218
constexpr uint32_t get_uint32_nc(const nsize_t i) const noexcept
Definition octets.hpp:190
constexpr uint64_t get_uint64_nc(const nsize_t i) const noexcept
Definition octets.hpp:206
constexpr uint8_t get_uint8_nc(const nsize_t i) const noexcept
Definition octets.hpp:166
constexpr uint8_t const * get_ptr() const noexcept
Definition octets.hpp:270
constexpr void setSize(nsize_t s) noexcept
Definition octets.hpp:109
uuid128_t get_uuid128(const nsize_t i) const
Definition octets.hpp:257
constexpr uint8_t const * get_ptr_nc(const nsize_t i) const noexcept
Definition octets.hpp:275
static constexpr jau::nsize_t number(const TypeSize rhs) noexcept
Definition uuid.hpp:64
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.
Definition uuid.hpp:61
jau::nsize_t getTypeSizeInt() const noexcept
Definition uuid.hpp:127
static std::unique_ptr< uuid_t > create(TypeSize const t, uint8_t const *const buffer, lb_endian_t const le_or_be)
Definition uuid.cpp:56
#define jau_ABORT(...)
Use for unconditional ::abort() call with given messages, prefix '[elapsed_time] ABORT @ file:line fu...
Definition debug.hpp:147
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...
std::string_view to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
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.
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
@ native
Identifier for native platform type, one of the above.
#define E_FILE_LINE
bool do_noexcept(UnaryPredicate p) noexcept
No throw wrap for given unary predicate p action. Returns true for success (no exception),...
uint_bytes_t< sizeof(unsigned long int)> nsize_t
Natural 'size_t' alternative using uint<XX>_t with xx = sizeof(unsigned long int)*8 as its natural si...
Definition int_types.hpp:85
constexpr T min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
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
Definition uuid.hpp:263
void zero_bytes_sec(void *s, size_t n) noexcept __attrdecl_no_optimize__
Wrapper to ::explicit_bzero(), ::bzero() or ::memset(), whichever is available in that order.
std::string string_noexcept(UnaryPredicate p) noexcept
No throw wrap for given unary predicate p producing a std::string. Returns an empty string if p cause...
std::string & appendHexString(std::string &dest, const void *data, const nsize_t length, const lb_endian_t byteOrder=lb_endian_t::big, const LoUpCase capitalization=LoUpCase::lower, const PrefixOpt prefix=PrefixOpt::prefix) noexcept
Appends a hexadecimal string representation of the given lsb-first byte values.
std::string toHexString(const void *data, const nsize_t length, const lb_endian_t byteOrder=lb_endian_t::big, const LoUpCase capitalization=LoUpCase::lower, const PrefixOpt prefix=PrefixOpt::prefix) noexcept
Produce a hexadecimal string representation of the given lsb-first byte values.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
STL namespace.
#define JAU_TRACE_OCTETS_PRINT(...)
Definition octets.hpp:46
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
Definition eui48.hpp:322
jau::nsize_t put(uint8_t *const sink, const lb_endian_t byte_order) const noexcept
Definition eui48.cpp:244
A 128-bit packed uint8_t data array.
A 196-bit packed uint8_t data array.
A 256-bit packed uint8_t data array.