Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VarInt decoding pseudocode #4316

Merged
merged 5 commits into from Nov 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 29 additions & 4 deletions draft-ietf-quic-transport.md
Expand Up @@ -4345,10 +4345,7 @@ encoding properties.
| 11 | 8 | 62 | 0-4611686018427387903 |
{: #integer-summary title="Summary of Integer Encodings"}

For example, the eight byte sequence c2 19 7c 5e ff 14 e8 8c (in hexadecimal)
decodes to the decimal value 151288809941952652; the four byte sequence 9d 7f 3e
7d decodes to 494878333; the two byte sequence 7b bd decodes to 15293; and the
single byte 25 decodes to 37 (as does the two byte sequence 40 25).
Examples and a sample decoding algorithm are shown in {{sample-varint}}.

Versions ({{versions}}) and packet numbers sent in the header
({{packet-encoding}}) are described using integers, but do not use this
Expand Down Expand Up @@ -7486,6 +7483,34 @@ ECN to be disabled. For those rare cases where marked packets are discarded by
the path, the short duration of the testing period limits the number of losses
incurred.

# Sample Variable-Length Integer Decoding {#sample-varint}

The pseudo-code in {{alg-varint}} shows how a variable-length integer can be
read from a stream of bytes. The function ReadVarint takes a single argument, a
sequence of bytes which can be read in network byte order.

~~~
ReadVarint(data):
// The length of variable-length integers is encoded in the
// first two bits of the first byte.
v = data.next_byte()
prefix = v >> 6
length = 1 << prefix

// Once the length is known, remove these bits and read any
// remaining bytes.
v = v & 0x3f
repeat length-1 times:
v = (v << 8) + data.next_byte()
return v
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v is also in network byte-order, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I'm not sure that's necessary to mention. We haven't changed it over the course of the algorithm.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could it not be in network byte order? Unless next_byte does something other than read the next byte.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is that we might want to say that the value still needs to converted into host byte order before it can be used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this do that? I mean, the operations here are numeric, so unless << means "divide by 2^n", we're good, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah -- there's nothing here that is endian-specific.

~~~
{: #alg-varint title="Sample Variable-Length Integer Decoding Algorithm"}

For example, the eight-byte sequence 0xc2197c5eff14e88c decodes to the decimal
value 151,288,809,941,952,652; the four-byte sequence 0x9d7f3e7d decodes to
494,878,333; the two-byte sequence 0x7bbd decodes to 15,293; and the single byte
0x25 decodes to 37 (as does the two-byte sequence 0x4025).



# Change Log
Expand Down