(reader: BinaryReader)
| 146 | } |
| 147 | |
| 148 | function readDecimal(reader: BinaryReader): Decimal { |
| 149 | const scale = reader.readVarInt32(); |
| 150 | const header = reader.readVarUInt64(); |
| 151 | if ((header & 1n) === 0n) { |
| 152 | return new Decimal(DecimalCodec.decodeZigZag64(header >> 1n), scale); |
| 153 | } |
| 154 | const meta = header >> 1n; |
| 155 | const length = Number(meta >> 1n); |
| 156 | if (length <= 0 || length > 0x7fffffff) { |
| 157 | throw new Error(`Invalid decimal magnitude length ${length}.`); |
| 158 | } |
| 159 | const magnitudeBytes = reader.buffer(length); |
| 160 | if (magnitudeBytes[length - 1] === 0) { |
| 161 | throw new Error( |
| 162 | "Non-canonical decimal magnitude bytes: trailing zero byte.", |
| 163 | ); |
| 164 | } |
| 165 | const magnitude |
| 166 | = DecimalCodec.fromCanonicalLittleEndianMagnitude(magnitudeBytes); |
| 167 | if (magnitude === 0n) { |
| 168 | throw new Error("Big decimal encoding must not represent zero."); |
| 169 | } |
| 170 | return new Decimal((meta & 1n) === 0n ? magnitude : -magnitude, scale); |
| 171 | } |
| 172 | |
| 173 | function pow10(exp: number): bigint { |
| 174 | let result = 1n; |
no test coverage detected