Editing rules

bytepeel [OPTIONS] set EDIT... [FILE]     # writes the new data to stdout

EDIT:  PATH=VALUE    change
       PATH+=VALUE   add
       --del PATH    remove

Edits apply in the order given; each one is a separate decode, splice and verification. Library: bytepeel_core::edit(input, &opts, &Op::{Set, Add, Del}); set(..) is edit with Op::Set.

set changes, adds and removes protobuf fields, CBOR/MessagePack values and DER elements, and writes the data back in the same outer format it arrived in.

  • Byte-exact splicing. Only the target field's payload changes. The length prefixes of the enclosing LEN fields are rewritten (minimal varints); every other byte is copied: field order, unknown fields, other fields' non-canonical encodings, and group start/end tags. The field's tag bytes are kept verbatim.

  • Paths are the --get paths. Protobuf paths enter LEN fields the same way --get does (the first message alt, or a string whose decoded layers end in a message, e.g. base64 inside a string field). CBOR/MessagePack paths are map keys and array indices; the item inside any tags is replaced and the tags are kept. Because containers count items rather than bytes, the new item is spliced in at the old one's range and nothing else moves. DER paths are the --get child indices (the first one picks the block of a PEM bundle, and only that block is re-encoded). DER lengths count bytes, so the length of every enclosing element is rewritten, including encapsulating BIT/OCTET STRINGs (a BIT STRING keeps its unused-bits byte). Tag bytes are kept verbatim; rewritten lengths use the minimal DER form.

  • Paths cross formats exactly as --get paths do (2.0.0.5.2.0.1=.. changes a certificate's common name in protobuf string field 2; p.1=300 a field of protobuf in a CBOR byte string). The decoded data is edited and re-encoded through its own layers (base64, PEM, hex, …), then the field or byte string holding it is set to the new bytes, which may repeat through several crossings. An add or remove is located by its parent, so p.2+=5 adds a field to the protobuf in byte string p. Verification decodes the whole output and follows the path back into the decoded data. Deleting the last item of decoded data fails verification when the empty result no longer decodes (REENCODE_VERIFY_FAILED).

  • Layers are re-encoded in their original style: hex keeps its separator, case, 0x form, wrapping and surrounding whitespace; dumps keep the xxd/hexdump -C layout; base64 keeps its alphabet, padding, line width and line endings; PEM keeps its label and the text around the block. With unchanged bytes, these reproduce the original text exactly. gzip keeps the original member header (name, mtime, flags) and writes a new CRC32/ISIZE; zlib keeps its 2-byte header and writes a new Adler-32; zstd is re-compressed. Bytes after a compressed stream are kept. The compressed bytes differ from the original compressor's.

  • A protobuf field keeps its wire type. Values:

    Value VARINT I64 I32 LEN
    42, -2 int (negative: 10-byte two's complement) fixed64/sfixed64 fixed32/sfixed32 —
    sint:-2 zigzag — — —
    1.5, float:NaN — double float (must fit) —
    true / false 1 / 0 — — —
    "text" (JSON escapes), str:text — — — UTF-8
    hex:0a0b, b64:CgM= — — — bytes

    null is TYPE_MISMATCH for protobuf.

  • A CBOR/MessagePack value may change type (items are self-describing). Integers use the minimal encoding (CBOR: [-2^64, 2^64), MessagePack: [-2^63, 2^64)); sint: is TYPE_MISMATCH. Floats use the smallest width that holds the value exactly (CBOR 16/32/64, MessagePack 32/64), but never narrower than the float being replaced, so a double stays a double. NaN and ±Infinity take that same width. Strings and byte strings are definite-length with minimal headers, even when they replace an indefinite-length item. true/false/null are the one-byte items. Nothing produces a MessagePack ext; replacing one gives a plain item.

  • A DER element keeps its tag and the value is encoded for its type:

    Element Value Content
    INTEGER, ENUMERATED 42, -129 minimal two's complement
    BOOLEAN / NULL true, false / null ff, 00 / empty
    OBJECT IDENTIFIER "1.2.840.113549.1.1.11" OID
    UTF8String, Graphic/GeneralString, OCTET STRING "text" UTF-8
    PrintableString, IA5String, NumericString, VisibleString "text" checked against the type's character set (VALUE_OUT_OF_RANGE)
    BMPString / UniversalString / T61, Videotex "text" UTF-16BE / UTF-32BE / Latin-1
    UTCTime, GeneralizedTime its own form (300102030405Z) or ISO 8601 ("2030-01-02T03:04:05Z", offsets and fractions allowed for GeneralizedTime) the type's form; UTCTime covers 1950-2049
    any primitive hex:.., b64:.. the content bytes verbatim; for a BIT STRING the bits, with 0 unused bits
    [n] and other non-universal primitives "text", hex:.. UTF-8 or the bytes

    A constructed element is TYPE_MISMATCH (set one of its children). Signatures are not updated: editing a certificate or any other signed structure invalidates its signature. That is intended for building test fixtures; re-sign the result if it must verify.

  • Verified. The output is decoded again with the same options; set fails with REENCODE_VERIFY_FAILED unless the path selects a field of the same wire type with exactly the new payload. This catches edits that change how auto-detection reads the data (pin it with --in/--as).

  • Adding (PATH+=VALUE):

    Codec PATH Placement Encoding
    protobuf PARENT.N (N alone = top level); no [i] appended at the end of the parent message (or group); a repeated field gets a new last occurrence integers, sint:, bools → VARINT; floats → I64 (double); text and bytes → LEN. null is TYPE_MISMATCH. Field numbers 1..=2^29-1
    CBOR / MessagePack map PARENT.KEY appended after the last entry key is a text string; ALREADY_EXISTS if the key is present
    CBOR / MessagePack array PARENT.I, 0 ≤ I ≤ length inserted before item I (I = length appends) as for a changed value
    DER PARENT.I, 0 ≤ I ≤ number of children inserted before element I the value is one complete element as hex:/b64: (e.g. hex:0101ff); BAD_VALUE otherwise

    The parent must be a message, map, array, or constructed/encapsulating element (PATH_NOT_MESSAGE). Verified: the new item is at PATH (for protobuf, the last occurrence) with the expected value, and the parent has one more child.

  • Removing (--del PATH): the field, entry (key and value) or element at PATH is cut out. Verified: the parent has one fewer child.

  • Containers are rewritten as needed: enclosing LEN prefixes (protobuf) and DER lengths up to the top, and the item count of a definite CBOR/MessagePack container (its header may change size, e.g. a 23-item CBOR array 97 becomes 98 18, a 15-entry MessagePack fixmap becomes map16). Indefinite CBOR containers have no count: items go before the break byte.

  • Not editable: JWT, including data inside a JWT part (editing would invalidate the signature), values inside JSON text, multi-member gzip / multi-frame zstd, PEM blocks with RFC 1421 headers (REENCODE_UNSUPPORTED), top-level items of a multi-item CBOR/MessagePack sequence, and whole PEM blocks of a bundle.