Skip to main content

encode_length

Function encode_length 

Source
pub fn encode_length(length: usize, out: &mut Vec<u8>) -> Result<(), Error>
Expand description

Encode one §3.2.1 length sequence (0..=1275) into out.

  • 0..=251 — one byte, the value itself.
  • 252..=1275 — two bytes: first ∈ 252..=255 with first ≡ length (mod 4) offset from 252, then second = (length - first) / 4, so that the §3.2.1 decode second * 4 + first reproduces length.

Errors when length > 1275 (R2: the two-byte form tops out at 255 * 4 + 255).