bytepeel-core 0.1.2

Zero-dependency recursive decoder and editor for encoded and binary data: hex, base64, PEM, JWT, gzip/zstd, DER/ASN.1, CBOR, MessagePack and schemaless protobuf, with confidence-ranked interpretations
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
//! `--get` across formats: one path walks protobuf, DER, CBOR/MessagePack and
//! JWT layers in turn.
//!
//! Each format's segments are walked natively (field numbers, child indices,
//! map keys, JWT parts and JSON keys). When the next segment cannot descend
//! natively but the current item holds a decoded node (a protobuf string that
//! is base64 DER, a CBOR byte string that is protobuf, a JWT part that is not
//! JSON), the rest of the path continues in that node's format. Native descent
//! always wins, so every single-format path keeps its meaning. Text that is a
//! JSON object or array (a decoded text layer, a protobuf string, a
//! CBOR/MessagePack text string) is walked by JSON keys and array indices.

use crate::Diag;
use crate::json;
use crate::jsonpath;
use crate::node::{AltKind, Asn1, Detail, Field, FieldValue, Node};
use crate::path::{
    self, JwtPick, bundle_parts, der_items, field_children, jwt_parts, map_index, message_fields,
    values_items,
};
use crate::tree;
use crate::value::{Value, ValueKind};

/// What a path selected.
#[derive(Debug)]
pub enum Pick<'a> {
    Field(&'a Field),
    Element(&'a Asn1),
    Value(&'a Value),
    Jwt(JwtPick<'a>),
    Json(JsonPick<'a>),
}

/// A value inside JSON text.
#[derive(Debug)]
pub struct JsonPick<'a> {
    /// The whole JSON document.
    pub doc: &'a str,
    /// The path within the document (the segments after the text's holder).
    pub path: String,
    /// The selected value's byte range in `doc`.
    pub range: [usize; 2],
}

impl JsonPick<'_> {
    /// The selected value as JSON text.
    pub fn value(&self) -> &str {
        &self.doc[self.range[0]..self.range[1]]
    }

    /// `{"path":..,"range":[s,e],"value":<JSON>}`.
    pub fn to_json(&self) -> String {
        let mut o = String::from("{\"path\":");
        json::esc(&mut o, &self.path);
        o.push_str(&format!(
            ",\"range\":[{},{}],\"value\":{}}}",
            self.range[0],
            self.range[1],
            self.value()
        ));
        o
    }

    /// A string's unescaped text, any other value verbatim.
    pub fn payload(&self) -> Vec<u8> {
        let v = self.value();
        jsonpath::string_value(v).map_or_else(|| v.as_bytes().to_vec(), String::into_bytes)
    }
}

impl Pick<'_> {
    /// The selection as JSON, in its own format's schema.
    pub fn to_json(&self) -> String {
        match self {
            Pick::Field(f) => json::field_to_json(f),
            Pick::Element(e) => json::asn1_to_json(e),
            Pick::Value(v) => json::value_to_json(v),
            Pick::Jwt(p) => p.to_json(),
            Pick::Json(p) => p.to_json(),
        }
    }

    /// The selection as an indented outline.
    pub fn to_tree(&self) -> String {
        match self {
            Pick::Field(f) => tree::field_to_tree(f),
            Pick::Element(e) => tree::asn1_to_tree(e),
            Pick::Value(v) => tree::value_to_tree(v),
            Pick::Jwt(p) => p.to_tree(),
            Pick::Json(p) => format!("{} = {}\n", p.path, p.value()),
        }
    }

    /// The selection's payload bytes, or `NO_PAYLOAD` for a group, a
    /// constructed element or a map/array value.
    pub fn payload(&self) -> Result<Vec<u8>, Diag> {
        let none = |what: &str| {
            Diag::new(
                "NO_PAYLOAD",
                format!("{what} has no single payload; use the JSON output"),
                None,
            )
        };
        match self {
            Pick::Field(f) => f.payload().ok_or_else(|| none("a group")),
            Pick::Element(e) => e.payload().ok_or_else(|| none("a constructed element")),
            Pick::Value(v) => v.payload().ok_or_else(|| none("this value")),
            Pick::Jwt(p) => Ok(p.payload()),
            Pick::Json(p) => Ok(p.payload()),
        }
    }
}

/// Select by a path that may cross formats, e.g. `2.0.0.5` for the subject
/// of a base64 certificate in protobuf string field 2, or `token.payload.sub`
/// for a JWT in a CBOR byte string.
pub fn get<'a>(root: &'a Node, path: &str) -> Result<Pick<'a>, Diag> {
    walk(root, &segments(path)?, 0, &mut None)
}

fn segments(path: &str) -> Result<Vec<&str>, Diag> {
    if path.trim().is_empty() {
        return Err(Diag::new("BAD_PATH", "empty path", None));
    }
    let segs: Vec<&str> = path.split('.').collect();
    if segs.iter().any(|s| s.is_empty()) {
        return Err(Diag::new(
            "BAD_PATH",
            "paths do not allow empty segments",
            None,
        ));
    }
    Ok(segs)
}

/// What holds a decoded node that a path crossed into.
#[derive(Debug, Clone, Copy)]
pub(crate) enum Holder<'a> {
    Field(&'a Field),
    Value(&'a Value),
    /// A JWT part that is not JSON.
    Jwt,
}

/// Where a path crosses into a decoded node: `segs[at..]` continue in `node`.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Crossing<'a> {
    pub at: usize,
    pub holder: Holder<'a>,
    pub node: &'a Node,
}

/// [`get`], plus the last place the path crossed formats.
pub(crate) fn locate<'a>(
    root: &'a Node,
    path: &str,
) -> Result<(Pick<'a>, Option<Crossing<'a>>), Diag> {
    let mut cx = None;
    let pick = walk(root, &segments(path)?, 0, &mut cx)?;
    Ok((pick, cx))
}

/// The decoded node of a selection that has no children in its own format
/// (a string or bytes field, a byte string), as a crossing at `at`.
pub(crate) fn leaf_crossing<'a>(pick: &Pick<'a>, at: usize) -> Option<Crossing<'a>> {
    match pick {
        Pick::Field(f) if field_children(f).is_none() => decoded_alt(f).map(|node| Crossing {
            at,
            holder: Holder::Field(f),
            node,
        }),
        Pick::Value(v) => match &v.untagged().kind {
            ValueKind::Bytes {
                decoded: Some(n), ..
            } => Some(Crossing {
                at,
                holder: Holder::Value(v),
                node: n,
            }),
            _ => None,
        },
        _ => None,
    }
}

fn at(segs: &[&str], n: usize) -> String {
    segs[..n].join(".")
}

fn not_found(msg: String) -> Diag {
    Diag::new("PATH_NOT_FOUND", msg, None)
}

fn no_children(segs: &[&str], n: usize, what: &str) -> Diag {
    Diag::new(
        "PATH_NOT_MESSAGE",
        format!("{what} at '{}' has nothing to descend into", at(segs, n)),
        None,
    )
}

/// Walk `segs[i..]` starting in `node` (text layers are skipped).
fn walk<'a>(
    node: &'a Node,
    segs: &[&str],
    i: usize,
    cx: &mut Option<Crossing<'a>>,
) -> Result<Pick<'a>, Diag> {
    if let Some(parts) = bundle_parts(node) {
        let block: usize = segs[i]
            .parse()
            .map_err(|_| bad_index(segs[i], "a PEM bundle block"))?;
        let part = parts
            .get(block)
            .ok_or_else(|| not_found(format!("no bundle block '{}'", at(segs, i + 1))))?;
        if i + 1 == segs.len() {
            return Err(not_found(
                "a bundle path needs an element index after the block".into(),
            ));
        }
        return walk(part, segs, i + 1, cx);
    }
    if let Some(items) = der_items(node) {
        return walk_der(items, segs, i);
    }
    if let Some(items) = values_items(node) {
        return walk_values(items, segs, i, cx);
    }
    if jwt_parts(node).is_some() {
        return walk_jwt(node, segs, i, cx);
    }
    if let Some(fields) = message_fields(node) {
        return walk_fields(fields, segs, i, cx);
    }
    if let Some(doc) = text_doc(node) {
        return walk_json(doc, segs, i);
    }
    Err(Diag::new(
        "PATH_NOT_MESSAGE",
        if i == 0 {
            "the input did not decode to protobuf, DER, CBOR/MessagePack, a JWT or JSON".to_string()
        } else {
            format!(
                "the data decoded at '{}' is not protobuf, DER, CBOR/MessagePack, a JWT or JSON",
                at(segs, i)
            )
        },
        None,
    ))
}

fn bad_index(seg: &str, what: &str) -> Diag {
    Diag::new(
        "BAD_PATH",
        format!("'{seg}' is not an index into {what}"),
        None,
    )
}

fn walk_fields<'a>(
    mut fields: &'a [Field],
    segs: &[&str],
    mut i: usize,
    cx: &mut Option<Crossing<'a>>,
) -> Result<Pick<'a>, Diag> {
    loop {
        let (num, idx) = path::parse_path(segs[i])?[0];
        let f = fields
            .iter()
            .filter(|f| f.number == num)
            .nth(idx)
            .ok_or_else(|| not_found(format!("no field '{}'", at(segs, i + 1))))?;
        i += 1;
        if i == segs.len() {
            return Ok(Pick::Field(f));
        }
        if let Some(children) = field_children(f) {
            fields = children;
            continue;
        }
        // A string or bytes field whose content decodes to another format.
        return match decoded_alt(f) {
            Some(node) => {
                *cx = Some(Crossing {
                    at: i,
                    holder: Holder::Field(f),
                    node,
                });
                walk(node, segs, i, cx)
            }
            None => match json_alt(f) {
                Some(doc) => walk_json(doc, segs, i),
                None => Err(no_children(segs, i, "the field")),
            },
        };
    }
}

/// The decoded node of a LEN field's best-ranked string or bytes alt that
/// has one.
fn decoded_alt(f: &Field) -> Option<&Node> {
    match &f.value {
        FieldValue::Len { alts, .. } => alts.iter().find_map(|a| match &a.kind {
            AltKind::String {
                decoded: Some(n), ..
            }
            | AltKind::Bytes { decoded: Some(n) } => Some(&**n),
            _ => None,
        }),
        _ => None,
    }
}

/// The text of a LEN field's string alt, when it is a JSON object or array.
fn json_alt(f: &Field) -> Option<&str> {
    match &f.value {
        FieldValue::Len { alts, .. } => alts.iter().find_map(|a| match &a.kind {
            AltKind::String { text, .. } if is_json_doc(text) => Some(text.as_str()),
            _ => None,
        }),
        _ => None,
    }
}

/// The whole text of a node that is (after any layers) JSON text. A layer's
/// decoded bytes hold the full text; a root text node holds it only when it
/// is short enough to be kept whole.
fn text_doc(node: &Node) -> Option<&str> {
    match &node.detail {
        Detail::Layer { decoded, child } => match &child.detail {
            Detail::Text { .. } => std::str::from_utf8(decoded).ok().filter(|t| is_json_doc(t)),
            _ => text_doc(child),
        },
        Detail::Text { text, len } if *len == text.len() => {
            Some(text.as_str()).filter(|t| is_json_doc(t))
        }
        _ => None,
    }
}

/// A JSON object or array (not a bare scalar: a number or a quoted word is
/// text, not a document to walk).
fn is_json_doc(t: &str) -> bool {
    matches!(t.trim_start().as_bytes().first(), Some(b'{' | b'['))
        && jsonpath::find(t, &[], "").is_ok()
}

fn walk_json<'a>(doc: &'a str, segs: &[&str], i: usize) -> Result<Pick<'a>, Diag> {
    let range = jsonpath::find(doc, &segs[i..], &at(segs, i))?;
    Ok(Pick::Json(JsonPick {
        doc,
        path: segs[i..].join("."),
        range,
    }))
}

fn walk_der<'a>(mut items: &'a [Asn1], segs: &[&str], mut i: usize) -> Result<Pick<'a>, Diag> {
    loop {
        let idx: usize = segs[i].parse().map_err(|_| {
            Diag::new(
                "BAD_PATH",
                format!(
                    "invalid path segment '{}' (DER paths are child indices, e.g. 0.0.6)",
                    segs[i]
                ),
                None,
            )
        })?;
        let e = items
            .get(idx)
            .ok_or_else(|| not_found(format!("no element '{}'", at(segs, i + 1))))?;
        i += 1;
        if i == segs.len() {
            return Ok(Pick::Element(e));
        }
        items = e
            .children()
            .ok_or_else(|| no_children(segs, i, "the element"))?;
    }
}

fn walk_values<'a>(
    items: &'a [Value],
    segs: &[&str],
    mut i: usize,
    cx: &mut Option<Crossing<'a>>,
) -> Result<Pick<'a>, Diag> {
    let mut cur = if items.len() == 1 {
        &items[0]
    } else {
        let idx: usize = segs[i]
            .parse()
            .map_err(|_| bad_index(segs[i], "the CBOR/MessagePack sequence"))?;
        i += 1;
        items
            .get(idx)
            .ok_or_else(|| not_found(format!("no element '{}'", at(segs, i))))?
    };
    while i < segs.len() {
        let seg = segs[i];
        cur = match &cur.untagged().kind {
            ValueKind::Map(entries) => {
                let k = map_index(entries, seg)
                    .ok_or_else(|| not_found(format!("no key '{seg}' at '{}'", at(segs, i))))?;
                &entries[k].1
            }
            ValueKind::Array(arr) => {
                let idx: usize = seg
                    .parse()
                    .map_err(|_| not_found(format!("'{seg}' is not an array index")))?;
                arr.get(idx)
                    .ok_or_else(|| not_found(format!("no index '{idx}' at '{}'", at(segs, i))))?
            }
            // A byte string that decodes to another format.
            ValueKind::Bytes {
                decoded: Some(node),
                ..
            } => {
                *cx = Some(Crossing {
                    at: i,
                    holder: Holder::Value(cur),
                    node,
                });
                return walk(node, segs, i, cx);
            }
            ValueKind::Text(t) if is_json_doc(t) => return walk_json(t, segs, i),
            _ => return Err(no_children(segs, i, "the value")),
        };
        i += 1;
    }
    Ok(Pick::Value(cur))
}

fn walk_jwt<'a>(
    node: &'a Node,
    segs: &[&str],
    i: usize,
    cx: &mut Option<Crossing<'a>>,
) -> Result<Pick<'a>, Diag> {
    let rest = segs[i..].join(".");
    if i + 1 < segs.len() {
        let part = path::select_jwt(node, segs[i])?;
        if !part_is_json(&part) {
            // Not JSON (e.g. a JWE part, or a binary payload): continue in
            // whatever the part decodes to.
            *cx = Some(Crossing {
                at: i + 1,
                holder: Holder::Jwt,
                node: part.node,
            });
            return walk(part.node, segs, i + 1, cx);
        }
    }
    path::select_jwt(node, &rest).map(Pick::Jwt)
}

fn part_is_json(p: &JwtPick) -> bool {
    match &p.node.detail {
        crate::node::Detail::Layer { decoded, .. } => std::str::from_utf8(decoded)
            .is_ok_and(|t| crate::jsonpath::find(t, &[], p.part).is_ok()),
        _ => false,
    }
}