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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
|
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2018 John Wellbelove
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 ETL_BIT_STREAM_INCLUDED
#define ETL_BIT_STREAM_INCLUDED
#include "platform.h"
#include "type_traits.h"
#include "nullptr.h"
#include "endianness.h"
#include "integral_limits.h"
#include "binary.h"
#include "algorithm.h"
#include "iterator.h"
#include "memory.h"
#include "delegate.h"
#include "span.h"
#include "optional.h"
#include "exception.h"
#include "error_handler.h"
#include <stdint.h>
#include <limits.h>
#include "private/minmax_push.h"
namespace etl
{
//***************************************************************************
/// Encodes and decodes bitstreams.
/// Data must be stored in the stream in network order.
//***************************************************************************
class bit_stream
{
public:
typedef const unsigned char* const_iterator;
//***************************************************************************
/// Default constructor.
//***************************************************************************
bit_stream()
: pdata(ETL_NULLPTR)
, length_chars(0U)
{
restart();
}
//***************************************************************************
/// Construct from range.
//***************************************************************************
bit_stream(void* begin_, void* end_)
: pdata(reinterpret_cast<unsigned char*>(begin_))
, length_chars(etl::distance(reinterpret_cast<unsigned char*>(begin_), reinterpret_cast<unsigned char*>(end_)))
{
restart();
}
//***************************************************************************
/// Construct from begin and length.
//***************************************************************************
bit_stream(void* begin_, size_t length_)
: pdata(reinterpret_cast<unsigned char*>(begin_))
, length_chars(length_)
{
restart();
}
//***************************************************************************
/// Construct from begin and length.
//***************************************************************************
void set_stream(void* begin_, size_t length_)
{
pdata = reinterpret_cast<unsigned char*>(begin_);
length_chars = length_;
restart();
}
//***************************************************************************
/// Construct from range.
//***************************************************************************
void set_stream(void* begin_, void* end_)
{
set_stream(begin_, etl::distance(reinterpret_cast<unsigned char*>(begin_), reinterpret_cast<unsigned char*>(end_)));
}
//***************************************************************************
/// Sets the indexes back to the beginning of the stream.
//***************************************************************************
void restart()
{
bits_available_in_char = CHAR_BIT;
char_index = 0U;
bits_available = CHAR_BIT * length_chars;
}
//***************************************************************************
/// Returns <b>true</b> if the bitsteam indexes have reached the end.
//***************************************************************************
bool at_end() const
{
return (bits_available == 0U);
}
//***************************************************************************
/// Writes a boolean to the stream
//***************************************************************************
bool put(bool value)
{
bool success = false;
if (pdata != ETL_NULLPTR)
{
if (bits_available > 0)
{
unsigned char chunk = value ? 1 : 0;
put_integral(uint32_t(chunk), 1);
success = true;
}
}
return success;
}
//***************************************************************************
/// For integral types
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value, bool>::type
put(T value, uint_least8_t nbits = CHAR_BIT * sizeof(T))
{
return put_integral(static_cast<uint32_t>(value), nbits);
}
#if ETL_USING_64BIT_TYPES
//***************************************************************************
/// For 64bit integral types
//***************************************************************************
bool put(int64_t value, uint_least8_t nbits = CHAR_BIT * sizeof(int64_t))
{
return put_integral(uint64_t(value), nbits);
}
//***************************************************************************
/// For 64bit integral types
//***************************************************************************
bool put(uint64_t value, uint_least8_t nbits = CHAR_BIT * sizeof(uint64_t))
{
return put_integral(value, nbits);
}
#endif
//***************************************************************************
/// For floating point types
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_floating_point<T>::value, bool>::type
put(T value)
{
bool success = true;
unsigned char data[sizeof(T)];
to_bytes(value, data);
for (size_t i = 0UL; i < sizeof(T); ++i)
{
if (!put_integral(uint32_t(data[i]), CHAR_BIT))
{
success = false;
}
}
return success;
}
//***************************************************************************
/// For bool types
//***************************************************************************
bool get(bool& value)
{
bool success = false;
if (pdata != ETL_NULLPTR)
{
// Do we have enough bits?
if (bits_available > 0U)
{
value = get_bit();
success = true;
}
}
return success;
}
//***************************************************************************
/// For integral types
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value, bool>::type
get(T& value, uint_least8_t nbits = CHAR_BIT * sizeof(T))
{
bool success = false;
uint_least8_t bits = nbits;
if (pdata != ETL_NULLPTR)
{
// Do we have enough bits?
if (bits_available >= nbits)
{
value = 0;
// Get the bits from the stream.
while (nbits != 0)
{
unsigned char mask_width = static_cast<unsigned char>(etl::min(nbits, bits_available_in_char));
typedef typename etl::make_unsigned<T>::type chunk_t;
chunk_t chunk = get_chunk(mask_width);
nbits -= mask_width;
value |= static_cast<T>(chunk << nbits);
}
success = true;
}
}
// Sign extend if signed type and not already full bit width.
if (etl::is_signed<T>::value && (bits != (CHAR_BIT * sizeof(T))))
{
typedef typename etl::make_signed<T>::type ST;
value = etl::sign_extend<ST, ST>(value, bits);
}
return success;
}
//***************************************************************************
/// For floating point types
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_floating_point<T>::value, bool>::type
get(T& value)
{
bool success = false;
if (pdata != ETL_NULLPTR)
{
uint_least8_t nbits = CHAR_BIT * sizeof(T);
// Do we have enough bits?
if (bits_available >= nbits)
{
// Temporary storage.
etl::uninitialized_buffer_of<T, 1U> data;
for (size_t i = 0UL; i < sizeof(T); ++i)
{
get(data.raw[i], CHAR_BIT);
}
from_bytes(reinterpret_cast<const unsigned char*>(data.raw), value);
success = true;
}
}
return success;
}
//***************************************************************************
/// Returns the number of bytes used in the stream.
//***************************************************************************
size_t size() const
{
size_t s = char_index;
// Current byte is used?
if (bits_available_in_char != CHAR_BIT)
{
++s;
}
return s;
}
//***************************************************************************
/// Returns the number of bits used in the stream.
//***************************************************************************
size_t bits() const
{
return (length_chars * CHAR_BIT) - bits_available;
}
//***************************************************************************
/// Returns start of the stream.
//***************************************************************************
const_iterator begin() const
{
return pdata;
}
//***************************************************************************
/// Returns end of the stream.
//***************************************************************************
const_iterator end() const
{
return pdata + size();
}
private:
//***************************************************************************
/// For unsigned integral types
//***************************************************************************
bool put_integral(uint32_t value, uint_least8_t nbits)
{
bool success = false;
if (pdata != ETL_NULLPTR)
{
// Do we have enough bits?
if (bits_available >= nbits)
{
// Send the bits to the stream.
while (nbits != 0)
{
unsigned char mask_width = static_cast<unsigned char>(etl::min(nbits, bits_available_in_char));
nbits -= mask_width;
uint32_t mask = ((1U << mask_width) - 1U) << nbits;
//uint32_t mask = ((uint32_t(1U) << mask_width) - 1U) << nbits;
// Move chunk to lowest char bits.
// Chunks are never larger than one char.
uint32_t chunk = ((value & mask) >> nbits) << (bits_available_in_char - mask_width);
put_chunk(static_cast<unsigned char>(chunk), mask_width);
}
success = true;
}
}
return success;
}
#if ETL_USING_64BIT_TYPES
//***************************************************************************
/// For unsigned integral types. 64bit
//***************************************************************************
bool put_integral(uint64_t value, uint_least8_t nbits)
{
bool success = false;
if (pdata != ETL_NULLPTR)
{
// Do we have enough bits?
if (bits_available >= nbits)
{
// Send the bits to the stream.
while (nbits != 0)
{
unsigned char mask_width = static_cast<unsigned char>(etl::min(nbits, bits_available_in_char));
nbits -= mask_width;
uint64_t mask = ((uint64_t(1U) << mask_width) - 1U) << nbits;
// Move chunk to lowest char bits.
// Chunks are never larger than one char.
uint64_t chunk = ((value & mask) >> nbits) << (bits_available_in_char - mask_width);
put_chunk(static_cast<unsigned char>(chunk), mask_width);
}
success = true;
}
}
return success;
}
#endif
//***************************************************************************
/// Put a data chunk to the stream
//***************************************************************************
void put_chunk(unsigned char chunk, unsigned char nbits)
{
// Clear if new byte.
if (bits_available_in_char == 8U)
{
pdata[char_index] = 0U;
}
pdata[char_index] |= chunk;
step(nbits);
}
//***************************************************************************
/// Get a data chunk from the stream
//***************************************************************************
unsigned char get_chunk(unsigned char nbits)
{
unsigned char value = pdata[char_index];
value >>= (bits_available_in_char - nbits);
unsigned char mask;
if (nbits == CHAR_BIT)
{
mask = etl::integral_limits<unsigned char>::max;
}
else
{
mask = (1U << nbits) - 1;
}
value &= mask;
step(nbits);
return value;
}
//***************************************************************************
/// Get a bool from the stream
//***************************************************************************
bool get_bit()
{
bool result = (pdata[char_index] & (1U << (bits_available_in_char - 1U))) != 0U;
step(1U);
return result;
}
//***************************************************************************
/// Helper function for floating point types
//***************************************************************************
template <typename T>
void from_bytes(const unsigned char* data, T& value)
{
etl::uninitialized_buffer_of<T, 1U> temp;
// Network to host.
if (etl::endianness::value() == etl::endian::little)
{
etl::reverse_copy(data, data + sizeof(T), temp.raw);
}
else
{
etl::copy(data, data + sizeof(T), temp.raw);
}
value = *reinterpret_cast<T*>(temp.raw);
}
//***************************************************************************
/// Helper function for floating point types
//***************************************************************************
template <typename T>
void to_bytes(T value, unsigned char* data)
{
unsigned char* pf = reinterpret_cast<unsigned char*>(&value);
// Host to network.
if (etl::endianness::value() == etl::endian::little)
{
etl::reverse_copy(pf, pf + sizeof(T), data);
}
else
{
etl::copy(pf, pf + sizeof(T), data);
}
}
//***************************************************************************
/// Step the specified number of bits along the stream.
/// The nbits will never be larger than 'bits_available_in_char'.
//***************************************************************************
void step(unsigned char nbits)
{
bits_available_in_char -= nbits;
if (bits_available_in_char == 0)
{
++char_index;
bits_available_in_char = 8;
}
bits_available -= nbits;
}
unsigned char *pdata; ///< The start of the bitstream buffer.
size_t length_chars; ///< The length, in char, of the bitstream buffer.
unsigned char bits_available_in_char; ///< The number of available bits in the current char.
size_t char_index; ///< The index of the char in the bitstream buffer.
size_t bits_available; ///< The number of bits still available in the bitstream buffer.
};
//***************************************************************************
/// Writes bits streams.
//***************************************************************************
class bit_stream_writer
{
public:
typedef char value_type;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef etl::span<value_type> callback_parameter_type;
typedef etl::delegate<void(callback_parameter_type)> callback_type;
//***************************************************************************
/// Construct from span.
//***************************************************************************
template <size_t Length>
bit_stream_writer(const etl::span<char, Length>& span_, etl::endian stream_endianness_, callback_type callback_ = callback_type())
: pdata(span_.begin())
, length_chars(span_.size_bytes())
, stream_endianness(stream_endianness_)
, callback(callback_)
{
restart();
}
//***************************************************************************
/// Construct from span.
//***************************************************************************
template <size_t Length>
bit_stream_writer(const etl::span<unsigned char, Length>& span_, etl::endian stream_endianness_, callback_type callback_ = callback_type())
: pdata(reinterpret_cast<char*>(span_.begin()))
, length_chars(span_.size_bytes())
, stream_endianness(stream_endianness_)
, callback(callback_)
{
restart();
}
//***************************************************************************
/// Construct from range.
//***************************************************************************
bit_stream_writer(void* begin_, void* end_, etl::endian stream_endianness_, callback_type callback_ = callback_type())
: pdata(reinterpret_cast<char*>(begin_))
, length_chars(etl::distance(reinterpret_cast<unsigned char*>(begin_), reinterpret_cast<unsigned char*>(end_)))
, stream_endianness(stream_endianness_)
, callback(callback_)
{
restart();
}
//***************************************************************************
/// Construct from begin and length.
//***************************************************************************
bit_stream_writer(void* begin_, size_t length_chars_, etl::endian stream_endianness_, callback_type callback_ = callback_type())
: pdata(reinterpret_cast<char*>(begin_))
, length_chars(length_chars_)
, stream_endianness(stream_endianness_)
, callback(callback_)
{
restart();
}
//***************************************************************************
/// Sets the indexes back to the beginning of the stream.
//***************************************************************************
void restart()
{
bits_available_in_char = CHAR_BIT;
char_index = 0U;
bits_available = CHAR_BIT * length_chars;
}
//***************************************************************************
/// Returns the maximum capacity in bits.
//***************************************************************************
size_t capacity_bytes() const
{
return length_chars;
}
//***************************************************************************
size_t capacity_bits() const
{
return length_chars * CHAR_BIT;
}
//***************************************************************************
/// Returns <b>true</b> if the bitsteam indexes have been reset.
//***************************************************************************
bool empty() const
{
return (bits_available == length_chars);
}
//***************************************************************************
/// Returns <b>true</b> if the bitsteam indexes have reached the end.
//***************************************************************************
bool full() const
{
return (bits_available == 0U);
}
//***************************************************************************
/// Writes a boolean to the stream
//***************************************************************************
void write_unchecked(bool value)
{
unsigned char chunk = value ? 1 : 0;
write_data<unsigned char>(chunk, 1);
}
//***************************************************************************
/// Writes a boolean to the stream
//***************************************************************************
bool write(bool value)
{
bool success = (available<1U>() > 0U);
if (success)
{
write_unchecked(value);
}
return success;
}
//***************************************************************************
/// For integral types
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value, void>::type
write_unchecked(T value, uint_least8_t nbits = CHAR_BIT * sizeof(T))
{
typedef typename etl::unsigned_type<T>::type unsigned_t;
write_data<unsigned_t>(static_cast<unsigned_t>(value), nbits);
}
//***************************************************************************
/// For integral types
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value, bool>::type
write(T value, uint_least8_t nbits = CHAR_BIT * sizeof(T))
{
bool success = (available(nbits) > 0U);
if (success)
{
write_unchecked(value, nbits);
}
return success;
}
//***************************************************************************
/// Skip n bits, up to the maximum space available.
/// Returns <b>true</b> if the skip was possible.
/// Returns <b>false</b> if the full skip size was not possible.
//***************************************************************************
bool skip(size_t nbits)
{
bool success = (nbits <= available_bits());
if (success)
{
while (nbits > bits_available_in_char)
{
step(bits_available_in_char);
nbits -= bits_available_in_char;
}
if (nbits != 0U)
{
step(static_cast<unsigned char>(nbits));
}
}
return success;
}
//***************************************************************************
/// Returns the number of bytes used in the stream.
//***************************************************************************
size_t size_bytes() const
{
size_t s = char_index;
// Is the current byte partially used?
if (bits_available_in_char != CHAR_BIT)
{
++s;
}
return s;
}
//***************************************************************************
/// Returns the number of bits used in the stream.
//***************************************************************************
size_t size_bits() const
{
return capacity_bits() - available_bits();
}
//***************************************************************************
/// The number of multiples of 'Nbits' available in the stream.
/// Compile time.
//***************************************************************************
template <size_t Nbits>
size_t available() const
{
return bits_available / Nbits;
}
//***************************************************************************
/// The number of T available in the stream.
/// Compile time.
//***************************************************************************
template <typename T>
size_t available() const
{
return available<CHAR_BIT * sizeof(T)>();
}
//***************************************************************************
/// The number of 'bit width' available in the stream.
/// Run time.
//***************************************************************************
size_t available(size_t nbits) const
{
return bits_available / nbits;
}
//***************************************************************************
/// The number of bits left in the stream.
//***************************************************************************
size_t available_bits() const
{
return bits_available;
}
//***************************************************************************
/// Returns start of the stream.
//***************************************************************************
iterator begin()
{
return pdata;
}
//***************************************************************************
/// Returns start of the stream.
//***************************************************************************
const_iterator begin() const
{
return pdata;
}
//***************************************************************************
/// Returns start of the stream.
//***************************************************************************
const_iterator cbegin() const
{
return pdata;
}
//***************************************************************************
/// Returns end of the stream.
//***************************************************************************
iterator end()
{
return pdata + size_bytes();
}
//***************************************************************************
/// Returns end of the stream.
//***************************************************************************
const_iterator end() const
{
return pdata + size_bytes();
}
//***************************************************************************
/// Returns end of the stream.
//***************************************************************************
const_iterator cend() const
{
return pdata + size_bytes();
}
//***************************************************************************
/// Returns a span of the used portion of the stream.
//***************************************************************************
etl::span<char> used_data()
{
return etl::span<char>(pdata, pdata + size_bytes());
}
//***************************************************************************
/// Returns a span of the used portion of the stream.
//***************************************************************************
etl::span<const char> used_data() const
{
return etl::span<const char>(pdata, pdata + size_bytes());
}
//***************************************************************************
/// Returns a span of whole the stream.
//***************************************************************************
etl::span<char> data()
{
return etl::span<char>(pdata, pdata + length_chars);
}
//***************************************************************************
/// Returns a span of whole the stream.
//***************************************************************************
etl::span<const char> data() const
{
return etl::span<const char>(pdata, pdata + length_chars);
}
//***************************************************************************
/// Flush the last byte, if partially filled, to the callback, if valid.
//***************************************************************************
void flush()
{
if (callback.is_valid())
{
if (bits_available_in_char != 0U)
{
char_index = 1U; // Indicate that the first char is actually 'full'.
flush_full_bytes();
}
restart();
}
}
//***************************************************************************
/// Sets the function to call after every write.
//***************************************************************************
void set_callback(callback_type callback_)
{
callback = callback_;
}
//***************************************************************************
/// Gets the function to call after every write.
//***************************************************************************
callback_type get_callback() const
{
return callback;
}
private:
//***************************************************************************
/// Write a value to the stream.
/// It will be passed one of five unsigned types.
//***************************************************************************
template <typename T>
void write_data(T value, uint_least8_t nbits)
{
// Make sure that we are not writing more bits than should be available.
nbits = (nbits > (CHAR_BIT * sizeof(T))) ? (CHAR_BIT * sizeof(T)) : nbits;
if (stream_endianness == etl::endian::little)
{
value = etl::reverse_bits(value);
value = value >> ((CHAR_BIT * sizeof(T)) - nbits);
}
// Send the bits to the stream.
while (nbits != 0)
{
unsigned char mask_width = static_cast<unsigned char>(etl::min(nbits, bits_available_in_char));
nbits -= mask_width;
T mask = ((T(1U) << mask_width) - 1U) << nbits;
// Move chunk to lowest char bits.
// Chunks are never larger than one char.
T chunk = ((value & mask) >> nbits) << (bits_available_in_char - mask_width);
write_chunk(static_cast<char>(chunk), mask_width);
}
if (callback.is_valid())
{
flush_full_bytes();
}
}
//***************************************************************************
/// Write a data chunk to the stream
//***************************************************************************
void write_chunk(char chunk, unsigned char nbits)
{
// Clear if new byte.
if (bits_available_in_char == CHAR_BIT)
{
pdata[char_index] = 0U;
}
pdata[char_index] |= chunk;
step(nbits);
}
//***************************************************************************
/// Flush full bytes to the callback, if valid.
/// Resets the stream to empty or the last unfilled byte.
//***************************************************************************
void flush_full_bytes()
{
// Is the first byte fully filled?
if (char_index > 0U)
{
callback(callback_parameter_type(pdata, pdata + char_index));
bits_available = CHAR_BIT * length_chars;
if (bits_available_in_char != 0U)
{
// Move a partially filled last byte to the start of the buffer.
pdata[0] = pdata[char_index];
bits_available -= (CHAR_BIT - bits_available_in_char);
}
char_index = 0U;
}
}
//***************************************************************************
/// Step the specified number of bits along the stream.
/// The width will never be larger than 'bits_available_in_char'.
//***************************************************************************
void step(unsigned char nbits)
{
bits_available_in_char -= nbits;
if (bits_available_in_char == 0)
{
++char_index;
bits_available_in_char = CHAR_BIT;
}
bits_available -= nbits;
}
char* const pdata; ///< The start of the bitstream buffer.
const size_t length_chars; ///< The length of the bitstream buffer.
const etl::endian stream_endianness; ///< The endianness of the stream data.
unsigned char bits_available_in_char; ///< The number of available bits in the current char.
size_t char_index; ///< The index of the current char in the bitstream buffer.
size_t bits_available; ///< The number of bits still available in the bitstream buffer.
callback_type callback; ///< An optional callback on every filled byte in buffer.
};
//***************************************************************************
/// Implementation of the write function.
/// For bool types only.
//***************************************************************************
inline void write_unchecked(etl::bit_stream_writer& stream, bool value)
{
stream.write_unchecked(value);
}
//***************************************************************************
/// Implementation of the write function.
/// For bool types only.
//***************************************************************************
inline bool write(etl::bit_stream_writer& stream, bool value)
{
return stream.write(value);
}
//***************************************************************************
/// Default implementation of the write function.
/// For integral types only (but not bool).
/// Overload this to support custom types.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value, void>::type
write_unchecked(etl::bit_stream_writer& stream, const T& value, uint_least8_t nbits = CHAR_BIT * sizeof(T))
{
stream.write_unchecked(value, nbits);
}
//***************************************************************************
/// Default implementation of the write function.
/// For integral types only (but not bool).
/// Overload this to support custom types.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value, bool>::type
write(etl::bit_stream_writer& stream, const T& value, uint_least8_t nbits = CHAR_BIT * sizeof(T))
{
return stream.write(value, nbits);
}
//***************************************************************************
/// Reads bit streams
//***************************************************************************
class bit_stream_reader
{
public:
typedef char value_type;
typedef const char* const_iterator;
//***************************************************************************
/// Construct from span.
//***************************************************************************
template <size_t Length>
bit_stream_reader(const etl::span<char, Length>& span_, etl::endian stream_endianness_)
: pdata(span_.begin())
, length_chars(span_.size_bytes())
, stream_endianness(stream_endianness_)
{
restart();
}
//***************************************************************************
/// Construct from span.
//***************************************************************************
template <size_t Length>
bit_stream_reader(const etl::span<unsigned char, Length>& span_, etl::endian stream_endianness_)
: pdata(reinterpret_cast<const char*>(span_.begin()))
, length_chars(span_.size_bytes())
, stream_endianness(stream_endianness_)
{
restart();
}
//***************************************************************************
/// Construct from span.
//***************************************************************************
template <size_t Length>
bit_stream_reader(const etl::span<const char, Length>& span_, etl::endian stream_endianness_)
: pdata(span_.begin())
, length_chars(span_.size_bytes())
, stream_endianness(stream_endianness_)
{
restart();
}
//***************************************************************************
/// Construct from span.
//***************************************************************************
template <size_t Length>
bit_stream_reader(const etl::span<const unsigned char, Length>& span_, etl::endian stream_endianness_)
: pdata(reinterpret_cast<const char*>(span_.begin()))
, length_chars(span_.size_bytes())
, stream_endianness(stream_endianness_)
{
restart();
}
//***************************************************************************
/// Construct from range.
//***************************************************************************
bit_stream_reader(void* begin_, void* end_, etl::endian stream_endianness_)
: pdata(reinterpret_cast<const char*>(begin_))
, length_chars(etl::distance(reinterpret_cast<const char*>(begin_), reinterpret_cast<const char*>(end_)))
, stream_endianness(stream_endianness_)
{
restart();
}
//***************************************************************************
/// Construct from begin and length.
//***************************************************************************
bit_stream_reader(void* begin_, size_t length_, etl::endian stream_endianness_)
: pdata(reinterpret_cast<const char*>(begin_))
, length_chars(length_)
, stream_endianness(stream_endianness_)
{
restart();
}
//***************************************************************************
/// Sets the indexes back to the beginning of the stream.
//***************************************************************************
void restart()
{
bits_available_in_char = CHAR_BIT;
char_index = 0U;
bits_available = CHAR_BIT * length_chars;
}
//***************************************************************************
/// For bool types
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_same<bool, T>::value, bool>::type
read_unchecked()
{
return get_bit();
}
//***************************************************************************
/// For bool types
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_same<bool, T>::value, etl::optional<bool> >::type
read()
{
etl::optional<bool> result;
if (bits_available > 0U)
{
result = read_unchecked<bool>();
}
return result;
}
//***************************************************************************
/// For integral types
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value && !etl::is_same<bool, T>::value, T>::type
read_unchecked(uint_least8_t nbits = CHAR_BIT * sizeof(T))
{
typedef typename etl::unsigned_type<T>::type unsigned_t;
T value = read_value<unsigned_t>(nbits, etl::is_signed<T>::value);
return static_cast<T>(value);
}
//***************************************************************************
/// For integral types
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value && !etl::is_same<bool, T>::value, etl::optional<T> >::type
read(uint_least8_t nbits = CHAR_BIT * sizeof(T))
{
etl::optional<T> result;
// Do we have enough bits?
if (bits_available >= nbits)
{
result = read_unchecked<T>(nbits);
}
return result;
}
//***************************************************************************
/// Returns the number of bytes in the stream buffer.
//***************************************************************************
size_t size_bytes() const
{
return length_chars;
}
//***************************************************************************
/// Returns the number of bits in the stream buffer.
//***************************************************************************
size_t size_bits() const
{
return length_chars * CHAR_BIT;
}
//***************************************************************************
/// Returns start of the stream.
//***************************************************************************
const_iterator begin() const
{
return pdata;
}
//***************************************************************************
/// Returns start of the stream.
//***************************************************************************
const_iterator cbegin() const
{
return pdata;
}
//***************************************************************************
/// Returns end of the stream.
//***************************************************************************
const_iterator end() const
{
return pdata + size_bytes();
}
//***************************************************************************
/// Returns end of the stream.
//***************************************************************************
const_iterator cend() const
{
return pdata + size_bytes();
}
//***************************************************************************
/// Returns a span of whole the stream.
//***************************************************************************
etl::span<const char> data() const
{
return etl::span<const char>(pdata, pdata + length_chars);
}
//***************************************************************************
/// Skip n bits, up to the maximum space available.
/// Returns <b>true</b> if the skip was possible.
/// Returns <b>false</b> if the full skip size was not possible.
//***************************************************************************
bool skip(size_t nbits)
{
bool success = (nbits <= bits_available);
if (success)
{
while (nbits > bits_available_in_char)
{
nbits -= bits_available_in_char;
step(bits_available_in_char);
}
if (nbits != 0U)
{
step(static_cast<unsigned char>(nbits));
}
}
return success;
}
private:
//***************************************************************************
/// Read a value from the stream.
/// It will be passed one of five unsigned types.
//***************************************************************************
template <typename T>
T read_value(uint_least8_t nbits, bool is_signed)
{
// Make sure that we are not reading more bits than should be available.
nbits = (nbits > (CHAR_BIT * sizeof(T))) ? (CHAR_BIT * sizeof(T)) : nbits;
T value = 0;
uint_least8_t bits = nbits;
// Get the bits from the stream.
while (nbits != 0)
{
unsigned char mask_width = static_cast<unsigned char>(etl::min(nbits, bits_available_in_char));
T chunk = get_chunk(mask_width);
nbits -= mask_width;
value |= static_cast<T>(chunk << nbits);
}
if (stream_endianness == etl::endian::little)
{
value = value << ((CHAR_BIT * sizeof(T)) - bits);
value = etl::reverse_bits(value);
}
if (is_signed && (bits != (CHAR_BIT * sizeof(T))))
{
value = etl::sign_extend<T, T>(value, bits);
}
return value;
}
//***************************************************************************
/// Get a data chunk from the stream
//***************************************************************************
unsigned char get_chunk(unsigned char nbits)
{
unsigned char value = pdata[char_index];
value >>= (bits_available_in_char - nbits);
unsigned char mask;
if (nbits == CHAR_BIT)
{
mask = etl::integral_limits<unsigned char>::max;
}
else
{
mask = (1U << nbits) - 1;
}
value &= mask;
step(nbits);
return value;
}
//***************************************************************************
/// Get a bool from the stream
//***************************************************************************
bool get_bit()
{
bool result = (pdata[char_index] & (1U << (bits_available_in_char - 1U))) != 0U;
step(1U);
return result;
}
//***************************************************************************
/// Step the specified number of bits along the stream.
/// The nbits will never be larger than 'bits_available_in_char'.
//***************************************************************************
void step(unsigned char nbits)
{
bits_available_in_char -= nbits;
if (bits_available_in_char == 0)
{
++char_index;
bits_available_in_char = 8;
}
bits_available -= nbits;
}
const char* pdata; ///< The start of the bitstream buffer.
size_t length_chars; ///< The length, in char, of the bitstream buffer.
const etl::endian stream_endianness; ///< The endianness of the stream data.
unsigned char bits_available_in_char; ///< The number of available bits in the current char.
size_t char_index; ///< The index of the char in the bitstream buffer.
size_t bits_available; ///< The number of bits still available in the bitstream buffer.
};
//***************************************************************************
/// Read an unchecked type from a stream.
//***************************************************************************
template <typename T>
T read_unchecked(etl::bit_stream_reader& stream)
{
return stream.read_unchecked<T>();
}
template <typename T>
T read_unchecked(etl::bit_stream_reader& stream, uint_least8_t nbits)
{
return stream.read_unchecked<T>(nbits);
}
//***************************************************************************
/// Read a checked type from a stream.
//***************************************************************************
template <typename T>
etl::optional<T> read(etl::bit_stream_reader& stream)
{
return stream.read<T>();
}
template <typename T>
etl::optional<T> read(etl::bit_stream_reader& stream, uint_least8_t nbits)
{
return stream.read<T>(nbits);
}
//***************************************************************************
/// Read an unchecked bool from a stream.
//***************************************************************************
template <>
inline bool read_unchecked<bool>(etl::bit_stream_reader& stream)
{
return stream.read_unchecked<bool>();
}
//***************************************************************************
/// Read a bool from a stream.
//***************************************************************************
template <>
inline etl::optional<bool> read<bool>(etl::bit_stream_reader& stream)
{
return stream.read<bool>();
}
}
#include "private/minmax_pop.h"
#endif
|