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 value of type \(\href{../syntax/values.html#syntax-int}{\mathit{u}N}\) must not exceed \(\mathrm{ceil}(N/7)\) bytes.
\[\begin{split}\begin{array}{llclll@{\qquad}l}
\def\mathdef2224#1{{}}\mathdef2224{unsigned integer} & \href{../binary/values.html#binary-int}{\def\mathdef2225#1{{\mathtt{u}#1}}\mathdef2225{N}} &::=&
n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}} &\Rightarrow& n & (\mathrel{\mbox{if}} n < 2^7 \wedge n < 2^N) \\ &&|&
n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}}~~m{:}\def\mathdef2267#1{{\mathtt{u}#1}}\mathdef2267{(N\mathtt{-7})} &\Rightarrow&
2^7\cdot m + (n-2^7) & (\mathrel{\mbox{if}} n \geq 2^7 \wedge 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 a value of type \(\href{../syntax/values.html#syntax-int}{\mathit{s}N}\) must not exceed \(\mathrm{ceil}(N/7)\) bytes.
\[\begin{split}\begin{array}{llclll@{\qquad}l}
\def\mathdef2224#1{{}}\mathdef2224{signed integer} & \href{../binary/values.html#binary-int}{\def\mathdef2231#1{{\mathtt{s}#1}}\mathdef2231{N}} &::=&
n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}} &\Rightarrow& n & (\mathrel{\mbox{if}} n < 2^6 \wedge n < 2^{N-1}) \\ &&|&
n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}} &\Rightarrow& n-2^7 & (\mathrel{\mbox{if}} 2^6 \leq n < 2^7 \wedge n \geq 2^7-2^{N-1}) \\ &&|&
n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}}~~m{:}\def\mathdef2268#1{{\mathtt{s}#1}}\mathdef2268{(N\mathtt{-7})} &\Rightarrow&
2^7\cdot m + (n-2^7) & (\mathrel{\mbox{if}} n \geq 2^7 \wedge N > 7) \\
\end{array}\end{split}\]
Uninterpreted integers are encoded as signed integers.
\[\begin{array}{llclll@{\qquad\qquad}l}
\def\mathdef2224#1{{}}\mathdef2224{uninterpreted integer} & \href{../binary/values.html#binary-int}{\def\mathdef2236#1{{\mathtt{i}#1}}\mathdef2236{N}} &::=&
n{:}\href{../binary/values.html#binary-int}{\def\mathdef2231#1{{\mathtt{s}#1}}\mathdef2231{N}} &\Rightarrow& i & (\mathrel{\mbox{if}} n = \href{../exec/numerics.html#aux-signed}{\mathrm{signed}}_N(i))
\end{array}\]
Note
The side conditions \(N > 7\) in the productions for non-terminal bytes of the \(\def\mathdef2269#1{{\mathit{u#1}}}\mathdef2269{}\) and \(\def\mathdef2270#1{{\mathit{s#1}}}\mathdef2270{}\) encodings restrict the encoding’s length.
However, “trailing zeros” are still allowed within these bounds.
For example, \(\def\mathdef2271#1{\mathtt{0x#1}}\mathdef2271{03}\) and \(\def\mathdef2272#1{\mathtt{0x#1}}\mathdef2272{83}~\def\mathdef2273#1{\mathtt{0x#1}}\mathdef2273{00}\) are both well-formed encodings for the value \(3\) as a \(\href{../syntax/values.html#syntax-int}{\mathit{u8}}\).
Similarly, either of \(\def\mathdef2274#1{\mathtt{0x#1}}\mathdef2274{7e}\) and \(\def\mathdef2275#1{\mathtt{0x#1}}\mathdef2275{FE}~\def\mathdef2276#1{\mathtt{0x#1}}\mathdef2276{7F}\) and \(\def\mathdef2277#1{\mathtt{0x#1}}\mathdef2277{FE}~\def\mathdef2278#1{\mathtt{0x#1}}\mathdef2278{FF}~\def\mathdef2279#1{\mathtt{0x#1}}\mathdef2279{7F}\) are well-formed encodings of the value \(-2\) as a \(\href{../syntax/values.html#syntax-int}{\mathit{s16}}\).
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, \(\def\mathdef2280#1{\mathtt{0x#1}}\mathdef2280{83}~\def\mathdef2281#1{\mathtt{0x#1}}\mathdef2281{10}\) is malformed as a \(\href{../syntax/values.html#syntax-int}{\mathit{u8}}\) encoding.
Similarly, both \(\def\mathdef2282#1{\mathtt{0x#1}}\mathdef2282{83}~\def\mathdef2283#1{\mathtt{0x#1}}\mathdef2283{3E}\) and \(\def\mathdef2284#1{\mathtt{0x#1}}\mathdef2284{FF}~\def\mathdef2285#1{\mathtt{0x#1}}\mathdef2285{7B}\) are malformed as \(\href{../syntax/values.html#syntax-int}{\mathit{s8}}\) encodings.
Names
Names are encoded as a vector of bytes containing the Unicode (Section 3.9) UTF-8 encoding of the name’s character sequence.
\[\begin{split}\begin{array}{llclllll}
\def\mathdef2224#1{{}}\mathdef2224{name} & \href{../binary/values.html#binary-name}{\mathtt{name}} &::=&
b^\ast{:}\href{../binary/conventions.html#binary-vec}{\mathtt{vec}}(\href{../binary/values.html#binary-byte}{\mathtt{byte}}) &\Rightarrow& \href{../syntax/values.html#syntax-name}{\mathit{name}}
&& (\mathrel{\mbox{if}} \href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(\href{../syntax/values.html#syntax-name}{\mathit{name}}) = b^\ast) \\
\end{array}\end{split}\]
The auxiliary \(\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}\) function expressing this encoding is defined as follows:
\[\begin{split}\begin{array}{@{}l@{}}
\begin{array}{@{}lcl@{\qquad}l@{}}
\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c^\ast) &=& (\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c))^\ast \\[1ex]
\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b &
(\begin{array}[t]{@{}c@{~}l@{}}
\mathrel{\mbox{if}} & c < \def\mathdef2286#1{\mathrm{U{+}#1}}\mathdef2286{80} \\
\wedge & c = b) \\
\end{array} \\
\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b_1~b_2 &
(\begin{array}[t]{@{}c@{~}l@{}}
\mathrel{\mbox{if}} & \def\mathdef2287#1{\mathrm{U{+}#1}}\mathdef2287{80} \leq c < \def\mathdef2288#1{\mathrm{U{+}#1}}\mathdef2288{800} \\
\wedge & c = 2^6(b_1-\def\mathdef2289#1{\mathtt{0x#1}}\mathdef2289{C0})+(b_2-\def\mathdef2290#1{\mathtt{0x#1}}\mathdef2290{80})) \\
\end{array} \\
\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b_1~b_2~b_3 &
(\begin{array}[t]{@{}c@{~}l@{}}
\mathrel{\mbox{if}} & \def\mathdef2291#1{\mathrm{U{+}#1}}\mathdef2291{800} \leq c < \def\mathdef2292#1{\mathrm{U{+}#1}}\mathdef2292{D800} \vee \def\mathdef2293#1{\mathrm{U{+}#1}}\mathdef2293{E000} \leq c < \def\mathdef2294#1{\mathrm{U{+}#1}}\mathdef2294{10000} \\
\wedge & c = 2^{12}(b_1-\def\mathdef2295#1{\mathtt{0x#1}}\mathdef2295{E0})+2^6(b_2-\def\mathdef2296#1{\mathtt{0x#1}}\mathdef2296{80})+(b_3-\def\mathdef2297#1{\mathtt{0x#1}}\mathdef2297{80})) \\
\end{array} \\
\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b_1~b_2~b_3~b_4 &
(\begin{array}[t]{@{}c@{~}l@{}}
\mathrel{\mbox{if}} & \def\mathdef2298#1{\mathrm{U{+}#1}}\mathdef2298{10000} \leq c < \def\mathdef2299#1{\mathrm{U{+}#1}}\mathdef2299{110000} \\
\wedge & c = 2^{18}(b_1-\def\mathdef2300#1{\mathtt{0x#1}}\mathdef2300{F0})+2^{12}(b_2-\def\mathdef2301#1{\mathtt{0x#1}}\mathdef2301{80})+2^6(b_3-\def\mathdef2302#1{\mathtt{0x#1}}\mathdef2302{80})+(b_4-\def\mathdef2303#1{\mathtt{0x#1}}\mathdef2303{80})) \\
\end{array} \\
\end{array} \\
\mathrel{\mbox{where}} b_2, b_3, b_4 < \def\mathdef2304#1{\mathtt{0x#1}}\mathdef2304{C0} \\
\end{array}\end{split}\]
Note
Unlike in some other formats, name strings are not 0-terminated.