cpa-exec 0.4.2

Part of cliproxy: upstream provider executors
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Go `encoding/json` decoding for the small, flat auth records Meta (and later Devin)
//! read from auth servers: `json.Unmarshal(body, &record)` with string, int and bool
//! fields. Vertex decodes Google key files and token responses with it too, using the
//! check-only time, string-slice and nested-struct slots.
//!
//! Go semantics reproduced: syntax errors with Go's scanner messages; keys matched to
//! field tags exactly, else case-insensitively (Go's fold, including `Å¿` and the Kelvin
//! sign); later duplicates win; `null` leaves a field unchanged; a value of the wrong
//! type is skipped, decoding continues, and the first such error is returned. Unknown
//! keys are ignored. Expected values come from tests/device_fixtures/meta/decode.json.

use cpa_common::json::{self as gj, Kind};

/// A destination field (`json:"<tag>"`).
pub(crate) enum Slot<'a> {
    Str(&'a mut String),
    /// Go `int` or `int64`; the name is used in error messages.
    Int(&'a mut i64, &'static str),
    Bool(&'a mut bool),
    /// `time.Time`, checked only: a JSON string holding a strict RFC 3339 time
    /// (`Time.UnmarshalJSON` reads the raw string body without unescaping).
    // ponytail: Go reports a bad time with Time.UnmarshalJSON's own message, and it
    // aborts decoding there; the generic type message is used instead.
    Time,
    /// `[]string`, checked only; `null` elements are allowed.
    Strs,
    /// A nested struct or struct pointer, checked only: an object.
    // ponytail: the nested struct's own fields are not type-checked.
    Object,
}

impl Slot<'_> {
    fn go_type(&self) -> &'static str {
        match self {
            Self::Str(_) => "string",
            Self::Int(_, name) => name,
            Self::Bool(_) => "bool",
            Self::Time => "time.Time",
            Self::Strs => "[]string",
            Self::Object => "struct",
        }
    }
}

/// `json.Unmarshal(raw, &v)` for a struct `package.name` with `fields` (tag, slot).
pub(crate) fn unmarshal(raw: &[u8], package: &str, name: &str, fields: &mut [(&str, Slot<'_>)]) -> Result<(), String> {
    check_valid(raw)?;
    let root = gj::parse(raw);
    if root.kind == Kind::Null {
        return Ok(());
    }
    if !root.is_object() {
        return Err(format!(
            "json: cannot unmarshal {} into Go value of type {package}.{name}",
            kind_name(&root)
        ));
    }
    let mut first_error: Option<String> = None;
    root.each(|key, value| {
        let key = gj::go_unquote(&key.raw).unwrap_or_default();
        let key = key.as_str();
        let index = fields
            .iter()
            .position(|(tag, _)| *tag == key)
            .or_else(|| fields.iter().position(|(tag, _)| fold(tag) == fold(key)));
        let Some(index) = index else {
            return true;
        };
        let (tag, slot) = &mut fields[index];
        if let Err(value_name) = store(slot, &value)
            && first_error.is_none()
        {
            first_error = Some(format!(
                "json: cannot unmarshal {value_name} into Go struct field {name}.{tag} of type {}",
                slot.go_type()
            ));
        }
        true
    });
    first_error.map_or(Ok(()), Err)
}

/// Stores `value` in `slot`; on a type mismatch returns Go's description of the value.
fn store(slot: &mut Slot<'_>, value: &gj::Res<'_>) -> Result<(), String> {
    match (slot, value.kind) {
        (_, Kind::Null) => Ok(()),
        (Slot::Str(s), Kind::String) => {
            // Go's unquote: invalid UTF-8 becomes U+FFFD.
            **s = gj::go_unquote(&value.raw).unwrap_or_default();
            Ok(())
        }
        (Slot::Int(n, _), Kind::Number) => {
            let literal = String::from_utf8_lossy(&value.raw);
            match literal.parse::<i64>() {
                Ok(parsed) if literal.bytes().all(|b| b.is_ascii_digit() || b == b'-') => {
                    **n = parsed;
                    Ok(())
                }
                _ => Err(format!("number {literal}")),
            }
        }
        (Slot::Bool(b), Kind::True | Kind::False) => {
            **b = value.kind == Kind::True;
            Ok(())
        }
        (Slot::Time, Kind::String) if go_rfc3339(&value.raw[1..value.raw.len() - 1]) => Ok(()),
        (Slot::Strs, Kind::Json)
            if value.is_array()
                && value
                    .array()
                    .iter()
                    .all(|item| matches!(item.kind, Kind::String | Kind::Null)) =>
        {
            Ok(())
        }
        (Slot::Object, Kind::Json) if value.is_object() => Ok(()),
        _ => Err(kind_name(value).to_owned()),
    }
}

/// Go's strict RFC 3339 parse (`time.parseStrictRFC3339`): `YYYY-MM-DDThh:mm:ss`, an
/// optional `.` fraction, then `Z` or `±hh:mm`, every field in range.
pub(crate) fn go_rfc3339(s: &[u8]) -> bool {
    let num = |digits: &[u8], min: u32, max: u32| -> bool {
        digits.iter().all(u8::is_ascii_digit)
            && (min..=max).contains(&digits.iter().fold(0u32, |x, c| x * 10 + u32::from(c - b'0')))
    };
    if s.len() < 19 || !(s[4] == b'-' && s[7] == b'-' && s[10] == b'T' && s[13] == b':' && s[16] == b':') {
        return false;
    }
    if !(num(&s[0..4], 0, 9999) && num(&s[5..7], 1, 12)) {
        return false;
    }
    let year = s[0..4].iter().fold(0u32, |x, c| x * 10 + u32::from(c - b'0'));
    let leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    let days = match &s[5..7] {
        b"02" if leap => 29,
        b"02" => 28,
        b"04" | b"06" | b"09" | b"11" => 30,
        _ => 31,
    };
    if !(num(&s[8..10], 1, days) && num(&s[11..13], 0, 23) && num(&s[14..16], 0, 59) && num(&s[17..19], 0, 59)) {
        return false;
    }
    let mut rest = &s[19..];
    if rest.len() >= 2 && rest[0] == b'.' && rest[1].is_ascii_digit() {
        let n = 1 + rest[1..].iter().take_while(|c| c.is_ascii_digit()).count();
        rest = &rest[n..];
    }
    rest == b"Z"
        || (rest.len() == 6
            && matches!(rest[0], b'+' | b'-')
            && rest[3] == b':'
            && num(&rest[1..3], 0, 23)
            && num(&rest[4..6], 0, 59))
}

/// The length of the object at the start of `s`, up to the brace that closes it: the
/// value a `json.Decoder` reads when more input follows (call [`check_valid`] on it).
pub(crate) fn object_end(s: &[u8]) -> Option<usize> {
    let (mut depth, mut in_string, mut escaped) = (0usize, false, false);
    for (i, &c) in s.iter().enumerate() {
        if in_string {
            if escaped {
                escaped = false;
            } else if c == b'\\' {
                escaped = true;
            } else if c == b'"' {
                in_string = false;
            }
            continue;
        }
        match c {
            b'"' => in_string = true,
            b'{' | b'[' => depth += 1,
            b'}' | b']' => {
                depth = depth.checked_sub(1)?;
                if depth == 0 {
                    return Some(i + 1);
                }
            }
            _ => {}
        }
    }
    None
}

pub(crate) fn kind_name(value: &gj::Res<'_>) -> &'static str {
    match value.kind {
        Kind::String => "string",
        Kind::Number => "number",
        Kind::True | Kind::False => "bool",
        Kind::Json if value.is_array() => "array",
        Kind::Json => "object",
        Kind::Null => "null",
    }
}

/// Go's `foldName`: ASCII upper case, and every other rune folded to the smallest rune of
/// its case-fold set. Only `Å¿` (to `S`) and the Kelvin sign (to `K`) fold onto ASCII.
pub(crate) fn fold(name: &str) -> String {
    name.chars()
        .map(|c| match c {
            'a'..='z' => c.to_ascii_uppercase(),
            '\u{17f}' => 'S',
            '\u{212a}' => 'K',
            _ => c,
        })
        .collect()
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Parse {
    ObjectKey,
    ObjectValue,
    ArrayValue,
}

#[derive(Clone, Copy)]
enum State {
    BeginValue,
    BeginValueOrEmpty,
    BeginStringOrEmpty,
    BeginString,
    EndValue,
    EndTop,
    InString,
    InStringEsc,
    /// Inside `\u`, with this many hex digits read.
    InStringEscU(u8),
    Neg,
    One,
    Zero,
    Dot,
    Dot0,
    E,
    ESign,
    E0,
    /// Inside `true`/`false`/`null`, at this byte of the word.
    Literal(&'static str, usize),
}

const MAX_DEPTH: usize = 10_000;

/// Go's `checkValid` (encoding/json scanner), with its error messages.
pub(crate) fn check_valid(raw: &[u8]) -> Result<(), String> {
    let mut s = Scanner {
        state: State::BeginValue,
        stack: Vec::new(),
        end_top: false,
    };
    for &c in raw {
        s.step(c)?;
    }
    if s.end_top {
        return Ok(());
    }
    s.step(b' ')?;
    if s.end_top {
        return Ok(());
    }
    Err("unexpected end of JSON input".into())
}

struct Scanner {
    state: State,
    stack: Vec<Parse>,
    end_top: bool,
}

fn space(c: u8) -> bool {
    matches!(c, b' ' | b'\t' | b'\r' | b'\n')
}

fn hex(c: u8) -> bool {
    c.is_ascii_hexdigit()
}

/// Go's `quoteChar`: the byte as a rune, in `strconv.Quote` form between single quotes.
fn quote_char(c: u8) -> String {
    let inner = match c {
        b'\'' => "\\'".to_owned(),
        b'"' => "\"".to_owned(),
        b'\\' => "\\\\".to_owned(),
        7 => "\\a".to_owned(),
        8 => "\\b".to_owned(),
        0x0c => "\\f".to_owned(),
        b'\n' => "\\n".to_owned(),
        b'\r' => "\\r".to_owned(),
        b'\t' => "\\t".to_owned(),
        0x0b => "\\v".to_owned(),
        0..=0x1f | 0x7f => format!("\\x{c:02x}"),
        0x80..=0xa0 | 0xad => format!("\\u{c:04x}"),
        _ => char::from(c).to_string(),
    };
    format!("'{inner}'")
}

fn invalid(c: u8, context: &str) -> String {
    format!("invalid character {} {context}", quote_char(c))
}

impl Scanner {
    fn push(&mut self, c: u8, parse: Parse) -> Result<(), String> {
        self.stack.push(parse);
        if self.stack.len() > MAX_DEPTH {
            return Err(invalid(c, "exceeded max depth"));
        }
        Ok(())
    }

    fn pop(&mut self) {
        self.stack.pop();
        if self.stack.is_empty() {
            self.state = State::EndTop;
            self.end_top = true;
        } else {
            self.state = State::EndValue;
        }
    }

    fn step(&mut self, c: u8) -> Result<(), String> {
        match self.state {
            State::BeginValueOrEmpty => {
                if space(c) {
                    return Ok(());
                }
                if c == b']' {
                    return self.end_value(c);
                }
                self.begin_value(c)
            }
            State::BeginValue => self.begin_value(c),
            State::BeginStringOrEmpty => {
                if space(c) {
                    return Ok(());
                }
                if c == b'}' {
                    if let Some(top) = self.stack.last_mut() {
                        *top = Parse::ObjectValue;
                    }
                    return self.end_value(c);
                }
                self.begin_string(c)
            }
            State::BeginString => self.begin_string(c),
            State::EndValue => self.end_value(c),
            State::EndTop => self.end_top(c),
            State::InString => {
                match c {
                    b'"' => self.state = State::EndValue,
                    b'\\' => self.state = State::InStringEsc,
                    0..=0x1f => return Err(invalid(c, "in string literal")),
                    _ => {}
                }
                Ok(())
            }
            State::InStringEsc => match c {
                b'b' | b'f' | b'n' | b'r' | b't' | b'\\' | b'/' | b'"' => {
                    self.state = State::InString;
                    Ok(())
                }
                b'u' => {
                    self.state = State::InStringEscU(0);
                    Ok(())
                }
                _ => Err(invalid(c, "in string escape code")),
            },
            State::InStringEscU(n) => {
                if !hex(c) {
                    return Err(invalid(c, "in \\u hexadecimal character escape"));
                }
                self.state = if n == 3 {
                    State::InString
                } else {
                    State::InStringEscU(n + 1)
                };
                Ok(())
            }
            State::Neg => match c {
                b'0' => {
                    self.state = State::Zero;
                    Ok(())
                }
                b'1'..=b'9' => {
                    self.state = State::One;
                    Ok(())
                }
                _ => Err(invalid(c, "in numeric literal")),
            },
            State::One => {
                if c.is_ascii_digit() {
                    return Ok(());
                }
                self.zero(c)
            }
            State::Zero => self.zero(c),
            State::Dot => {
                if c.is_ascii_digit() {
                    self.state = State::Dot0;
                    return Ok(());
                }
                Err(invalid(c, "after decimal point in numeric literal"))
            }
            State::Dot0 => {
                if c.is_ascii_digit() {
                    return Ok(());
                }
                if c == b'e' || c == b'E' {
                    self.state = State::E;
                    return Ok(());
                }
                self.end_value(c)
            }
            State::E => {
                if c == b'+' || c == b'-' {
                    self.state = State::ESign;
                    return Ok(());
                }
                self.e_sign(c)
            }
            State::ESign => self.e_sign(c),
            State::E0 => {
                if c.is_ascii_digit() {
                    return Ok(());
                }
                self.end_value(c)
            }
            State::Literal(word, at) => {
                let expected = word.as_bytes()[at];
                if c != expected {
                    return Err(invalid(
                        c,
                        &format!("in literal {word} (expecting '{}')", expected as char),
                    ));
                }
                self.state = if at + 1 == word.len() {
                    State::EndValue
                } else {
                    State::Literal(word, at + 1)
                };
                Ok(())
            }
        }
    }

    fn begin_value(&mut self, c: u8) -> Result<(), String> {
        if space(c) {
            return Ok(());
        }
        self.state = match c {
            b'{' => {
                self.state = State::BeginStringOrEmpty;
                return self.push(c, Parse::ObjectKey);
            }
            b'[' => {
                self.state = State::BeginValueOrEmpty;
                return self.push(c, Parse::ArrayValue);
            }
            b'"' => State::InString,
            b'-' => State::Neg,
            b'0' => State::Zero,
            b't' => State::Literal("true", 1),
            b'f' => State::Literal("false", 1),
            b'n' => State::Literal("null", 1),
            b'1'..=b'9' => State::One,
            _ => return Err(invalid(c, "looking for beginning of value")),
        };
        Ok(())
    }

    fn begin_string(&mut self, c: u8) -> Result<(), String> {
        if space(c) {
            return Ok(());
        }
        if c == b'"' {
            self.state = State::InString;
            return Ok(());
        }
        Err(invalid(c, "looking for beginning of object key string"))
    }

    fn zero(&mut self, c: u8) -> Result<(), String> {
        match c {
            b'.' => {
                self.state = State::Dot;
                Ok(())
            }
            b'e' | b'E' => {
                self.state = State::E;
                Ok(())
            }
            _ => self.end_value(c),
        }
    }

    fn e_sign(&mut self, c: u8) -> Result<(), String> {
        if c.is_ascii_digit() {
            self.state = State::E0;
            return Ok(());
        }
        Err(invalid(c, "in exponent of numeric literal"))
    }

    fn end_value(&mut self, c: u8) -> Result<(), String> {
        let Some(&top) = self.stack.last() else {
            self.state = State::EndTop;
            self.end_top = true;
            return self.end_top(c);
        };
        if space(c) {
            self.state = State::EndValue;
            return Ok(());
        }
        match top {
            Parse::ObjectKey => {
                if c == b':' {
                    *self.stack.last_mut().unwrap() = Parse::ObjectValue;
                    self.state = State::BeginValue;
                    return Ok(());
                }
                Err(invalid(c, "after object key"))
            }
            Parse::ObjectValue => match c {
                b',' => {
                    *self.stack.last_mut().unwrap() = Parse::ObjectKey;
                    self.state = State::BeginString;
                    Ok(())
                }
                b'}' => {
                    self.pop();
                    Ok(())
                }
                _ => Err(invalid(c, "after object key:value pair")),
            },
            Parse::ArrayValue => match c {
                b',' => {
                    self.state = State::BeginValue;
                    Ok(())
                }
                b']' => {
                    self.pop();
                    Ok(())
                }
                _ => Err(invalid(c, "after array element")),
            },
        }
    }

    fn end_top(&mut self, c: u8) -> Result<(), String> {
        if !space(c) {
            return Err(invalid(c, "after top-level value"));
        }
        Ok(())
    }
}