aboutsummaryrefslogtreecommitdiffstats
path: root/include/jau/octets.hpp
blob: 9e75e7ff832de594af1d4342bfcdbf38ebce14e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef JAU_OCTETS_HPP_
#define JAU_OCTETS_HPP_

#include <cstring>
#include <string>
#include <memory>
#include <cstdint>
#include <algorithm>

#include <mutex>
#include <atomic>

#include <jau/basic_types.hpp>
#include <jau/uuid.hpp>
#include <jau/eui48.hpp>

#include <jau/debug.hpp>

// #define JAU_TRACE_OCTETS 1
#ifdef JAU_TRACE_OCTETS
    #define JAU_TRACE_OCTETS_PRINT(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); fflush(stderr); }
#else
    #define JAU_TRACE_OCTETS_PRINT(...)
#endif

namespace jau {

    /** \addtogroup ByteUtils
     *
     *  @{
     */

    /**
     * Transient read only and endian aware octet data, i.e. non persistent passthrough, owned by caller.
     *
     * Endian byte order is passed at construction.
     *
     * Constructor and assignment operations are `noexcept`. In case invalid arguments are passed, abort() is being called.
     * This is a design choice based on reusing already existing underlying resources.
     */
    class TROOctets
    {
        private:
            /** Used memory size <= capacity, maybe zero. */
            nsize_t _size;
            /** Memory pointer, might be nullptr. Actual capacity known by owner, e.g. POctets. */
            uint8_t * _data;
            /** byte-order flag */
            bool _little_endian;

        protected:
            /**
             * Validates the given data_ and size_.
             *
             * Aborts if data_ is nullptr and size_ > 0.
             *
             * @param data_ a memory pointer
             * @param size_ claimed memory size
             */
            static inline void checkPtr(uint8_t *data_, nsize_t size_) noexcept {
                if( nullptr == data_ && 0 < size_ ) {
                    ABORT("TROOctets: nullptr with size %s > 0", std::to_string(size_).c_str());
                    abort(); // never reached
                }
            }

            static inline bool is_little_endian(const endian v) noexcept {
                switch(v) {
                    case endian::little: return true;
                    case endian::big: return false;
                    default: {
                        ABORT("TROOctets: endian choice must be little or big, given: %s", jau::to_string(v).c_str());
                        abort(); // never reached
                    }
                }
            }

            constexpr bool little_endian() const noexcept { return _little_endian; }

            constexpr uint8_t * data() noexcept { return _data; }

            /**
             * Internally sets the _size and _data fields after validation.
             *
             * Aborts if data_ is nullptr and size_ > 0
             * or byte_order not endian::little nor endian::big, see abort().
             *
             * @param data_ a memory pointer
             * @param size_ used memory size
             * @param byte_order endian::little or endian::big byte order, one may pass endian::native.
             */
            inline void setData(uint8_t *data_, nsize_t size_, const endian byte_order) noexcept {
                JAU_TRACE_OCTETS_PRINT("POctets setData: %zu bytes @ %p -> %zu bytes @ %p",
                        _size, _data, size_, data_);
                checkPtr(data_, size_);
                _size = size_;
                _data = data_;
                _little_endian = is_little_endian(byte_order);
            }
            constexpr void setSize(nsize_t s) noexcept { _size = s; }

        public:
            /**
             * Transient passthrough read-only memory, w/o ownership ..
             *
             * Aborts if source is nullptr and len > 0
             * or byte_order not endian::little nor endian::big, see abort().
             *
             * @param source a non nullptr memory, otherwise throws exception. Actual capacity known by owner.
             * @param len readable size of the memory, may be zero
             * @param byte_order endian::little or endian::big byte order, one may pass endian::native.
             */
            TROOctets(const uint8_t *source, const nsize_t len, const endian byte_order) noexcept
            : _size( len ), _data( const_cast<uint8_t *>(source) ),
              _little_endian( is_little_endian(byte_order) )
            {
                checkPtr(_data, _size);
            }

            /**
             * Default constructor with nullptr memory, zero size and endian::native byte order.
             *
             * Conveniently exists to allow instantiation of variables
             * intended for later assignment.
             */
            TROOctets() noexcept
            : _size( 0 ), _data( nullptr ),
              _little_endian( is_little_endian(endian::native) )
            { }

            TROOctets(const TROOctets &o) noexcept = default;
            TROOctets(TROOctets &&o) noexcept = default;
            TROOctets& operator=(const TROOctets &o) noexcept = default;
            TROOctets& operator=(TROOctets &&o) noexcept = default;

            virtual ~TROOctets() noexcept = default;

            inline void check_range(const nsize_t i, const nsize_t count, const char *file, int line) const {
                if( i+count > _size ) {
                    throw IndexOutOfBoundsException(i, count, _size, file, line);
                }
            }

            constexpr bool is_range_valid(const nsize_t i, const nsize_t count) const noexcept {
                return i+count <= _size;
            }

            /** Returns byte order of this octet store. */
            constexpr endian byte_order() const noexcept { return _little_endian ? endian::little : endian::big; }

            /** Returns the used memory size for read and write operations, may be zero. */
            constexpr nsize_t size() const noexcept { return _size; }

            uint8_t get_uint8(const nsize_t i) const {
                check_range(i, 1, E_FILE_LINE);
                return _data[i];
            }
            constexpr uint8_t get_uint8_nc(const nsize_t i) const noexcept {
                return _data[i];
            }

            int8_t get_int8(const nsize_t i) const {
                check_range(i, 1, E_FILE_LINE);
                return jau::get_int8(_data, i);
            }
            constexpr int8_t get_int8_nc(const nsize_t i) const noexcept {
                return jau::get_int8(_data, i);
            }

            uint16_t get_uint16(const nsize_t i) const {
                check_range(i, 2, E_FILE_LINE);
                return jau::get_uint16(_data, i, _little_endian);
            }
            constexpr uint16_t get_uint16_nc(const nsize_t i) const noexcept {
                return jau::get_uint16(_data, i, _little_endian);
            }

            uint32_t get_uint32(const nsize_t i) const {
                check_range(i, 4, E_FILE_LINE);
                return jau::get_uint32(_data, i, _little_endian);
            }
            constexpr uint32_t get_uint32_nc(const nsize_t i) const noexcept {
                return jau::get_uint32(_data, i, _little_endian);
            }

            EUI48 get_eui48(const nsize_t i) const {
                check_range(i, sizeof(EUI48), E_FILE_LINE);
                return EUI48(_data+i, byte_order());
            }
            inline EUI48 get_eui48_nc(const nsize_t i) const noexcept {
                return EUI48(_data+i, byte_order());
            }

            uint64_t get_uint64(const nsize_t i) const {
                check_range(i, 8, E_FILE_LINE);
                return jau::get_uint64(_data, i, _little_endian);
            }
            constexpr uint64_t get_uint64_nc(const nsize_t i) const noexcept {
                return jau::get_uint64(_data, i, _little_endian);
            }

            uint128_t get_uint128(const nsize_t i) const {
                check_range(i, 8, E_FILE_LINE);
                return jau::get_uint128(_data, i, _little_endian);
            }
            constexpr uint128_t get_uint128_nc(const nsize_t i) const noexcept {
                return jau::get_uint128(_data, i, _little_endian);
            }

            uint192_t get_uint192(const nsize_t i) const {
                check_range(i, 8, E_FILE_LINE);
                return jau::get_uint192(_data, i, _little_endian);
            }
            constexpr uint192_t get_uint192_nc(const nsize_t i) const noexcept {
                return jau::get_uint192(_data, i, _little_endian);
            }

            uint256_t get_uint256(const nsize_t i) const {
                check_range(i, 8, E_FILE_LINE);
                return jau::get_uint256(_data, i, _little_endian);
            }
            constexpr uint256_t get_uint256_nc(const nsize_t i) const noexcept {
                return jau::get_uint256(_data, i, _little_endian);
            }

            /** Assumes a null terminated string */
            std::string get_string(const nsize_t i) const {
                check_range(i, 1, E_FILE_LINE); // minimum size
                return std::string( (const char*)(_data+i) );
            }
            /** Assumes a null terminated string */
            inline std::string get_string_nc(const nsize_t i) const noexcept {
                return std::string( (const char*)(_data+i) );
            }

            /** Assumes a string with defined length, not necessarily null terminated */
            std::string get_string(const nsize_t i, const nsize_t length) const {
                check_range(i, length, E_FILE_LINE);
                return std::string( (const char*)(_data+i), length );
            }

            uuid16_t get_uuid16(const nsize_t i) const {
                return uuid16_t(get_uint16(i));
            }
            inline uuid16_t get_uuid16_nc(const nsize_t i) const noexcept {
                return uuid16_t(get_uint16_nc(i));
            }

            uuid128_t get_uuid128(const nsize_t i) const {
                check_range(i, uuid_t::number(uuid_t::TypeSize::UUID128_SZ), E_FILE_LINE);
                return jau::get_uuid128(_data, i, _little_endian);
            }
            inline uuid128_t get_uuid128_nc(const nsize_t i) const noexcept {
                return jau::get_uuid128(_data, i, _little_endian);
            }

            std::unique_ptr<const uuid_t> get_uuid(const nsize_t i, const uuid_t::TypeSize tsize) const {
                check_range(i, uuid_t::number(tsize), E_FILE_LINE);
                return uuid_t::create(tsize, _data, i, _little_endian);
            }

            constexpr uint8_t const * get_ptr() const noexcept { return _data; }
            uint8_t const * get_ptr(const nsize_t i) const {
                check_range(i, 1, E_FILE_LINE);
                return _data + i;
            }
            constexpr uint8_t const * get_ptr_nc(const nsize_t i) const noexcept {
                return _data + i;
            }

            bool operator==(const TROOctets& rhs) const noexcept {
                return _size == rhs._size && 0 == std::memcmp(_data, rhs._data, _size);
            }
            bool operator!=(const TROOctets& rhs) const noexcept {
                return !(*this == rhs);
            }

            std::string toString() const noexcept {
                return "size "+std::to_string(_size)+", ro: "+bytesHexString(_data, 0, _size, true /* lsbFirst */);
            }
    };

    /**
     * Transient endian aware octet data, i.e. non persistent passthrough, owned by caller.
     *
     * Endian byte order is passed at construction.
     *
     * Constructor and assignment operations are `noexcept`. In case invalid arguments are passed, abort() is being called.
     * This is a design choice based on reusing already existing underlying resources.
     */
    class TOctets : public TROOctets
    {
        public:
            /**
             * Transient passthrough r/w memory, w/o ownership ..
             *
             * Aborts if source is nullptr and len > 0
             * or byte_order not endian::little nor endian::big, see abort().
             *
             * @param source transient data source
             * @param len length of transient data source
             * @param byte_order endian::little or endian::big byte order, one may pass endian::native.
             */
            TOctets(uint8_t *source, const nsize_t len, const endian byte_order) noexcept
            : TROOctets(source, len, byte_order) {}

            TOctets(const TOctets &o) noexcept = default;
            TOctets(TOctets &&o) noexcept = default;
            TOctets& operator=(const TOctets &o) noexcept = default;
            TOctets& operator=(TOctets &&o) noexcept = default;

            ~TOctets() noexcept override = default;

            void put_int8(const nsize_t i, const int8_t v) {
                check_range(i, 1, E_FILE_LINE);
                data()[i] = static_cast<uint8_t>(v);
            }
            constexpr void put_int8_nc(const nsize_t i, const int8_t v) noexcept {
                data()[i] = static_cast<uint8_t>(v);
            }

            void put_uint8(const nsize_t i, const uint8_t v) {
                check_range(i, 1, E_FILE_LINE);
                data()[i] = v;
            }
            constexpr void put_uint8_nc(const nsize_t i, const uint8_t v) noexcept {
                data()[i] = v;
            }

            void put_uint16(const nsize_t i, const uint16_t v) {
                check_range(i, 2, E_FILE_LINE);
                jau::put_uint16(data(), i, v, little_endian());
            }
            constexpr void put_uint16_nc(const nsize_t i, const uint16_t v) noexcept {
                jau::put_uint16(data(), i, v, little_endian());
            }

            void put_uint32(const nsize_t i, const uint32_t v) {
                check_range(i, 4, E_FILE_LINE);
                jau::put_uint32(data(), i, v, little_endian());
            }
            constexpr void put_uint32_nc(const nsize_t i, const uint32_t v) noexcept {
                jau::put_uint32(data(), i, v, little_endian());
            }

            void put_eui48(const nsize_t i, const EUI48 & v) {
                check_range(i, sizeof(v.b), E_FILE_LINE);
                v.put(data(), i, byte_order());
            }
            inline void put_eui48_nc(const nsize_t i, const EUI48 & v) noexcept {
                v.put(data(), i, byte_order());
            }

            void put_uint64(const nsize_t i, const uint64_t & v) {
                check_range(i, 8, E_FILE_LINE);
                jau::put_uint64(data(), i, v, little_endian());
            }
            constexpr void put_uint64_nc(const nsize_t i, const uint64_t & v) noexcept {
                jau::put_uint64(data(), i, v, little_endian());
            }

            void put_uint128(const nsize_t i, const uint128_t & v) {
                check_range(i, 8, E_FILE_LINE);
                jau::put_uint128(data(), i, v, little_endian());
            }
            constexpr void put_uint128_nc(const nsize_t i, const uint128_t & v) noexcept {
                jau::put_uint128(data(), i, v, little_endian());
            }

            void put_uint192(const nsize_t i, const uint192_t & v) {
                check_range(i, 8, E_FILE_LINE);
                jau::put_uint192(data(), i, v, little_endian());
            }
            constexpr void put_uint192_nc(const nsize_t i, const uint192_t & v) noexcept {
                jau::put_uint192(data(), i, v, little_endian());
            }

            void put_uint256(const nsize_t i, const uint256_t & v) {
                check_range(i, 8, E_FILE_LINE);
                jau::put_uint256(data(), i, v, little_endian());
            }
            constexpr void put_uint256_nc(const nsize_t i, const uint256_t & v) noexcept {
                jau::put_uint256(data(), i, v, little_endian());
            }

            void put_octets(const nsize_t i, const TROOctets & v) {
                check_range(i, v.size(), E_FILE_LINE);
                std::memcpy(data() + i, v.get_ptr(), v.size());
            }
            void put_octets_nc(const nsize_t i, const TROOctets & v) noexcept {
                std::memcpy(data() + i, v.get_ptr(), v.size());
            }

            void put_bytes(const nsize_t i, const uint8_t *source, const nsize_t byte_count) {
                check_range(i, byte_count, E_FILE_LINE);
                std::memcpy(data() + i, source, byte_count);
            }
            void put_bytes_nc(const nsize_t i, const uint8_t *source, const nsize_t byte_count) noexcept {
                std::memcpy(data() + i, source, byte_count);
            }

            void memmove(const nsize_t i, const uint8_t *source, const nsize_t byte_count) {
                check_range(i, byte_count, E_FILE_LINE);
                std::memmove(data() + i, source, byte_count);
            }
            void memmove_nc(const nsize_t i, const uint8_t *source, const nsize_t byte_count) noexcept {
                std::memmove(data() + i, source, byte_count);
            }

            void memset(const nsize_t i, const uint8_t c, const nsize_t byte_count) {
                check_range(i, byte_count, E_FILE_LINE);
                std::memset(data() + i, c, byte_count);
            }
            void memset_nc(const nsize_t i, const uint8_t c, const nsize_t byte_count) noexcept {
                std::memset(data() + i, c, byte_count);
            }
            void bzero(const nsize_t i, const nsize_t byte_count) {
                check_range(i, byte_count, E_FILE_LINE);
                ::bzero(data() + i, byte_count);
            }
            void bzero_nc(const nsize_t i, const nsize_t byte_count) noexcept {
                ::bzero(data() + i, byte_count);
            }
            void bzero() noexcept {
                if( size() > 0 ) {
                    ::bzero(data(), size());
                }
            }

            void put_string(const nsize_t i, const std::string & v, const nsize_t max_len, const bool includeEOS) {
                const nsize_t size1 = v.size() + ( includeEOS ? 1 : 0 );
                const nsize_t size = std::min(size1, max_len);
                check_range(i, size, E_FILE_LINE);
                std::memcpy(data() + i, v.c_str(), size);
                if( size < size1 && includeEOS ) {
                    *(data() + i + size - 1) = 0; // ensure EOS
                }
            }
            void put_string_nc(const nsize_t i, const std::string & v, const nsize_t max_len, const bool includeEOS) noexcept {
                const nsize_t size1 = v.size() + ( includeEOS ? 1 : 0 );
                const nsize_t size = std::min(size1, max_len);
                std::memcpy(data() + i, v.c_str(), size);
                if( size < size1 && includeEOS ) {
                    *(data() + i + size - 1) = 0; // ensure EOS
                }
            }

            void put_uuid(const nsize_t i, const uuid_t & v) {
                check_range(i, v.getTypeSizeInt(), E_FILE_LINE);
                v.put(data(), i, little_endian());
            }
            void put_uuid_nc(const nsize_t i, const uuid_t & v) noexcept {
                v.put(data(), i, little_endian());
            }

            inline uint8_t * get_wptr() noexcept { return data(); }

            uint8_t * get_wptr(const nsize_t i) {
                check_range(i, 1, E_FILE_LINE);
                return data() + i;
            }
            inline uint8_t * get_wptr_nc(const nsize_t i) noexcept {
                return data() + i;
            }

            std::string toString() const noexcept {
                return "size "+std::to_string(size())+", rw: "+bytesHexString(get_ptr(), 0, size(), true /* lsbFirst */);
            }
    };

    /**
     * Transient endian aware octet data slice, i.e. a view of an TOctet.
     *
     * Endian byte order is defined by its parent TOctet.
     */
    class TOctetSlice
    {
        private:
            const TOctets & _parent;
            nsize_t const _offset;
            nsize_t const _size;

        public:
            /**
             * Creates a view of a given TOctet with the specified offset_ and size_.
             *
             * @param buffer_ the parent TOctet buffer
             * @param offset_ offset to the parent TOctet buffer
             * @param size_ size of this view, starting at offset_
             * @throws IndexOutOfBoundsException if offset_ + size_ > parent buffer_.size()
             */
            TOctetSlice(const TOctets &buffer_, const nsize_t offset_, const nsize_t size_)
            : _parent(buffer_), _offset(offset_), _size(size_)
            {
                if( offset_+_size > buffer_.size() ) {
                    throw IndexOutOfBoundsException(offset_, _size, buffer_.size(), E_FILE_LINE);
                }
            }

            /** Returns byte order of this octet store. */
            constexpr endian byte_order() const noexcept { return _parent.byte_order(); }

            constexpr nsize_t size() const noexcept { return _size; }
            constexpr nsize_t offset() const noexcept { return _offset; }
            constexpr const TOctets& parent() const noexcept { return _parent; }

            uint8_t get_uint8(const nsize_t i) const {
                return _parent.get_uint8(_offset+i);
            }
            constexpr uint8_t get_uint8_nc(const nsize_t i) const noexcept {
                return _parent.get_uint8_nc(_offset+i);
            }

            uint16_t get_uint16(const nsize_t i) const {
                return _parent.get_uint16(_offset+i);
            }
            constexpr uint16_t get_uint16_nc(const nsize_t i) const noexcept {
                return _parent.get_uint16_nc(_offset+i);
            }

            uint8_t const * get_ptr(const nsize_t i) const {
                return _parent.get_ptr(_offset+i);
            }
            constexpr uint8_t const * get_ptr_nc(const nsize_t i) const noexcept {
                return _parent.get_ptr_nc(_offset+i);
            }

            std::string toString() const noexcept {
                return "offset "+std::to_string(_offset)+", size "+std::to_string(_size)+": "+bytesHexString(_parent.get_ptr(), _offset, _size, true /* lsbFirst */);
            }
    };

    /**
     * Persistent endian aware octet data, i.e. owned memory allocation.
     *
     * Endian byte order is passed at construction.
     *
     * Constructor and assignment operations are **not** completely `noexcept` and may throw exceptions.
     * This is a design choice based on dynamic resource allocation performed by this class.
     */
    class POctets : public TOctets
    {
        private:
            nsize_t _capacity;

            void freeData() {
                uint8_t * ptr = data();
                if( nullptr != ptr ) {
                    JAU_TRACE_OCTETS_PRINT("POctets release: %p", ptr);
                    free(ptr);
                } // else: zero sized POctets w/ nullptr are supported
            }

            /**
             * Allocate a memory chunk.
             * @param size
             * @return
             * @throws OutOfMemoryError if running out of memory
             */
            static uint8_t * allocData(const nsize_t size) {
                if( size <= 0 ) {
                    return nullptr;
                }
                uint8_t * m = static_cast<uint8_t*>( std::malloc(size) );
                if( nullptr == m ) {
                    throw OutOfMemoryError("allocData size "+std::to_string(size)+" -> nullptr", E_FILE_LINE);
                }
                return m;
            }

        public:
            /** Returns the memory capacity, never zero, greater or equal {@link #getSize()}. */
            constexpr nsize_t capacity() const noexcept { return _capacity; }

            /**
             * Zero sized POctets instance.
             *
             * Aborts if byte_order not endian::little nor endian::big, see abort().
             *
             * Will not throw an OutOfMemoryError exception due to no allocation.
             *
             * @param byte_order endian::little or endian::big byte order, one may pass endian::native.
             */
            POctets(const endian byte_order) noexcept
            : TOctets(nullptr, 0, byte_order), _capacity(0)
            {
                JAU_TRACE_OCTETS_PRINT("POctets ctor0: zero-sized");
            }

            /**
             * Takes ownership (malloc(size) and copy, free) ..
             *
             * Aborts if byte_order not endian::little nor endian::big, see abort().
             *
             * Capacity and size will be of given source size.
             *
             * @param source_ source data to be copied into this new instance
             * @param size_ length of source data
             * @param byte_order endian::little or endian::big byte order, one may pass endian::native.
             * @throws IllegalArgumentException if source_ is nullptr and size_ > 0
             * @throws OutOfMemoryError if allocation fails
             */
            POctets(const uint8_t *source_, const nsize_t size_, const endian byte_order)
            : TOctets( allocData(size_), size_, byte_order),
              _capacity( size_ )
            {
                if( 0 < size_ ) {
                    if( nullptr == source_ ) {
                        throw IllegalArgumentException("source nullptr with size "+std::to_string(size_)+" > 0", E_FILE_LINE);
                    }
                    std::memcpy(data(), source_, size_);
                }
                JAU_TRACE_OCTETS_PRINT("POctets ctor1: %p", data());
            }

            /**
             * New buffer (malloc(capacity), free)
             *
             * Aborts if byte_order not endian::little nor endian::big, see abort().
             *
             * @param capacity_ new capacity
             * @param size_ new size with size <= capacity
             * @param byte_order endian::little or endian::big byte order, one may pass endian::native.
             * @throws IllegalArgumentException if capacity_ < size_
             * @throws OutOfMemoryError if allocation fails
             */
            POctets(const nsize_t capacity_, const nsize_t size_, const endian byte_order)
            : TOctets( allocData( capacity_ ), size_, byte_order ),
              _capacity( capacity_ )
            {
                if( capacity() < size() ) {
                    throw IllegalArgumentException("capacity "+std::to_string(capacity())+" < size "+std::to_string(size()), E_FILE_LINE);
                }
                JAU_TRACE_OCTETS_PRINT("POctets ctor2: %p", data());
            }

            /**
             * New buffer (malloc, free)
             *
             * Aborts if byte_order not endian::little nor endian::big, see abort().
             *
             * @param size new size and capacity
             * @param byte_order endian::little or endian::big byte order, one may pass endian::native.
             * @throws OutOfMemoryError if allocation fails
             */
            POctets(const nsize_t size, const endian byte_order)
            : POctets(size, size, byte_order)
            {
                JAU_TRACE_OCTETS_PRINT("POctets ctor3: %p", data());
            }

            /**
             * Copy constructor
             *
             * Capacity of this new instance will be of source.size() only.
             *
             * @param source POctet source to be copied
             * @throws OutOfMemoryError if allocation fails
             */
            POctets(const POctets &source)
            : TOctets( allocData(source.size()), source.size(), source.byte_order() ),
              _capacity( source.size() )
            {
                std::memcpy(data(), source.get_ptr(), source.size());
                JAU_TRACE_OCTETS_PRINT("POctets ctor-cpy0: %p -> %p", source.get_ptr(), data());
            }

            /**
             * Copy constructor (explicit), allowing to set a higher capacity
             * than source.size() in contrast to the copy-constructor.
             *
             * @param source POctet source to be copied
             * @param capacity_ used to determine new capacity `std::max(capacity_, source.size())`
             * @throws OutOfMemoryError if allocation fails
             */
            explicit POctets(const POctets &source, const nsize_t capacity_)
            : TOctets( allocData(source.size()), source.size(), source.byte_order() ),
              _capacity( std::max(capacity_, source.size()) )
            {
                std::memcpy(data(), source.get_ptr(), source.size());
                JAU_TRACE_OCTETS_PRINT("POctets ctor-cpy-extra1: %p -> %p", source.get_ptr(), data());
            }

            /**
             * Move constructor
             * @param o POctet source to be taken over
             */
            POctets(POctets &&o) noexcept
            : TOctets( o.data(), o.size(), o.byte_order() ),
              _capacity( o.capacity() )
            {
                // moved origin data references
                // purge origin
                o.setData(nullptr, 0, o.byte_order());
                o._capacity = 0;
                JAU_TRACE_OCTETS_PRINT("POctets ctor-move0: %p", data());
            }

            /**
             * Assignment operator
             * @param _source POctet source to be copied
             * @return
             * @throws OutOfMemoryError if allocation fails
             */
            POctets& operator=(const POctets &_source) {
                if( this == &_source ) {
                    return *this;
                }
                freeData();
                setData(allocData(_source.size()), _source.size(), _source.byte_order());
                _capacity = _source.size();
                std::memcpy(data(), _source.get_ptr(), _source.size());
                JAU_TRACE_OCTETS_PRINT("POctets assign0: %p", data());
                return *this;
            }

            /**
             * Move assignment operator
             * @param o POctet source to be taken over
             * @return
             */
            POctets& operator=(POctets &&o) noexcept {
                // move origin data references
                setData(o.data(), o.size(), o.byte_order());
                _capacity = o._capacity;
                // purge origin
                o.setData(nullptr, 0, o.byte_order());
                o._capacity = 0;
                JAU_TRACE_OCTETS_PRINT("POctets assign-move0: %p", data());
                return *this;
            }

            ~POctets() noexcept override {
                freeData();
                setData(nullptr, 0, byte_order());
                _capacity=0;
            }

            /**
             * Makes a persistent POctets by copying the data from TROOctets.
             * @param _source TROOctets to be copied
             * @throws OutOfMemoryError if allocation fails
             */
            POctets(const TROOctets & _source)
            : TOctets( allocData(_source.size()), _source.size(), _source.byte_order() ),
              _capacity( _source.size() )
            {
                std::memcpy(data(), _source.get_ptr(), _source.size());
                JAU_TRACE_OCTETS_PRINT("POctets ctor-cpy1: %p", data());
            }

            /**
             * Assignment operator for TROOctets
             * @param _source TROOctets to be copied
             * @return
             * @throws OutOfMemoryError if allocation fails
             */
            POctets& operator=(const TROOctets &_source) {
                if( static_cast<TROOctets *>(this) == &_source ) {
                    return *this;
                }
                freeData();
                setData(allocData(_source.size()), _source.size(), _source.byte_order());
                _capacity = _source.size();
                std::memcpy(data(), _source.get_ptr(), _source.size());
                JAU_TRACE_OCTETS_PRINT("POctets assign1: %p", data());
                return *this;
            }

            /**
             * Makes a persistent POctets by copying the data from TOctetSlice.
             * @param _source TROOctetSlice to be copied
             * @throws OutOfMemoryError if allocation fails
             */
            POctets(const TOctetSlice & _source)
            : TOctets( allocData(_source.size()), _source.size(), _source.byte_order() ),
              _capacity( _source.size() )
            {
                std::memcpy(data(), _source.parent().get_ptr() + _source.offset(), _source.size());
                JAU_TRACE_OCTETS_PRINT("POctets ctor-cpy2: %p", data());
            }

            /**
             * Assignment operator for TOctetSlice
             * @param _source TOctetSlice to be copied
             * @return
             * @throws OutOfMemoryError if allocation fails
             */
            POctets& operator=(const TOctetSlice &_source) {
                freeData();
                setData(allocData(_source.size()), _source.size(), _source.byte_order());
                _capacity = _source.size();
                std::memcpy(data(), _source.get_ptr(0), _source.size());
                JAU_TRACE_OCTETS_PRINT("POctets assign2: %p", data());
                return *this;
            }

            /**
             * Resizes this instance, including its capacity
             * @param newCapacity new capacity, must be >= newSize
             * @param newSize new size, must be < newCapacity
             * @return
             * @throws OutOfMemoryError if allocation fails
             * @throws IllegalArgumentException if newCapacity < newSize
             */
            POctets & resize(const nsize_t newCapacity, const nsize_t newSize) {
                if( newCapacity < newSize ) {
                    throw IllegalArgumentException("newCapacity "+std::to_string(newCapacity)+" < newSize "+std::to_string(newSize), E_FILE_LINE);
                }
                if( newCapacity != _capacity ) {
                    if( newSize > size() ) {
                        recapacity(newCapacity);
                        setSize(newSize);
                    } else {
                        setSize(newSize);
                        recapacity(newCapacity);
                    }
                } else {
                    setSize(newSize);
                }
                return *this;
            }

            /**
             * Sets a new size for this instance.
             * @param newSize new size, must be <= current capacity()
             * @return
             * @throws IllegalArgumentException if newSize > current capacity()
             */
            POctets & resize(const nsize_t newSize) {
                if( _capacity < newSize ) {
                    throw IllegalArgumentException("capacity "+std::to_string(_capacity)+" < newSize "+std::to_string(newSize), E_FILE_LINE);
                }
                setSize(newSize);
                return *this;
            }

            /**
             * Changes the capacity.
             *
             * @param newCapacity new capacity, must be >= size()
             * @return
             * @throws OutOfMemoryError if allocation fails
             * @throws IllegalArgumentException if newCapacity < size()
             */
            POctets & recapacity(const nsize_t newCapacity) {
                if( newCapacity < size() ) {
                    throw IllegalArgumentException("newCapacity "+std::to_string(newCapacity)+" < size "+std::to_string(size()), E_FILE_LINE);
                }
                if( newCapacity == _capacity ) {
                    return *this;
                }
                uint8_t* data2 = allocData(newCapacity);
                if( size() > 0 ) {
                    memcpy(data2, get_ptr(), size());
                }
                JAU_TRACE_OCTETS_PRINT("POctets recapacity: %p -> %p", data(), data2);
                free(data());
                setData(data2, size(), byte_order());
                _capacity = newCapacity;
                return *this;
            }

            /**
             * Append and assign operator
             * @param b
             * @return
             * @throws OutOfMemoryError if allocation due to potential recapacity() fails
             */
            POctets & operator+=(const TROOctets &b) {
                if( 0 < b.size() ) {
                    const nsize_t newSize = size() + b.size();
                    if( _capacity < newSize ) {
                        recapacity( newSize );
                    }
                    memcpy(data()+size(), b.get_ptr(), b.size());
                    setSize(newSize);
                }
                return *this;
            }
            /**
             * Append and assign operator
             * @param b
             * @return
             * @throws OutOfMemoryError if allocation due to potential recapacity() fails
             */
            POctets & operator+=(const TOctetSlice &b) {
                if( 0 < b.size() ) {
                    const nsize_t newSize = size() + b.size();
                    if( _capacity < newSize ) {
                        recapacity( newSize );
                    }
                    memcpy(data()+size(), b.parent().get_ptr()+b.offset(), b.size());
                    setSize(newSize);
                }
                return *this;
            }

            std::string toString() const {
                return "size "+std::to_string(size())+", capacity "+std::to_string(capacity())+", "+bytesHexString(get_ptr(), 0, size(), true /* lsbFirst */);
            }
    };

    /**@}*/

} /* namespace jau */


#endif /* JAU_OCTETS_HPP_ */