A conditional as an operand loses its parentheses (sex#59): box-blur allocated a quarter of its buffer, deep water got the shallow colour, and verify's delta histogram wrote out of bounds into the height classes. Plan.org quoted the corrupted count. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
97 lines
3.2 KiB
Scheme
97 lines
3.2 KiB
Scheme
;;; Protobuf wire reader for the blobs in a .vcdbs save. The engine's
|
|
;;; message classes are closed, so callers walk fields by number and
|
|
;;; keep only what they know; everything else is skipped.
|
|
;;;
|
|
;;; A reader is a window over borrowed bytes. A malformed blob sets
|
|
;;; `err' and empties the window, so a loop on `more' always ends.
|
|
|
|
(pub include stddef.h)
|
|
(include string.h)
|
|
|
|
;; TODO sex#46: make these a (pub enum wire ...) once explicit enum
|
|
;; values are kebab-converted.
|
|
(pub define WIRE-VARINT 0)
|
|
(pub define WIRE-FIXED64 1)
|
|
(pub define WIRE-BYTES 2)
|
|
(pub define WIRE-FIXED32 5)
|
|
|
|
(pub record pb ((p (* const u8)) (end (* const u8)) (err bool)))
|
|
|
|
;; TODO sex#44: the methods are ordered so each calls only those above
|
|
;; it; order them by topic once a method can call one below.
|
|
(ops pb
|
|
(pub fn of ((data (* const void)) (len size-t)) pb
|
|
(var p (* const u8) data)
|
|
(return #(pb : p (+ p len) false)))
|
|
|
|
(fn fail ((self (* pb))) void
|
|
(= (. self err) true)
|
|
(= (. self p) (. self end)))
|
|
|
|
(pub fn more ((self (* const pb))) bool
|
|
(return (< (. self p) (. self end))))
|
|
|
|
(pub fn len ((self (* const pb))) size-t
|
|
(return (cast (- (. self end) (. self p)) size-t)))
|
|
|
|
(pub fn varint ((self (* pb))) u64
|
|
(var v u64 0)
|
|
(for (var shift int 0) (< shift 64) (+= shift 7)
|
|
(if (>= (. self p) (. self end))
|
|
(break))
|
|
(var b u8 (* (. self p)))
|
|
(++ (. self p))
|
|
;; TODO sex#47: write the varint masks in hex once there are hex
|
|
;; literals.
|
|
(c-bit-or= v (<< (cast (& b 127) u64) shift))
|
|
(if (< b 128)
|
|
(return v)))
|
|
(.fail self)
|
|
(return 0))
|
|
|
|
;; A packed or repeated `int' field: negative values arrive as ten
|
|
;; byte varints and keep their meaning when truncated.
|
|
(pub fn int32 ((self (* pb))) i32
|
|
(return (cast (.varint self) i32)))
|
|
|
|
;; The next field's number, or 0 at the end. WIRE gets its wire type.
|
|
(pub fn field ((self (* pb)) (wire (* int))) u32
|
|
(if (! (.more self))
|
|
(return 0))
|
|
(var key u64 (.varint self))
|
|
(= (* wire) (cast (& key 7) int))
|
|
(return (cast (>> key 3) u32)))
|
|
|
|
;; A length-delimited field as a reader of its own: a submessage, a
|
|
;; string, or a packed array.
|
|
(pub fn bytes ((self (* pb))) pb
|
|
(var len u64 (.varint self))
|
|
;; TODO sex#45: use (. self p) in the literals once member access
|
|
;; inside a compound literal follows the pointer.
|
|
(var p (* const u8) (. self p))
|
|
(if (> len (.len self))
|
|
(do
|
|
(.fail self)
|
|
(return #(pb : p p true))))
|
|
(+= (. self p) len)
|
|
(return #(pb : p (+ p len) false)))
|
|
|
|
;; True when the rest of the window spells exactly S, e.g. a map
|
|
;; entry's key against "riverDistance".
|
|
(pub fn equals ((self (* const pb)) (s (* const char))) bool
|
|
(var n size-t (.len self))
|
|
(return (&& (== (strlen s) n) (== (memcmp (. self p) s n) 0))))
|
|
|
|
(fn advance ((self (* pb)) (n size-t)) void
|
|
(if (> n (.len self))
|
|
(.fail self)
|
|
(+= (. self p) n)))
|
|
|
|
(pub fn skip ((self (* pb)) (wire int)) void
|
|
(switch wire
|
|
(case WIRE-VARINT (.varint self) (break))
|
|
(case WIRE-BYTES (.bytes self) (break))
|
|
(case WIRE-FIXED64 (.advance self 8) (break))
|
|
(case WIRE-FIXED32 (.advance self 4) (break))
|
|
(default (.fail self)))))
|