interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / synapse / syn_codec.osc.in

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