@pairdraw/kiwi 0.16.2 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,14 @@ import { deflateSync, inflateSync } from "fflate";
3
3
  let int32 = /* @__PURE__ */ new Int32Array(1);
4
4
  let float32 = new Float32Array(int32.buffer);
5
5
  const textDecoder = new TextDecoder();
6
+ /**
7
+ * A read that would pass the end of the bytes. A varint is checked once, after its last byte
8
+ * rather than at each: past its end a typed array yields `undefined`, which ends the varint, so
9
+ * one that ran over finishes past `length` and is refused before its value is used.
10
+ */
11
+ function truncated() {
12
+ throw new Error("Unexpected end of Kiwi message");
13
+ }
6
14
  var ByteBuffer = class {
7
15
  _data;
8
16
  _index;
@@ -35,16 +43,21 @@ var ByteBuffer = class {
35
43
  return this._data.subarray(0, this.length);
36
44
  }
37
45
  readByte() {
38
- return this._data[this._index++];
46
+ const index = this._index;
47
+ if (index >= this.length) truncated();
48
+ this._index = index + 1;
49
+ return this._data[index];
39
50
  }
40
51
  readByteArray() {
41
52
  const length = this.readVarUint();
42
53
  const start = this._index;
54
+ if (length > this.length - start) truncated();
43
55
  this._index = start + length;
44
56
  return this._data.slice(start, start + length);
45
57
  }
46
58
  skipByteArray() {
47
59
  const length = this.readVarUint();
60
+ if (length > this.length - this._index) truncated();
48
61
  this._index += length;
49
62
  }
50
63
  readVarFloat() {
@@ -52,9 +65,11 @@ var ByteBuffer = class {
52
65
  const data = this._data;
53
66
  const first = data[index];
54
67
  if (first === 0) {
68
+ if (index >= this.length) truncated();
55
69
  this._index = index + 1;
56
70
  return 0;
57
71
  }
72
+ if (index + 4 > this.length) truncated();
58
73
  let bits = first | data[index + 1] << 8 | data[index + 2] << 16 | data[index + 3] << 24;
59
74
  this._index = index + 4;
60
75
  bits = bits << 23 | bits >>> 9;
@@ -66,32 +81,25 @@ var ByteBuffer = class {
66
81
  let i = this._index;
67
82
  let b = data[i++];
68
83
  let value = b & 127;
69
- if (b < 128) {
70
- this._index = i;
71
- return value;
72
- }
73
- b = data[i++];
74
- value |= (b & 127) << 7;
75
- if (b < 128) {
76
- this._index = i;
77
- return value;
78
- }
79
- b = data[i++];
80
- value |= (b & 127) << 14;
81
- if (b < 128) {
82
- this._index = i;
83
- return value;
84
- }
85
- b = data[i++];
86
- value |= (b & 127) << 21;
87
- if (b < 128) {
88
- this._index = i;
89
- return value;
84
+ if (b > 127) {
85
+ b = data[i++];
86
+ value |= (b & 127) << 7;
87
+ if (b > 127) {
88
+ b = data[i++];
89
+ value |= (b & 127) << 14;
90
+ if (b > 127) {
91
+ b = data[i++];
92
+ value |= (b & 127) << 21;
93
+ if (b > 127) {
94
+ b = data[i++];
95
+ value = (value | (b & 127) << 28) >>> 0;
96
+ }
97
+ }
98
+ }
90
99
  }
91
- b = data[i++];
92
- value |= (b & 127) << 28;
100
+ if (i > this.length) truncated();
93
101
  this._index = i;
94
- return value >>> 0;
102
+ return value;
95
103
  }
96
104
  readVarInt() {
97
105
  let value = this.readVarUint() | 0;
@@ -128,9 +136,10 @@ var ByteBuffer = class {
128
136
  }
129
137
  findStringTerminator(start) {
130
138
  const data = this._data;
139
+ const end = this.length;
131
140
  let index = start;
132
- while (index < data.length && data[index] !== 0) index++;
133
- if (index >= data.length) throw new Error("Unterminated string in Kiwi message");
141
+ while (index < end && data[index] !== 0) index++;
142
+ if (index >= end) throw new Error("Unterminated string in Kiwi message");
134
143
  return index;
135
144
  }
136
145
  _growBy(amount) {
@@ -204,10 +213,13 @@ var ByteBuffer = class {
204
213
  let codePoint;
205
214
  for (let i = 0; i < value.length; i++) {
206
215
  let a = value.charCodeAt(i);
207
- if (i + 1 === value.length || a < 55296 || a >= 56320) codePoint = a;
216
+ if (a < 55296 || a >= 57344) codePoint = a;
208
217
  else {
209
- let b = value.charCodeAt(++i);
210
- codePoint = (a << 10) + b + -56613888;
218
+ let b = value.charCodeAt(i + 1);
219
+ if (a < 56320 && b >= 56320 && b < 57344) {
220
+ i++;
221
+ codePoint = (a << 10) + b + -56613888;
222
+ } else codePoint = 65533;
211
223
  }
212
224
  if (codePoint === 0) throw new Error("Cannot encode a string containing the null character");
213
225
  if (codePoint < 128) this.writeByte(codePoint);
@@ -299,6 +311,18 @@ function hide(message, key, value) {
299
311
  function invalidMessage() {
300
312
  throw new Error("Attempted to parse invalid message");
301
313
  }
314
+ /**
315
+ * An array's declared length, refused when more items are declared than bytes remain: every
316
+ * item takes at least a byte (only a struct with no fields takes none), so such an array cannot
317
+ * be in the message, and trusting its length would allocate and loop over it before any read
318
+ * found the end.
319
+ */
320
+ function arrayLength(bb) {
321
+ const length = bb.readVarUint();
322
+ const remaining = bb.length - bb.offset;
323
+ if (length > remaining) throw new Error(`Kiwi array declares ${length} items, more than the bytes left in the message (${remaining})`);
324
+ return length;
325
+ }
302
326
  function compileDefinitions(schema, headers) {
303
327
  const byName = /* @__PURE__ */ new Map();
304
328
  for (const definition of schema.definitions) byName.set(definition.name, definition);
@@ -386,7 +410,7 @@ function compileSchemaTables(schema, headers) {
386
410
  const read = (field, bb) => {
387
411
  if (!field.isArray) return readValue(field, bb);
388
412
  if (field.type === TYPE.BYTE) return bb.readByteArray();
389
- const length = bb.readVarUint();
413
+ const length = arrayLength(bb);
390
414
  const values = Array.from({ length });
391
415
  for (let i = 0; i < length; i++) values[i] = readValue(field, bb);
392
416
  return values;
@@ -430,7 +454,7 @@ function compileSchemaTables(schema, headers) {
430
454
  bb.skipByteArray();
431
455
  return;
432
456
  }
433
- for (let length = bb.readVarUint(); length > 0; length--) skipValue(field, bb);
457
+ for (let length = arrayLength(bb); length > 0; length--) skipValue(field, bb);
434
458
  };
435
459
  const readInto = (field, bb, result) => {
436
460
  if (field.isDeprecated) skip(field, bb);
@@ -1048,7 +1072,7 @@ function parseVarint(data, pos) {
1048
1072
  while (pos < data.length) {
1049
1073
  const byte = data[pos];
1050
1074
  pos++;
1051
- value |= (byte & KIWI.VARINT_VALUE_MASK) << shift;
1075
+ value += (byte & KIWI.VARINT_VALUE_MASK) * 2 ** shift;
1052
1076
  if (!(byte & KIWI.VARINT_CONTINUE_BIT)) break;
1053
1077
  shift += KIWI.VARINT_BITS_PER_BYTE;
1054
1078
  }
@@ -1192,14 +1216,19 @@ function decompressZstd(compressed) {
1192
1216
  if (!wasmZstd) throw new Error("Zstd is not loaded: await loadFigDataDecompressor() before reading");
1193
1217
  return wasmZstd.decompress(compressed);
1194
1218
  }
1219
+ /**
1220
+ * The chunks of a raw `fig-kiwi` container, or null for other data or fewer than two chunks.
1221
+ * Fewer than four trailing bytes declare no chunk; a chunk longer than the bytes left throws.
1222
+ */
1195
1223
  function parseFigKiwiChunks(binary) {
1196
1224
  if (!figFileKind(binary)) return null;
1197
1225
  const view = new DataView(binary.buffer, binary.byteOffset, binary.byteLength);
1198
1226
  let offset = 12;
1199
1227
  const chunks = [];
1200
- while (offset < binary.length) {
1228
+ while (offset + 4 <= binary.length) {
1201
1229
  const chunkLen = view.getUint32(offset, true);
1202
1230
  offset += 4;
1231
+ if (chunkLen > binary.length - offset) throw new Error(`Corrupted .fig file: chunk at offset ${offset - 4} declares length ${chunkLen} but only ${binary.length - offset} bytes remain`);
1203
1232
  chunks.push(binary.slice(offset, offset + chunkLen));
1204
1233
  offset += chunkLen;
1205
1234
  }