|
| 1 | +/** |
| 2 | + * BinaryEncoder implements encoders for all the wire types specified in |
| 3 | + * https://developers.google.com/protocol-buffers/docs/encoding. |
| 4 | + */ |
| 5 | +export class BinaryEncoder { |
| 6 | + private buffer: number[] = []; |
| 7 | + |
| 8 | + get Length() { |
| 9 | + return this.buffer.length; |
| 10 | + } |
| 11 | + |
| 12 | + get Buffer() { |
| 13 | + return this.buffer; |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Encodes a 32-bit unsigned integer into its wire-format varint representation |
| 18 | + * and stores it in the buffer. |
| 19 | + */ |
| 20 | + UnsignedVarint32(value: number) { |
| 21 | + while (value > 127) { |
| 22 | + this.buffer.push((value & 0x7f) | 0x80); |
| 23 | + value = value >>> 7; |
| 24 | + } |
| 25 | + this.buffer.push(value); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Encodes a 32-bit signed integer into its wire-format varint representation |
| 30 | + * and stores it in the buffer. |
| 31 | + */ |
| 32 | + Varint32(v: number) { |
| 33 | + // Use the unsigned version if the value is not negative. |
| 34 | + if (v >= 0) { |
| 35 | + this.UnsignedVarint32(v); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Write nine bytes with a _signed_ right shift so we preserve the sign bit. |
| 40 | + for (var i = 0; i < 4; i++) { |
| 41 | + this.buffer.push((v & 0x7f) | 0x80); |
| 42 | + v = v >> 7; |
| 43 | + } |
| 44 | + |
| 45 | + // The above loop writes out 28 bits, so the last byte is always the sign bit |
| 46 | + // which is always set for negative numbers. |
| 47 | + this.buffer.push(v); |
| 48 | + this.buffer.push(1); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Encodes a 64-bit unsigned integer into its wire-format varint representation |
| 53 | + * and stores it in the buffer. Integers that are not representable in 64 bits |
| 54 | + * will be truncated. |
| 55 | + */ |
| 56 | + UnsignedVarint(v: bigint) { |
| 57 | + while (v > 127) { |
| 58 | + this.buffer.push(Number((v & 0x7fn) | 0x80n)); |
| 59 | + v = v >> 7n; |
| 60 | + } |
| 61 | + this.buffer.push(Number(v)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Encodes a 64-bit signed integer into its wire-format varint representation |
| 66 | + * and stores it in the buffer. Integers that are not representable in 64 bits |
| 67 | + * will be truncated. |
| 68 | + */ |
| 69 | + Varint(v: bigint) { |
| 70 | + // Use the unsigned version if the value is not negative. |
| 71 | + if (v >= 0n) { |
| 72 | + this.UnsignedVarint(v); |
| 73 | + return; |
| 74 | + } |
| 75 | + // Write nine bytes with a _signed_ right shift so we preserve the sign bit. |
| 76 | + for (var i = 0; i < 9; i++) { |
| 77 | + this.buffer.push(Number((v & 0x7fn) | 0x80n)); |
| 78 | + v = v >> 7n; |
| 79 | + } |
| 80 | + // The above loop writes out 28 bits, so the last byte is always the sign bit |
| 81 | + // which is always set for negative numbers. |
| 82 | + this.buffer.push(1); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Encodes a JavaScript integer into its wire-format, zigzag-encoded varint |
| 87 | + * representation and stores it in the buffer. |
| 88 | + */ |
| 89 | + Zigzag32(value: number) { |
| 90 | + this.UnsignedVarint32(((value << 1) ^ (value >> 31)) >>> 0); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Encodes a JavaScript integer into its wire-format, zigzag-encoded varint |
| 95 | + * representation and stores it in the buffer. Integers not representable in 64 |
| 96 | + * bits will be truncated. |
| 97 | + */ |
| 98 | + Zigzag64(x: bigint) { |
| 99 | + this.Varint((x << 1n) ^ (x >> 63n)); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Writes a unsigned 32-bit integer to the buffer. |
| 104 | + */ |
| 105 | + Fixed32(value: number) { |
| 106 | + this.buffer.push((value >>> 0) & 0xff); |
| 107 | + this.buffer.push((value >>> 8) & 0xff); |
| 108 | + this.buffer.push((value >>> 16) & 0xff); |
| 109 | + this.buffer.push((value >>> 24) & 0xff); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Writes a unsigned 64-bit integer to the buffer. |
| 114 | + */ |
| 115 | + Fixed64(value: bigint) { |
| 116 | + this.buffer.push(Number(value >> 0n)); |
| 117 | + this.buffer.push(Number(value >> 8n)); |
| 118 | + this.buffer.push(Number(value >> 16n)); |
| 119 | + this.buffer.push(Number(value >> 24n)); |
| 120 | + this.buffer.push(Number(value >> 32n)); |
| 121 | + this.buffer.push(Number(value >> 40n)); |
| 122 | + this.buffer.push(Number(value >> 48n)); |
| 123 | + this.buffer.push(Number(value >> 56n)); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Writes a single-precision floating point value to the buffer. Numbers |
| 128 | + * requiring more than 32 bits of precision will be truncated. |
| 129 | + */ |
| 130 | + Float(value: number) { |
| 131 | + const frame = new ArrayBuffer(4); |
| 132 | + new DataView(frame).setFloat32(0, value, true); |
| 133 | + this.buffer.push(...new Uint8Array(frame)); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Writes a double-precision floating point value to the buffer. As this is |
| 138 | + * the native format used by JavaScript, no precision will be lost. |
| 139 | + */ |
| 140 | + Double(value: number) { |
| 141 | + const frame = new ArrayBuffer(8); |
| 142 | + new DataView(frame).setFloat64(0, value, true); |
| 143 | + this.buffer.push(...new Uint8Array(frame)); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Writes a boolean value to the buffer as a varint. |
| 148 | + */ |
| 149 | + Bool(value: boolean) { |
| 150 | + this.buffer.push(value ? 1 : 0); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Writes an enum value to the buffer as a varint. |
| 155 | + */ |
| 156 | + Enum(value: number) { |
| 157 | + this.Varint32(value); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Writes an arbitrary byte array to the buffer. |
| 162 | + */ |
| 163 | + Bytes(bytes: Uint8Array) { |
| 164 | + this.buffer.push.apply(bytes); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Writes a UTF16 Javascript string to the buffer encoded as UTF8. |
| 169 | + * Returns length of encoded string. |
| 170 | + */ |
| 171 | + String(str: string): number { |
| 172 | + const oLength = this.buffer.length; |
| 173 | + // https://gist.github.com/joni/3760795#file-toutf8array-js |
| 174 | + for (var i = 0; i < str.length; i++) { |
| 175 | + let charcode = str.charCodeAt(i); |
| 176 | + if (charcode < 0x80) this.buffer.push(charcode); |
| 177 | + else if (charcode < 0x800) { |
| 178 | + this.buffer.push(0xc0 | (charcode >> 6), 0x80 | (charcode & 0x3f)); |
| 179 | + } else if (charcode < 0xd800 || charcode >= 0xe000) { |
| 180 | + this.buffer.push( |
| 181 | + 0xe0 | (charcode >> 12), |
| 182 | + 0x80 | ((charcode >> 6) & 0x3f), |
| 183 | + 0x80 | (charcode & 0x3f) |
| 184 | + ); |
| 185 | + } |
| 186 | + // surrogate pair |
| 187 | + else { |
| 188 | + i++; |
| 189 | + // UTF-16 encodes 0x10000-0x10FFFF by |
| 190 | + // subtracting 0x10000 and splitting the |
| 191 | + // 20 bits of 0x0-0xFFFFF into two halves |
| 192 | + charcode = 0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff)); |
| 193 | + this.buffer.push( |
| 194 | + 0xf0 | (charcode >> 18), |
| 195 | + 0x80 | ((charcode >> 12) & 0x3f), |
| 196 | + 0x80 | ((charcode >> 6) & 0x3f), |
| 197 | + 0x80 | (charcode & 0x3f) |
| 198 | + ); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return this.buffer.length - oLength; |
| 203 | + } |
| 204 | +} |
0 commit comments