Twinjo and ASN.1 length bytes
Lassi Kortela 01 May 2021 07:12 UTC
The current Twinjo draft at
https://github.com/johnwcowan/r7rs-work/blob/276681081f757dfac050771a8526fd0cbaae00e7/Twinjo.md
says about length encoding in the the binary format:
If length is less than 2^7 bytes, length byte is 00 through 7F.
If length is less than 2^15 bytes, meta-length byte is 82, followed by 2
length bytes representing a big-endian 2's-complement integer.
If length is less than 2^23 bytes, meta-length byte is 83, followed by 3
length bytes representing the length as a big-endian 2's-complement integer.
...
If length is less than 2^63 bytes, meta-length byte is 88, followed by 8
length bytes representing the length as a big-endian 2's-complement integer.
The explainer "A Layman's Guide to a Subset of ASN.1, BER, and DER" at
http://luca.ntop.org/Teaching/Appunti/asn1.html (recommended by the
people who authored the ASN.1 package in the Go standard library) says:
Length octets
Long form. Two to 127 octets. Bit 8 of first octet has value "1" and
bits 7-1 give the number of additional length octets. Second and
following octets give the length, base 256, most significant digit first.
The exclusive upper bounds 2^7 in the Twinjo spec looks right to me. But
are the exponents in 2^15, 2^23, w^63 deliberately one less than a power
of two? The only explanation I can think of for why they would be one
less is that the length is a signed integer and the final bit is the
sign bit. But the Layman's Guide doesn't say anything about signed lengths.