# Formats and pipeline

> Which formats bytepeel decodes, the order it tries them in, and the depth and size limits.

## Formats

| Layer | Notes |
|---|---|
| hex | whitespace, `:` and `0x` separators |
| hex dumps | `xxd` (any `-g`) and `hexdump -C` (with `*` repeats); the ASCII column is ignored |
| base64 | standard and URL-safe alphabets, padded and unpadded, line-wrapped |
| PEM | any label; RFC 1421 headers skipped; multiple blocks (chain, key + cert) become a `bundle` of per-block layers |
| JWT | JWS (3 parts) and JWE (5 parts); each part decoded, and `exp`/`nbf`/`iat` rendered as ISO-8601 UTC |
| protobuf | schemaless; groups, negative varints, fixed32/64 as int and float |
| DER / ASN.1 | X.509, PKCS, CMS; ~175 named OIDs, ISO times, any-width integers, DER nested in BIT/OCTET STRINGs decoded in place |
| CBOR | RFC 8949: maps, arrays, tags (epoch → ISO-8601, bignums, uuid), indefinite lengths, half/single/double floats; `--as cbor` for scalars |
| MessagePack | maps, arrays, str/bin, int64/uint64, float32/64, ext (timestamp type -1 → ISO-8601 with nanoseconds); `--as msgpack` for scalars |
| utf8 / bytes | leaves |
| gzip, zlib, zstd | checksum-verified, multi-member/frame, 64 MiB output cap (optional core features `deflate` and `zstd`, on in the CLI) |
| PNG, zip | recognized by magic bytes (not decoded) |

## Pipeline

```
input ─┬─ text? ─► best text layer (PEM | JWT | xxd/hexdump | hex | base64) ─► recurse on decoded bytes
       │            JWT recurses into one base64 layer per dot-separated part;
       │            a PEM bundle recurses into one pem layer per block
       └─ bytes ─► gzip | zlib | zstd (checksum-verified) ─► recurse on decompressed bytes
                 └► DER (a single well-formed SEQUENCE) ─► element tree
                 └► magic sniff (png/zip) ─► leaf + UNSUPPORTED_FORMAT
                 └► CBOR | MessagePack | schemaless protobuf:
                       map/array (or self-describe) candidates compete; the highest
                       confidence wins and every other is named in AMBIGUOUS_FORMAT
                 └► bytes leaf
```

`--depth` limits total nesting (text and compression layers plus protobuf
messages, default 16). `--as` applies after any text and compression layers.
`--max-items` limits the total number of protobuf fields, CBOR/MessagePack
values and DER elements per run (default 1,000,000; `Options::max_items`).
One item can take a single input byte but costs hundreds of bytes of memory
and tens of bytes of JSON, so without it a few megabytes of input could build
gigabytes. A detection candidate that runs out is rejected and the data stays
a `bytes` leaf with a `TOO_MANY_ITEMS` warning (a forced `--as` returns the
error); a winner whose nested message ran out keeps that field undecoded and
carries the warning. Only the winning candidate's items count.

## Cargo features (`bytepeel-core`)

The core is zero-dependency by default. Decoders that need a third-party crate
are opt-in; the `bytepeel` CLI enables all of them.

| Feature | Adds | Dependency |
|---|---|---|
| `deflate` | gzip (incl. multi-member), zlib | `miniz_oxide` |
| `zstd` | Zstandard (incl. multi-frame) | `ruzstd` |

Without a feature, a stream it would decode is a `bytes` leaf with an
`UNSUPPORTED_FORMAT` warning naming the missing feature.
