omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / synapse / syn_codec.osc
| 1 | // GENERATED by src/synapse/syn_gen.sh from syn_layout.lst and syn_codec.osc.in. Do not edit; edit the .in or the list. |
| 2 | // Synapse binary codec v2 in Omega (aien-dev/synapse ADR 0003 section 9 step 33, J1). |
| 3 | // Entry: syn_codec(inp, w). Specification: synapse docs/J1_OMEGA_CODEC.md and the Rust codec |
| 4 | // crates/synapse-store/src/codec.rs (Reader, decode_record, CheckpointState::decode_content) with the |
| 5 | // synapse-core canonical encodings it embeds (crates/synapse-core/src/revision.rs). The shared corpus |
| 6 | // tests/synapse/j1-corpus/corpus.bin pins both directions. |
| 7 | // |
| 8 | // Two entries (W_ENTRY): 1 one WAL record (`CODEC_V2 | kind | body`, kinds 1..8; 9..11 are reserved by open |
| 9 | // synapse PRs and refused as an unknown record kind today), 2 the content of a binary checkpoint (the five |
| 10 | // maps and the optional revision section). Two modes (W_MODE): |
| 11 | // 1 decode: inp holds the bytes. Accepted bytes are written as a logical form (LF) into the LF region and |
| 12 | // W_LF_N holds its length. Refused bytes leave W_ERR = the CodecRefusal kind (1..9, declaration order) |
| 13 | // with W_ERR_AT / W_ERR_X / W_ERR_Y / W_ERR_FIELD as the corpus records them: Truncated (at, need), |
| 14 | // LengthOverCap (at, declared, cap), TrailingBytes (at, count), UnknownTag (at, tag, field), InvalidUtf8 |
| 15 | // (at), VersionUnsupported (found), NotCanonical (at, field), InvalidRepoId (at), EmbeddedMalformed |
| 16 | // (at, code: 1 EnvelopeMalformed 2 EnvelopeVersionUnsupported, field). Offsets are relative to the record |
| 17 | // being read (a revision record inside a checkpoint restarts at 0, as the Rust decoder's nested Reader). |
| 18 | // 2 encode: the LF region (W_LF_N cells) holds a logical form; the bytes go to the output region, one byte |
| 19 | // per cell, W_OUT their count. A logical form that is not a value of the Rust types is refused with an |
| 20 | // encode kind (20..27); like the Rust encoder, encode does not apply the decoder's length caps. |
| 21 | // Logical form: a token is a header cell `type | len << 8` then len cells; scalars (u16, u64, bool, tag, |
| 22 | // count) have len 1, byte runs (fixed arrays, strings, repository ids, byte strings) one cell per byte. |
| 23 | // Field order is the Rust field order; version bytes and length prefixes of embedded encodings are not in it. |
| 24 | // A checkpoint's revision section is always present in the LF (count 0 when the bytes hold none). |
| 25 | // |
| 26 | // The reader rules are the Rust ones. WAL rules (W_CORE = 0): a byte run length is checked against W_CAP and |
| 27 | // then the input; a sequence count against the cap (count x minimum encoded size, count x size_of) and then |
| 28 | // the input; tags out of range are UnknownTag at the tag byte. Inside an embedded synapse-core encoding |
| 29 | // (W_CORE = 1) every failure is EnvelopeMalformed, except a revision content version other than 2 |
| 30 | // (EnvelopeVersionUnsupported); fail() turns it into EmbeddedMalformed at the embedding's length prefix. |
| 31 | // The first refusal wins: every primitive is a no-op once W_ERR is set. |
| 32 | // |
| 33 | // LIMITS (read before adding a record kind): |
| 34 | // - Functions: an OSC unit holds at most 32 functions (OSC_MAX_FUNCS, src/compiler/osc_ir.h), and this unit |
| 35 | // uses all 32 (the driver prints "unit: N of 32 functions"). Record kinds 9, 10 and 11 |
| 36 | // (reserved by open synapse PRs) must land as `if k == N` arms inside record() built from the existing |
| 37 | // helpers (fint, ftag, fcount, frun, ids, emb_open/emb_close, ...); a new helper function does not fit |
| 38 | // until one is merged away or the unit is split. |
| 39 | // - Input: inputs above INPUT_MAX (1,000,000) bytes, encode logical forms above INPUT_MAX cells, LF regions |
| 40 | // above 4 x INPUT_MAX cells and output regions above INPUT_MAX cells are refused LIMIT (loop bound LOOP_MAX). |
| 41 | // - Packing: emb_open keeps a region's end, base and prefix offset in one word, 20 bits each, so every |
| 42 | // position must stay below 2^20 = 1,048,576. syn_gen.sh refuses a layout with INPUT_MAX > MASK20, and |
| 43 | // syn_codec() also refuses LIMIT for an input or output region above MASK20. |
| 44 | // - Scope: the record layer only. Frame headers, CRCs, the SHA-256 chain and checkpoint roots are not here. |
| 45 | |
| 46 | fn fail(w: cells, kf: u64, at: u64, x: u64) { |
| 47 | if w[7] != 0 { return; } |
| 48 | let k: u64 = kf & 255; |
| 49 | if w[15] != 0 && (k < 20 || k == 98) { |
| 50 | w[7] = 9; |
| 51 | w[8] = w[23] - w[14]; |
| 52 | if k == 98 { w[9] = 2; } else { w[9] = 1; } |
| 53 | w[10] = 0; |
| 54 | w[11] = w[24]; |
| 55 | return; |
| 56 | } |
| 57 | w[7] = k; |
| 58 | if k < 20 { w[8] = at - w[14]; } else { w[8] = at; } |
| 59 | w[9] = x; |
| 60 | if k == 2 { w[10] = w[5]; } else { w[10] = 0; } |
| 61 | w[11] = kf >> 8; |
| 62 | } |
| 63 | |
| 64 | // Decode: k more bytes must remain in the region, else Truncated (at the cursor, need the shortfall). |
| 65 | fn need(w: cells, k: u64) -> u64 { |
| 66 | if w[7] != 0 { return 1; } |
| 67 | let rem: u64 = w[13] - w[12]; |
| 68 | if k > rem { |
| 69 | fail(w, 1, w[12], k - rem); |
| 70 | return 1; |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | // Decode: nb little-endian bytes at the cursor (need() checked them). |
| 76 | fn rdle(inp: bytes, w: cells, nb: u64) -> u64 { |
| 77 | let p: u64 = w[12]; |
| 78 | let mut v: u64 = 0; |
| 79 | let mut i: u64 = 0; |
| 80 | while i < nb bound 8 { |
| 81 | v = v | (inp[p + i] << (i * 8)); |
| 82 | i = i + 1; |
| 83 | } |
| 84 | w[12] = p + nb; |
| 85 | return v; |
| 86 | } |
| 87 | |
| 88 | // Decode: append one LF cell. |
| 89 | fn lfput(w: cells, v: u64) { |
| 90 | if w[7] != 0 { return; } |
| 91 | let n: u64 = w[17]; |
| 92 | if n >= w[20] { |
| 93 | fail(w, 32, n, 0); |
| 94 | return; |
| 95 | } |
| 96 | w[w[19] + n] = v; |
| 97 | w[17] = n + 1; |
| 98 | } |
| 99 | |
| 100 | // Encode: the next LF cell. |
| 101 | fn lfget(w: cells) -> u64 { |
| 102 | if w[7] != 0 { return 0; } |
| 103 | let i: u64 = w[16]; |
| 104 | if i >= w[17] { |
| 105 | fail(w, 21, i, 0); |
| 106 | return 0; |
| 107 | } |
| 108 | w[16] = i + 1; |
| 109 | return w[w[19] + i]; |
| 110 | } |
| 111 | |
| 112 | // Encode: write v as nb little-endian bytes. |
| 113 | fn outle(w: cells, v: u64, nb: u64) { |
| 114 | let mut j: u64 = 0; |
| 115 | while j < nb && w[7] == 0 bound 8 { |
| 116 | let o: u64 = w[18]; |
| 117 | if o >= w[22] { |
| 118 | fail(w, 25, o, 0); |
| 119 | } else { |
| 120 | w[w[21] + o] = (v >> (j * 8)) & 255; |
| 121 | w[18] = o + 1; |
| 122 | } |
| 123 | j = j + 1; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Stream byte i: the input in decode, LF cell i in encode (byte runs and keys are checked there). |
| 128 | fn sb(inp: bytes, w: cells, i: u64) -> u64 { |
| 129 | if w[4] == 1 { return inp[i]; } |
| 130 | return w[w[19] + i]; |
| 131 | } |
| 132 | |
| 133 | // 1 when stream bytes [s, s + n) are UTF-8 under the Rust std::str::from_utf8 rules, else 0. |
| 134 | fn utf8ok(inp: bytes, w: cells, s: u64, n: u64) -> u64 { |
| 135 | let e: u64 = s + n; |
| 136 | let mut i: u64 = s; |
| 137 | let mut ok: u64 = 1; |
| 138 | while i < e && ok == 1 bound 1000000 { |
| 139 | let c: u64 = sb(inp, w, i); |
| 140 | if c < 128 { |
| 141 | i = i + 1; |
| 142 | } else { |
| 143 | let mut k: u64 = 0; |
| 144 | let mut lo: u64 = 128; |
| 145 | let mut hi: u64 = 191; |
| 146 | if c >= 194 && c <= 223 { |
| 147 | k = 1; |
| 148 | } else if c == 224 { |
| 149 | k = 2; |
| 150 | lo = 160; |
| 151 | } else if c >= 225 && c <= 236 { |
| 152 | k = 2; |
| 153 | } else if c == 237 { |
| 154 | k = 2; |
| 155 | hi = 159; |
| 156 | } else if c >= 238 && c <= 239 { |
| 157 | k = 2; |
| 158 | } else if c == 240 { |
| 159 | k = 3; |
| 160 | lo = 144; |
| 161 | } else if c >= 241 && c <= 243 { |
| 162 | k = 3; |
| 163 | } else if c == 244 { |
| 164 | k = 3; |
| 165 | hi = 143; |
| 166 | } |
| 167 | if k == 0 || i + k >= e { |
| 168 | ok = 0; |
| 169 | } else { |
| 170 | let c1: u64 = sb(inp, w, i + 1); |
| 171 | if c1 < lo || c1 > hi { ok = 0; } |
| 172 | if k >= 2 { |
| 173 | let c2: u64 = sb(inp, w, i + 2); |
| 174 | if c2 < 128 || c2 > 191 { ok = 0; } |
| 175 | } |
| 176 | if k == 3 { |
| 177 | let c3: u64 = sb(inp, w, i + 3); |
| 178 | if c3 < 128 || c3 > 191 { ok = 0; } |
| 179 | } |
| 180 | i = i + k + 1; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | return ok; |
| 185 | } |
| 186 | |
| 187 | // 1 when stream bytes [s, s + n) are a RepoId (synapse-core RepoId::new): 1..=128 bytes of ASCII |
| 188 | // alphanumerics and - _ . /, not starting or ending with /, no // and no .., else 0. |
| 189 | fn repook(inp: bytes, w: cells, s: u64, n: u64) -> u64 { |
| 190 | if n == 0 || n > 128 { return 0; } |
| 191 | if sb(inp, w, s) == 47 || sb(inp, w, s + n - 1) == 47 { return 0; } |
| 192 | let mut i: u64 = 0; |
| 193 | let mut prev: u64 = 0; |
| 194 | let mut ok: u64 = 1; |
| 195 | while i < n && ok == 1 bound 128 { |
| 196 | let c: u64 = sb(inp, w, s + i); |
| 197 | let mut alnum: u64 = 0; |
| 198 | if (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) { alnum = 1; } |
| 199 | if alnum == 0 && c != 45 && c != 95 && c != 46 && c != 47 { ok = 0; } |
| 200 | if (c == 47 || c == 46) && prev == c { ok = 0; } |
| 201 | prev = c; |
| 202 | i = i + 1; |
| 203 | } |
| 204 | return ok; |
| 205 | } |
| 206 | |
| 207 | // A little-endian integer field (desc = type | byte count << 8). Returns its value. |
| 208 | fn fint(inp: bytes, w: cells, d: u64) -> u64 { |
| 209 | if w[7] != 0 { return 0; } |
| 210 | let t: u64 = d & 255; |
| 211 | let nb: u64 = d >> 8; |
| 212 | if w[4] == 1 { |
| 213 | if need(w, nb) != 0 { return 0; } |
| 214 | let v: u64 = rdle(inp, w, nb); |
| 215 | lfput(w, t + 256); |
| 216 | lfput(w, v); |
| 217 | return v; |
| 218 | } |
| 219 | let h: u64 = lfget(w); |
| 220 | let ev: u64 = lfget(w); |
| 221 | if w[7] != 0 { return 0; } |
| 222 | if h != t + 256 { |
| 223 | fail(w, 20, w[16] - 2, h); |
| 224 | return 0; |
| 225 | } |
| 226 | if nb < 8 && (ev >> (nb * 8)) != 0 { |
| 227 | fail(w, 22, w[16] - 1, ev); |
| 228 | return 0; |
| 229 | } |
| 230 | outle(w, ev, nb); |
| 231 | return ev; |
| 232 | } |
| 233 | |
| 234 | // A one-byte tag (desc = field | lo << 8 | hi << 16 | LF type << 24): outside lo..=hi is UnknownTag at the |
| 235 | // tag byte (decode) or LF_VALUE (encode). Returns the tag. |
| 236 | fn ftag(inp: bytes, w: cells, d: u64) -> u64 { |
| 237 | if w[7] != 0 { return 0; } |
| 238 | let f: u64 = d & 255; |
| 239 | let lo: u64 = (d >> 8) & 255; |
| 240 | let hi: u64 = (d >> 16) & 255; |
| 241 | let t: u64 = d >> 24; |
| 242 | if w[4] == 1 { |
| 243 | let at: u64 = w[12]; |
| 244 | if need(w, 1) != 0 { return 0; } |
| 245 | let v: u64 = rdle(inp, w, 1); |
| 246 | if v < lo || v > hi { |
| 247 | fail(w, 4 | (f << 8), at, v); |
| 248 | return 0; |
| 249 | } |
| 250 | lfput(w, t + 256); |
| 251 | lfput(w, v); |
| 252 | return v; |
| 253 | } |
| 254 | let h: u64 = lfget(w); |
| 255 | let ev: u64 = lfget(w); |
| 256 | if w[7] != 0 { return 0; } |
| 257 | if h != t + 256 { |
| 258 | fail(w, 20, w[16] - 2, h); |
| 259 | return 0; |
| 260 | } |
| 261 | if ev < lo || ev > hi { |
| 262 | fail(w, 22, w[16] - 1, ev); |
| 263 | return 0; |
| 264 | } |
| 265 | outle(w, ev, 1); |
| 266 | return ev; |
| 267 | } |
| 268 | |
| 269 | // A u32 sequence count. WAL rules: min = minimum encoded item size, t2 = size_of the item |
| 270 | // (Reader::vec). synapse-core rules: min as above, t2 = the count cap (0: none) (Reader::count). |
| 271 | // Returns the count; every caller's loop is bounded by it. |
| 272 | fn fcount(inp: bytes, w: cells, min: u64, t2: u64) -> u64 { |
| 273 | if w[7] != 0 { return 0; } |
| 274 | if w[4] == 1 { |
| 275 | let at: u64 = w[12]; |
| 276 | if need(w, 4) != 0 { return 0; } |
| 277 | let n: u64 = rdle(inp, w, 4); |
| 278 | let rem: u64 = w[13] - w[12]; |
| 279 | let nd: u64 = n * min; |
| 280 | if w[15] == 0 { |
| 281 | if nd > w[5] || n * t2 > w[5] { |
| 282 | fail(w, 2, at, n); |
| 283 | return 0; |
| 284 | } |
| 285 | if nd > rem { |
| 286 | fail(w, 1, w[12], nd - rem); |
| 287 | return 0; |
| 288 | } |
| 289 | } else { |
| 290 | if (t2 != 0 && n > t2) || nd > rem { |
| 291 | fail(w, 1, w[12], 0); |
| 292 | return 0; |
| 293 | } |
| 294 | } |
| 295 | lfput(w, 267); |
| 296 | lfput(w, n); |
| 297 | return n; |
| 298 | } |
| 299 | let h: u64 = lfget(w); |
| 300 | let en: u64 = lfget(w); |
| 301 | if w[7] != 0 { return 0; } |
| 302 | if h != 267 { |
| 303 | fail(w, 20, w[16] - 2, h); |
| 304 | return 0; |
| 305 | } |
| 306 | if en > w[17] - w[16] { |
| 307 | fail(w, 21, w[16] - 1, en); |
| 308 | return 0; |
| 309 | } |
| 310 | outle(w, en, 4); |
| 311 | return en; |
| 312 | } |
| 313 | |
| 314 | // A byte run (desc = type | arg << 8): T_FIX has arg bytes and no prefix; T_STR, T_REPO and T_BYTES have |
| 315 | // a u32 length (WAL: capped by W_CAP; synapse-core: capped by arg). Strings must be UTF-8, repository ids |
| 316 | // a RepoId (InvalidRepoId at the length prefix). Returns the byte count. |
| 317 | fn frun(inp: bytes, w: cells, d: u64) -> u64 { |
| 318 | if w[7] != 0 { return 0; } |
| 319 | let t: u64 = d & 255; |
| 320 | let arg: u64 = d >> 8; |
| 321 | if w[4] == 1 { |
| 322 | let at: u64 = w[12]; |
| 323 | let mut n: u64 = arg; |
| 324 | if t != 7 { |
| 325 | if need(w, 4) != 0 { return 0; } |
| 326 | n = rdle(inp, w, 4); |
| 327 | let rem: u64 = w[13] - w[12]; |
| 328 | if w[15] == 0 { |
| 329 | if n > w[5] { |
| 330 | fail(w, 2, at, n); |
| 331 | return 0; |
| 332 | } |
| 333 | if n > rem { |
| 334 | fail(w, 1, w[12], n - rem); |
| 335 | return 0; |
| 336 | } |
| 337 | } else { |
| 338 | if n > arg || n > rem { |
| 339 | fail(w, 1, w[12], 0); |
| 340 | return 0; |
| 341 | } |
| 342 | } |
| 343 | } else { |
| 344 | if need(w, n) != 0 { return 0; } |
| 345 | } |
| 346 | let s: u64 = w[12]; |
| 347 | if t == 8 || t == 10 { |
| 348 | if utf8ok(inp, w, s, n) == 0 { |
| 349 | fail(w, 5, s, 0); |
| 350 | return 0; |
| 351 | } |
| 352 | if t == 10 && repook(inp, w, s, n) == 0 { |
| 353 | fail(w, 8, at, 0); |
| 354 | return 0; |
| 355 | } |
| 356 | } |
| 357 | lfput(w, t | (n << 8)); |
| 358 | let mut i: u64 = 0; |
| 359 | while i < n && w[7] == 0 bound 1000000 { |
| 360 | lfput(w, inp[s + i]); |
| 361 | i = i + 1; |
| 362 | } |
| 363 | w[12] = s + n; |
| 364 | return n; |
| 365 | } |
| 366 | let h: u64 = lfget(w); |
| 367 | if w[7] != 0 { return 0; } |
| 368 | let en: u64 = h >> 8; |
| 369 | let s2: u64 = w[16]; |
| 370 | if (h & 255) != t || (t == 7 && en != arg) { |
| 371 | fail(w, 20, s2 - 1, h); |
| 372 | return 0; |
| 373 | } |
| 374 | if en > w[17] - s2 { |
| 375 | fail(w, 21, s2 - 1, en); |
| 376 | return 0; |
| 377 | } |
| 378 | let b: u64 = w[19] + s2; |
| 379 | let mut j: u64 = 0; |
| 380 | while j < en && w[7] == 0 bound 1000000 { |
| 381 | if w[b + j] > 255 { fail(w, 22, s2 + j, w[b + j]); } |
| 382 | j = j + 1; |
| 383 | } |
| 384 | if w[7] != 0 { return 0; } |
| 385 | if t == 8 || t == 10 { |
| 386 | if utf8ok(inp, w, s2, en) == 0 { |
| 387 | fail(w, 23, s2 - 1, 0); |
| 388 | return 0; |
| 389 | } |
| 390 | if t == 10 && repook(inp, w, s2, en) == 0 { |
| 391 | fail(w, 24, s2 - 1, 0); |
| 392 | return 0; |
| 393 | } |
| 394 | } |
| 395 | if t != 7 { outle(w, en, 4); } |
| 396 | let mut q: u64 = 0; |
| 397 | while q < en && w[7] == 0 bound 1000000 { |
| 398 | outle(w, w[b + q], 1); |
| 399 | q = q + 1; |
| 400 | } |
| 401 | w[16] = s2 + en; |
| 402 | return en; |
| 403 | } |
| 404 | |
| 405 | // Open a length-prefixed region. f = a field id: an embedded synapse-core encoding (WAL length rules for |
| 406 | // the prefix, synapse-core rules inside); EMB_CORE: the inner content length of an envelope v2 |
| 407 | // (synapse-core rules); EMB_REV: a checkpoint revision record (WAL rules; offsets restart at its start). |
| 408 | // Returns the save word for emb_close: old end | old base << 20 | prefix offset << 40. |
| 409 | fn emb_open(inp: bytes, w: cells, f: u64) -> u64 { |
| 410 | if w[7] != 0 { return 0; } |
| 411 | if w[4] == 2 { |
| 412 | let o: u64 = w[18]; |
| 413 | outle(w, 0, 4); |
| 414 | return o << 40; |
| 415 | } |
| 416 | let at: u64 = w[12]; |
| 417 | if need(w, 4) != 0 { return 0; } |
| 418 | let n: u64 = rdle(inp, w, 4); |
| 419 | let rem: u64 = w[13] - w[12]; |
| 420 | if f == 0 { |
| 421 | if n > rem { |
| 422 | fail(w, 1, w[12], 0); |
| 423 | return 0; |
| 424 | } |
| 425 | } else { |
| 426 | if n > w[5] { |
| 427 | fail(w, 2, at, n); |
| 428 | return 0; |
| 429 | } |
| 430 | if n > rem { |
| 431 | fail(w, 1, w[12], n - rem); |
| 432 | return 0; |
| 433 | } |
| 434 | } |
| 435 | let save: u64 = w[13] | (w[14] << 20) | (at << 40); |
| 436 | w[13] = w[12] + n; |
| 437 | if f == 255 { |
| 438 | w[14] = w[12]; |
| 439 | } else if f != 0 { |
| 440 | w[15] = 1; |
| 441 | w[23] = at; |
| 442 | w[24] = f; |
| 443 | } |
| 444 | return save; |
| 445 | } |
| 446 | |
| 447 | // Close the region emb_open opened. Decode: the region must be used up (synapse-core finish; a revision |
| 448 | // record checks its own end), then end and base come back. Encode: the length prefix is written. |
| 449 | fn emb_close(w: cells, save: u64, f: u64) { |
| 450 | if w[7] != 0 { return; } |
| 451 | if w[4] == 1 { |
| 452 | if f != 255 && w[12] != w[13] { |
| 453 | fail(w, 3, w[12], w[13] - w[12]); |
| 454 | return; |
| 455 | } |
| 456 | w[13] = save & 1048575; |
| 457 | w[14] = (save >> 20) & 1048575; |
| 458 | if f != 0 && f != 255 { w[15] = 0; } |
| 459 | return; |
| 460 | } |
| 461 | let at: u64 = save >> 40; |
| 462 | let n: u64 = w[18] - at - 4; |
| 463 | let b: u64 = w[21] + at; |
| 464 | w[b] = n & 255; |
| 465 | w[b + 1] = (n >> 8) & 255; |
| 466 | w[b + 2] = (n >> 16) & 255; |
| 467 | w[b + 3] = (n >> 24) & 255; |
| 468 | } |
| 469 | |
| 470 | // A count then that many 32-byte ids (fcount rules for min and t2). |
| 471 | fn ids(inp: bytes, w: cells, min: u64, t2: u64) { |
| 472 | let n: u64 = fcount(inp, w, min, t2); |
| 473 | let mut i: u64 = 0; |
| 474 | while i < n && w[7] == 0 bound 1000000 { |
| 475 | frun(inp, w, 8199); |
| 476 | i = i + 1; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | fn attachment(inp: bytes, w: cells) { |
| 481 | frun(inp, w, 8); |
| 482 | ftag(inp, w, 83951617); |
| 483 | frun(inp, w, 8199); |
| 484 | frun(inp, w, 8); |
| 485 | } |
| 486 | |
| 487 | fn attachments(inp: bytes, w: cells) { |
| 488 | let n: u64 = fcount(inp, w, 41, 88); |
| 489 | let mut i: u64 = 0; |
| 490 | while i < n && w[7] == 0 bound 1000000 { |
| 491 | attachment(inp, w); |
| 492 | i = i + 1; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // One path mutation: path, op tag (1 upsert: mode tag, blob digest, size; 2 delete). The same layout in the |
| 497 | // v1 envelope (WAL rules) and in revision content (synapse-core rules, path cap 2048). |
| 498 | fn mutation(inp: bytes, w: cells) { |
| 499 | frun(inp, w, 524296); |
| 500 | let op: u64 = ftag(inp, w, 100794627); |
| 501 | if op == 1 { |
| 502 | ftag(inp, w, 100860164); |
| 503 | frun(inp, w, 8199); |
| 504 | fint(inp, w, 2052); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // A mutation list: count (min, t2 as fcount), then the mutations. |
| 509 | fn mutations(inp: bytes, w: cells, t2: u64) { |
| 510 | let n: u64 = fcount(inp, w, 5, t2); |
| 511 | let mut i: u64 = 0; |
| 512 | while i < n && w[7] == 0 bound 1000000 { |
| 513 | mutation(inp, w); |
| 514 | i = i + 1; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | // ChangesetEnvelope v1 (get_envelope). |
| 519 | fn envelope(inp: bytes, w: cells) { |
| 520 | fint(inp, w, 514); |
| 521 | frun(inp, w, 32778); |
| 522 | frun(inp, w, 5127); |
| 523 | ids(inp, w, 32, 32); |
| 524 | frun(inp, w, 8); |
| 525 | frun(inp, w, 8199); |
| 526 | fint(inp, w, 2052); |
| 527 | frun(inp, w, 8); |
| 528 | mutations(inp, w, 72); |
| 529 | attachments(inp, w); |
| 530 | frun(inp, w, 9); |
| 531 | } |
| 532 | |
| 533 | fn review(inp: bytes, w: cells) { |
| 534 | frun(inp, w, 8); |
| 535 | ftag(inp, w, 83951617); |
| 536 | frun(inp, w, 8); |
| 537 | fint(inp, w, 2052); |
| 538 | } |
| 539 | |
| 540 | fn projection(inp: bytes, w: cells) { |
| 541 | frun(inp, w, 32778); |
| 542 | fint(inp, w, 2052); |
| 543 | frun(inp, w, 8199); |
| 544 | frun(inp, w, 5127); |
| 545 | frun(inp, w, 5127); |
| 546 | frun(inp, w, 8); |
| 547 | frun(inp, w, 8); |
| 548 | frun(inp, w, 8); |
| 549 | frun(inp, w, 8199); |
| 550 | ftag(inp, w, 83951617); |
| 551 | } |
| 552 | |
| 553 | fn canonical(inp: bytes, w: cells) { |
| 554 | frun(inp, w, 32778); |
| 555 | frun(inp, w, 8); |
| 556 | frun(inp, w, 5127); |
| 557 | let opt: u64 = ftag(inp, w, 100728834); |
| 558 | if opt == 1 { frun(inp, w, 8199); } |
| 559 | frun(inp, w, 5127); |
| 560 | fint(inp, w, 2052); |
| 561 | let n: u64 = fcount(inp, w, 4, 24); |
| 562 | let mut i: u64 = 0; |
| 563 | while i < n && w[7] == 0 bound 1000000 { |
| 564 | frun(inp, w, 8); |
| 565 | i = i + 1; |
| 566 | } |
| 567 | fint(inp, w, 2052); |
| 568 | } |
| 569 | |
| 570 | // StoredChangeset (get_stored). |
| 571 | fn stored(inp: bytes, w: cells) { |
| 572 | frun(inp, w, 8199); |
| 573 | fint(inp, w, 2052); |
| 574 | envelope(inp, w); |
| 575 | attachments(inp, w); |
| 576 | let n: u64 = fcount(inp, w, 17, 64); |
| 577 | let mut i: u64 = 0; |
| 578 | while i < n && w[7] == 0 bound 1000000 { |
| 579 | review(inp, w); |
| 580 | i = i + 1; |
| 581 | } |
| 582 | fint(inp, w, 2052); |
| 583 | } |
| 584 | |
| 585 | // Embedded ChangesetEnvelopeV2: u32 content length, content (u16 version 2, change id, base tree, parents, |
| 586 | // mutations, intent), then the authorship attestation. |
| 587 | fn env2(inp: bytes, w: cells) { |
| 588 | let sv: u64 = emb_open(inp, w, 6); |
| 589 | let sc: u64 = emb_open(inp, w, 0); |
| 590 | if w[4] == 1 { |
| 591 | if need(w, 2) == 0 { |
| 592 | let ver: u64 = rdle(inp, w, 2); |
| 593 | if ver != 2 { fail(w, 98, 0, ver); } |
| 594 | } |
| 595 | } else { |
| 596 | outle(w, 2, 2); |
| 597 | } |
| 598 | frun(inp, w, 4103); |
| 599 | frun(inp, w, 8199); |
| 600 | ids(inp, w, 32, 0); |
| 601 | mutations(inp, w, 0); |
| 602 | frun(inp, w, 1048584); |
| 603 | emb_close(w, sc, 0); |
| 604 | frun(inp, w, 32778); |
| 605 | frun(inp, w, 8199); |
| 606 | frun(inp, w, 8199); |
| 607 | frun(inp, w, 8199); |
| 608 | fint(inp, w, 2052); |
| 609 | frun(inp, w, 16391); |
| 610 | emb_close(w, sv, 6); |
| 611 | } |
| 612 | |
| 613 | // Embedded ChainPath: u8 depth 1..=16, then that many link digests. |
| 614 | fn chain(inp: bytes, w: cells) { |
| 615 | let sv: u64 = emb_open(inp, w, 7); |
| 616 | let n: u64 = ftag(inp, w, 185598208); |
| 617 | let mut i: u64 = 0; |
| 618 | while i < n && w[7] == 0 bound 16 { |
| 619 | frun(inp, w, 8199); |
| 620 | i = i + 1; |
| 621 | } |
| 622 | emb_close(w, sv, 7); |
| 623 | } |
| 624 | |
| 625 | // Embedded ObsolescenceMarker. |
| 626 | fn marker(inp: bytes, w: cells) { |
| 627 | let sv: u64 = emb_open(inp, w, 8); |
| 628 | frun(inp, w, 32778); |
| 629 | frun(inp, w, 4103); |
| 630 | frun(inp, w, 8199); |
| 631 | ids(inp, w, 32, 256); |
| 632 | frun(inp, w, 8199); |
| 633 | fint(inp, w, 2052); |
| 634 | frun(inp, w, 16391); |
| 635 | emb_close(w, sv, 8); |
| 636 | } |
| 637 | |
| 638 | // One WAL record in [W_POS, W_END): version, kind, body, finish (decode_record). Returns the kind. |
| 639 | // Kinds 9..11 (reserved by open synapse PRs) slot in as one more `if k == N` arm and a wider D_KIND. |
| 640 | fn record(inp: bytes, w: cells) -> u64 { |
| 641 | if w[7] != 0 { return 0; } |
| 642 | if w[4] == 1 { |
| 643 | if need(w, 1) != 0 { return 0; } |
| 644 | let v: u64 = rdle(inp, w, 1); |
| 645 | if v != 2 { |
| 646 | fail(w, 6, w[14], v); |
| 647 | return 0; |
| 648 | } |
| 649 | } else { |
| 650 | outle(w, 2, 1); |
| 651 | } |
| 652 | let k: u64 = ftag(inp, w, 101187845); |
| 653 | if k == 1 { |
| 654 | envelope(inp, w); |
| 655 | frun(inp, w, 8199); |
| 656 | ids(inp, w, 32, 32); |
| 657 | } |
| 658 | if k == 2 { |
| 659 | frun(inp, w, 8199); |
| 660 | attachment(inp, w); |
| 661 | } |
| 662 | if k == 3 { |
| 663 | frun(inp, w, 8199); |
| 664 | review(inp, w); |
| 665 | } |
| 666 | if k == 4 { projection(inp, w); } |
| 667 | if k == 5 { canonical(inp, w); } |
| 668 | if k == 6 { |
| 669 | frun(inp, w, 8199); |
| 670 | fint(inp, w, 2052); |
| 671 | } |
| 672 | if k == 7 { |
| 673 | env2(inp, w); |
| 674 | chain(inp, w); |
| 675 | fint(inp, w, 2052); |
| 676 | ids(inp, w, 32, 32); |
| 677 | } |
| 678 | if k == 8 { |
| 679 | marker(inp, w); |
| 680 | chain(inp, w); |
| 681 | fint(inp, w, 2052); |
| 682 | } |
| 683 | if w[4] == 1 && w[7] == 0 && w[12] != w[13] { |
| 684 | fail(w, 3, w[12], w[13] - w[12]); |
| 685 | } |
| 686 | return k; |
| 687 | } |
| 688 | |
| 689 | // Checkpoint map order: the entry at stream offset `at` must have a key strictly above the previous entry's |
| 690 | // (kf = key kind | field << 8). Keys: K_FIX32 the first 32 bytes; K_STR the leading repository id; K_STRFIX |
| 691 | // the repository id then the 32 bytes after it; K_U64 the u64 after the leading repository id. Decode |
| 692 | // refuses NotCanonical at the entry, encode LF_ORDER. |
| 693 | fn cmpkey(inp: bytes, w: cells, kf: u64, at: u64) { |
| 694 | if w[7] != 0 { return; } |
| 695 | if w[25] == 1 { |
| 696 | let kind: u64 = kf & 255; |
| 697 | let a: u64 = w[26]; |
| 698 | let mut dec: u64 = 0; |
| 699 | if w[4] == 1 { dec = 1; } |
| 700 | let mut c: u64 = 0; |
| 701 | let mut pa: u64 = a; |
| 702 | let mut pb: u64 = at; |
| 703 | if dec == 0 { |
| 704 | pa = a + 1; |
| 705 | pb = at + 1; |
| 706 | } |
| 707 | if kind != 1 { |
| 708 | let mut na: u64 = 0; |
| 709 | let mut nb: u64 = 0; |
| 710 | if dec == 1 { |
| 711 | na = sb(inp, w, a) | (sb(inp, w, a + 1) << 8) | (sb(inp, w, a + 2) << 16) | (sb(inp, w, a + 3) << 24); |
| 712 | nb = sb(inp, w, at) | (sb(inp, w, at + 1) << 8) | (sb(inp, w, at + 2) << 16) | (sb(inp, w, at + 3) << 24); |
| 713 | pa = a + 4; |
| 714 | pb = at + 4; |
| 715 | } else { |
| 716 | na = w[w[19] + a] >> 8; |
| 717 | nb = w[w[19] + at] >> 8; |
| 718 | } |
| 719 | if kind == 4 { |
| 720 | let mut va: u64 = 0; |
| 721 | let mut vb: u64 = 0; |
| 722 | if dec == 1 { |
| 723 | let mut i: u64 = 0; |
| 724 | while i < 8 bound 8 { |
| 725 | va = va | (sb(inp, w, pa + na + i) << (i * 8)); |
| 726 | vb = vb | (sb(inp, w, pb + nb + i) << (i * 8)); |
| 727 | i = i + 1; |
| 728 | } |
| 729 | } else { |
| 730 | va = w[w[19] + pa + na + 1]; |
| 731 | vb = w[w[19] + pb + nb + 1]; |
| 732 | } |
| 733 | if va < vb { c = 1; } else { c = 2; } |
| 734 | } else { |
| 735 | let mut m: u64 = na; |
| 736 | if nb < m { m = nb; } |
| 737 | let mut j: u64 = 0; |
| 738 | while j < m && c == 0 bound 128 { |
| 739 | let xa: u64 = sb(inp, w, pa + j); |
| 740 | let xb: u64 = sb(inp, w, pb + j); |
| 741 | if xa < xb { c = 1; } |
| 742 | if xa > xb { c = 2; } |
| 743 | j = j + 1; |
| 744 | } |
| 745 | if c == 0 && na < nb { c = 1; } |
| 746 | if c == 0 && na > nb { c = 2; } |
| 747 | if c == 0 && kind == 3 { |
| 748 | pa = pa + na; |
| 749 | pb = pb + nb; |
| 750 | if dec == 0 { |
| 751 | pa = pa + 1; |
| 752 | pb = pb + 1; |
| 753 | } |
| 754 | } |
| 755 | if c == 0 && kind == 2 { c = 2; } |
| 756 | } |
| 757 | } |
| 758 | let mut q: u64 = 0; |
| 759 | while q < 32 && c == 0 bound 32 { |
| 760 | let ya: u64 = sb(inp, w, pa + q); |
| 761 | let yb: u64 = sb(inp, w, pb + q); |
| 762 | if ya < yb { c = 1; } |
| 763 | if ya > yb { c = 2; } |
| 764 | q = q + 1; |
| 765 | } |
| 766 | if c != 1 { |
| 767 | if dec == 1 { fail(w, 7 | (kf >> 8 << 8), at, 0); } else { fail(w, 27, at, 0); } |
| 768 | return; |
| 769 | } |
| 770 | } |
| 771 | w[26] = at; |
| 772 | w[25] = 1; |
| 773 | } |
| 774 | |
| 775 | // Checkpoint content (CheckpointState::decode_content / encode_content). |
| 776 | fn ckpt(inp: bytes, w: cells) { |
| 777 | let mut dec: u64 = 0; |
| 778 | if w[4] == 1 { dec = 1; } |
| 779 | if dec == 1 { |
| 780 | if need(w, 1) != 0 { return; } |
| 781 | let v: u64 = rdle(inp, w, 1); |
| 782 | if v != 2 { |
| 783 | fail(w, 6, 0, v); |
| 784 | return; |
| 785 | } |
| 786 | } else { |
| 787 | outle(w, 2, 1); |
| 788 | } |
| 789 | let mut at: u64 = 0; |
| 790 | let n1: u64 = fcount(inp, w, 40, 1); |
| 791 | w[25] = 0; |
| 792 | let mut i1: u64 = 0; |
| 793 | while i1 < n1 && w[7] == 0 bound 1000000 { |
| 794 | at = w[12]; |
| 795 | if dec == 0 { at = w[16]; } |
| 796 | stored(inp, w); |
| 797 | cmpkey(inp, w, 1 | (9 << 8), at); |
| 798 | i1 = i1 + 1; |
| 799 | } |
| 800 | let n2: u64 = fcount(inp, w, 76, 1); |
| 801 | w[25] = 0; |
| 802 | let mut i2: u64 = 0; |
| 803 | while i2 < n2 && w[7] == 0 bound 1000000 { |
| 804 | at = w[12]; |
| 805 | if dec == 0 { at = w[16]; } |
| 806 | frun(inp, w, 32778); |
| 807 | frun(inp, w, 8199); |
| 808 | fint(inp, w, 2052); |
| 809 | frun(inp, w, 8199); |
| 810 | cmpkey(inp, w, 3 | (10 << 8), at); |
| 811 | i2 = i2 + 1; |
| 812 | } |
| 813 | let n3: u64 = fcount(inp, w, 40, 1); |
| 814 | w[25] = 0; |
| 815 | let mut i3: u64 = 0; |
| 816 | while i3 < n3 && w[7] == 0 bound 1000000 { |
| 817 | at = w[12]; |
| 818 | if dec == 0 { at = w[16]; } |
| 819 | frun(inp, w, 8199); |
| 820 | cmpkey(inp, w, 1 | (11 << 8), at); |
| 821 | fint(inp, w, 2052); |
| 822 | i3 = i3 + 1; |
| 823 | } |
| 824 | let n4: u64 = fcount(inp, w, 12, 1); |
| 825 | w[25] = 0; |
| 826 | let mut i4: u64 = 0; |
| 827 | while i4 < n4 && w[7] == 0 bound 1000000 { |
| 828 | at = w[12]; |
| 829 | if dec == 0 { at = w[16]; } |
| 830 | projection(inp, w); |
| 831 | cmpkey(inp, w, 4 | (12 << 8), at); |
| 832 | i4 = i4 + 1; |
| 833 | } |
| 834 | let n5: u64 = fcount(inp, w, 4, 1); |
| 835 | w[25] = 0; |
| 836 | let mut i5: u64 = 0; |
| 837 | while i5 < n5 && w[7] == 0 bound 1000000 { |
| 838 | at = w[12]; |
| 839 | if dec == 0 { at = w[16]; } |
| 840 | canonical(inp, w); |
| 841 | cmpkey(inp, w, 2 | (13 << 8), at); |
| 842 | i5 = i5 + 1; |
| 843 | } |
| 844 | if w[7] != 0 { return; } |
| 845 | // Revision section: in the bytes only when it holds records; always a count in the LF. |
| 846 | let mut present: u64 = 1; |
| 847 | if dec == 1 && w[12] >= w[13] { |
| 848 | present = 0; |
| 849 | lfput(w, 267); |
| 850 | lfput(w, 0); |
| 851 | } |
| 852 | if dec == 0 { |
| 853 | let l: u64 = w[16]; |
| 854 | if l + 1 < w[17] && w[w[19] + l] == 267 && w[w[19] + l + 1] == 0 { |
| 855 | present = 0; |
| 856 | w[16] = l + 2; |
| 857 | } |
| 858 | } |
| 859 | if present == 1 { |
| 860 | let sat: u64 = w[12]; |
| 861 | let n6: u64 = fcount(inp, w, 12, 1); |
| 862 | if dec == 1 && w[7] == 0 && n6 == 0 { |
| 863 | fail(w, 7 | (14 << 8), sat, 0); |
| 864 | return; |
| 865 | } |
| 866 | let mut prev: u64 = 0; |
| 867 | let mut has: u64 = 0; |
| 868 | let mut i6: u64 = 0; |
| 869 | while i6 < n6 && w[7] == 0 bound 1000000 { |
| 870 | let rat: u64 = w[12]; |
| 871 | let seq: u64 = fint(inp, w, 2052); |
| 872 | if dec == 1 && w[7] == 0 && ((has == 1 && prev >= seq) || seq > w[6]) { |
| 873 | fail(w, 7 | (14 << 8), rat, 0); |
| 874 | } |
| 875 | prev = seq; |
| 876 | has = 1; |
| 877 | let tag_at: u64 = w[12] + 5; |
| 878 | let sv: u64 = emb_open(inp, w, 255); |
| 879 | let k: u64 = record(inp, w); |
| 880 | emb_close(w, sv, 255); |
| 881 | if dec == 1 && w[7] == 0 && k != 7 && k != 8 { |
| 882 | fail(w, 3844, tag_at, k); |
| 883 | } |
| 884 | i6 = i6 + 1; |
| 885 | } |
| 886 | } |
| 887 | if dec == 1 && w[7] == 0 && w[12] != w[13] { |
| 888 | fail(w, 3, w[12], w[13] - w[12]); |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | // Entry. Header checks (ABI, regions inside w and apart, input within the loop bound), state reset, |
| 893 | // one record or one checkpoint content, then (encode) the whole LF must have been used. |
| 894 | fn syn_codec(inp: bytes, w: cells) -> u64 { |
| 895 | if w.len < 32 { return 30; } |
| 896 | w[7] = 0; |
| 897 | w[8] = 0; |
| 898 | w[9] = 0; |
| 899 | w[10] = 0; |
| 900 | w[11] = 0; |
| 901 | let mode: u64 = w[4]; |
| 902 | let lb: u64 = w[19]; |
| 903 | let lc: u64 = w[20]; |
| 904 | let ob: u64 = w[21]; |
| 905 | let oc: u64 = w[22]; |
| 906 | let mut bad: u64 = 0; |
| 907 | if w[0] != 1398361674 || w[1] != 1 || w[2] != w.len { bad = 1; } |
| 908 | if w[3] != 1 && w[3] != 2 { bad = 1; } |
| 909 | if mode != 1 && mode != 2 { bad = 1; } |
| 910 | if lb < 32 || lb > w.len || lc > w.len - lb { bad = 1; } |
| 911 | if bad == 0 && (ob < 32 || ob > w.len || oc > w.len - ob) { bad = 1; } |
| 912 | if bad == 0 && oc != 0 && lc != 0 && ob < lb + lc && lb < ob + oc { bad = 1; } |
| 913 | if bad == 0 && mode == 2 && w[17] > lc { bad = 1; } |
| 914 | if bad == 1 { |
| 915 | w[7] = 30; |
| 916 | return 30; |
| 917 | } |
| 918 | // The second line keeps every packed position (emb_open) inside 20 bits even if INPUT_MAX grows. |
| 919 | if inp.len > 1000000 || lc > 1000000 * 4 || oc > 1000000 || |
| 920 | inp.len > 1048575 || oc > 1048575 { |
| 921 | w[7] = 31; |
| 922 | return 31; |
| 923 | } |
| 924 | if mode == 2 && w[17] > 1000000 { |
| 925 | w[7] = 31; |
| 926 | return 31; |
| 927 | } |
| 928 | w[12] = 0; |
| 929 | w[13] = inp.len; |
| 930 | w[14] = 0; |
| 931 | w[15] = 0; |
| 932 | w[16] = 0; |
| 933 | w[18] = 0; |
| 934 | w[23] = 0; |
| 935 | w[24] = 0; |
| 936 | w[25] = 0; |
| 937 | w[26] = 0; |
| 938 | if mode == 1 { w[17] = 0; } |
| 939 | if w[3] == 1 { |
| 940 | record(inp, w); |
| 941 | } else { |
| 942 | if mode == 1 { w[5] = inp.len; } |
| 943 | ckpt(inp, w); |
| 944 | } |
| 945 | if mode == 2 && w[7] == 0 && w[16] != w[17] { |
| 946 | fail(w, 26, w[16], w[17] - w[16]); |
| 947 | } |
| 948 | return w[7]; |
| 949 | } |