| 1 | #cxx . . $ `<file>/mlspp/lib/tls_syntax/src/tls_syntax.cpp`/include <tls/tls_syntax.h>cxx . . $ `<file>/mlspp/lib/tls_syntax/include/tls/tls_syntax.h`/ |
| 2 | |
| 3 | // NOLINTNEXTLINE(llvmlibc-implementation-in-namespace) |
| 4 | namespace mlsppcxx . . $ mlspp/::tlscxx . . $ mlspp/tls/ { |
| 5 | |
| 6 | void |
| 7 | ostreamcxx . . $ mlspp/tls/ostream#::write_rawcxx . . $ mlspp/tls/ostream#write_raw(981a1a29c6e5fab4).(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<uint8_tcxx . . $ uint8_t#>& bytes) |
| 8 | { |
| 9 | // Not sure what the default argument is here |
| 10 | _buffercxx . . $ mlspp/tls/ostream#_buffer..insertcxx . . $ std/vector#insert(134ef7853f0b3ebe).(_buffercxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640)..endcxx . . $ std/vector#end(55a6de06420d4903).(), bytes.begincxx . . $ std/vector#begin(d1e1699016b9d7e6).(), bytes.endcxx . . $ std/vector#end(d1e1699016b9d7e6).()); |
| 11 | } |
| 12 | |
| 13 | // Primitive type writers |
| 14 | ostreamcxx . . $ mlspp/tls/ostream#& |
| 15 | ostreamcxx . . $ mlspp/tls/ostream#::write_uintcxx . . $ mlspp/tls/ostream#write_uint(fd0940e664e3073f).(uint64_tcxx . . $ uint64_t# value, int length) |
| 16 | { |
| 17 | for (int i = length - 1; i >= 0; --i) { |
| 18 | _buffercxx . . $ mlspp/tls/ostream#_buffer..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(static_cast<uint8_tcxx . . $ uint8_t#>(value >> unsigned(8 * i))); |
| 19 | } |
| 20 | return *this; |
| 21 | } |
| 22 | |
| 23 | ostreamcxx . . $ mlspp/tls/ostream#& |
| 24 | operatorcxx . . $ mlspp/tls/`operator<<`(593271e9d336fe5c).<<(ostreamcxx . . $ mlspp/tls/ostream#& out, bool data) |
| 25 | { |
| 26 | if (data) { |
| 27 | return out <<cxx . . $ mlspp/tls/`operator<<`(c96373cd8b21f370). uint8_tcxx . . $ uint8_t#(1); |
| 28 | } |
| 29 | |
| 30 | return out <<cxx . . $ mlspp/tls/`operator<<`(c96373cd8b21f370). uint8_tcxx . . $ uint8_t#(0); |
| 31 | } |
| 32 | |
| 33 | ostreamcxx . . $ mlspp/tls/ostream#& |
| 34 | operatorcxx . . $ mlspp/tls/`operator<<`(c96373cd8b21f370).<<(ostreamcxx . . $ mlspp/tls/ostream#& out, uint8_tcxx . . $ uint8_t# data) // NOLINT(llvmlibc-callee-namespace) |
| 35 | { |
| 36 | return out.write_uintcxx . . $ mlspp/tls/ostream#write_uint(fd0940e664e3073f).(data, 1); |
| 37 | } |
| 38 | |
| 39 | ostreamcxx . . $ mlspp/tls/ostream#& |
| 40 | operatorcxx . . $ mlspp/tls/`operator<<`(5fcc4a612bacb2f0).<<(ostreamcxx . . $ mlspp/tls/ostream#& out, uint16_tcxx . . $ uint16_t# data) |
| 41 | { |
| 42 | return out.write_uintcxx . . $ mlspp/tls/ostream#write_uint(fd0940e664e3073f).(data, 2); |
| 43 | } |
| 44 | |
| 45 | ostreamcxx . . $ mlspp/tls/ostream#& |
| 46 | operatorcxx . . $ mlspp/tls/`operator<<`(7958520ae221e50c).<<(ostreamcxx . . $ mlspp/tls/ostream#& out, uint32_tcxx . . $ uint32_t# data) |
| 47 | { |
| 48 | return out.write_uintcxx . . $ mlspp/tls/ostream#write_uint(fd0940e664e3073f).(data, 4); |
| 49 | } |
| 50 | |
| 51 | ostreamcxx . . $ mlspp/tls/ostream#& |
| 52 | operatorcxx . . $ mlspp/tls/`operator<<`(c3368cf33e4c1b90).<<(ostreamcxx . . $ mlspp/tls/ostream#& out, uint64_tcxx . . $ uint64_t# data) |
| 53 | { |
| 54 | return out.write_uintcxx . . $ mlspp/tls/ostream#write_uint(fd0940e664e3073f).(data, 8); |
| 55 | } |
| 56 | |
| 57 | // Because pop_back() on an empty vector is undefined |
| 58 | uint8_tcxx . . $ uint8_t# |
| 59 | istreamcxx . . $ mlspp/tls/istream#::nextcxx . . $ mlspp/tls/istream#next(7c4be072098b358f).() |
| 60 | { |
| 61 | if (_buffercxx . . $ mlspp/tls/istream#_buffer..emptycxx . . $ std/vector#empty(3482b152b9333168).()) { |
| 62 | throw ReadErrorcxx . . $ mlspp/tls/ReadError#("Attempt to read from empty buffer"); |
| 63 | } |
| 64 | |
| 65 | const uint8_tcxx . . $ uint8_t# value = _buffercxx . . $ mlspp/tls/istream#_buffer..backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).(); |
| 66 | _buffercxx . . $ mlspp/tls/istream#_buffer..pop_backcxx . . $ std/vector#pop_back(ced63f7c635d850d).(); |
| 67 | return value; |
| 68 | } |
| 69 | |
| 70 | // Primitive type readers |
| 71 | |
| 72 | istreamcxx . . $ mlspp/tls/istream#& |
| 73 | operatorcxx . . $ mlspp/tls/`operator>>`(1b4bd6eaff4d2256).>>(istreamcxx . . $ mlspp/tls/istream#& in, bool& data) |
| 74 | { |
| 75 | uint8_tcxx . . $ uint8_t# val = 0; |
| 76 | in >>cxx . . $ mlspp/tls/`operator>>`(fa7a7be219541263). val; |
| 77 | |
| 78 | // Linter thinks uint8_t is signed (?) |
| 79 | // NOLINTNEXTLINE(hicpp-signed-bitwise) |
| 80 | if ((val & 0xFE) != 0) { |
| 81 | throw ReadErrorcxx . . $ mlspp/tls/ReadError#("Malformed boolean"); |
| 82 | } |
| 83 | |
| 84 | data = (val == 1); |
| 85 | return in; |
| 86 | } |
| 87 | |
| 88 | istreamcxx . . $ mlspp/tls/istream#& |
| 89 | operatorcxx . . $ mlspp/tls/`operator>>`(fa7a7be219541263).>>(istreamcxx . . $ mlspp/tls/istream#& in, uint8_tcxx . . $ uint8_t#& data) // NOLINT(llvmlibc-callee-namespace) |
| 90 | { |
| 91 | return in.read_uintcxx . . $ mlspp/tls/istream#read_uint(99a67fe8c4e4f583).(data, 1); |
| 92 | } |
| 93 | |
| 94 | istreamcxx . . $ mlspp/tls/istream#& |
| 95 | operatorcxx . . $ mlspp/tls/`operator>>`(5428fbbf866aa1f9).>>(istreamcxx . . $ mlspp/tls/istream#& in, uint16_tcxx . . $ uint16_t#& data) |
| 96 | { |
| 97 | return in.read_uintcxx . . $ mlspp/tls/istream#read_uint(99a67fe8c4e4f583).(data, 2); |
| 98 | } |
| 99 | |
| 100 | istreamcxx . . $ mlspp/tls/istream#& |
| 101 | operatorcxx . . $ mlspp/tls/`operator>>`(17588a6d1f669cbf).>>(istreamcxx . . $ mlspp/tls/istream#& in, uint32_tcxx . . $ uint32_t#& data) |
| 102 | { |
| 103 | return in.read_uintcxx . . $ mlspp/tls/istream#read_uint(99a67fe8c4e4f583).(data, 4); |
| 104 | } |
| 105 | |
| 106 | istreamcxx . . $ mlspp/tls/istream#& |
| 107 | operatorcxx . . $ mlspp/tls/`operator>>`(6bd9e578f60e7a6c).>>(istreamcxx . . $ mlspp/tls/istream#& in, uint64_tcxx . . $ uint64_t#& data) |
| 108 | { |
| 109 | return in.read_uintcxx . . $ mlspp/tls/istream#read_uint(99a67fe8c4e4f583).(data, 8); |
| 110 | } |
| 111 | |
| 112 | // Varint encoding |
| 113 | static constexpr size_t VARINT_HEADER_OFFSETcxx . . $ mlspp/tls/VARINT_HEADER_OFFSET. = 6; |
| 114 | static constexpr uint64_tcxx . . $ uint64_t# VARINT_1_HEADERcxx . . $ mlspp/tls/VARINT_1_HEADER. = 0x00; // 0 << V1_OFFSET |
| 115 | static constexpr uint64_tcxx . . $ uint64_t# VARINT_2_HEADERcxx . . $ mlspp/tls/VARINT_2_HEADER. = 0x4000; // 1 << V2_OFFSET |
| 116 | static constexpr uint64_tcxx . . $ uint64_t# VARINT_4_HEADERcxx . . $ mlspp/tls/VARINT_4_HEADER. = 0x80000000; // 2 << V4_OFFSET |
| 117 | static constexpr uint64_tcxx . . $ uint64_t# VARINT_1_MAXcxx . . $ mlspp/tls/VARINT_1_MAX. = 0x3f; |
| 118 | static constexpr uint64_tcxx . . $ uint64_t# VARINT_2_MAXcxx . . $ mlspp/tls/VARINT_2_MAX. = 0x3fff; |
| 119 | static constexpr uint64_tcxx . . $ uint64_t# VARINT_4_MAXcxx . . $ mlspp/tls/VARINT_4_MAX. = 0x3fffffff; |
| 120 | |
| 121 | ostreamcxx . . $ mlspp/tls/ostream#& |
| 122 | varintcxx . . $ mlspp/tls/varint#::encodecxx . . $ mlspp/tls/varint#encode(78a8a3c64c29bc39).(ostreamcxx . . $ mlspp/tls/ostream#& str, const uint64_tcxx . . $ uint64_t#& val) |
| 123 | { |
| 124 | if (val <= VARINT_1_MAXcxx . . $ mlspp/tls/VARINT_1_MAX.) { |
| 125 | return str.write_uintcxx . . $ mlspp/tls/ostream#write_uint(fd0940e664e3073f).(VARINT_1_HEADERcxx . . $ mlspp/tls/VARINT_1_HEADER. | val, 1); |
| 126 | } |
| 127 | |
| 128 | if (val <= VARINT_2_MAXcxx . . $ mlspp/tls/VARINT_2_MAX.) { |
| 129 | return str.write_uintcxx . . $ mlspp/tls/ostream#write_uint(fd0940e664e3073f).(VARINT_2_HEADERcxx . . $ mlspp/tls/VARINT_2_HEADER. | val, 2); |
| 130 | } |
| 131 | |
| 132 | if (val <= VARINT_4_MAXcxx . . $ mlspp/tls/VARINT_4_MAX.) { |
| 133 | return str.write_uintcxx . . $ mlspp/tls/ostream#write_uint(fd0940e664e3073f).(VARINT_4_HEADERcxx . . $ mlspp/tls/VARINT_4_HEADER. | val, 4); |
| 134 | } |
| 135 | |
| 136 | throw WriteErrorcxx . . $ mlspp/tls/WriteError#("Varint value exceeds maximum size"); |
| 137 | } |
| 138 | |
| 139 | istreamcxx . . $ mlspp/tls/istream#& |
| 140 | varintcxx . . $ mlspp/tls/varint#::decodecxx . . $ mlspp/tls/varint#decode(6bd9e578f60e7a6c).(istreamcxx . . $ mlspp/tls/istream#& str, uint64_tcxx . . $ uint64_t#& val) |
| 141 | { |
| 142 | auto log_size = size_t(str._buffercxx . . $ mlspp/tls/istream#_buffer..backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).() >> VARINT_HEADER_OFFSETcxx . . $ mlspp/tls/VARINT_HEADER_OFFSET.); |
| 143 | if (log_size > 2) { |
| 144 | throw ReadErrorcxx . . $ mlspp/tls/ReadError#("Malformed varint header"); |
| 145 | } |
| 146 | |
| 147 | auto read = uint64_tcxx . . $ uint64_t#(0); |
| 148 | auto read_bytes = size_t(size_t(1) << log_size); |
| 149 | str.read_uintcxx . . $ mlspp/tls/istream#read_uint(99a67fe8c4e4f583).(read, read_bytes); |
| 150 | |
| 151 | switch (log_size) { |
| 152 | case 0: |
| 153 | read ^= VARINT_1_HEADERcxx . . $ mlspp/tls/VARINT_1_HEADER.; |
| 154 | break; |
| 155 | |
| 156 | case 1: |
| 157 | read ^= VARINT_2_HEADERcxx . . $ mlspp/tls/VARINT_2_HEADER.; |
| 158 | if (read <= VARINT_1_MAXcxx . . $ mlspp/tls/VARINT_1_MAX.) { |
| 159 | throw ReadErrorcxx . . $ mlspp/tls/ReadError#("Non-minimal varint"); |
| 160 | } |
| 161 | break; |
| 162 | |
| 163 | case 2: |
| 164 | read ^= VARINT_4_HEADERcxx . . $ mlspp/tls/VARINT_4_HEADER.; |
| 165 | if (read <= VARINT_2_MAXcxx . . $ mlspp/tls/VARINT_2_MAX.) { |
| 166 | throw ReadErrorcxx . . $ mlspp/tls/ReadError#("Non-minimal varint"); |
| 167 | } |
| 168 | break; |
| 169 | |
| 170 | default: |
| 171 | throw ReadErrorcxx . . $ mlspp/tls/ReadError#("Malformed varint header"); |
| 172 | } |
| 173 | |
| 174 | val = read; |
| 175 | return str; |
| 176 | } |
| 177 | |
| 178 | } // namespace mlspp::tls |