# Detection heuristics

> The scores and thresholds bytepeel uses to recognize each format and rank protobuf field interpretations.

Each signal has a test; changing one needs a test for the case that motivated it.

| Signal | Effect |
|---|---|
| Hex: `0x` prefix or whitespace/`:`-separated byte pairs | 0.95 |
| Hex: a single all-decimal token without a leading zero | ×0.5 (probably a number) |
| Base64: ≥16 chars / has `+/-_` or `=` / unpadded | +0.1 / +0.1 / −0.05 |
| Base64: non-zero unused trailing bits | ×0.3, plus `NONCANONICAL_BASE64` warning |
| Base64: only hex chars / no digit and no symbol | ×0.5 / ×0.6 |
| Dump (`xxd` / `hexdump -C`) | 0.98; needs a hex-offset first line (any start, e.g. `xxd -s` / `hexdump -s`) with `:` (xxd) or two spaces and a `|` column (hexdump), contiguous offsets, ≥1 byte; the non-zero start is reported as `"start"` |
| JWT | 0.97; 3 (`jws`) or 5 (`jwe`) dot-separated unpadded base64url segments whose header is canonical base64url for printable UTF-8 JSON starting with `{` and containing `"alg"`; any segment may be empty |
| PEM bundle | 0.99; 2+ `BEGIN`/`END` blocks, each label matching between `BEGIN` and `END` (one block stays a `pem` node); non-whitespace text outside all blocks → a single `TRAILING_DATA` warning carrying the ignored byte count; a *later* broken block stops the scan and becomes a warning on the node (`PEM_UNTERMINATED`, or the base64 error) with its offset, keeping the blocks before it |
| Text layer threshold: top level / nested | > 0.5 / > 0.75 (nested also needs hex ≥8 chars, base64 ≥16 chars) |
| Protobuf plausibility | starts at 0.9; ×0.85 per overlong varint, ×0.5 if a field number >1000 (×0.3 if >100k), ×0.7 per group, ×0.6 per wire-type conflict, ×0.6 for a single field in ≤3 bytes |
| DER (auto) | buffer starts with `0x30`, ≥4 bytes, parses as exactly one element with no malformed universal content: 0.95 (0.8 plus `NONCANONICAL_DER` for non-minimal lengths or integers) |
| DER encapsulation | BIT STRING (unused bits 0) / OCTET STRING content ≥2 bytes that parses completely as canonical, well-formed DER; a lone primitive must have a typed universal tag |
| CBOR (auto) | a self-describe (`d9 d9 f7`) prefix: 0.99; otherwise the buffer must parse as exactly one item of ≥2 bytes whose top level (after tags) is a map or array, with valid UTF-8 everywhere: 0.9, or 0.7 when any map key is not a text string or a small int (`|n| ≤ 23`), or 0.6 when the tree contains no map at all |
| MessagePack (auto) | the buffer must parse as exactly one item of ≥2 bytes whose top level is a map or array: 0.9, or 0.7 when any map key is not a string or a fixint (`0..=127`, or `n ≤ 31` for `nint`), or 0.6 when the tree contains no map at all |
| CBOR / MessagePack / protobuf | when several accept the buffer, the highest confidence wins; on a tie prefer all-text map keys, then CBOR, then MessagePack, then protobuf. Every other interpretation is named with its confidence in one `AMBIGUOUS_FORMAT` warning. Map-less trees score 0.6 because many protobuf messages whose first tag is for field 16+ also parse as a CBOR/MessagePack array. Candidates are parsed structurally; nested detection (byte strings, protobuf string fields) runs once, on the winner |
| gzip / zstd | magic `1f 8b 08` / `28 b5 2f fd`; decoded (confidence 1.0) only if the stream is complete and its CRC32 / content checksum matches, else a `bytes` leaf with the error as a warning |
| zlib | a valid 2-byte header (CM 8, CINFO ≤ 7, FCHECK, no FDICT) whose stream inflates with a matching Adler-32. Failures are reported only for the common headers `78 01/5e/9c/da`; other headers fall through to DER/protobuf |
| Item limit | 1,000,000 fields/values/elements per run (`--max-items`), losing candidates refunded |
| Decompressed size | capped at 64 MiB per stream (concatenated gzip members and zstd frames share the cap) and 256 MiB in total per peel across every compressed layer (`OUTPUT_TOO_LARGE`) |
| LEN alt: message | plausibility, ×0.3 if the payload is fully printable |
| LEN alt: string | 0.9 × printable_ratio⁴ |
| LEN alt: packed_varint | 0.25 if mostly non-printable, else 0.03 |
| LEN alt: packed_fixed64 / packed_fixed32 | offered when the length is a multiple of 8 / 4. If mostly non-printable, the higher of two scores: 0.3 / 0.28 × p⁴ + 0.01 (p = share of values that read as a plausible float: 0, or finite with magnitude in [1e-9, 1e12]), and 0.6 / 0.56 × c⁴ + 0.01 (c = integer-column evidence: at least 4 values, not all equal, sharing s ≥ 3 high bits; c = min(1, s × (n−1) / 24); 0 when the bytes also parse as varints whose encodings differ in length by at most one byte, as real varint lists do). Otherwise 0.01. Two floats can also read as one plausible double, so both alts may appear |
| LEN alt: bytes | always present at 0.02 |
| LEN alt: bytes, decoded | after a message wins, a LEN field whose best alt is bytes or packed numbers gets nested detection; a non-protobuf result with confidence ≥ 0.8 (compressed, DER, CBOR/MessagePack with map keys) becomes the bytes alt's `decoded`, weighted 4 × its confidence against the already-normalized alts, then all are renormalized. Messages and strings are not re-detected (that would double the cost per level) |

Alt scores are normalized to sum to 1, then truncated to `--alts`.
