Skip to content

Instantly share code, notes, and snippets.

@nickva
Last active September 8, 2026 19:48
Show Gist options
  • Select an option

  • Save nickva/0afdfbbf07c430f4f8d2abd36c499423 to your computer and use it in GitHub Desktop.

Select an option

Save nickva/0afdfbbf07c430f4f8d2abd36c499423 to your computer and use it in GitHub Desktop.
Byte stuffing couch_file

Byte stuffing like in https://datatracker.ietf.org/doc/html/rfc1662#section-4.2 except that we use a long byte prefix to make it easier to find and less chance of constantly escaping every random 255th byte

Pick some 7 bytes prefix P, something less likely to occur in erlang term_to_binary or utf8 encodings.

P = f5, f6, f7, f8, f9, fa, fb

One byte H (=fc) is the header byte. The header tag: P|H = f5, f6, f7, f8, f9, fa, fb, fc

Another X (=fd) is the escape header byte. The whole escape prefix is P|X = f5, f6, f7, f8, f9, fa, fb, fd

Encoding a non-header region:

  • P|H -> P|X|H
  • P|X -> P|X|X

We never have a raw P|H in a non-header by construction

Decoding (from file, read backwards in overlapping windows):

  • P|H -> header
  • P|X|H -> P|H un-escaped
  • P|X|X -> P|X un-escaped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment