| … | … | @@ -2,7 +2,27 @@ |
| 2 | 2 | #define HYPERSWARM_COMPACT_ENCODING_H |
| 3 | 3 | |
| 4 | 4 | // Compact encoding codec — wire-compatible with holepunchto/compact-encoding. |
| 5 | | // Header-only. All multi-byte integers are little-endian. |
| 5 | // All multi-byte integers are little-endian. |
| 6 | // |
| 7 | // ⛔ THE BYTES ARE NOT WRITTEN HERE ANY MORE. Every primitive below forwards to |
| 8 | // libcompact, which is holepunch's own C port of compact-encoding and is what |
| 9 | // swarmd's generated wire already links against. Two codecs for one format in |
| 10 | // one repo is one codec too many: the unannounce signable in |
| 11 | // docs/PARITY-hyperswarm.md §7 was written twice and diverged, and that is the |
| 12 | // same failure at a level up. |
| 13 | // |
| 14 | // What remains here is what libcompact does NOT have and hyperswarm DOES: the |
| 15 | // ipv4Address pair from compact-encoding-net, the BLAKE2b node id from |
| 16 | // dht-rpc/lib/peer.js, and the Kademlia XOR helpers. |
| 17 | // |
| 18 | // The two contracts this wrapper keeps, and libcompact does not state: |
| 19 | // |
| 20 | // * decoders return a VALUE, not an error code, because every call site here |
| 21 | // is written that way |
| 22 | // * a truncated decode yields 0 / null AND leaves start == end, so a loop |
| 23 | // over a malformed frame terminates. libcompact returns -1 and leaves |
| 24 | // `start` wherever it stopped; a caller looping `while (start < end)` on |
| 25 | // that would spin. test/test_compact_encoding.cpp pins all of it. |
| 6 | 26 | // |
| 7 | 27 | // Reference: holepunchto/compact-encoding/index.js |
| 8 | 28 | // holepunchto/compact-encoding-net/index.js |
| … | … | @@ -14,13 +34,15 @@ |
| 14 | 34 | |
| 15 | 35 | #include <sodium.h> |
| 16 | 36 | |
| 37 | extern "C" { |
| 38 | #include <compact.h> |
| 39 | } |
| 40 | |
| 17 | 41 | // ---- Data structures ---- |
| 18 | 42 | |
| 19 | | struct EncodeState { |
| 20 | | size_t start; |
| 21 | | size_t end; |
| 22 | | uint8_t *buffer; // May be null during preencode pass. |
| 23 | | }; |
| 43 | // Layout-identical to what it replaced: { start, end, buffer }. Aggregate |
| 44 | // initialisation at the call sites is unchanged. |
| 45 | typedef compact_state_t EncodeState; |
| 24 | 46 | |
| 25 | 47 | struct IPv4Address { |
| 26 | 48 | uint8_t ip[4]; // Network byte order (big-endian octets). |
| … | … | @@ -42,210 +64,126 @@ namespace cenc { |
| 42 | 64 | |
| 43 | 65 | // ---- Variable-width unsigned integer (varint) ---- |
| 44 | 66 | |
| 45 | | inline void uint_preencode(EncodeState &s, uint64_t n) { |
| 46 | | if (n <= 0xfc) { |
| 47 | | s.end += 1; |
| 48 | | } else if (n <= 0xffff) { |
| 49 | | s.end += 3; |
| 50 | | } else if (n <= 0xffffffff) { |
| 51 | | s.end += 5; |
| 52 | | } else { |
| 53 | | s.end += 9; |
| 54 | | } |
| 55 | | } |
| 67 | // A decode that ran off the end. libcompact tells us with -1; our callers read |
| 68 | // a value, so give them the harmless one and make sure the frame is finished. |
| 69 | inline void _truncated(EncodeState &s) { s.start = s.end; } |
| 56 | 70 | |
| 57 | | inline void uint_encode(EncodeState &s, uint64_t n) { |
| 58 | | if (n <= 0xfc) { |
| 59 | | s.buffer[s.start++] = static_cast<uint8_t>(n); |
| 60 | | } else if (n <= 0xffff) { |
| 61 | | s.buffer[s.start++] = 0xfd; |
| 62 | | s.buffer[s.start++] = static_cast<uint8_t>(n & 0xff); |
| 63 | | s.buffer[s.start++] = static_cast<uint8_t>((n >> 8) & 0xff); |
| 64 | | } else if (n <= 0xffffffff) { |
| 65 | | s.buffer[s.start++] = 0xfe; |
| 66 | | s.buffer[s.start++] = static_cast<uint8_t>(n & 0xff); |
| 67 | | s.buffer[s.start++] = static_cast<uint8_t>((n >> 8) & 0xff); |
| 68 | | s.buffer[s.start++] = static_cast<uint8_t>((n >> 16) & 0xff); |
| 69 | | s.buffer[s.start++] = static_cast<uint8_t>((n >> 24) & 0xff); |
| 70 | | } else { |
| 71 | | s.buffer[s.start++] = 0xff; |
| 72 | | uint32_t lo = static_cast<uint32_t>(n & 0xffffffff); |
| 73 | | uint32_t hi = static_cast<uint32_t>(n >> 32); |
| 74 | | s.buffer[s.start++] = static_cast<uint8_t>(lo & 0xff); |
| 75 | | s.buffer[s.start++] = static_cast<uint8_t>((lo >> 8) & 0xff); |
| 76 | | s.buffer[s.start++] = static_cast<uint8_t>((lo >> 16) & 0xff); |
| 77 | | s.buffer[s.start++] = static_cast<uint8_t>((lo >> 24) & 0xff); |
| 78 | | s.buffer[s.start++] = static_cast<uint8_t>(hi & 0xff); |
| 79 | | s.buffer[s.start++] = static_cast<uint8_t>((hi >> 8) & 0xff); |
| 80 | | s.buffer[s.start++] = static_cast<uint8_t>((hi >> 16) & 0xff); |
| 81 | | s.buffer[s.start++] = static_cast<uint8_t>((hi >> 24) & 0xff); |
| 82 | | } |
| 83 | | } |
| 71 | inline void uint_preencode(EncodeState &s, uint64_t n) { compact_preencode_uint(&s, n); } |
| 72 | |
| 73 | inline void uint_encode(EncodeState &s, uint64_t n) { compact_encode_uint(&s, n); } |
| 84 | 74 | |
| 85 | 75 | inline uint64_t uint_decode(EncodeState &s) { |
| 86 | | if (s.start >= s.end) return 0; |
| 87 | | uint8_t a = s.buffer[s.start++]; |
| 88 | | if (a <= 0xfc) return a; |
| 89 | | if (a == 0xfd) { |
| 90 | | if (s.start + 2 > s.end) { s.start = s.end; return 0; } |
| 91 | | uint16_t v = static_cast<uint16_t>(s.buffer[s.start]) |
| 92 | | | (static_cast<uint16_t>(s.buffer[s.start + 1]) << 8); |
| 93 | | s.start += 2; |
| 94 | | return v; |
| 95 | | } |
| 96 | | if (a == 0xfe) { |
| 97 | | if (s.start + 4 > s.end) { s.start = s.end; return 0; } |
| 98 | | uint32_t v = static_cast<uint32_t>(s.buffer[s.start]) |
| 99 | | | (static_cast<uint32_t>(s.buffer[s.start + 1]) << 8) |
| 100 | | | (static_cast<uint32_t>(s.buffer[s.start + 2]) << 16) |
| 101 | | | (static_cast<uint32_t>(s.buffer[s.start + 3]) << 24); |
| 102 | | s.start += 4; |
| 103 | | return v; |
| 104 | | } |
| 105 | | // 0xff: uint64 |
| 106 | | if (s.start + 8 > s.end) { s.start = s.end; return 0; } |
| 107 | | uint32_t lo = static_cast<uint32_t>(s.buffer[s.start]) |
| 108 | | | (static_cast<uint32_t>(s.buffer[s.start + 1]) << 8) |
| 109 | | | (static_cast<uint32_t>(s.buffer[s.start + 2]) << 16) |
| 110 | | | (static_cast<uint32_t>(s.buffer[s.start + 3]) << 24); |
| 111 | | uint32_t hi = static_cast<uint32_t>(s.buffer[s.start + 4]) |
| 112 | | | (static_cast<uint32_t>(s.buffer[s.start + 5]) << 8) |
| 113 | | | (static_cast<uint32_t>(s.buffer[s.start + 6]) << 16) |
| 114 | | | (static_cast<uint32_t>(s.buffer[s.start + 7]) << 24); |
| 115 | | s.start += 8; |
| 116 | | return static_cast<uint64_t>(lo) | (static_cast<uint64_t>(hi) << 32); |
| 76 | uint64_t v = 0; |
| 77 | if (compact_decode_uint(&s, &v) < 0) { _truncated(s); return 0; } |
| 78 | return v; |
| 117 | 79 | } |
| 118 | 80 | |
| 119 | 81 | // ---- Fixed-width little-endian integers ---- |
| 82 | // |
| 83 | // The preencode helpers take no value because these widths do not depend on |
| 84 | // one; libcompact's take one and ignore it for exactly the same reason. |
| 120 | 85 | |
| 121 | | inline void uint8_encode(EncodeState &s, uint8_t n) { |
| 122 | | s.buffer[s.start++] = n; |
| 123 | | } |
| 86 | inline void uint8_encode(EncodeState &s, uint8_t n) { compact_encode_uint8(&s, n); } |
| 124 | 87 | |
| 125 | 88 | inline uint8_t uint8_decode(EncodeState &s) { |
| 126 | | if (s.start >= s.end) return 0; |
| 127 | | return s.buffer[s.start++]; |
| 89 | uint8_t v = 0; |
| 90 | if (compact_decode_uint8(&s, &v) < 0) { _truncated(s); return 0; } |
| 91 | return v; |
| 128 | 92 | } |
| 129 | 93 | |
| 130 | | inline void uint16_preencode(EncodeState &s) { |
| 131 | | s.end += 2; |
| 132 | | } |
| 94 | inline void uint16_preencode(EncodeState &s) { compact_preencode_uint16(&s, 0); } |
| 133 | 95 | |
| 134 | | inline void uint16_encode(EncodeState &s, uint16_t n) { |
| 135 | | s.buffer[s.start++] = static_cast<uint8_t>(n & 0xff); |
| 136 | | s.buffer[s.start++] = static_cast<uint8_t>((n >> 8) & 0xff); |
| 137 | | } |
| 96 | inline void uint16_encode(EncodeState &s, uint16_t n) { compact_encode_uint16(&s, n); } |
| 138 | 97 | |
| 139 | 98 | inline uint16_t uint16_decode(EncodeState &s) { |
| 140 | | if (s.start + 2 > s.end) { s.start = s.end; return 0; } |
| 141 | | uint16_t v = static_cast<uint16_t>(s.buffer[s.start]) |
| 142 | | | (static_cast<uint16_t>(s.buffer[s.start + 1]) << 8); |
| 143 | | s.start += 2; |
| 99 | uint16_t v = 0; |
| 100 | if (compact_decode_uint16(&s, &v) < 0) { _truncated(s); return 0; } |
| 144 | 101 | return v; |
| 145 | 102 | } |
| 146 | 103 | |
| 147 | | inline void uint32_preencode(EncodeState &s) { |
| 148 | | s.end += 4; |
| 149 | | } |
| 104 | inline void uint32_preencode(EncodeState &s) { compact_preencode_uint32(&s, 0); } |
| 150 | 105 | |
| 151 | | inline void uint32_encode(EncodeState &s, uint32_t n) { |
| 152 | | s.buffer[s.start++] = static_cast<uint8_t>(n & 0xff); |
| 153 | | s.buffer[s.start++] = static_cast<uint8_t>((n >> 8) & 0xff); |
| 154 | | s.buffer[s.start++] = static_cast<uint8_t>((n >> 16) & 0xff); |
| 155 | | s.buffer[s.start++] = static_cast<uint8_t>((n >> 24) & 0xff); |
| 156 | | } |
| 106 | inline void uint32_encode(EncodeState &s, uint32_t n) { compact_encode_uint32(&s, n); } |
| 157 | 107 | |
| 158 | 108 | inline uint32_t uint32_decode(EncodeState &s) { |
| 159 | | if (s.start + 4 > s.end) { s.start = s.end; return 0; } |
| 160 | | uint32_t v = static_cast<uint32_t>(s.buffer[s.start]) |
| 161 | | | (static_cast<uint32_t>(s.buffer[s.start + 1]) << 8) |
| 162 | | | (static_cast<uint32_t>(s.buffer[s.start + 2]) << 16) |
| 163 | | | (static_cast<uint32_t>(s.buffer[s.start + 3]) << 24); |
| 164 | | s.start += 4; |
| 109 | uint32_t v = 0; |
| 110 | if (compact_decode_uint32(&s, &v) < 0) { _truncated(s); return 0; } |
| 165 | 111 | return v; |
| 166 | 112 | } |
| 167 | 113 | |
| 168 | | inline void uint64_preencode(EncodeState &s) { |
| 169 | | s.end += 8; |
| 170 | | } |
| 114 | inline void uint64_preencode(EncodeState &s) { compact_preencode_uint64(&s, 0); } |
| 171 | 115 | |
| 172 | | inline void uint64_encode(EncodeState &s, uint64_t n) { |
| 173 | | uint32_t lo = static_cast<uint32_t>(n & 0xffffffff); |
| 174 | | uint32_t hi = static_cast<uint32_t>(n >> 32); |
| 175 | | uint32_encode(s, lo); |
| 176 | | uint32_encode(s, hi); |
| 177 | | } |
| 116 | inline void uint64_encode(EncodeState &s, uint64_t n) { compact_encode_uint64(&s, n); } |
| 178 | 117 | |
| 118 | // ⛔ ONE BYTE-LEVEL BEHAVIOUR CHANGED HERE, deliberately. This used to be two |
| 119 | // uint32 decodes, so four remaining bytes yielded the LOW WORD of a number that |
| 120 | // was never on the wire. It is one decode now and a short read yields 0, which |
| 121 | // is the contract the other decoders already kept. |
| 179 | 122 | inline uint64_t uint64_decode(EncodeState &s) { |
| 180 | | uint32_t lo = uint32_decode(s); |
| 181 | | uint32_t hi = uint32_decode(s); |
| 182 | | return static_cast<uint64_t>(lo) | (static_cast<uint64_t>(hi) << 32); |
| 123 | uint64_t v = 0; |
| 124 | if (compact_decode_uint64(&s, &v) < 0) { _truncated(s); return 0; } |
| 125 | return v; |
| 183 | 126 | } |
| 184 | 127 | |
| 185 | 128 | // ---- Fixed-size raw buffers (no length prefix) ---- |
| 129 | // |
| 130 | // These decode ZERO-COPY — the pointer is into the caller's frame, which is |
| 131 | // what every call site expects. libcompact's fixed32/64 decoders copy into a |
| 132 | // destination, so the pointer is taken here and libcompact is asked to do the |
| 133 | // bounds check and the advance with a null destination. |
| 186 | 134 | |
| 187 | | inline void fixed32_preencode(EncodeState &s) { |
| 188 | | s.end += 32; |
| 189 | | } |
| 135 | inline void fixed32_preencode(EncodeState &s) { compact_preencode_fixed32(&s, nullptr); } |
| 190 | 136 | |
| 191 | 137 | inline void fixed32_encode(EncodeState &s, const uint8_t *data) { |
| 192 | | memcpy(s.buffer + s.start, data, 32); |
| 193 | | s.start += 32; |
| 138 | compact_encode_fixed32(&s, data); |
| 194 | 139 | } |
| 195 | 140 | |
| 196 | 141 | inline const uint8_t *fixed32_decode(EncodeState &s) { |
| 197 | | if (s.start + 32 > s.end) { s.start = s.end; return nullptr; } |
| 198 | 142 | const uint8_t *ptr = s.buffer + s.start; |
| 199 | | s.start += 32; |
| 143 | if (compact_decode_fixed32(&s, nullptr) < 0) { _truncated(s); return nullptr; } |
| 200 | 144 | return ptr; |
| 201 | 145 | } |
| 202 | 146 | |
| 203 | | inline void fixed64_preencode(EncodeState &s) { |
| 204 | | s.end += 64; |
| 205 | | } |
| 147 | inline void fixed64_preencode(EncodeState &s) { compact_preencode_fixed64(&s, nullptr); } |
| 206 | 148 | |
| 207 | 149 | inline void fixed64_encode(EncodeState &s, const uint8_t *data) { |
| 208 | | memcpy(s.buffer + s.start, data, 64); |
| 209 | | s.start += 64; |
| 150 | compact_encode_fixed64(&s, data); |
| 210 | 151 | } |
| 211 | 152 | |
| 212 | 153 | inline const uint8_t *fixed64_decode(EncodeState &s) { |
| 213 | | if (s.start + 64 > s.end) { s.start = s.end; return nullptr; } |
| 214 | 154 | const uint8_t *ptr = s.buffer + s.start; |
| 215 | | s.start += 64; |
| 155 | if (compact_decode_fixed64(&s, nullptr) < 0) { _truncated(s); return nullptr; } |
| 216 | 156 | return ptr; |
| 217 | 157 | } |
| 218 | 158 | |
| 219 | 159 | // ---- Length-prefixed buffer ---- |
| 160 | // |
| 161 | // compact-encoding's `buffer` is uint8array with one extra rule this codebase |
| 162 | // relies on: a null buffer is a single 0x00, and it decodes back to null rather |
| 163 | // than to a zero-length view. libcompact has no null, so that rule stays here. |
| 220 | 164 | |
| 221 | 165 | inline void buffer_preencode(EncodeState &s, const uint8_t *data, size_t len) { |
| 222 | 166 | if (data == nullptr) { |
| 223 | | s.end += 1; // null encoded as single 0x00 byte |
| 167 | s.end += 1; |
| 224 | 168 | } else { |
| 225 | | uint_preencode(s, len); |
| 226 | | s.end += len; |
| 169 | compact_preencode_uint8array(&s, data, len); |
| 227 | 170 | } |
| 228 | 171 | } |
| 229 | 172 | |
| 230 | 173 | inline void buffer_encode(EncodeState &s, const uint8_t *data, size_t len) { |
| 231 | 174 | if (data == nullptr) { |
| 232 | | s.buffer[s.start++] = 0; |
| 175 | compact_encode_uint8(&s, 0); |
| 233 | 176 | } else { |
| 234 | | uint_encode(s, len); |
| 235 | | if (len > 0) { |
| 236 | | memcpy(s.buffer + s.start, data, len); |
| 237 | | s.start += len; |
| 238 | | } |
| 177 | compact_encode_uint8array(&s, data, len); |
| 239 | 178 | } |
| 240 | 179 | } |
| 241 | 180 | |
| 242 | 181 | inline BufView buffer_decode(EncodeState &s) { |
| 243 | | uint64_t len = uint_decode(s); |
| 182 | uint8_t *ptr = nullptr; |
| 183 | size_t len = 0; |
| 184 | if (compact_decode_uint8array(&s, &ptr, &len) < 0) { _truncated(s); return { nullptr, 0 }; } |
| 244 | 185 | if (len == 0) return { nullptr, 0 }; |
| 245 | | if (s.start + len > s.end) { s.start = s.end; return { nullptr, 0 }; } |
| 246 | | const uint8_t *ptr = s.buffer + s.start; |
| 247 | | s.start += static_cast<size_t>(len); |
| 248 | | return { ptr, static_cast<size_t>(len) }; |
| 186 | return { ptr, len }; |
| 249 | 187 | } |
| 250 | 188 | |
| 251 | 189 | // ---- IPv4 Address (6 bytes: 4 IP octets + 2 port LE) ---- |