Skip to content

Commit

Permalink
Reorder pseudocode sections
Browse files Browse the repository at this point in the history
  • Loading branch information
martinthomson committed Nov 4, 2020
1 parent e6140bb commit caf1e12
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions draft-ietf-quic-transport.md
Expand Up @@ -7479,6 +7479,36 @@ intended to be correct and clear, rather than being optimally performant.
The pseudocode segments in this section are licensed as Code Components; see the
copyright notice.


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

The pseudocode 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).


## Sample Packet Number Encoding Algorithm {#sample-packet-number-encoding}

The pseudocode in {{alg-encode-pn}} shows how an implementation can select
Expand Down Expand Up @@ -7519,6 +7549,7 @@ In the same state, sending a packet with a number of 0xace8fe uses the 24-bit
encoding, because at least 18 bits are required to represent twice the range
(131,182 packets, or 0x2006e).


## Sample Packet Number Decoding Algorithm {#sample-packet-number-decoding}

The pseudocode in {{alg-decode-pn}} includes an example algorithm for decoding
Expand Down Expand Up @@ -7605,34 +7636,6 @@ 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 pseudocode 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

Expand Down

0 comments on commit caf1e12

Please sign in to comment.