Integers
All integers are encoded using the LEB128 variable-length integer encoding, in either unsigned or signed variant.
Unsigned integers are encoded in unsigned LEB128 format.
As an additional constraint, the total number of bytes encoding a \({{\href{../syntax/values.html#syntax-int}{\mathit{u}\kern-0.1em}}}{N}\) value must not exceed \({\mathrm{ceil}}(N / 7)\) bytes.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {{\href{../binary/values.html#binary-int}{\def\mathdef1873#1{{\mathtt{u}#1}}\mathdef1873{}}}}{N} & ::= & n{:}{\href{../binary/values.html#binary-byte}{\mathtt{byte}}} & \quad\Rightarrow\quad{} & n & \quad \mbox{if}~ n < {2^{7}} \land n < {2^{N}} \\
& & | & n{:}{\href{../binary/values.html#binary-byte}{\mathtt{byte}}}~~m{:}{{\href{../binary/values.html#binary-int}{\def\mathdef1873#1{{\mathtt{u}#1}}\mathdef1873{}}}}{(N - 7)} & \quad\Rightarrow\quad{} & {2^{7}} \cdot m + (n - {2^{7}}) & \quad \mbox{if}~ n \geq {2^{7}} \land N > 7 \\
\end{array}\end{split}\]
Signed integers are encoded in signed LEB128 format, which uses a two’s complement representation.
As an additional constraint, the total number of bytes encoding an \({{\href{../syntax/values.html#syntax-int}{\mathit{s}\kern-0.1em}}}{N}\) value must not exceed \({\mathrm{ceil}}(N / 7)\) bytes.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {{\href{../binary/values.html#binary-int}{\def\mathdef1880#1{{\mathtt{s}#1}}\mathdef1880{}}}}{N} & ::= & n{:}{\href{../binary/values.html#binary-byte}{\mathtt{byte}}} & \quad\Rightarrow\quad{} & n & \quad \mbox{if}~ n < {2^{6}} \land n < {2^{N - 1}} \\
& & | & n{:}{\href{../binary/values.html#binary-byte}{\mathtt{byte}}} & \quad\Rightarrow\quad{} & n - {2^{7}} & \quad \mbox{if}~ {2^{6}} \leq n < {2^{7}} \land n \geq {2^{7}} - {2^{N - 1}} \\
& & | & n{:}{\href{../binary/values.html#binary-byte}{\mathtt{byte}}}~~i{:}{{\href{../binary/values.html#binary-int}{\def\mathdef1873#1{{\mathtt{u}#1}}\mathdef1873{}}}}{(N - 7)} & \quad\Rightarrow\quad{} & {2^{7}} \cdot i + (n - {2^{7}}) & \quad \mbox{if}~ n \geq {2^{7}} \land N > 7 \\
\end{array}\end{split}\]
Uninterpreted integers are encoded as signed integers.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {{\href{../binary/values.html#binary-int}{\def\mathdef1886#1{{\mathtt{i}#1}}\mathdef1886{}}}}{N} & ::= & i{:}{{\href{../binary/values.html#binary-int}{\def\mathdef1880#1{{\mathtt{s}#1}}\mathdef1880{}}}}{N} & \quad\Rightarrow\quad{} & {{{{\href{../exec/numerics.html#aux-signed}{\mathrm{signed}}}}_{N}^{{-1}}}}{(i)} \\
\end{array}\end{split}\]
Note
The side conditions \(N > 7\) in the productions for non-terminal bytes of the \({{\href{../syntax/values.html#syntax-int}{\mathit{u}\kern-0.1em}}}{N}\) and \({{\href{../syntax/values.html#syntax-int}{\mathit{s}\kern-0.1em}}}{N}\) encodings restrict the encoding’s length.
However, “trailing zeros” are still allowed within these bounds.
For example, \(\mathtt{0x03}\) and \(\mathtt{0x83}~\mathtt{0x00}\) are both well-formed encodings for the value \(3\) as a \({\href{../syntax/values.html#syntax-int}{\mathit{u\scriptstyle\kern-0.1em8}}}\).
Similarly, either of \(\mathtt{0x7E}\) and \(\mathtt{0xFE}~\mathtt{0x7F}\) and \(\mathtt{0xFE}~\mathtt{0xFF}~\mathtt{0x7F}\) are well-formed encodings of the value \({-2}\) as an \({\mathit{s{\kern-0.1em\scriptstyle 16}}}\).
The side conditions on the value \(n\) of terminal bytes further enforce that
any unused bits in these bytes must be \(0\) for positive values and \(1\) for negative ones.
For example, \(\mathtt{0x83}~\mathtt{0x10}\) is malformed as a \({\href{../syntax/values.html#syntax-int}{\mathit{u\scriptstyle\kern-0.1em8}}}\) encoding.
Similarly, both \(\mathtt{0x83}~\mathtt{0x3E}\) and \(\mathtt{0xFF}~\mathtt{0x7B}\) are malformed as \({\mathit{s{\kern-0.1em\scriptstyle 8}}}\) encodings.
Names
Names are encoded as a list of bytes containing the Unicode (Section 3.9) UTF-8 encoding of the name’s character sequence.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/values.html#binary-name}{\mathtt{name}}} & ::= & {b^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/values.html#binary-byte}{\mathtt{byte}}}) & \quad\Rightarrow\quad{} & {\href{../syntax/values.html#syntax-name}{\mathit{name}}} & \quad \mbox{if}~ {\href{../binary/values.html#binary-utf8}{\mathrm{utf\scriptstyle8}}}({\href{../syntax/values.html#syntax-name}{\mathit{name}}}) = {b^\ast} \\
\end{array}\end{split}\]
The auxiliary \({\href{../binary/values.html#binary-utf8}{\mathrm{utf\scriptstyle8}}}\) function expressing this encoding is defined as follows:
\[\begin{split}\begin{array}[t]{@{}lcl@{}l@{}}
{\href{../binary/values.html#binary-utf8}{\mathrm{utf\scriptstyle8}}}({{\mathit{ch}}^\ast}) & = & {\href{../syntax/conventions.html#notation-concat}{\bigoplus}}\, {{\href{../binary/values.html#binary-utf8}{\mathrm{utf\scriptstyle8}}}({\mathit{ch}})^\ast} \\
{\href{../binary/values.html#binary-utf8}{\mathrm{utf\scriptstyle8}}}({\mathit{ch}}) & = & b & \quad
\begin{array}[t]{@{}l@{}}
\mbox{if}~ {\mathit{ch}} < \mathrm{U{+}80} \\
{\land}~ {\mathit{ch}} = b \\
\end{array} \\
{\href{../binary/values.html#binary-utf8}{\mathrm{utf\scriptstyle8}}}({\mathit{ch}}) & = & b_1~b_2 & \quad
\begin{array}[t]{@{}l@{}}
\mbox{if}~ \mathrm{U{+}80} \leq {\mathit{ch}} < \mathrm{U{+}0800} \\
{\land}~ {\mathit{ch}} = {2^{6}} \cdot (b_1 - \mathtt{0xC0}) + {\mathrm{cont}}(b_2) \\
\end{array} \\
{\href{../binary/values.html#binary-utf8}{\mathrm{utf\scriptstyle8}}}({\mathit{ch}}) & = & b_1~b_2~b_3 & \quad
\begin{array}[t]{@{}l@{}}
\mbox{if}~ \mathrm{U{+}0800} \leq {\mathit{ch}} < \mathrm{U{+}D800} \lor \mathrm{U{+}E000} \leq {\mathit{ch}} < \mathrm{U{+}10000} \\
{\land}~ {\mathit{ch}} = {2^{12}} \cdot (b_1 - \mathtt{0xE0}) + {2^{6}} \cdot {\mathrm{cont}}(b_2) + {\mathrm{cont}}(b_3) \\
\end{array} \\
{\href{../binary/values.html#binary-utf8}{\mathrm{utf\scriptstyle8}}}({\mathit{ch}}) & = & b_1~b_2~b_3~b_4 & \quad
\begin{array}[t]{@{}l@{}}
\mbox{if}~ \mathrm{U{+}10000} \leq {\mathit{ch}} < \mathrm{U{+}11000} \\
{\land}~ {\mathit{ch}} = {2^{18}} \cdot (b_1 - \mathtt{0xF0}) + {2^{12}} \cdot {\mathrm{cont}}(b_2) + {2^{6}} \cdot {\mathrm{cont}}(b_3) + {\mathrm{cont}}(b_4) \\
\end{array} \\
\end{array}\end{split}\]
where \(\begin{array}[t]{@{}l@{~}c@{~}l@{}l@{}} {\mathrm{cont}}(b) & = & b - \mathtt{0x80} & \quad \mbox{if}~ (\mathtt{0x80} < b < \mathtt{0xC0}) \\ \end{array}\)
Note
Unlike in some other formats, name strings are not 0-terminated.