|
| 1 | +// Copyright 2024 The quic-go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file of |
| 4 | +// the quic-go repository. |
| 5 | + |
| 6 | +package quicvarint |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + |
| 12 | + "github.com/refraction-networking/utls/internal/quicvarint/protocol" |
| 13 | +) |
| 14 | + |
| 15 | +// taken from the QUIC draft |
| 16 | +const ( |
| 17 | + // Min is the minimum value allowed for a QUIC varint. |
| 18 | + Min = 0 |
| 19 | + |
| 20 | + // Max is the maximum allowed value for a QUIC varint (2^62-1). |
| 21 | + Max = maxVarInt8 |
| 22 | + |
| 23 | + maxVarInt1 = 63 |
| 24 | + maxVarInt2 = 16383 |
| 25 | + maxVarInt4 = 1073741823 |
| 26 | + maxVarInt8 = 4611686018427387903 |
| 27 | +) |
| 28 | + |
| 29 | +// Read reads a number in the QUIC varint format from r. |
| 30 | +func Read(r io.ByteReader) (uint64, error) { |
| 31 | + firstByte, err := r.ReadByte() |
| 32 | + if err != nil { |
| 33 | + return 0, err |
| 34 | + } |
| 35 | + // the first two bits of the first byte encode the length |
| 36 | + len := 1 << ((firstByte & 0xc0) >> 6) |
| 37 | + b1 := firstByte & (0xff - 0xc0) |
| 38 | + if len == 1 { |
| 39 | + return uint64(b1), nil |
| 40 | + } |
| 41 | + b2, err := r.ReadByte() |
| 42 | + if err != nil { |
| 43 | + return 0, err |
| 44 | + } |
| 45 | + if len == 2 { |
| 46 | + return uint64(b2) + uint64(b1)<<8, nil |
| 47 | + } |
| 48 | + b3, err := r.ReadByte() |
| 49 | + if err != nil { |
| 50 | + return 0, err |
| 51 | + } |
| 52 | + b4, err := r.ReadByte() |
| 53 | + if err != nil { |
| 54 | + return 0, err |
| 55 | + } |
| 56 | + if len == 4 { |
| 57 | + return uint64(b4) + uint64(b3)<<8 + uint64(b2)<<16 + uint64(b1)<<24, nil |
| 58 | + } |
| 59 | + b5, err := r.ReadByte() |
| 60 | + if err != nil { |
| 61 | + return 0, err |
| 62 | + } |
| 63 | + b6, err := r.ReadByte() |
| 64 | + if err != nil { |
| 65 | + return 0, err |
| 66 | + } |
| 67 | + b7, err := r.ReadByte() |
| 68 | + if err != nil { |
| 69 | + return 0, err |
| 70 | + } |
| 71 | + b8, err := r.ReadByte() |
| 72 | + if err != nil { |
| 73 | + return 0, err |
| 74 | + } |
| 75 | + return uint64(b8) + uint64(b7)<<8 + uint64(b6)<<16 + uint64(b5)<<24 + uint64(b4)<<32 + uint64(b3)<<40 + uint64(b2)<<48 + uint64(b1)<<56, nil |
| 76 | +} |
| 77 | + |
| 78 | +// Append appends i in the QUIC varint format. |
| 79 | +func Append(b []byte, i uint64) []byte { |
| 80 | + if i <= maxVarInt1 { |
| 81 | + return append(b, uint8(i)) |
| 82 | + } |
| 83 | + if i <= maxVarInt2 { |
| 84 | + return append(b, []byte{uint8(i>>8) | 0x40, uint8(i)}...) |
| 85 | + } |
| 86 | + if i <= maxVarInt4 { |
| 87 | + return append(b, []byte{uint8(i>>24) | 0x80, uint8(i >> 16), uint8(i >> 8), uint8(i)}...) |
| 88 | + } |
| 89 | + if i <= maxVarInt8 { |
| 90 | + return append(b, []byte{ |
| 91 | + uint8(i>>56) | 0xc0, uint8(i >> 48), uint8(i >> 40), uint8(i >> 32), |
| 92 | + uint8(i >> 24), uint8(i >> 16), uint8(i >> 8), uint8(i), |
| 93 | + }...) |
| 94 | + } |
| 95 | + panic(fmt.Sprintf("%#x doesn't fit into 62 bits", i)) |
| 96 | +} |
| 97 | + |
| 98 | +// AppendWithLen append i in the QUIC varint format with the desired length. |
| 99 | +func AppendWithLen(b []byte, i uint64, length protocol.ByteCount) []byte { |
| 100 | + if length != 1 && length != 2 && length != 4 && length != 8 { |
| 101 | + panic("invalid varint length") |
| 102 | + } |
| 103 | + l := Len(i) |
| 104 | + if l == length { |
| 105 | + return Append(b, i) |
| 106 | + } |
| 107 | + if l > length { |
| 108 | + panic(fmt.Sprintf("cannot encode %d in %d bytes", i, length)) |
| 109 | + } |
| 110 | + if length == 2 { |
| 111 | + b = append(b, 0b01000000) |
| 112 | + } else if length == 4 { |
| 113 | + b = append(b, 0b10000000) |
| 114 | + } else if length == 8 { |
| 115 | + b = append(b, 0b11000000) |
| 116 | + } |
| 117 | + for j := protocol.ByteCount(1); j < length-l; j++ { |
| 118 | + b = append(b, 0) |
| 119 | + } |
| 120 | + for j := protocol.ByteCount(0); j < l; j++ { |
| 121 | + b = append(b, uint8(i>>(8*(l-1-j)))) |
| 122 | + } |
| 123 | + return b |
| 124 | +} |
| 125 | + |
| 126 | +// Len determines the number of bytes that will be needed to write the number i. |
| 127 | +func Len(i uint64) protocol.ByteCount { |
| 128 | + if i <= maxVarInt1 { |
| 129 | + return 1 |
| 130 | + } |
| 131 | + if i <= maxVarInt2 { |
| 132 | + return 2 |
| 133 | + } |
| 134 | + if i <= maxVarInt4 { |
| 135 | + return 4 |
| 136 | + } |
| 137 | + if i <= maxVarInt8 { |
| 138 | + return 8 |
| 139 | + } |
| 140 | + // Don't use a fmt.Sprintf here to format the error message. |
| 141 | + // The function would then exceed the inlining budget. |
| 142 | + panic(struct { |
| 143 | + message string |
| 144 | + num uint64 |
| 145 | + }{"value doesn't fit into 62 bits: ", i}) |
| 146 | +} |
0 commit comments