sim-codec-chat 0.1.1

Canonical chat transcript codec for SIM.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! The `SIMCHAT1` text grammar: encodes an `Expr` transcript to canonical chat
//! text and parses that text back to an `Expr`, under a decode budget.

use sim_codec::DecodeBudget;
use sim_kernel::{CodecId, Error, Expr, NumberLiteral, QuoteMode, Result, Symbol};

use crate::base64::{base64_decode, base64_encode};

const HEADER: &str = "SIMCHAT1\n";

/// Cap on the initial capacity reserved for an attacker-declared collection
/// length. The length is checked against the collection-length limit, but eager
/// `Vec::with_capacity(len)` at every nesting level lets a small, deeply nested
/// input force gigabytes of simultaneous reservations. We reserve at most this
/// many slots up front and let the vector grow as items actually decode; the
/// node-count and depth budgets bound the realized total.
const ALLOC_RESERVE_CAP: usize = 4096;

pub(crate) fn encode_chat_text(expr: &Expr) -> String {
    let mut out = String::from(HEADER);
    encode_node(expr, &mut out);
    out
}

pub(crate) fn decode_chat_text(
    codec: CodecId,
    source: &str,
    budget: &mut DecodeBudget,
) -> Result<Expr> {
    let body = source
        .strip_prefix(HEADER)
        .ok_or_else(|| codec_error(codec, "chat transcript must start with SIMCHAT1 header"))?;
    let mut parser = Parser {
        codec,
        bytes: body.as_bytes(),
        index: 0,
        budget,
    };
    let expr = parser.parse_node(0)?;
    if parser.index != parser.bytes.len() {
        return Err(codec_error(codec, "trailing data after chat transcript"));
    }
    Ok(expr)
}

fn encode_node(expr: &Expr, out: &mut String) {
    match expr {
        Expr::Nil => out.push_str("z;"),
        Expr::Bool(true) => out.push_str("t;"),
        Expr::Bool(false) => out.push_str("f;"),
        Expr::Number(number) => {
            out.push('d');
            encode_text(&number.domain.to_string(), out);
            encode_text(&number.canonical, out);
            out.push(';');
        }
        Expr::Symbol(symbol) => {
            out.push('y');
            encode_text(&symbol.to_string(), out);
            out.push(';');
        }
        Expr::Local(symbol) => {
            out.push('l');
            encode_text(&symbol.to_string(), out);
            out.push(';');
        }
        Expr::String(text) => {
            out.push('s');
            encode_text(text, out);
            out.push(';');
        }
        Expr::Bytes(bytes) => {
            out.push('x');
            encode_text(&base64_encode(bytes), out);
            out.push(';');
        }
        Expr::List(items) => encode_sequence('a', items, out),
        Expr::Vector(items) => encode_sequence('v', items, out),
        Expr::Map(entries) => {
            let mut sorted = entries.clone();
            sorted.sort_by_key(|(key, value)| (key.canonical_key(), value.canonical_key()));
            out.push('m');
            encode_count(sorted.len(), out);
            for (key, value) in sorted {
                encode_node(&key, out);
                encode_node(&value, out);
            }
            out.push(';');
        }
        Expr::Set(items) => {
            let mut sorted = items.clone();
            sorted.sort_by_key(Expr::canonical_key);
            encode_sequence('e', &sorted, out);
        }
        Expr::Call { operator, args } => {
            out.push('c');
            encode_count(args.len(), out);
            encode_node(operator, out);
            for arg in args {
                encode_node(arg, out);
            }
            out.push(';');
        }
        Expr::Infix {
            operator,
            left,
            right,
        } => {
            out.push('i');
            encode_text(&operator.to_string(), out);
            encode_node(left, out);
            encode_node(right, out);
            out.push(';');
        }
        Expr::Prefix { operator, arg } => {
            out.push('p');
            encode_text(&operator.to_string(), out);
            encode_node(arg, out);
            out.push(';');
        }
        Expr::Postfix { operator, arg } => {
            out.push('o');
            encode_text(&operator.to_string(), out);
            encode_node(arg, out);
            out.push(';');
        }
        Expr::Block(items) => encode_sequence('k', items, out),
        Expr::Quote { mode, expr } => {
            out.push('q');
            out.push(quote_mode_code(*mode));
            out.push(':');
            encode_node(expr, out);
            out.push(';');
        }
        Expr::Annotated { expr, annotations } => {
            out.push('r');
            encode_count(annotations.len(), out);
            encode_node(expr, out);
            for (symbol, value) in annotations {
                encode_text(&symbol.to_string(), out);
                encode_node(value, out);
            }
            out.push(';');
        }
        Expr::Extension { tag, payload } => {
            out.push('g');
            encode_text(&tag.to_string(), out);
            encode_node(payload, out);
            out.push(';');
        }
    }
}

fn encode_sequence(tag: char, items: &[Expr], out: &mut String) {
    out.push(tag);
    encode_count(items.len(), out);
    for item in items {
        encode_node(item, out);
    }
    out.push(';');
}

fn encode_count(count: usize, out: &mut String) {
    out.push_str(&count.to_string());
    out.push(':');
}

fn encode_text(text: &str, out: &mut String) {
    out.push_str(&text.len().to_string());
    out.push(':');
    out.push_str(text);
}

struct Parser<'a, 'b> {
    codec: CodecId,
    bytes: &'a [u8],
    index: usize,
    budget: &'b mut DecodeBudget,
}

impl Parser<'_, '_> {
    fn parse_node(&mut self, depth: usize) -> Result<Expr> {
        self.budget.enter_node(self.codec, depth)?;
        let tag = self.next_byte("expected expression tag")?;
        match tag {
            b'z' => {
                self.expect(b';')?;
                Ok(Expr::Nil)
            }
            b't' => {
                self.expect(b';')?;
                Ok(Expr::Bool(true))
            }
            b'f' => {
                self.expect(b';')?;
                Ok(Expr::Bool(false))
            }
            b'd' => {
                let domain = self.parse_text()?;
                let canonical = self.parse_text()?;
                self.expect(b';')?;
                Ok(Expr::Number(NumberLiteral {
                    domain: parse_symbol(&domain),
                    canonical,
                }))
            }
            b'y' => {
                let text = self.parse_text()?;
                self.expect(b';')?;
                Ok(Expr::Symbol(parse_symbol(&text)))
            }
            b'l' => {
                let text = self.parse_text()?;
                self.expect(b';')?;
                Ok(Expr::Local(parse_symbol(&text)))
            }
            b's' => {
                let text = self.parse_text()?;
                self.expect(b';')?;
                Ok(Expr::String(text))
            }
            b'x' => {
                let text = self.parse_text()?;
                self.expect(b';')?;
                Ok(Expr::Bytes(base64_decode(self.codec, &text)?))
            }
            b'a' => self.parse_sequence(depth, Expr::List),
            b'v' => self.parse_sequence(depth, Expr::Vector),
            b'm' => self.parse_map(depth),
            b'e' => self.parse_sequence(depth, Expr::Set),
            b'c' => self.parse_call(depth),
            b'i' => self.parse_infix(depth),
            b'p' => self.parse_prefix(depth),
            b'o' => self.parse_postfix(depth),
            b'k' => self.parse_sequence(depth, Expr::Block),
            b'q' => self.parse_quote(depth),
            b'r' => self.parse_annotated(depth),
            b'g' => self.parse_extension(depth),
            other => Err(codec_error(
                self.codec,
                format!("unknown chat expression tag {}", other as char),
            )),
        }
    }

    fn parse_sequence(
        &mut self,
        depth: usize,
        make: impl FnOnce(Vec<Expr>) -> Expr,
    ) -> Result<Expr> {
        let count = self.parse_len()?;
        self.budget.check_collection_len(self.codec, count)?;
        let mut items = Vec::with_capacity(count.min(ALLOC_RESERVE_CAP));
        for _ in 0..count {
            items.push(self.parse_node(depth + 1)?);
        }
        self.expect(b';')?;
        Ok(make(items))
    }

    fn parse_map(&mut self, depth: usize) -> Result<Expr> {
        let count = self.parse_len()?;
        self.budget.check_collection_len(self.codec, count)?;
        let mut entries = Vec::with_capacity(count.min(ALLOC_RESERVE_CAP));
        for _ in 0..count {
            let key = self.parse_node(depth + 1)?;
            let value = self.parse_node(depth + 1)?;
            entries.push((key, value));
        }
        self.expect(b';')?;
        Ok(Expr::Map(entries))
    }

    fn parse_call(&mut self, depth: usize) -> Result<Expr> {
        let count = self.parse_len()?;
        self.budget.check_collection_len(self.codec, count)?;
        let operator = Box::new(self.parse_node(depth + 1)?);
        let mut args = Vec::with_capacity(count.min(ALLOC_RESERVE_CAP));
        for _ in 0..count {
            args.push(self.parse_node(depth + 1)?);
        }
        self.expect(b';')?;
        Ok(Expr::Call { operator, args })
    }

    fn parse_infix(&mut self, depth: usize) -> Result<Expr> {
        let operator = parse_symbol(&self.parse_text()?);
        let left = Box::new(self.parse_node(depth + 1)?);
        let right = Box::new(self.parse_node(depth + 1)?);
        self.expect(b';')?;
        Ok(Expr::Infix {
            operator,
            left,
            right,
        })
    }

    fn parse_prefix(&mut self, depth: usize) -> Result<Expr> {
        let operator = parse_symbol(&self.parse_text()?);
        let arg = Box::new(self.parse_node(depth + 1)?);
        self.expect(b';')?;
        Ok(Expr::Prefix { operator, arg })
    }

    fn parse_postfix(&mut self, depth: usize) -> Result<Expr> {
        let operator = parse_symbol(&self.parse_text()?);
        let arg = Box::new(self.parse_node(depth + 1)?);
        self.expect(b';')?;
        Ok(Expr::Postfix { operator, arg })
    }

    fn parse_quote(&mut self, depth: usize) -> Result<Expr> {
        let mode = self.next_byte("expected quote mode")?;
        self.expect(b':')?;
        let expr = Box::new(self.parse_node(depth + 1)?);
        self.expect(b';')?;
        Ok(Expr::Quote {
            mode: parse_quote_mode(self.codec, mode)?,
            expr,
        })
    }

    fn parse_annotated(&mut self, depth: usize) -> Result<Expr> {
        let count = self.parse_len()?;
        self.budget.check_collection_len(self.codec, count)?;
        let expr = Box::new(self.parse_node(depth + 1)?);
        let mut annotations = Vec::with_capacity(count.min(ALLOC_RESERVE_CAP));
        for _ in 0..count {
            let symbol = parse_symbol(&self.parse_text()?);
            let value = self.parse_node(depth + 1)?;
            annotations.push((symbol, value));
        }
        self.expect(b';')?;
        Ok(Expr::Annotated { expr, annotations })
    }

    fn parse_extension(&mut self, depth: usize) -> Result<Expr> {
        let tag = parse_symbol(&self.parse_text()?);
        let payload = Box::new(self.parse_node(depth + 1)?);
        self.expect(b';')?;
        Ok(Expr::Extension { tag, payload })
    }

    fn parse_text(&mut self) -> Result<String> {
        let len = self.parse_len()?;
        self.budget.check_string_bytes(self.codec, len)?;
        let end = self
            .index
            .checked_add(len)
            .ok_or_else(|| codec_error(self.codec, "chat text length overflowed input position"))?;
        let Some(raw) = self.bytes.get(self.index..end) else {
            return Err(codec_error(
                self.codec,
                "chat text field exceeds input length",
            ));
        };
        self.index = end;
        std::str::from_utf8(raw)
            .map(str::to_owned)
            .map_err(|err| codec_error(self.codec, format!("chat text is not valid UTF-8: {err}")))
    }

    fn parse_len(&mut self) -> Result<usize> {
        let start = self.index;
        while matches!(self.bytes.get(self.index), Some(b'0'..=b'9')) {
            self.index += 1;
        }
        if self.index == start {
            return Err(codec_error(self.codec, "expected decimal length"));
        }
        self.expect(b':')?;
        let text = std::str::from_utf8(&self.bytes[start..self.index - 1])
            .map_err(|err| codec_error(self.codec, format!("invalid length text: {err}")))?;
        text.parse::<usize>()
            .map_err(|err| codec_error(self.codec, format!("invalid decimal length: {err}")))
    }

    fn next_byte(&mut self, message: &'static str) -> Result<u8> {
        let byte = self
            .bytes
            .get(self.index)
            .copied()
            .ok_or_else(|| codec_error(self.codec, message))?;
        self.index += 1;
        Ok(byte)
    }

    fn expect(&mut self, expected: u8) -> Result<()> {
        let found = self.next_byte("unexpected end of chat transcript")?;
        if found != expected {
            return Err(codec_error(
                self.codec,
                format!(
                    "expected byte {:?}, found {:?}",
                    expected as char, found as char
                ),
            ));
        }
        Ok(())
    }
}

fn parse_symbol(raw: &str) -> Symbol {
    match raw.split_once('/') {
        Some((namespace, name)) => Symbol::qualified(namespace.to_owned(), name.to_owned()),
        None => Symbol::new(raw.to_owned()),
    }
}

fn quote_mode_code(mode: QuoteMode) -> char {
    match mode {
        QuoteMode::Quote => '0',
        QuoteMode::QuasiQuote => '1',
        QuoteMode::Unquote => '2',
        QuoteMode::Splice => '3',
        QuoteMode::Syntax => '4',
    }
}

fn parse_quote_mode(codec: CodecId, raw: u8) -> Result<QuoteMode> {
    match raw {
        b'0' => Ok(QuoteMode::Quote),
        b'1' => Ok(QuoteMode::QuasiQuote),
        b'2' => Ok(QuoteMode::Unquote),
        b'3' => Ok(QuoteMode::Splice),
        b'4' => Ok(QuoteMode::Syntax),
        other => Err(codec_error(
            codec,
            format!("unknown quote mode code {}", other as char),
        )),
    }
}

fn codec_error(codec: CodecId, message: impl Into<String>) -> Error {
    Error::CodecError {
        codec,
        message: message.into(),
    }
}