SSP21-CPP
VLength.h
1 #ifndef SSP21_SEQUENCELENGTH_H
2 #define SSP21_SEQUENCELENGTH_H
3 
4 #include "ssp21/util/SequenceTypes.h"
5 #include "ssp21/crypto/gen/ParseError.h"
6 #include "ssp21/crypto/gen/FormatError.h"
7 
8 namespace ssp21
9 {
10  /**
11  *
12  * Variable-length unsigned 32-bit integer encoding/decoding
13  *
14  * num bytes | # bits | max value | encoding
15  * ______________________________________________________
16  * 1 | 7 | 127 | { 0x7F }
17  * 2 | 14 | 16383 | { 0xFF, 0x7F }
18  * 3 | 21 | 2097151 | { 0xFF, 0xFF, 0x7F }
19  * 4 | 28 | 268435455 | { 0xFF, 0xFF, 0xFF, 0x7F }
20  *
21  * We can only use the bottom 4 bits of the 5th byte.
22  *
23  * 2^32 - 1 encodes to { 0xFF, 0xFF, 0xFF, 0xFF, 0x0F }
24  *
25  * Therefore, the 5th byte can NEVER be > than 0x0F
26  *
27  */
28  class VLength : private ser4cpp::StaticOnly
29  {
30  static const uint8_t top_bit_mask = 0x80;
31  static const uint8_t bottom_bits_mask = 0x7F;
32 
33  static const uint32_t max_1_byte_value = 127;
34  static const uint32_t max_2_byte_value = 16383;
35  static const uint32_t max_3_byte_value = 2097151;
36  static const uint32_t max_4_byte_value = 268435455;
37 
38  public:
39 
40  static FormatError write(uint32_t value, wseq32_t& dest);
41 
42  static ParseError read(uint32_t& value, seq32_t& src);
43 
44  static size_t size_in_bytes(uint32_t value);
45  };
46 
47 }
48 
49 #endif
SSP21-cpp main namespace.
Definition: BufferTypes.h:12
ParseError
Definition: ParseError.h:27
FormatError
Definition: FormatError.h:28