-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhexi.h
More file actions
5533 lines (4571 loc) · 134 KB
/
Copy pathhexi.h
File metadata and controls
5533 lines (4571 loc) · 134 KB
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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// _ _
// | |__ _____ _(_)
// | '_ \ / _ \ \/ / | MIT & Apache 2.0 dual licensed
// | | | | __/> <| | Version 1.0
// |_| |_|\___/_/\_\_| https://github.com/EmberEmu/hexi
#pragma once
// #include <hexi/binary_stream.h>
// _ _
// | |__ _____ _(_)
// | '_ \ / _ \ \/ / | MIT & Apache 2.0 dual licensed
// | | | | __/> <| | Version 1.0
// |_| |_|\___/_/\_\_| https://github.com/EmberEmu/hexi
// #include <hexi/shared.h>
// _ _
// | |__ _____ _(_)
// | '_ \ / _ \ \/ / | MIT & Apache 2.0 dual licensed
// | | | | __/> <| | Version 1.0
// |_| |_|\___/_/\_\_| https://github.com/EmberEmu/hexi
#include <algorithm>
#include <array>
#include <bit>
#include <concepts>
#include <type_traits>
#include <cstddef>
#include <cstdint>
namespace hexi {
#if defined(_EXCEPTIONS) || defined(__cpp_exceptions) || defined(_CPPUNWIND)
#define HEXI_TRY try
#define HEXI_CATCH(exception) catch(exception)
#define HEXI_THROW(...) throw __VA_ARGS__
#define HEXI_EXCEPTION_TAG allow_throw_t
#else
#include <cstdlib>
#define HEXI_TRY if(true)
#define HEXI_CATCH(exception) if(false)
#define HEXI_THROW(...) std::abort()
#define HEXI_EXCEPTION_TAG no_throw_t
#endif
struct is_contiguous {};
struct is_non_contiguous {};
struct supported {};
struct unsupported {};
struct except_tag {};
struct allow_throw_t : except_tag {};
struct no_throw_t : except_tag {};
[[maybe_unused]] constexpr static no_throw_t no_throw {};
[[maybe_unused]] constexpr static allow_throw_t allow_throw {};
struct init_empty_t {};
constexpr static init_empty_t init_empty {};
#define STRING_ADAPTOR(adaptor_name) \
template<typename string_type> \
struct adaptor_name { \
string_type& str; \
string_type* operator->() { return &str; } \
}; \
/* deduction guide required for clang 17 support */ \
template<typename string_type> \
adaptor_name(string_type&) -> adaptor_name<string_type>; \
STRING_ADAPTOR(raw)
STRING_ADAPTOR(prefixed)
STRING_ADAPTOR(prefixed_varint)
STRING_ADAPTOR(null_terminated)
enum class buffer_seek {
sk_absolute, sk_backward, sk_forward
};
enum class stream_seek {
// Seeks within the entire underlying buffer
sk_buffer_absolute,
sk_backward,
sk_forward,
// Seeks only within the range written by the current stream
sk_stream_absolute
};
enum class stream_state {
ok,
read_limit_err,
buff_limit_err,
buff_write_err,
invalid_stream,
user_defined_err
};
namespace detail {
template<typename size_type, typename stream_type>
constexpr auto varint_decode(stream_type& stream) -> size_type {
int shift { 0 };
size_type value { 0 };
std::uint8_t byte { 0 };
do {
byte = 0; // clear in case an error occurs
stream.get(&byte, 1);
value |= (static_cast<size_type>(byte & 0x7f) << shift);
shift += 7;
} while(byte & 0x80);
return value;
}
template<typename size_type, typename stream_type>
constexpr auto varint_encode(stream_type& stream, size_type value) -> size_type {
size_type written = 0;
while(value > 0x7f) {
const std::uint8_t byte = (value & 0x7f) | 0x80;
stream.put(&byte, 1);
value >>= 7;
++written;
}
const std::uint8_t byte = value & 0x7f;
stream.put(&byte, 1);
return ++written;
}
template<decltype(auto) size>
static constexpr auto generate_filled(const std::uint8_t value) {
std::array<std::uint8_t, size> target{};
std::ranges::fill(target, value);
return target;
}
// Returns true if there's any overlap between source and destination ranges
[[maybe_unused]]
static inline bool region_overlap(const void* src, std::size_t src_len, const void* dst, std::size_t dst_len) {
const auto src_beg = std::bit_cast<std::uintptr_t>(src);
const auto src_end = src_beg + src_len;
const auto dst_beg = std::bit_cast<std::uintptr_t>(dst);
const auto dst_end = dst_beg + dst_len;
return src_beg < dst_end && dst_beg < src_end;
}
} // detail
} // hexi
// #include <hexi/concepts.h>
// _ _
// | |__ _____ _(_)
// | '_ \ / _ \ \/ / | MIT & Apache 2.0 dual licensed
// | | | | __/> <| | Version 1.0
// |_| |_|\___/_/\_\_| https://github.com/EmberEmu/hexi
// #include <hexi/stream_adaptors.h>
// _ _
// | |__ _____ _(_)
// | '_ \ / _ \ \/ / | MIT & Apache 2.0 dual licensed
// | | | | __/> <| | Version 1.0
// |_| |_|\___/_/\_\_| https://github.com/EmberEmu/hexi
#include <utility>
namespace hexi {
template<typename stream_type>
class stream_read_adaptor final {
stream_type& _stream;
public:
stream_read_adaptor(stream_type& stream)
: _stream(stream) {}
void operator&(auto&& arg) {
_stream >> arg;
}
template<typename ...Ts>
void operator()(Ts&&... args) {
(_stream >> ... >> args);
}
template<typename ...Ts>
void forward(Ts&&... args) {
_stream.get(std::forward<Ts>(args)...);
}
};
template<typename stream_type>
class stream_write_adaptor final {
stream_type& _stream;
public:
stream_write_adaptor(stream_type& stream)
: _stream(stream) {}
void operator&(auto&& arg) {
_stream << arg;
}
template<typename ...Ts>
void operator()(Ts&&... args) {
(_stream << ... << args);
}
template<typename ...Ts>
void forward(Ts&&... args) {
_stream.put(std::forward<Ts>(args)...);
}
};
} // hexi
#include <bit>
#include <concepts>
#include <ranges>
#include <type_traits>
namespace hexi {
template<typename buf_type>
concept writeable =
requires(buf_type t, void* v, typename buf_type::size_type s) {
{ t.write(v, s) } -> std::same_as<void>;
};
template<typename buf_type>
concept seekable = requires(buf_type t) {
std::is_same_v<typename buf_type::seeking, supported>;
};
template<typename buf_type>
concept contiguous = requires(buf_type t) {
std::is_same_v<typename buf_type::contiguous, is_contiguous>;
};
template<typename T>
concept arithmetic = std::integral<T> || std::floating_point<T>;
template<typename T>
concept byte_type = sizeof(T) == 1;
template<typename T>
concept byte_oriented = byte_type<typename T::value_type>;
template<typename T>
concept pod = std::is_standard_layout_v<T> && std::is_trivial_v<T>;
template<typename T>
concept has_resize_overwrite =
requires(T t) {
{ t.resize_and_overwrite(typename T::size_type(), [](char*, T::size_type) {}) } -> std::same_as<void>;
};
template<typename T>
concept has_resize =
requires(T t) {
{ t.resize(typename T::size_type() ) } -> std::same_as<void>;
};
template<typename T>
concept has_reserve =
requires(T t) {
{ t.reserve(typename T::size_type() ) } -> std::same_as<void>;
};
template<typename T>
concept has_clear =
requires(T t) {
{ t.clear() } -> std::same_as<void>;
};
template<typename T, typename U>
concept has_shl_override =
requires(T t, U& u) {
{ t.operator<<(u) } -> std::same_as<U&>;
};
template<typename T, typename U>
concept has_shr_override =
requires(T t, U& u) {
{ t.operator>>(u) } -> std::same_as<U&>;
};
template<typename T, typename U>
concept has_serialise =
requires(T t, stream_write_adaptor<U>& u) {
{ t.serialise(u) } -> std::same_as<void>;
};
template<typename T, typename U>
concept has_deserialise =
requires(T t, stream_read_adaptor<U>& u) {
{ t.serialise(u) } -> std::same_as<void>;
};
template<typename T>
concept is_iterable =
requires(T t) {
t.begin(); t.end();
};
template<typename T, typename U>
concept memcpy_read =
pod<typename T::value_type> && std::ranges::contiguous_range<T>
&& !has_shr_override<typename T::value_type, U>
&& !has_deserialise<typename T::value_type, U>;
template<typename T, typename U>
concept memcpy_write =
pod<typename T::value_type> && std::ranges::contiguous_range<T>
&& !has_shl_override<typename T::value_type, U>
&& !has_serialise<typename T::value_type, U>;
} // hexi
// #include <hexi/exception.h>
// _ _
// | |__ _____ _(_)
// | '_ \ / _ \ \/ / | MIT & Apache 2.0 dual licensed
// | | | | __/> <| | Version 1.0
// |_| |_|\___/_/\_\_| https://github.com/EmberEmu/hexi
#include <format>
#include <stdexcept>
#include <utility>
#include <cstddef>
namespace hexi {
class exception : public std::runtime_error {
public:
exception(std::string msg)
: std::runtime_error(std::move(msg)) {}
};
class buffer_underrun final : public exception {
public:
const std::size_t buff_size, read_size, total_read;
buffer_underrun(std::size_t read_size, std::size_t total_read, std::size_t buff_size)
: exception(std::format(
"Buffer underrun: {} byte read requested, buffer contains {} bytes and total bytes read was {}",
read_size, buff_size, total_read)),
buff_size(buff_size), read_size(read_size), total_read(total_read) {}
};
class buffer_overflow final : public exception {
public:
const std::size_t free, write_size, total_write;
buffer_overflow(std::size_t write_size, std::size_t total_write, std::size_t free)
: exception(std::format(
"Buffer overflow: {} byte write requested, free space is {} bytes and total bytes written was {}",
write_size, free, total_write)),
free(free), write_size(write_size), total_write(total_write) {}
};
class stream_read_limit final : public exception {
public:
const std::size_t read_limit, read_size, total_read;
stream_read_limit(std::size_t read_size, std::size_t total_read, std::size_t read_limit)
: exception(std::format(
"Read boundary exceeded: {} byte read requested, read limit was {} bytes and total bytes read was {}",
read_size, read_limit, total_read)),
read_limit(read_limit), read_size(read_size), total_read(total_read) {}
};
} // hexi
// #include <hexi/endian.h>
// _ _
// | |__ _____ _(_)
// | '_ \ / _ \ \/ / | MIT & Apache 2.0 dual licensed
// | | | | __/> <| | Version 1.0
// |_| |_|\___/_/\_\_| https://github.com/EmberEmu/hexi
// #include <hexi/concepts.h>
#include <bit>
#include <type_traits>
#include <utility>
#include <cstdint>
namespace hexi::endian {
enum class conversion {
big_to_native,
native_to_big,
little_to_native,
native_to_little
};
constexpr auto conditional_reverse(arithmetic auto value, std::endian from, std::endian to) {
using type = decltype(value);
if(from != to) {
if constexpr(std::is_same_v<type, float>) {
value = std::bit_cast<type>(std::byteswap(std::bit_cast<std::uint32_t>(value)));
} else if constexpr(std::is_same_v<type, double>) {
value = std::bit_cast<type>(std::byteswap(std::bit_cast<std::uint64_t>(value)));
} else {
value = std::byteswap(value);
}
}
return value;
}
template<std::endian from, std::endian to>
constexpr auto conditional_reverse(arithmetic auto value) {
using type = decltype(value);
if constexpr(from != to) {
if constexpr(std::is_same_v<type, float>) {
value = std::bit_cast<type>(std::byteswap(std::bit_cast<std::uint32_t>(value)));
} else if constexpr(std::is_same_v<type, double>) {
value = std::bit_cast<type>(std::byteswap(std::bit_cast<std::uint64_t>(value)));
} else {
value = std::byteswap(value);
}
}
return value;
}
constexpr auto little_to_native(arithmetic auto value) {
return conditional_reverse<std::endian::little, std::endian::native>(value);
}
constexpr auto big_to_native(arithmetic auto value) {
return conditional_reverse<std::endian::big, std::endian::native>(value);
}
constexpr auto native_to_little(arithmetic auto value) {
return conditional_reverse<std::endian::native, std::endian::little>(value);
}
constexpr auto native_to_big(arithmetic auto value) {
return conditional_reverse<std::endian::native, std::endian::big>(value);
}
template<std::endian from, std::endian to>
constexpr void conditional_reverse_inplace(arithmetic auto& value) {
using type = std::remove_reference_t<decltype(value)>;
if constexpr(from != to) {
if constexpr(std::is_same_v<type, float>) {
value = std::bit_cast<type>(std::byteswap(std::bit_cast<std::uint32_t>(value)));
} else if constexpr(std::is_same_v<type, double>) {
value = std::bit_cast<type>(std::byteswap(std::bit_cast<std::uint64_t>(value)));
} else {
value = std::byteswap(value);
}
}
}
constexpr void conditional_reverse_inplace(arithmetic auto& value, std::endian from, std::endian to) {
using type = std::remove_reference_t<decltype(value)>;
if(from != to) {
if constexpr(std::is_same_v<type, float>) {
value = std::bit_cast<type>(std::byteswap(std::bit_cast<std::uint32_t>(value)));
} else if constexpr(std::is_same_v<type, double>) {
value = std::bit_cast<type>(std::byteswap(std::bit_cast<std::uint64_t>(value)));
} else {
value = std::byteswap(value);
}
}
}
constexpr void little_to_native_inplace(arithmetic auto& value) {
conditional_reverse_inplace<std::endian::little, std::endian::native>(value);
}
constexpr void big_to_native_inplace(arithmetic auto& value) {
conditional_reverse_inplace<std::endian::big, std::endian::native>(value);
}
constexpr void native_to_little_inplace(arithmetic auto& value) {
conditional_reverse_inplace<std::endian::native, std::endian::little>(value);
}
constexpr void native_to_big_inplace(arithmetic auto& value) {
conditional_reverse_inplace<std::endian::native, std::endian::big>(value);
}
template<conversion _conversion>
constexpr auto convert(arithmetic auto value) -> decltype(value) {
switch(_conversion) {
case conversion::big_to_native:
return big_to_native(value);
case conversion::native_to_big:
return native_to_big(value);
case conversion::little_to_native:
return little_to_native(value);
case conversion::native_to_little:
return native_to_little(value);
default:
std::unreachable();
};
}
struct adaptor_tag_t {};
#define ENDIAN_ADAPTOR(name, func_to, func_from) \
template<arithmetic T> \
struct name final : adaptor_tag_t { \
T& value; \
\
name(T& t) : value(t) {} \
name(T&& t) : value(t) {} \
\
auto to() -> T { \
return func_to(value); \
} \
auto from() -> T { \
return func_from(value); \
} \
};
ENDIAN_ADAPTOR(be, native_to_big, big_to_native)
ENDIAN_ADAPTOR(le, native_to_little, little_to_native)
struct storage_tag {};
struct as_big_t final : storage_tag {};
struct as_little_t final : storage_tag {};
struct as_native_t final : storage_tag {};
[[maybe_unused]] constexpr static as_big_t big {};
[[maybe_unused]] constexpr static as_little_t little {};
[[maybe_unused]] constexpr static as_native_t native {};
inline auto storage_in(const arithmetic auto& value, as_native_t) {
return value;
}
inline auto storage_in(const arithmetic auto& value, as_little_t) {
return native_to_little(value);
}
inline auto storage_in(const arithmetic auto& value, as_big_t) {
return native_to_big(value);
}
inline void storage_out(arithmetic auto& value, as_native_t) {}
inline void storage_out(arithmetic auto& value, as_little_t) {
return little_to_native_inplace(value);
}
inline void storage_out(arithmetic auto& value, as_big_t) {
return big_to_native_inplace(value);
}
} // endian, hexi
// #include <hexi/stream_adaptors.h>
#include <concepts>
#include <ranges>
#include <span>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
namespace hexi {
using namespace detail;
#define STREAM_READ_BOUNDS_ENFORCE(read_size, ret_var) \
if(state_ != stream_state::ok) [[unlikely]] { \
return ret_var; \
} \
\
enforce_read_bounds(read_size); \
\
if constexpr(std::is_same_v<exceptions, no_throw_t>) { \
if(state_ != stream_state::ok) [[unlikely]] { \
return ret_var; \
} \
}
#define SAFE_READ(dest, read_size, ret_var) \
STREAM_READ_BOUNDS_ENFORCE(read_size, ret_var) \
buffer_.read(dest, read_size);
template<
byte_oriented buf_type,
std::derived_from<except_tag> exceptions = HEXI_EXCEPTION_TAG,
std::derived_from<endian::storage_tag> endianness = endian::as_native_t
>
class binary_stream final {
public:
using size_type = typename buf_type::size_type;
using offset_type = typename buf_type::offset_type;
using seeking = typename buf_type::seeking;
using value_type = typename buf_type::value_type;
using contiguous_type = typename buf_type::contiguous;
static constexpr endianness byte_order{};
private:
using cond_size_type = std::conditional_t<writeable<buf_type>, size_type, std::monostate>;
buf_type& buffer_;
[[no_unique_address]] cond_size_type total_write_{};
size_type total_read_ = 0;
stream_state state_ = stream_state::ok;
const size_type read_limit_;
inline void enforce_read_bounds(const size_type read_size) {
if(read_size > buffer_.size()) [[unlikely]] {
state_ = stream_state::buff_limit_err;
if constexpr(std::is_same_v<exceptions, allow_throw_t>) {
HEXI_THROW(buffer_underrun(read_size, total_read_, buffer_.size()));
}
return;
}
if(read_limit_) {
const auto max_read_remaining = read_limit_ - total_read_;
if(read_size > max_read_remaining) [[unlikely]] {
state_ = stream_state::read_limit_err;
if constexpr(std::is_same_v<exceptions, allow_throw_t>) {
HEXI_THROW(stream_read_limit(read_size, total_read_, read_limit_));
}
return;
}
}
total_read_ += read_size;
}
template<typename T>
inline void advance_write(T&& arg) {
total_write_ += sizeof(T);
}
template<typename T, typename U>
inline void advance_write(T&&, U&& size) {
total_write_ += size;
}
template<typename... Ts>
inline void write(Ts&&... args) {
HEXI_TRY {
if(state_ == stream_state::ok) [[likely]] {
buffer_.write(std::forward<Ts>(args)...);
advance_write(std::forward<Ts>(args)...);
}
} HEXI_CATCH(...) {
state_ = stream_state::buff_write_err;
if constexpr(std::is_same_v<exceptions, allow_throw_t>) {
HEXI_THROW();
}
}
}
template<typename container_type>
void write_container(container_type& container) {
using c_value_type = typename container_type::value_type;
if constexpr(memcpy_write<container_type, binary_stream>) {
const auto bytes = container.size() * sizeof(c_value_type);
write(container.data(), static_cast<size_type>(bytes));
} else {
for(auto& element : container) {
*this << element;
}
}
}
template<typename container_type, typename count_type>
void read_container(container_type& container, const count_type count) {
using c_value_type = typename container_type::value_type;
container.clear();
if constexpr(memcpy_read<container_type, binary_stream>) {
container.resize(count);
const auto bytes = static_cast<size_type>(count * sizeof(c_value_type));
SAFE_READ(container.data(), bytes, void());
} else {
for(count_type i = 0; i < count; ++i) {
c_value_type value;
*this >> value;
container.emplace_back(std::move(value));
}
}
}
public:
explicit binary_stream(buf_type& source, size_type read_limit = 0)
: buffer_(source),
read_limit_(read_limit) {};
explicit binary_stream(buf_type& source, exceptions)
: binary_stream(source, 0) {}
explicit binary_stream(buf_type& source, endianness)
: binary_stream(source, 0) {}
explicit binary_stream(buf_type& source, exceptions, endianness)
: binary_stream(source, 0) {}
explicit binary_stream(buf_type& source, size_type read_limit, exceptions)
: binary_stream(source, read_limit) {}
explicit binary_stream(buf_type& source, size_type read_limit, endianness)
: binary_stream(source, read_limit) {}
explicit binary_stream(buf_type& source, size_type read_limit, exceptions, endianness)
: binary_stream(source, read_limit) {}
binary_stream(binary_stream&& rhs) noexcept
: buffer_(rhs.buffer_),
total_write_(rhs.total_write_),
total_read_(rhs.total_read_),
state_(rhs.state_),
read_limit_(rhs.read_limit_) {
rhs.total_read_ = static_cast<size_type>(-1);
rhs.state_ = stream_state::invalid_stream;
}
binary_stream& operator=(binary_stream&&) = delete;
binary_stream& operator=(binary_stream&) = delete;
binary_stream(binary_stream&) = delete;
/*** Write ***/
void serialise(auto&& object) requires writeable<buf_type> {
stream_write_adaptor adaptor(*this);
object.serialise(adaptor);
}
template<typename T>
requires has_serialise<T, binary_stream>
binary_stream& operator<<(T& data) requires writeable<buf_type> {
serialise(data);
return *this;
}
binary_stream& operator<<(const has_shl_override<binary_stream> auto& data)
requires writeable<buf_type> {
return *this << data;
}
template<std::derived_from<endian::adaptor_tag_t> endian_func>
binary_stream& operator<<(endian_func adaptor) requires writeable<buf_type> {
const auto converted = adaptor.to();
write(&converted, sizeof(converted));
return *this;
}
binary_stream& operator<<(const arithmetic auto& data) requires writeable<buf_type> {
const auto converted = endian::storage_in(data, byte_order);
write(&converted, sizeof(converted));
return *this;
}
template<pod T>
requires (!has_shl_override<T, binary_stream> && !arithmetic<T>)
binary_stream& operator<<(const T& data) requires writeable<buf_type> {
write(&data, sizeof(T));
return *this;
}
template<typename T>
requires std::is_same_v<std::decay_t<T>, std::string> || std::is_same_v<std::decay_t<T>, std::string_view>
binary_stream& operator<<(prefixed<T> adaptor) requires writeable<buf_type> {
const auto size = static_cast<std::uint32_t>(adaptor->size());
write(endian::native_to_little(size));
write(adaptor->data(), static_cast<size_type>(size));
return *this;
}
template<typename T>
requires std::is_same_v<std::decay_t<T>, std::string> || std::is_same_v<std::decay_t<T>, std::string_view>
binary_stream& operator<<(prefixed_varint<T> adaptor) requires writeable<buf_type> {
varint_encode(*this, adaptor->size());
write(adaptor->data(), adaptor->size());
return *this;
}
template<typename T>
requires std::is_same_v<std::decay_t<T>, std::string_view>
binary_stream& operator<<(null_terminated<T> adaptor) requires writeable<buf_type> {
assert(adaptor->find_first_of('\0') == adaptor->npos);
write(adaptor->data(), adaptor->size());
write('\0');
return *this;
}
template<typename T>
requires std::is_same_v<std::decay_t<T>, std::string>
binary_stream& operator<<(null_terminated<T> adaptor) requires writeable<buf_type> {
assert(adaptor->find_first_of('\0') == adaptor->npos);
write(adaptor->data(), adaptor->size() + 1); // yes, the standard allows this
return *this;
}
template<typename T>
binary_stream& operator<<(raw<T> adaptor) requires writeable<buf_type> {
write(adaptor->data(), adaptor->size());
return *this;
}
binary_stream& operator<<(std::string_view string) requires writeable<buf_type> {
return *this << prefixed(string);
}
binary_stream& operator<<(const std::string& string) requires writeable<buf_type> {
return *this << prefixed(string);
}
binary_stream& operator<<(const char* data) requires writeable<buf_type> {
assert(data);
const auto len = std::strlen(data);
write(data, len + 1); // include terminator
return *this;
}
template<std::ranges::contiguous_range range>
requires pod<typename range::value_type>
binary_stream& operator <<(const range& data) requires writeable<buf_type> {
const auto write_size = data.size() * sizeof(typename range::value_type);
write(data.data(), write_size);
return *this;
}
template<is_iterable T>
requires (!pod<typename T::value_type> || !std::ranges::contiguous_range<T>)
binary_stream& operator<<(T& data) requires writeable<buf_type> {
for(auto& element : data) {
*this << element;
}
return *this;
}
template<is_iterable T>
requires (!std::is_same_v<std::decay_t<T>, std::string>
&& !std::is_same_v<std::decay_t<T>, std::string_view>)
binary_stream& operator<<(prefixed<T> adaptor) requires writeable<buf_type> {
const auto count = static_cast<std::uint32_t>(adaptor->size());
write(endian::native_to_little(count));
write_container(adaptor.str);
return *this;
}
template<is_iterable T>
requires (!std::is_same_v<std::decay_t<T>, std::string>
&& !std::is_same_v<std::decay_t<T>, std::string_view>)
binary_stream& operator<<(prefixed_varint<T> adaptor) requires writeable<buf_type> {
varint_encode(*this, adaptor->size());
write(adaptor->data(), adaptor->size());
write_container(adaptor.str);
return *this;
}
/**
* @brief Writes a contiguous range to the stream.
*
* @param data The contiguous range to be written to the stream.
*/
template<std::ranges::contiguous_range range>
void put(const range& data) requires writeable<buf_type> {
const auto write_size = data.size() * sizeof(typename range::value_type);
write(data.data(), write_size);
}
/**
* @brief Writes a the provided value to the stream.
*
* @param data The value to be written to the stream.
*/
void put(const arithmetic auto& data) requires writeable<buf_type> {
write(&data, sizeof(data));
}
/**
* @brief Writes data to the stream.
*
* @param data The element to be written to the stream.
*/
template<std::derived_from<endian::adaptor_tag_t> endian_func>
void put(const endian_func& adaptor) requires writeable<buf_type> {
const auto swapped = adaptor.to();
write(&swapped, sizeof(swapped));
}
/**
* @brief Writes count elements from the provided buffer to the stream.
*
* @param data Pointer to the buffer from which data will be copied to the stream.
* @param count The number of elements to write.
*/
template<pod T>
void put(const T* data, size_type count) requires writeable<buf_type> {
const auto write_size = count * sizeof(T);
write(data, write_size);
}
/**
* @brief Writes the data from the iterator range to the stream.
*
* @param begin Iterator to the beginning of the data.
* @param end Iterator to the end of the data.
*/
template<typename It>
void put(It begin, const It end) requires writeable<buf_type> {
for(auto it = begin; it != end; ++it) {
*this << *it;
}
}
/**
* @brief Allows for writing a provided byte value a specified number of times to
* the stream.
*
* @param The byte value that will fill the specified number of bytes.
*/
template<size_type size>
constexpr void fill(const std::uint8_t value) requires writeable<buf_type> {
const auto filled = generate_filled<size>(value);
write(filled.data(), filled.size());
}
/*** Read ***/
void deserialise(auto& object) {
stream_read_adaptor adaptor(*this);
object.serialise(adaptor);
}
template<typename T>
requires has_deserialise<T, binary_stream>
binary_stream& operator>>(T& data) {
deserialise(data);
return *this;
}
binary_stream& operator>>(prefixed<std::string> adaptor) {
std::uint32_t size = 0;
*this >> endian::le(size);
if(state_ != stream_state::ok) {
return *this;
}
STREAM_READ_BOUNDS_ENFORCE(size, *this);
adaptor->resize_and_overwrite(size, [&](char* strbuf, std::size_t size) {
buffer_.read(strbuf, size);
return size;
});
return *this;
}
binary_stream& operator>>(prefixed<std::string_view> adaptor) {
std::uint32_t size = 0;
*this >> endian::le(size);
if(state_ != stream_state::ok) {
return *this;
}
adaptor.str = std::string_view { span<char>(size) };
return *this;
}