# Decoding

> Decode data from a file or stdin, read the tree outline, select one item with --get, and extract its bytes with --format raw.

```
bytepeel [OPTIONS] [FILE]            # stdin when FILE is omitted or '-'

--in <auto|hex|base64|raw>           how to read the input (default: auto)
--as <protobuf|der|cbor|msgpack|text|bytes>
                                     force interpretation of the decoded bytes
--depth <N>                          max nesting (default 16)
--alts <N>                           interpretations kept per LEN field (default 3)
--max-items <N>                      max fields/values/elements decoded in total (default 1000000)
--get <PATH>                         select one item (see Paths)
--format <json|tree|raw>             output format (default: json)
```

Exit codes: `0` success, `1` decode error, `2` usage or I/O error.

## Output formats

- **`json`** (the default) is the full tree in compact JSON, described in
  [JSON schema](https://bytepeel.pinta.land/reference/json-schema/index.md). Long text and bytes are
  previewed (text capped at 1024 characters, hex at 64 bytes) with
  `"truncated":true`.
- **`tree`** is a short indented outline, one line per node or field. Start
  here to see the structure.
- **`raw`** writes bytes as they are: with `--get`, the selected item's
  payload; without it, the innermost layer's whole bytes (for base64 → gzip →
  text, the full text, past the preview).

```console
$ echo '10 fe ff ff ff ff ff ff ff ff 01' | bytepeel
{"codec":"hex",…,"child":{"codec":"protobuf",…,"fields":[{"field":2,"wire":"VARINT","range":[0,11],"uint":"18446744073709551614","int":"-2","sint":"9223372036854775807"}]}}

$ bytepeel --format raw < blob.b64 > out.txt   # all of base64 -> gzip -> text
```

## Selecting one item

`--get PATH` prints one item in its own format's schema. The path syntax
depends on the format, and a path can cross from one format into another:

| Format | Path | Example |
|---|---|---|
| protobuf | field numbers; `N[i]` = i-th occurrence | `1.3[2].4` |
| DER | 0-based child indices | `0.0.5` = a certificate's subject |
| PEM bundle | first index picks the block | `1.0.0.5` = 2nd cert's subject |
| CBOR / MessagePack | map keys and array indices | `users.0.name` |
| JWT | part, then JSON keys | `payload.sub`, `header.alg` |
| JSON text (decoded, or in a string) | keys and array indices | `users.37.id` |

```console
$ echo 1a03089601 | bytepeel --get 3.1 --format raw | xxd
00000000: 9601

$ bytepeel cert.pem --get 0.0.4 --format tree        # X.509 validity
SEQUENCE [124..156]
  UTCTime [126..141] 2020-09-04T00:00:00Z
  UTCTime [141..156] 2040-09-17T16:00:00Z

$ bytepeel record.cbor --get tags.1 --format raw     # a CBOR map value
b

$ echo "$TOKEN" | bytepeel --get payload.sub --format raw   # a JWT claim
1234567890
```

The full rules are in [Paths](https://bytepeel.pinta.land/reference/paths/index.md).

## Reading the result

- **Ambiguity is reported, not resolved.** A protobuf LEN field lists ranked
  `alts` (`message`, `string`, `packed_varint`, `packed_fixed32/64`,
  `bytes`) with scores; the first is the best guess. A varint shows `uint`,
  `int` and `sint`: without a schema, which one is right is unknown.
- **64-bit integers are JSON strings**, so jq and JavaScript can't corrupt
  values above 2^53.
- **Warnings** such as `AMBIGUOUS_FORMAT`, `CHECKSUM_MISMATCH` or
  `TRAILING_DATA` are attached to the node they concern. See
  [Error and warning codes](https://bytepeel.pinta.land/reference/errors/index.md).
- If auto-detection picks the wrong format, pin it: `--in hex|base64|raw`
  for the input text, `--as protobuf|der|cbor|msgpack|text|bytes` for the
  decoded bytes.
