@omnibuf/core 0.0.3 → 0.0.5

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.
package/dist/readers.js CHANGED
@@ -1,8 +1,8 @@
1
1
  // ============================================================================
2
2
  // Header-Scanning Field Readers (O(1) Per-Field Skip) & Wire Splicing (Opaque API)
3
3
  // ============================================================================
4
- import { Encoder, mapHeaderByte } from './encoder.js';
5
- import { WireBoolFalse, WireBoolTrue, WireBytes, WireFixed32, WireFixed64, WireFixedArr, WireMap, WireSint, WireSintArr, WireString, WireStringArr, WireSubMsg, WireSubMsgArr, WireUint, WireUintArr, decodeUTF8, decodeVarint, decodeVarintAuto, decodeVarintBigInt, decodeZigZag, decodeZigZagAuto, decodeZigZagBigInt, fastSkipValue, float32FromBits, float64FromBits, locateField, nextContainerField, roundFloat32Shortest, skipValue, } from './wire.js';
4
+ import { Encoder } from './encoder.js';
5
+ import { WireArray, WireBoolFalse, WireBoolTrue, WireBytes, WireFixed32, WireFixed64, WireFloat32, WireFloat64, WireMap, WireSfixed32, WireSfixed64, WireSint, WireString, WireSubMsg, WireUint, decodeUTF8, decodeVarint, decodeVarintAuto, decodeVarintBigInt, decodeZigZag, decodeZigZagAuto, decodeZigZagBigInt, fastSkipValue, float32FromBits, float64FromBits, locateField, nextContainerField, parseArrayHeader, parseMapHeader, roundFloat32Shortest, skipValue, } from './wire.js';
6
6
  // ============================================================================
7
7
  // Scalar Readers
8
8
  // ============================================================================
@@ -14,13 +14,7 @@ export function readUint(packet, fieldNum) {
14
14
  return [v, n > 0];
15
15
  }
16
16
  export function readEnum(packet, fieldNum) {
17
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
18
- if (!ok || wireType !== WireUint)
19
- return [0, false];
20
- const [v, n] = decodeVarintBigInt(valSlice, 0);
21
- if (n === 0)
22
- return [0, false];
23
- return [Number(v & 0xffffffffn) | 0, true];
17
+ return readInt(packet, fieldNum);
24
18
  }
25
19
  export function readUintBigInt(packet, fieldNum) {
26
20
  const { valSlice, wireType, ok } = locateField(packet, fieldNum);
@@ -79,15 +73,19 @@ export function readFixed32(packet, fieldNum) {
79
73
  return [bits, true];
80
74
  }
81
75
  export function readSfixed32(packet, fieldNum) {
82
- const [bits, ok] = readFixed32(packet, fieldNum);
83
- if (!ok)
76
+ const { valSlice, wireType, ok } = locateField(packet, fieldNum);
77
+ if (!ok || wireType !== WireSfixed32 || valSlice.length < 4) {
84
78
  return [0, false];
85
- return [bits | 0, true];
79
+ }
80
+ const bits = valSlice[0] | (valSlice[1] << 8) | (valSlice[2] << 16) | (valSlice[3] << 24) | 0;
81
+ return [bits, true];
86
82
  }
87
83
  export function readFloat32(packet, fieldNum) {
88
- const [bits, ok] = readFixed32(packet, fieldNum);
89
- if (!ok)
84
+ const { valSlice, wireType, ok } = locateField(packet, fieldNum);
85
+ if (!ok || wireType !== WireFloat32 || valSlice.length < 4) {
90
86
  return [0, false];
87
+ }
88
+ const bits = (valSlice[0] | (valSlice[1] << 8) | (valSlice[2] << 16) | (valSlice[3] << 24)) >>> 0;
91
89
  return [float32FromBits(bits), true];
92
90
  }
93
91
  export function readFixed64(packet, fieldNum) {
@@ -108,9 +106,14 @@ export function readFixed64Auto(packet, fieldNum) {
108
106
  return [u64 <= 9007199254740991n ? Number(u64) : u64, true];
109
107
  }
110
108
  export function readSfixed64(packet, fieldNum) {
111
- const [u64, ok] = readFixed64(packet, fieldNum);
112
- if (!ok)
109
+ const { valSlice, wireType, ok } = locateField(packet, fieldNum);
110
+ if (!ok || wireType !== WireSfixed64 || valSlice.length < 8) {
113
111
  return [0n, false];
112
+ }
113
+ let u64 = 0n;
114
+ for (let i = 7; i >= 0; i--) {
115
+ u64 = (u64 << 8n) | BigInt(valSlice[i]);
116
+ }
114
117
  return [BigInt.asIntN(64, u64), true];
115
118
  }
116
119
  export function readSfixed64Auto(packet, fieldNum) {
@@ -120,10 +123,15 @@ export function readSfixed64Auto(packet, fieldNum) {
120
123
  return [s64 >= -9007199254740991n && s64 <= 9007199254740991n ? Number(s64) : s64, true];
121
124
  }
122
125
  export function readFloat64(packet, fieldNum) {
123
- const [bits, ok] = readFixed64(packet, fieldNum);
124
- if (!ok)
126
+ const { valSlice, wireType, ok } = locateField(packet, fieldNum);
127
+ if (!ok || wireType !== WireFloat64 || valSlice.length < 8) {
125
128
  return [0, false];
126
- return [float64FromBits(bits), true];
129
+ }
130
+ let u64 = 0n;
131
+ for (let i = 7; i >= 0; i--) {
132
+ u64 = (u64 << 8n) | BigInt(valSlice[i]);
133
+ }
134
+ return [float64FromBits(u64), true];
127
135
  }
128
136
  export function readStringBytes(packet, fieldNum) {
129
137
  const { valSlice, wireType, ok } = locateField(packet, fieldNum);
@@ -178,9 +186,15 @@ export function readSubMsg(packet, fieldNum) {
178
186
  export function readElem(arrPayload, index) {
179
187
  if (index < 0)
180
188
  return [new Uint8Array(0), false];
181
- let pos = 0;
189
+ const [elemType, count, startP, ok] = parseArrayHeader(arrPayload);
190
+ if (!ok || elemType !== WireSubMsg || index >= count) {
191
+ return [new Uint8Array(0), false];
192
+ }
193
+ let pos = startP;
182
194
  const n = arrPayload.length;
183
- for (let i = 0; pos < n; i++) {
195
+ for (let i = 0; i < count; i++) {
196
+ if (pos >= n)
197
+ return [new Uint8Array(0), false];
184
198
  let elemLen = 0;
185
199
  let en = 0;
186
200
  const b0 = arrPayload[pos];
@@ -210,10 +224,15 @@ export function readElem(arrPayload, index) {
210
224
  return [new Uint8Array(0), false];
211
225
  }
212
226
  export function countSubMsgArrElems(arrPayload) {
213
- let pos = 0;
227
+ const [elemType, count, startP, ok] = parseArrayHeader(arrPayload);
228
+ if (!ok || elemType !== WireSubMsg) {
229
+ return [0, false];
230
+ }
231
+ let pos = startP;
214
232
  const n = arrPayload.length;
215
- let count = 0;
216
- while (pos < n) {
233
+ for (let i = 0; i < count; i++) {
234
+ if (pos >= n)
235
+ return [0, false];
217
236
  const b0 = arrPayload[pos];
218
237
  if ((b0 & 2) !== 0) {
219
238
  if (pos + 2 > n)
@@ -222,14 +241,12 @@ export function countSubMsgArrElems(arrPayload) {
222
241
  pos += 2 + el;
223
242
  if (pos > n)
224
243
  return [0, false];
225
- count++;
226
244
  continue;
227
245
  }
228
246
  if ((b0 & 1) !== 0) {
229
247
  pos += 1 + (b0 >>> 1);
230
248
  if (pos > n)
231
249
  return [0, false];
232
- count++;
233
250
  continue;
234
251
  }
235
252
  const [el, en] = decodeVarintBigInt(arrPayload, pos);
@@ -237,15 +254,16 @@ export function countSubMsgArrElems(arrPayload) {
237
254
  return [0, false];
238
255
  }
239
256
  pos += en + Number(el);
240
- count++;
241
257
  }
242
- return [count, pos === n];
258
+ if (pos !== n)
259
+ return [0, false];
260
+ return [count, true];
243
261
  }
244
262
  export function subMsgArrLen(packet, fieldNum) {
245
263
  let total = 0;
246
264
  let pos = 0;
247
265
  while (true) {
248
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireSubMsgArr, 0);
266
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
249
267
  if (!valid)
250
268
  return 0;
251
269
  if (!matched)
@@ -263,16 +281,16 @@ export function readSubMsgArrElem(packet, fieldNum, index) {
263
281
  let remIdx = index;
264
282
  let pos = 0;
265
283
  while (true) {
266
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireSubMsgArr, 0);
284
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
267
285
  if (!valid || !matched)
268
286
  return [new Uint8Array(0), false];
269
287
  pos = nextPos;
270
- const [elem, ok] = readElem(body, remIdx);
271
- if (ok)
272
- return [elem, true];
273
288
  const [c, validArr] = countSubMsgArrElems(body);
274
289
  if (!validArr)
275
290
  return [new Uint8Array(0), false];
291
+ if (remIdx < c) {
292
+ return readElem(body, remIdx);
293
+ }
276
294
  remIdx -= c;
277
295
  }
278
296
  }
@@ -309,39 +327,28 @@ export function readArrNestedString(packet, arrField, index, targetField) {
309
327
  // ============================================================================
310
328
  // Packed & Counted Array Readers
311
329
  // ============================================================================
312
- export function countedArrLen(packet, fieldNum, expWT) {
330
+ export function countedArrLen(packet, fieldNum, expElemWT) {
313
331
  const { valSlice, wireType, ok } = locateField(packet, fieldNum);
314
- if (!ok || wireType !== expWT)
332
+ if (!ok || wireType !== WireArray)
315
333
  return 0;
316
334
  const [bLen, bn] = decodeVarintBigInt(valSlice, 0);
317
- if (bn === 0 || bLen > BigInt(valSlice.length - bn))
335
+ if (bn === 0 || bLen < 2n || bLen > BigInt(valSlice.length - bn))
336
+ return 0;
337
+ const body = valSlice.subarray(bn, bn + Number(bLen));
338
+ const [elemType, c, , headerOk] = parseArrayHeader(body);
339
+ if (!headerOk)
318
340
  return 0;
319
- const [c, cn] = decodeVarint(valSlice.subarray(bn, bn + Number(bLen)), 0);
320
- if (cn === 0)
341
+ if (expElemWT !== undefined && expElemWT !== WireArray && elemType !== expElemWT) {
321
342
  return 0;
343
+ }
322
344
  return c;
323
345
  }
324
346
  export function appendStringArrBody(dst, body) {
325
347
  const n = body.length;
326
- if (n === 0)
327
- return [[], false];
328
- let c = 0;
329
- let p = 0;
330
- const b0 = body[0];
331
- if ((b0 & 1) !== 0) {
332
- c = b0 >>> 1;
333
- p = 1;
334
- }
335
- else {
336
- const [count, cn] = decodeVarintBigInt(body, 0);
337
- if (cn === 0 || count > BigInt(n - cn)) {
338
- return [[], false];
339
- }
340
- c = Number(count);
341
- p = cn;
342
- }
343
- if (c > n - p)
348
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
349
+ if (!ok || elemType !== WireString)
344
350
  return [[], false];
351
+ let p = startP;
345
352
  for (let i = 0; i < c; i++) {
346
353
  if (p >= n)
347
354
  return [[], false];
@@ -376,12 +383,11 @@ export function appendStringArrBody(dst, body) {
376
383
  return [dst, true];
377
384
  }
378
385
  export function appendBytesArrBody(dst, body, zeroCopy = false) {
379
- const [count, cn] = decodeVarintBigInt(body, 0);
380
- if (cn === 0 || count > BigInt(body.length - cn)) {
386
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
387
+ if (!ok || elemType !== WireBytes) {
381
388
  return [[], false];
382
389
  }
383
- const c = Number(count);
384
- let p = cn;
390
+ let p = startP;
385
391
  for (let i = 0; i < c; i++) {
386
392
  const [sLen, sn] = decodeVarintBigInt(body, p);
387
393
  if (sn === 0 || sLen > BigInt(body.length - (p + sn))) {
@@ -399,45 +405,24 @@ export function appendBytesArrBody(dst, body, zeroCopy = false) {
399
405
  return [dst, true];
400
406
  }
401
407
  export function appendBoolArrBody(dst, body) {
402
- const [count, cn] = decodeVarintBigInt(body, 0);
403
- if (cn === 0 || count > BigInt(body.length - cn)) {
408
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
409
+ if (!ok || elemType !== WireBoolFalse || body.length - startP !== c) {
404
410
  return [[], false];
405
411
  }
406
- const c = Number(count);
407
- let p = cn;
408
412
  for (let i = 0; i < c; i++) {
409
- const [v, vn] = decodeVarintBigInt(body, p);
410
- if (vn === 0)
413
+ const b = body[startP + i];
414
+ if (b !== 0 && b !== 1)
411
415
  return [[], false];
412
- p += vn;
413
- dst.push(v !== 0n);
414
- }
415
- if (p !== body.length) {
416
- return [[], false];
416
+ dst.push(b === 1);
417
417
  }
418
418
  return [dst, true];
419
419
  }
420
420
  export function appendUint32ArrBody(dst, body) {
421
421
  const n = body.length;
422
- if (n === 0)
423
- return [[], false];
424
- let c = 0;
425
- let p = 0;
426
- const b0 = body[0];
427
- if ((b0 & 1) !== 0) {
428
- c = b0 >>> 1;
429
- p = 1;
430
- }
431
- else {
432
- const [count, cn] = decodeVarintBigInt(body, 0);
433
- if (cn === 0 || count > BigInt(n - cn)) {
434
- return [[], false];
435
- }
436
- c = Number(count);
437
- p = cn;
438
- }
439
- if (c > n - p)
422
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
423
+ if (!ok || elemType !== WireUint)
440
424
  return [[], false];
425
+ let p = startP;
441
426
  for (let i = 0; i < c; i++) {
442
427
  if (p >= n)
443
428
  return [[], false];
@@ -459,7 +444,7 @@ export function appendUint32ArrBody(dst, body) {
459
444
  p += 4;
460
445
  }
461
446
  else {
462
- if (p + 9 > n)
447
+ if (vb !== 0 || p + 9 > n)
463
448
  return [[], false];
464
449
  const lo = (body[p + 1] | (body[p + 2] << 8) | (body[p + 3] << 16) | (body[p + 4] << 24)) >>> 0;
465
450
  dst.push(lo);
@@ -472,67 +457,29 @@ export function appendUint32ArrBody(dst, body) {
472
457
  return [dst, true];
473
458
  }
474
459
  export function appendEnumArrBody(dst, body) {
475
- const n = body.length;
476
- if (n === 0)
460
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
461
+ if (!ok || elemType !== WireSint) {
477
462
  return [[], false];
478
- let c = 0;
479
- let p = 0;
480
- const b0 = body[0];
481
- if ((b0 & 1) !== 0) {
482
- c = b0 >>> 1;
483
- p = 1;
484
- }
485
- else {
486
- const [count, cn] = decodeVarintBigInt(body, 0);
487
- if (cn === 0 || count > BigInt(n - cn)) {
488
- return [[], false];
489
- }
490
- c = Number(count);
491
- p = cn;
492
463
  }
493
- if (c > n - p)
494
- return [[], false];
464
+ let p = startP;
495
465
  for (let i = 0; i < c; i++) {
496
- if (p >= n)
466
+ const [v, vn] = decodeVarintBigInt(body, p);
467
+ if (vn === 0)
497
468
  return [[], false];
498
- const vb = body[p];
499
- if ((vb & 1) !== 0) {
500
- dst.push((vb >>> 1) | 0);
501
- p += 1;
502
- }
503
- else if ((vb & 2) !== 0) {
504
- if (p + 2 > n)
505
- return [[], false];
506
- dst.push(((vb | (body[p + 1] << 8)) >>> 2) | 0);
507
- p += 2;
508
- }
509
- else if ((vb & 4) !== 0) {
510
- if (p + 4 > n)
511
- return [[], false];
512
- dst.push((((vb | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24)) >>> 3) >>> 0) |
513
- 0);
514
- p += 4;
515
- }
516
- else {
517
- if (p + 9 > n)
518
- return [[], false];
519
- const lo = body[p + 1] | (body[p + 2] << 8) | (body[p + 3] << 16) | (body[p + 4] << 24) | 0;
520
- dst.push(lo);
521
- p += 9;
522
- }
469
+ p += vn;
470
+ dst.push(decodeZigZag(v) | 0);
523
471
  }
524
- if (p !== n) {
472
+ if (p !== body.length) {
525
473
  return [[], false];
526
474
  }
527
475
  return [dst, true];
528
476
  }
529
477
  export function appendUintArrAutoBody(dst, body) {
530
- const [count, cn] = decodeVarintBigInt(body, 0);
531
- if (cn === 0 || count > BigInt(body.length - cn)) {
478
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
479
+ if (!ok || elemType !== WireUint) {
532
480
  return [[], false];
533
481
  }
534
- const c = Number(count);
535
- let p = cn;
482
+ let p = startP;
536
483
  for (let i = 0; i < c; i++) {
537
484
  const [v, vn] = decodeVarintAuto(body, p);
538
485
  if (vn === 0)
@@ -546,12 +493,11 @@ export function appendUintArrAutoBody(dst, body) {
546
493
  return [dst, true];
547
494
  }
548
495
  export function appendSintArrAutoBody(dst, body) {
549
- const [count, cn] = decodeVarintBigInt(body, 0);
550
- if (cn === 0 || count > BigInt(body.length - cn)) {
496
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
497
+ if (!ok || elemType !== WireSint) {
551
498
  return [[], false];
552
499
  }
553
- const c = Number(count);
554
- let p = cn;
500
+ let p = startP;
555
501
  for (let i = 0; i < c; i++) {
556
502
  const [v, vn] = decodeVarintBigInt(body, p);
557
503
  if (vn === 0)
@@ -565,24 +511,24 @@ export function appendSintArrAutoBody(dst, body) {
565
511
  return [dst, true];
566
512
  }
567
513
  export function appendFloat32ArrBody(dst, body) {
568
- if (body.length < 1 || body[0] !== WireFixed32 || (body.length - 1) % 4 !== 0) {
514
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
515
+ if (!ok || elemType !== WireFloat32 || body.length - startP !== c * 4) {
569
516
  return [[], false];
570
517
  }
571
- const c = (body.length - 1) / 4;
572
518
  for (let i = 0; i < c; i++) {
573
- const off = 1 + i * 4;
519
+ const off = startP + i * 4;
574
520
  const bits = (body[off] | (body[off + 1] << 8) | (body[off + 2] << 16) | (body[off + 3] << 24)) >>> 0;
575
521
  dst.push(float32FromBits(bits));
576
522
  }
577
523
  return [dst, true];
578
524
  }
579
525
  export function appendFloat64ArrBody(dst, body) {
580
- if (body.length < 1 || body[0] !== WireFixed64 || (body.length - 1) % 8 !== 0) {
526
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
527
+ if (!ok || elemType !== WireFloat64 || body.length - startP !== c * 8) {
581
528
  return [[], false];
582
529
  }
583
- const c = (body.length - 1) / 8;
584
530
  for (let i = 0; i < c; i++) {
585
- const off = 1 + i * 8;
531
+ const off = startP + i * 8;
586
532
  let u64 = 0n;
587
533
  for (let b = 7; b >= 0; b--) {
588
534
  u64 = (u64 << 8n) | BigInt(body[off + b]);
@@ -596,7 +542,7 @@ export function readStringArr(packet, fieldNum) {
596
542
  let found = false;
597
543
  let pos = 0;
598
544
  while (true) {
599
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireStringArr, 1);
545
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
600
546
  if (!valid)
601
547
  return [[], false];
602
548
  if (!matched)
@@ -613,7 +559,7 @@ export function readUint32Arr(packet, fieldNum) {
613
559
  let found = false;
614
560
  let pos = 0;
615
561
  while (true) {
616
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireUintArr, 1);
562
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
617
563
  if (!valid)
618
564
  return [[], false];
619
565
  if (!matched)
@@ -630,7 +576,7 @@ export function readEnumArr(packet, fieldNum) {
630
576
  let found = false;
631
577
  let pos = 0;
632
578
  while (true) {
633
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireUintArr, 1);
579
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
634
580
  if (!valid)
635
581
  return [[], false];
636
582
  if (!matched)
@@ -647,7 +593,7 @@ export function readSintArr(packet, fieldNum) {
647
593
  let found = false;
648
594
  let pos = 0;
649
595
  while (true) {
650
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireSintArr, 1);
596
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
651
597
  if (!valid)
652
598
  return [[], false];
653
599
  if (!matched)
@@ -664,7 +610,7 @@ export function readFloat32Arr(packet, fieldNum) {
664
610
  let found = false;
665
611
  let pos = 0;
666
612
  while (true) {
667
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireFixedArr, 1);
613
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
668
614
  if (!valid)
669
615
  return [[], false];
670
616
  if (!matched)
@@ -681,7 +627,7 @@ export function readFloat64Arr(packet, fieldNum) {
681
627
  let found = false;
682
628
  let pos = 0;
683
629
  while (true) {
684
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireFixedArr, 1);
630
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
685
631
  if (!valid)
686
632
  return [[], false];
687
633
  if (!matched)
@@ -698,7 +644,7 @@ export function readBytesArr(packet, fieldNum) {
698
644
  let found = false;
699
645
  let pos = 0;
700
646
  while (true) {
701
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireStringArr, 1);
647
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
702
648
  if (!valid)
703
649
  return [[], false];
704
650
  if (!matched)
@@ -715,7 +661,7 @@ export function readBoolArr(packet, fieldNum) {
715
661
  let found = false;
716
662
  let pos = 0;
717
663
  while (true) {
718
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireUintArr, 1);
664
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
719
665
  if (!valid)
720
666
  return [[], false];
721
667
  if (!matched)
@@ -732,7 +678,7 @@ export function readUintArr(packet, fieldNum) {
732
678
  let found = false;
733
679
  let pos = 0;
734
680
  while (true) {
735
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireUintArr, 1);
681
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
736
682
  if (!valid)
737
683
  return [[], false];
738
684
  if (!matched)
@@ -749,19 +695,19 @@ export function readFixed32Arr(packet, fieldNum) {
749
695
  let found = false;
750
696
  let pos = 0;
751
697
  while (true) {
752
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireFixedArr, 1);
698
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
753
699
  if (!valid)
754
700
  return [[], false];
755
701
  if (!matched)
756
702
  return [out, found];
757
703
  pos = nextPos;
758
- if (body.length < 1 || body[0] !== WireFixed32 || (body.length - 1) % 4 !== 0) {
704
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
705
+ if (!ok || elemType !== WireFixed32 || body.length - startP !== c * 4) {
759
706
  return [[], false];
760
707
  }
761
708
  found = true;
762
- const c = (body.length - 1) / 4;
763
709
  for (let i = 0; i < c; i++) {
764
- const off = 1 + i * 4;
710
+ const off = startP + i * 4;
765
711
  out.push((body[off] | (body[off + 1] << 8) | (body[off + 2] << 16) | (body[off + 3] << 24)) >>>
766
712
  0);
767
713
  }
@@ -772,19 +718,19 @@ export function readSfixed32Arr(packet, fieldNum) {
772
718
  let found = false;
773
719
  let pos = 0;
774
720
  while (true) {
775
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireFixedArr, 1);
721
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
776
722
  if (!valid)
777
723
  return [[], false];
778
724
  if (!matched)
779
725
  return [out, found];
780
726
  pos = nextPos;
781
- if (body.length < 1 || body[0] !== WireFixed32 || (body.length - 1) % 4 !== 0) {
727
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
728
+ if (!ok || elemType !== WireSfixed32 || body.length - startP !== c * 4) {
782
729
  return [[], false];
783
730
  }
784
731
  found = true;
785
- const c = (body.length - 1) / 4;
786
732
  for (let i = 0; i < c; i++) {
787
- const off = 1 + i * 4;
733
+ const off = startP + i * 4;
788
734
  out.push(body[off] | (body[off + 1] << 8) | (body[off + 2] << 16) | (body[off + 3] << 24) | 0);
789
735
  }
790
736
  }
@@ -794,19 +740,19 @@ export function readFixed64Arr(packet, fieldNum) {
794
740
  let found = false;
795
741
  let pos = 0;
796
742
  while (true) {
797
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireFixedArr, 1);
743
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
798
744
  if (!valid)
799
745
  return [[], false];
800
746
  if (!matched)
801
747
  return [out, found];
802
748
  pos = nextPos;
803
- if (body.length < 1 || body[0] !== WireFixed64 || (body.length - 1) % 8 !== 0) {
749
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
750
+ if (!ok || elemType !== WireFixed64 || body.length - startP !== c * 8) {
804
751
  return [[], false];
805
752
  }
806
753
  found = true;
807
- const c = (body.length - 1) / 8;
808
754
  for (let i = 0; i < c; i++) {
809
- const off = 1 + i * 8;
755
+ const off = startP + i * 8;
810
756
  let u64 = 0n;
811
757
  for (let b = 7; b >= 0; b--) {
812
758
  u64 = (u64 << 8n) | BigInt(body[off + b]);
@@ -820,19 +766,19 @@ export function readSfixed64Arr(packet, fieldNum) {
820
766
  let found = false;
821
767
  let pos = 0;
822
768
  while (true) {
823
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireFixedArr, 1);
769
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
824
770
  if (!valid)
825
771
  return [[], false];
826
772
  if (!matched)
827
773
  return [out, found];
828
774
  pos = nextPos;
829
- if (body.length < 1 || body[0] !== WireFixed64 || (body.length - 1) % 8 !== 0) {
775
+ const [elemType, c, startP, ok] = parseArrayHeader(body);
776
+ if (!ok || elemType !== WireSfixed64 || body.length - startP !== c * 8) {
830
777
  return [[], false];
831
778
  }
832
779
  found = true;
833
- const c = (body.length - 1) / 8;
834
780
  for (let i = 0; i < c; i++) {
835
- const off = 1 + i * 8;
781
+ const off = startP + i * 8;
836
782
  let u64 = 0n;
837
783
  for (let b = 7; b >= 0; b--) {
838
784
  u64 = (u64 << 8n) | BigInt(body[off + b]);
@@ -846,23 +792,79 @@ export function readSubMsgMerged(packet, fieldNum, decodeFn) {
846
792
  let out;
847
793
  let found = false;
848
794
  let pos = 0;
849
- while (true) {
850
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireSubMsg, 0);
851
- if (!valid)
795
+ const n = packet.length;
796
+ const useRange = decodeFn.length >= 4;
797
+ while (pos < n) {
798
+ let fnum = 0;
799
+ let wt = 0;
800
+ const b0 = packet[pos];
801
+ if ((b0 & 1) !== 0) {
802
+ fnum = b0 >>> 5;
803
+ wt = (b0 >>> 1) & 0x0f;
804
+ pos += 1;
805
+ }
806
+ else if ((b0 & 2) !== 0) {
807
+ if (pos + 2 > n)
808
+ return [null, false];
809
+ const u16 = (b0 | (packet[pos + 1] << 8)) >>> 2;
810
+ fnum = u16 >>> 4;
811
+ wt = u16 & 0x0f;
812
+ pos += 2;
813
+ }
814
+ else {
815
+ const [tag, tn] = decodeVarintBigInt(packet, pos);
816
+ if (tn === 0 || tag >> 4n > 536870911n)
817
+ return [null, false];
818
+ fnum = Number(tag >> 4n);
819
+ wt = Number(tag & 0x0fn);
820
+ pos += tn;
821
+ }
822
+ if (fnum === fieldNum) {
823
+ if (wt !== WireSubMsg || pos >= n)
824
+ return [null, false];
825
+ let lenNum = 0;
826
+ const lb = packet[pos];
827
+ if ((lb & 2) !== 0) {
828
+ if (pos + 2 > n)
829
+ return [null, false];
830
+ lenNum = (lb | (packet[pos + 1] << 8)) >>> 2;
831
+ pos += 2;
832
+ }
833
+ else if ((lb & 1) !== 0) {
834
+ lenNum = lb >>> 1;
835
+ pos += 1;
836
+ }
837
+ else {
838
+ const [bLen, bBytes] = decodeVarintBigInt(packet, pos);
839
+ if (bBytes === 0 || bLen > BigInt(n - (pos + bBytes)))
840
+ return [null, false];
841
+ lenNum = Number(bLen);
842
+ pos += bBytes;
843
+ }
844
+ if (pos + lenNum > n)
845
+ return [null, false];
846
+ out = useRange
847
+ ? decodeFn(packet, out, found, pos, pos + lenNum)
848
+ : decodeFn(packet.subarray(pos, pos + lenNum), out, found);
849
+ found = true;
850
+ pos += lenNum;
851
+ continue;
852
+ }
853
+ try {
854
+ pos = skipValue(packet, pos, wt);
855
+ }
856
+ catch {
852
857
  return [null, false];
853
- if (!matched)
854
- return [out ?? null, found];
855
- pos = nextPos;
856
- out = decodeFn(body, out, found);
857
- found = true;
858
+ }
858
859
  }
860
+ return [out ?? null, found];
859
861
  }
860
862
  export function readSubMsgArrDecoded(packet, fieldNum, decodeFn) {
861
863
  const out = [];
862
864
  let found = false;
863
865
  let pos = 0;
864
866
  while (true) {
865
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireSubMsgArr, 0);
867
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
866
868
  if (!valid)
867
869
  return [[], false];
868
870
  if (!matched)
@@ -872,7 +874,7 @@ export function readSubMsgArrDecoded(packet, fieldNum, decodeFn) {
872
874
  if (!ok)
873
875
  return [[], false];
874
876
  found = true;
875
- let p = 0;
877
+ let [, , p] = parseArrayHeader(body);
876
878
  for (let i = 0; i < count; i++) {
877
879
  const [el, en] = decodeVarintBigInt(body, p);
878
880
  p += en;
@@ -900,6 +902,12 @@ function readMapEntryPayload(body, p, wt, scalarKind, decodeSubMsg) {
900
902
  const b = body.subarray(p + ln, p + ln + Number(len)).slice();
901
903
  return [b, p + ln + Number(len)];
902
904
  }
905
+ case WireBoolFalse: {
906
+ if (p >= body.length || (body[p] !== 0 && body[p] !== 1)) {
907
+ throw new Error('omnibuf: invalid map bool');
908
+ }
909
+ return [body[p] === 1, p + 1];
910
+ }
903
911
  case WireUint: {
904
912
  const [v, vn] = decodeVarintAuto(body, p);
905
913
  if (vn === 0)
@@ -909,9 +917,6 @@ function readMapEntryPayload(body, p, wt, scalarKind, decodeSubMsg) {
909
917
  if (scalarKind === 'uint32') {
910
918
  return [Number(BigInt(v) & 0xffffffffn) >>> 0, p + vn];
911
919
  }
912
- if (scalarKind === 'enum') {
913
- return [Number(BigInt(v) & 0xffffffffn) | 0, p + vn];
914
- }
915
920
  return [v, p + vn];
916
921
  }
917
922
  case WireSint: {
@@ -919,7 +924,7 @@ function readMapEntryPayload(body, p, wt, scalarKind, decodeSubMsg) {
919
924
  if (vn === 0)
920
925
  throw new Error('omnibuf: truncated map sint');
921
926
  const s = decodeZigZagAuto(v);
922
- if (scalarKind === 'int32' || scalarKind === 'sint32') {
927
+ if (scalarKind === 'int32' || scalarKind === 'sint32' || scalarKind === 'enum') {
923
928
  return [Number(s) | 0, p + vn];
924
929
  }
925
930
  return [s, p + vn];
@@ -928,12 +933,20 @@ function readMapEntryPayload(body, p, wt, scalarKind, decodeSubMsg) {
928
933
  if (body.length - p < 4)
929
934
  throw new Error('omnibuf: truncated map fixed32');
930
935
  const bits = (body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24)) >>> 0;
931
- if (scalarKind === 'float32')
932
- return [roundFloat32Shortest(float32FromBits(bits)), p + 4];
933
- if (scalarKind === 'sfixed32')
934
- return [bits | 0, p + 4];
935
936
  return [bits, p + 4];
936
937
  }
938
+ case WireSfixed32: {
939
+ if (body.length - p < 4)
940
+ throw new Error('omnibuf: truncated map sfixed32');
941
+ const bits = body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24) | 0;
942
+ return [bits, p + 4];
943
+ }
944
+ case WireFloat32: {
945
+ if (body.length - p < 4)
946
+ throw new Error('omnibuf: truncated map float32');
947
+ const bits = (body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24)) >>> 0;
948
+ return [roundFloat32Shortest(float32FromBits(bits)), p + 4];
949
+ }
937
950
  case WireFixed64: {
938
951
  if (body.length - p < 8)
939
952
  throw new Error('omnibuf: truncated map fixed64');
@@ -941,14 +954,27 @@ function readMapEntryPayload(body, p, wt, scalarKind, decodeSubMsg) {
941
954
  for (let b = 7; b >= 0; b--) {
942
955
  u64 = (u64 << 8n) | BigInt(body[p + b]);
943
956
  }
944
- if (scalarKind === 'float64')
945
- return [float64FromBits(u64), p + 8];
946
- if (scalarKind === 'sfixed64') {
947
- const s64 = BigInt.asIntN(64, u64);
948
- return [s64 >= -9007199254740991n && s64 <= 9007199254740991n ? Number(s64) : s64, p + 8];
949
- }
950
957
  return [u64 <= 9007199254740991n ? Number(u64) : u64, p + 8];
951
958
  }
959
+ case WireSfixed64: {
960
+ if (body.length - p < 8)
961
+ throw new Error('omnibuf: truncated map sfixed64');
962
+ let u64 = 0n;
963
+ for (let b = 7; b >= 0; b--) {
964
+ u64 = (u64 << 8n) | BigInt(body[p + b]);
965
+ }
966
+ const s64 = BigInt.asIntN(64, u64);
967
+ return [s64 >= -9007199254740991n && s64 <= 9007199254740991n ? Number(s64) : s64, p + 8];
968
+ }
969
+ case WireFloat64: {
970
+ if (body.length - p < 8)
971
+ throw new Error('omnibuf: truncated map float64');
972
+ let u64 = 0n;
973
+ for (let b = 7; b >= 0; b--) {
974
+ u64 = (u64 << 8n) | BigInt(body[p + b]);
975
+ }
976
+ return [float64FromBits(u64), p + 8];
977
+ }
952
978
  case WireSubMsg: {
953
979
  const [len, ln] = decodeVarintBigInt(body, p);
954
980
  if (ln === 0 || len > BigInt(body.length - (p + ln))) {
@@ -966,22 +992,18 @@ export function readTypedMap(packet, fieldNum, keyType, valType, decodeSubMsg) {
966
992
  let found = false;
967
993
  let pos = 0;
968
994
  while (true) {
969
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireMap, 2);
995
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireMap, 3);
970
996
  if (!valid)
971
997
  return [{}, false];
972
998
  if (!matched)
973
999
  return [out, found];
974
1000
  pos = nextPos;
975
- const mapType = body[0];
976
- const keyWT = mapType >>> 4;
977
- const valWT = mapType & 0x0f;
978
- const [count, cn] = decodeVarintBigInt(body, 1);
979
- if (cn === 0 || count * 2n > BigInt(body.length - (1 + cn))) {
1001
+ const [keyWT, valWT, c, startP, ok] = parseMapHeader(body);
1002
+ if (!ok) {
980
1003
  return [{}, false];
981
1004
  }
982
1005
  found = true;
983
- const c = Number(count);
984
- let p = 1 + cn;
1006
+ let p = startP;
985
1007
  for (let i = 0; i < c; i++) {
986
1008
  const [kVal, p1] = readMapEntryPayload(body, p, keyWT, keyType);
987
1009
  const [vVal, p2] = readMapEntryPayload(body, p1, valWT, valType, decodeSubMsg);
@@ -1006,21 +1028,17 @@ export function lookupMapStringUint(packet, fieldNum, targetKey) {
1006
1028
  let found = false;
1007
1029
  let pos = 0;
1008
1030
  while (true) {
1009
- const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireMap, 2);
1031
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireMap, 3);
1010
1032
  if (!valid)
1011
1033
  return [0, false];
1012
1034
  if (!matched)
1013
1035
  return [lastVal, found];
1014
1036
  pos = nextPos;
1015
- if (body[0] !== mapHeaderByte(WireString, WireUint)) {
1016
- return [0, false];
1017
- }
1018
- const [count, cn] = decodeVarintBigInt(body, 1);
1019
- if (cn === 0 || count > BigInt(body.length - (1 + cn))) {
1037
+ const [keyWT, valWT, c, startP, ok] = parseMapHeader(body);
1038
+ if (!ok || keyWT !== WireString || valWT !== WireUint) {
1020
1039
  return [0, false];
1021
1040
  }
1022
- const c = Number(count);
1023
- let p = 1 + cn;
1041
+ let p = startP;
1024
1042
  for (let i = 0; i < c; i++) {
1025
1043
  const [kLen, kn] = decodeVarintBigInt(body, p);
1026
1044
  if (kn === 0 || kLen > BigInt(body.length - (p + kn))) {
@@ -1078,7 +1096,7 @@ export function activeOneofSlice(packet, targetField, oneofSpecs) {
1078
1096
  }
1079
1097
  else {
1080
1098
  const [tag, tn] = decodeVarintBigInt(packet, pos);
1081
- if (tn === 0 || tag >> 4n > 0xffffffffn) {
1099
+ if (tn === 0 || tag >> 4n > 536870911n) {
1082
1100
  return null;
1083
1101
  }
1084
1102
  fnum = Number(tag >> 4n);
@@ -1144,7 +1162,7 @@ export function spliceFields(raw, dirtyMask, maxField, appendDirty, enc) {
1144
1162
  }
1145
1163
  else {
1146
1164
  const [t64, width] = decodeVarintBigInt(raw, pos);
1147
- if (!width || t64 >> 4n > 0xffffffffn)
1165
+ if (!width || t64 >> 4n > 536870911n)
1148
1166
  throw new Error('omnibuf: invalid tag while splicing');
1149
1167
  tag = Number(t64);
1150
1168
  pos += width;
@@ -1153,14 +1171,15 @@ export function spliceFields(raw, dirtyMask, maxField, appendDirty, enc) {
1153
1171
  const wt = tag & 0x0f;
1154
1172
  pos = skipValue(raw, pos, wt);
1155
1173
  const fieldEnd = pos;
1156
- const bit = fnum <= maxField ? 1 << fnum : 0;
1174
+ const bit = fnum >= 1 && fnum <= maxField ? 1 << fnum : 0;
1157
1175
  if ((mask32 & bit) !== 0) {
1158
1176
  if (cleanStart < fieldStart) {
1159
1177
  activeEnc.writeRaw(raw.subarray(cleanStart, fieldStart));
1160
1178
  }
1161
- if ((emitted32 & bit) === 0)
1179
+ if ((emitted32 & bit) === 0) {
1162
1180
  appendDirty(activeEnc, fnum);
1163
- emitted32 |= bit;
1181
+ emitted32 |= bit;
1182
+ }
1164
1183
  cleanStart = fieldEnd;
1165
1184
  }
1166
1185
  else {
@@ -1198,20 +1217,21 @@ export function spliceFields(raw, dirtyMask, maxField, appendDirty, enc) {
1198
1217
  while (pos < n) {
1199
1218
  const fieldStart = pos;
1200
1219
  const [tag, width] = decodeVarintBigInt(raw, pos);
1201
- if (!width || tag >> 4n > 0xffffffffn)
1220
+ if (!width || tag >> 4n > 536870911n)
1202
1221
  throw new Error('omnibuf: invalid tag while splicing');
1203
1222
  const fnum = Number(tag >> 4n);
1204
1223
  const wt = Number(tag & 15n);
1205
1224
  pos = skipValue(raw, pos + width, wt);
1206
1225
  const fieldEnd = pos;
1207
- const bit = fnum <= maxField ? 1n << BigInt(fnum) : 0n;
1226
+ const bit = fnum >= 1 && fnum <= maxField ? 1n << BigInt(fnum) : 0n;
1208
1227
  if ((mask & bit) !== 0n) {
1209
1228
  if (cleanStart < fieldStart) {
1210
1229
  activeEnc.writeRaw(raw.subarray(cleanStart, fieldStart));
1211
1230
  }
1212
- if ((emitted & bit) === 0n)
1231
+ if ((emitted & bit) === 0n) {
1213
1232
  appendDirty(activeEnc, fnum);
1214
- emitted |= bit;
1233
+ emitted |= bit;
1234
+ }
1215
1235
  cleanStart = fieldEnd;
1216
1236
  }
1217
1237
  else {