( input: Uint8Array, aI: number, offset: number, )
| 76 | } |
| 77 | |
| 78 | function decodeTwo( |
| 79 | input: Uint8Array, |
| 80 | aI: number, |
| 81 | offset: number, |
| 82 | ): [Uint8Array, number] { |
| 83 | if (aI < 24) return [input.subarray(offset, offset + aI), offset + aI]; |
| 84 | if (aI <= 27) { |
| 85 | const x = calcLength(input, aI, offset); |
| 86 | const start = x[1]; |
| 87 | const end = Number(x[0]) + x[1]; |
| 88 | return [input.subarray(start, end), end]; |
| 89 | } |
| 90 | if (aI === 31) { |
| 91 | let byte = input[offset++]; |
| 92 | if (byte == undefined) throw new RangeError("More bytes were expected"); |
| 93 | |
| 94 | const output: Uint8Array[] = []; |
| 95 | while (byte !== 0b111_11111) { |
| 96 | if (byte >> 5 !== 2) { |
| 97 | throw new TypeError( |
| 98 | `Cannot decode value (0b${(byte >> 5).toString(2).padStart(3, "0")}_${ |
| 99 | (byte & 0b11111).toString(2).padStart(5, "0") |
| 100 | }) inside an indefinite length byte string`, |
| 101 | ); |
| 102 | } |
| 103 | const aI = byte & 0b000_11111; |
| 104 | if (aI === 31) { |
| 105 | throw new TypeError( |
| 106 | "Indefinite length byte strings cannot contain indefinite length byte strings", |
| 107 | ); |
| 108 | } |
| 109 | const x = decodeTwo(input, aI, offset); |
| 110 | output.push(x[0]); |
| 111 | offset = x[1]; |
| 112 | byte = input[offset++]; |
| 113 | if (byte == undefined) throw new RangeError("More bytes were expected"); |
| 114 | } |
| 115 | return [concat(output), offset + 1]; |
| 116 | } |
| 117 | throw new RangeError( |
| 118 | `Cannot decode value (0b010_${aI.toString(2).padStart(5, "0")})`, |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | function decodeThree( |
| 123 | input: Uint8Array, |
no test coverage detected