diff --git a/draft-ietf-quic-transport.md b/draft-ietf-quic-transport.md index b242445ee7..ab73a44897 100644 --- a/draft-ietf-quic-transport.md +++ b/draft-ietf-quic-transport.md @@ -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 @@ -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 +~~~ +{: #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