@omnibuf/core 0.0.7 → 0.1.1

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,38 +1,61 @@
1
1
  // ============================================================================
2
2
  // Header-Scanning Field Readers (O(1) Per-Field Skip) & Wire Splicing (Opaque API)
3
3
  // ============================================================================
4
+ import { decodeSchemaMeta } from './blind.js';
4
5
  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
+ import { validateMessage, validateValue } from './validation.js';
7
+ import { WireArray, WireBoolFalse, WireBoolTrue, WireBytes, WireFixed32, WireFixed64, WireFloat32, WireFloat64, WireMap, WireSfixed32, WireSfixed64, WireSint, WireString, WireSubMsg, WireUint, decodeUTF8, decodeVarint, decodeVarintAuto, decodeVarintBigInt, decodeZigZag, decodeZigZag32, decodeZigZagAuto, decodeZigZagBigInt, float32FromBits, float64FromBits, locateField, nextContainerField, parseArrayHeader, parseMapHeader, readLenSlice, roundFloat32Shortest, skipValidatedValue, validateField0At, } from './wire.js';
6
8
  // ============================================================================
7
9
  // Scalar Readers
8
10
  // ============================================================================
9
11
  export function readUint(packet, fieldNum) {
10
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
11
- if (!ok || wireType !== WireUint)
12
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireUint);
13
+ if (!ok)
12
14
  return [0, false];
13
15
  const [v, n] = decodeVarint(valSlice, 0);
14
16
  return [v, n > 0];
15
17
  }
16
- export function readEnum(packet, fieldNum) {
17
- return readInt(packet, fieldNum);
18
+ export function readUint32(packet, fieldNum) {
19
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireUint);
20
+ if (!ok || valSlice.length === 0)
21
+ return [0, false];
22
+ const b0 = valSlice[0];
23
+ if ((b0 & 7) !== 0) {
24
+ const [v, n] = decodeVarint(valSlice, 0);
25
+ return [v >>> 0, n > 0];
26
+ }
27
+ if (b0 !== 0 || valSlice.length < 9)
28
+ return [0, false];
29
+ const lo = (valSlice[1] | (valSlice[2] << 8) | (valSlice[3] << 16) | (valSlice[4] << 24)) >>> 0;
30
+ return [lo, true];
18
31
  }
32
+ export function readInt32(packet, fieldNum) {
33
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireSint);
34
+ if (!ok)
35
+ return [0, false];
36
+ const [u, n] = decodeVarintBigInt(valSlice, 0);
37
+ if (n === 0)
38
+ return [0, false];
39
+ return [decodeZigZag32(u), true];
40
+ }
41
+ export const readEnum = readInt32;
19
42
  export function readUintBigInt(packet, fieldNum) {
20
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
21
- if (!ok || wireType !== WireUint)
43
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireUint);
44
+ if (!ok)
22
45
  return [0n, false];
23
46
  const [v, n] = decodeVarintBigInt(valSlice, 0);
24
47
  return [v, n > 0];
25
48
  }
26
49
  export function readUintAuto(packet, fieldNum) {
27
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
28
- if (!ok || wireType !== WireUint)
50
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireUint);
51
+ if (!ok)
29
52
  return [0, false];
30
53
  const [v, n] = decodeVarintAuto(valSlice, 0);
31
54
  return [v, n > 0];
32
55
  }
33
56
  export function readInt(packet, fieldNum) {
34
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
35
- if (!ok || wireType !== WireSint)
57
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireSint);
58
+ if (!ok)
36
59
  return [0, false];
37
60
  const [u, n] = decodeVarintBigInt(valSlice, 0);
38
61
  if (n === 0)
@@ -40,8 +63,8 @@ export function readInt(packet, fieldNum) {
40
63
  return [decodeZigZag(u), true];
41
64
  }
42
65
  export function readIntBigInt(packet, fieldNum) {
43
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
44
- if (!ok || wireType !== WireSint)
66
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireSint);
67
+ if (!ok)
45
68
  return [0n, false];
46
69
  const [u, n] = decodeVarintBigInt(valSlice, 0);
47
70
  if (n === 0)
@@ -49,8 +72,8 @@ export function readIntBigInt(packet, fieldNum) {
49
72
  return [decodeZigZagBigInt(u), true];
50
73
  }
51
74
  export function readIntAuto(packet, fieldNum) {
52
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
53
- if (!ok || wireType !== WireSint)
75
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireSint);
76
+ if (!ok)
54
77
  return [0, false];
55
78
  const [u, n] = decodeVarintBigInt(valSlice, 0);
56
79
  if (n === 0)
@@ -58,39 +81,39 @@ export function readIntAuto(packet, fieldNum) {
58
81
  return [decodeZigZagAuto(u), true];
59
82
  }
60
83
  export function readBool(packet, fieldNum) {
61
- const { wireType, ok } = locateField(packet, fieldNum);
62
- if (!ok || (wireType !== WireBoolTrue && wireType !== WireBoolFalse)) {
84
+ const { wireType, ok } = locateField(packet, fieldNum, (1 << WireBoolFalse) | (1 << WireBoolTrue));
85
+ if (!ok) {
63
86
  return [false, false];
64
87
  }
65
88
  return [wireType === WireBoolTrue, true];
66
89
  }
67
90
  export function readFixed32(packet, fieldNum) {
68
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
69
- if (!ok || wireType !== WireFixed32 || valSlice.length < 4) {
91
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireFixed32);
92
+ if (!ok || valSlice.length < 4) {
70
93
  return [0, false];
71
94
  }
72
95
  const bits = (valSlice[0] | (valSlice[1] << 8) | (valSlice[2] << 16) | (valSlice[3] << 24)) >>> 0;
73
96
  return [bits, true];
74
97
  }
75
98
  export function readSfixed32(packet, fieldNum) {
76
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
77
- if (!ok || wireType !== WireSfixed32 || valSlice.length < 4) {
99
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireSfixed32);
100
+ if (!ok || valSlice.length < 4) {
78
101
  return [0, false];
79
102
  }
80
103
  const bits = valSlice[0] | (valSlice[1] << 8) | (valSlice[2] << 16) | (valSlice[3] << 24) | 0;
81
104
  return [bits, true];
82
105
  }
83
106
  export function readFloat32(packet, fieldNum) {
84
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
85
- if (!ok || wireType !== WireFloat32 || valSlice.length < 4) {
107
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireFloat32);
108
+ if (!ok || valSlice.length < 4) {
86
109
  return [0, false];
87
110
  }
88
111
  const bits = (valSlice[0] | (valSlice[1] << 8) | (valSlice[2] << 16) | (valSlice[3] << 24)) >>> 0;
89
112
  return [float32FromBits(bits), true];
90
113
  }
91
114
  export function readFixed64(packet, fieldNum) {
92
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
93
- if (!ok || wireType !== WireFixed64 || valSlice.length < 8) {
115
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireFixed64);
116
+ if (!ok || valSlice.length < 8) {
94
117
  return [0n, false];
95
118
  }
96
119
  let u64 = 0n;
@@ -106,8 +129,8 @@ export function readFixed64Auto(packet, fieldNum) {
106
129
  return [u64 <= 9007199254740991n ? Number(u64) : u64, true];
107
130
  }
108
131
  export function readSfixed64(packet, fieldNum) {
109
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
110
- if (!ok || wireType !== WireSfixed64 || valSlice.length < 8) {
132
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireSfixed64);
133
+ if (!ok || valSlice.length < 8) {
111
134
  return [0n, false];
112
135
  }
113
136
  let u64 = 0n;
@@ -123,8 +146,8 @@ export function readSfixed64Auto(packet, fieldNum) {
123
146
  return [s64 >= -9007199254740991n && s64 <= 9007199254740991n ? Number(s64) : s64, true];
124
147
  }
125
148
  export function readFloat64(packet, fieldNum) {
126
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
127
- if (!ok || wireType !== WireFloat64 || valSlice.length < 8) {
149
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireFloat64);
150
+ if (!ok || valSlice.length < 8) {
128
151
  return [0, false];
129
152
  }
130
153
  let u64 = 0n;
@@ -133,9 +156,9 @@ export function readFloat64(packet, fieldNum) {
133
156
  }
134
157
  return [float64FromBits(u64), true];
135
158
  }
136
- export function readStringBytes(packet, fieldNum) {
137
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
138
- if (!ok || wireType !== WireString || valSlice.length === 0) {
159
+ function locateStringSlice(packet, fieldNum) {
160
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireString);
161
+ if (!ok || valSlice.length === 0) {
139
162
  return [new Uint8Array(0), false];
140
163
  }
141
164
  const b0 = valSlice[0];
@@ -152,15 +175,32 @@ export function readStringBytes(packet, fieldNum) {
152
175
  }
153
176
  return [valSlice.subarray(sn, sn + Number(sLen)), true];
154
177
  }
178
+ export function readStringBytes(packet, fieldNum) {
179
+ const [slice, ok] = locateStringSlice(packet, fieldNum);
180
+ if (!ok)
181
+ return [new Uint8Array(0), false];
182
+ try {
183
+ decodeUTF8(slice);
184
+ return [slice, true];
185
+ }
186
+ catch {
187
+ return [new Uint8Array(0), false];
188
+ }
189
+ }
155
190
  export function readString(packet, fieldNum) {
156
- const [b, ok] = readStringBytes(packet, fieldNum);
191
+ const [b, ok] = locateStringSlice(packet, fieldNum);
157
192
  if (!ok)
158
193
  return ['', false];
159
- return [decodeUTF8(b), true];
194
+ try {
195
+ return [decodeUTF8(b), true];
196
+ }
197
+ catch {
198
+ return ['', false];
199
+ }
160
200
  }
161
201
  export function readBytes(packet, fieldNum) {
162
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
163
- if (!ok || wireType !== WireBytes) {
202
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireBytes);
203
+ if (!ok) {
164
204
  return [new Uint8Array(0), false];
165
205
  }
166
206
  const [bLen, bn] = decodeVarintBigInt(valSlice, 0);
@@ -206,6 +246,12 @@ export function readSubMsg(packet, fieldNum) {
206
246
  let off = 0;
207
247
  for (let i = 0; i < chunks.length; i++) {
208
248
  const c = chunks[i];
249
+ try {
250
+ validateMessage(c, 1);
251
+ }
252
+ catch {
253
+ return [new Uint8Array(0), false];
254
+ }
209
255
  merged.set(c, off);
210
256
  off += c.length;
211
257
  }
@@ -220,6 +266,7 @@ export function readElem(arrPayload, index) {
220
266
  }
221
267
  let pos = startP;
222
268
  const n = arrPayload.length;
269
+ let foundElem = new Uint8Array(0);
223
270
  for (let i = 0; i < count; i++) {
224
271
  if (pos >= n)
225
272
  return [new Uint8Array(0), false];
@@ -249,11 +296,14 @@ export function readElem(arrPayload, index) {
249
296
  return [new Uint8Array(0), false];
250
297
  }
251
298
  if (i === index) {
252
- return [arrPayload.subarray(pos, pos + elemLen), true];
299
+ foundElem = arrPayload.subarray(pos, pos + elemLen);
253
300
  }
254
301
  pos += elemLen;
255
302
  }
256
- return [new Uint8Array(0), false];
303
+ if (pos !== n) {
304
+ return [new Uint8Array(0), false];
305
+ }
306
+ return [foundElem, true];
257
307
  }
258
308
  export function countSubMsgArrElems(arrPayload) {
259
309
  const [elemType, count, startP, ok] = parseArrayHeader(arrPayload);
@@ -312,17 +362,26 @@ export function readSubMsgArrElem(packet, fieldNum, index) {
312
362
  return [new Uint8Array(0), false];
313
363
  let remIdx = index;
314
364
  let pos = 0;
365
+ let foundElem = new Uint8Array(0);
366
+ let found = false;
315
367
  while (true) {
316
368
  const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
317
- if (!valid || !matched)
369
+ if (!valid)
318
370
  return [new Uint8Array(0), false];
371
+ if (!matched)
372
+ return [foundElem, found];
319
373
  pos = nextPos;
374
+ if (!found) {
375
+ const [elem, elemOk] = readElem(body, remIdx);
376
+ if (elemOk) {
377
+ foundElem = elem;
378
+ found = true;
379
+ continue;
380
+ }
381
+ }
320
382
  const [c, validArr] = countSubMsgArrElems(body);
321
383
  if (!validArr)
322
384
  return [new Uint8Array(0), false];
323
- if (remIdx < c) {
324
- return readElem(body, remIdx);
325
- }
326
385
  remIdx -= c;
327
386
  }
328
387
  }
@@ -359,31 +418,136 @@ export function readArrNestedString(packet, arrField, index, targetField) {
359
418
  // ============================================================================
360
419
  // Packed & Counted Array Readers
361
420
  // ============================================================================
421
+ function validateCountedArrBody(body, elemType, count, startP) {
422
+ const n = body.length;
423
+ let p = startP;
424
+ switch (elemType) {
425
+ case WireBoolFalse: {
426
+ if (n - p !== count)
427
+ return false;
428
+ for (let i = 0; i < count; i++) {
429
+ const b = body[p + i];
430
+ if (b !== 0 && b !== 1)
431
+ return false;
432
+ }
433
+ return true;
434
+ }
435
+ case WireFixed32:
436
+ case WireSfixed32:
437
+ case WireFloat32:
438
+ return n - p === count * 4;
439
+ case WireFixed64:
440
+ case WireSfixed64:
441
+ case WireFloat64:
442
+ return n - p === count * 8;
443
+ case WireString: {
444
+ for (let i = 0; i < count; i++) {
445
+ if (p >= n)
446
+ return false;
447
+ const [sLen, sn] = decodeVarintBigInt(body, p);
448
+ if (sn === 0 || sLen > BigInt(n - (p + sn)))
449
+ return false;
450
+ p += sn;
451
+ const end = p + Number(sLen);
452
+ try {
453
+ decodeUTF8(body, p, end);
454
+ }
455
+ catch {
456
+ return false;
457
+ }
458
+ p = end;
459
+ }
460
+ return p === n;
461
+ }
462
+ case WireBytes: {
463
+ for (let i = 0; i < count; i++) {
464
+ if (p >= n)
465
+ return false;
466
+ const [bLen, bn] = decodeVarintBigInt(body, p);
467
+ if (bn === 0 || bLen > BigInt(n - (p + bn)))
468
+ return false;
469
+ p += bn + Number(bLen);
470
+ }
471
+ return p === n;
472
+ }
473
+ case WireUint:
474
+ case WireSint: {
475
+ for (let i = 0; i < count; i++) {
476
+ if (p >= n)
477
+ return false;
478
+ const [, vn] = decodeVarintBigInt(body, p);
479
+ if (vn === 0)
480
+ return false;
481
+ p += vn;
482
+ }
483
+ return p === n;
484
+ }
485
+ case WireSubMsg: {
486
+ for (let i = 0; i < count; i++) {
487
+ if (p >= n)
488
+ return false;
489
+ const [sLen, sn] = decodeVarintBigInt(body, p);
490
+ if (sn === 0 || sLen > BigInt(n - (p + sn)))
491
+ return false;
492
+ p += sn;
493
+ const end = p + Number(sLen);
494
+ try {
495
+ validateMessage(body.subarray(p, end), 1);
496
+ }
497
+ catch {
498
+ return false;
499
+ }
500
+ p = end;
501
+ }
502
+ return p === n;
503
+ }
504
+ default: {
505
+ for (let i = 0; i < count; i++) {
506
+ try {
507
+ p = validateValue(body, p, elemType, 1);
508
+ }
509
+ catch {
510
+ return false;
511
+ }
512
+ }
513
+ return p === n;
514
+ }
515
+ }
516
+ }
362
517
  export function countedArrLen(packet, fieldNum, expElemWT) {
363
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
364
- if (!ok || wireType !== WireArray)
365
- return 0;
366
- const [bLen, bn] = decodeVarintBigInt(valSlice, 0);
367
- if (bn === 0 || bLen < 2n || bLen > BigInt(valSlice.length - bn))
368
- return 0;
369
- const body = valSlice.subarray(bn, bn + Number(bLen));
370
- const [elemType, c, , headerOk] = parseArrayHeader(body);
371
- if (!headerOk)
372
- return 0;
373
- if (expElemWT !== undefined && expElemWT !== WireArray && elemType !== expElemWT) {
374
- return 0;
375
- }
376
- return c;
518
+ let total = 0;
519
+ let pos = 0;
520
+ while (true) {
521
+ const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
522
+ if (!valid)
523
+ return 0;
524
+ if (!matched)
525
+ return total;
526
+ pos = nextPos;
527
+ const [elemType, c, startP, headerOk] = parseArrayHeader(body);
528
+ if (!headerOk)
529
+ return 0;
530
+ if (expElemWT !== undefined && elemType !== expElemWT) {
531
+ return 0;
532
+ }
533
+ if (!validateCountedArrBody(body, elemType, c, startP)) {
534
+ return 0;
535
+ }
536
+ total += c;
537
+ }
377
538
  }
378
539
  export function appendStringArrBody(dst, body) {
540
+ const origLen = dst.length;
379
541
  const n = body.length;
380
542
  const [elemType, c, startP, ok] = parseArrayHeader(body);
381
543
  if (!ok || elemType !== WireString)
382
544
  return [[], false];
383
545
  let p = startP;
384
546
  for (let i = 0; i < c; i++) {
385
- if (p >= n)
547
+ if (p >= n) {
548
+ dst.length = origLen;
386
549
  return [[], false];
550
+ }
387
551
  const sb = body[p];
388
552
  let lenNum = 0;
389
553
  if ((sb & 1) !== 0) {
@@ -391,25 +555,37 @@ export function appendStringArrBody(dst, body) {
391
555
  p += 1;
392
556
  }
393
557
  else if ((sb & 2) !== 0) {
394
- if (p + 2 > n)
558
+ if (p + 2 > n) {
559
+ dst.length = origLen;
395
560
  return [[], false];
561
+ }
396
562
  lenNum = (sb | (body[p + 1] << 8)) >>> 2;
397
563
  p += 2;
398
564
  }
399
565
  else {
400
566
  const [sLen, sn] = decodeVarintBigInt(body, p);
401
567
  if (sn === 0 || sLen > BigInt(n - (p + sn))) {
568
+ dst.length = origLen;
402
569
  return [[], false];
403
570
  }
404
571
  p += sn;
405
572
  lenNum = Number(sLen);
406
573
  }
407
- if (p + lenNum > n)
574
+ if (p + lenNum > n) {
575
+ dst.length = origLen;
408
576
  return [[], false];
409
- dst.push(decodeUTF8(body, p, p + lenNum));
577
+ }
578
+ try {
579
+ dst.push(decodeUTF8(body, p, p + lenNum));
580
+ }
581
+ catch {
582
+ dst.length = origLen;
583
+ return [[], false];
584
+ }
410
585
  p += lenNum;
411
586
  }
412
587
  if (p !== n) {
588
+ dst.length = origLen;
413
589
  return [[], false];
414
590
  }
415
591
  return [dst, true];
@@ -499,13 +675,14 @@ export function appendEnumArrBody(dst, body) {
499
675
  if (vn === 0)
500
676
  return [[], false];
501
677
  p += vn;
502
- dst.push(decodeZigZag(v) | 0);
678
+ dst.push(decodeZigZag32(v));
503
679
  }
504
680
  if (p !== body.length) {
505
681
  return [[], false];
506
682
  }
507
683
  return [dst, true];
508
684
  }
685
+ export const appendSint32ArrBody = appendEnumArrBody;
509
686
  export function appendUintArrAutoBody(dst, body) {
510
687
  const [elemType, c, startP, ok] = parseArrayHeader(body);
511
688
  if (!ok || elemType !== WireUint) {
@@ -620,6 +797,7 @@ export function readEnumArr(packet, fieldNum) {
620
797
  return [[], false];
621
798
  }
622
799
  }
800
+ export const readSint32Arr = readEnumArr;
623
801
  export function readSintArr(packet, fieldNum) {
624
802
  const out = [];
625
803
  let found = false;
@@ -851,8 +1029,15 @@ export function readSubMsgMerged(packet, fieldNum, decodeFn) {
851
1029
  wt = Number(tag & 0x0fn);
852
1030
  pos += tn;
853
1031
  }
854
- if (fnum === fieldNum) {
855
- if (wt !== WireSubMsg || pos >= n)
1032
+ if (fnum === 0) {
1033
+ const f0End = validateField0At(packet, pos, wt, 0);
1034
+ if (f0End < 0)
1035
+ return [null, false];
1036
+ pos = f0End;
1037
+ continue;
1038
+ }
1039
+ if (fnum === fieldNum && wt === WireSubMsg) {
1040
+ if (pos >= n)
856
1041
  return [null, false];
857
1042
  let lenNum = 0;
858
1043
  const lb = packet[pos];
@@ -875,19 +1060,23 @@ export function readSubMsgMerged(packet, fieldNum, decodeFn) {
875
1060
  }
876
1061
  if (pos + lenNum > n)
877
1062
  return [null, false];
878
- out = useRange
879
- ? decodeFn(packet, out, found, pos, pos + lenNum)
880
- : decodeFn(packet.subarray(pos, pos + lenNum), out, found);
1063
+ try {
1064
+ out = useRange
1065
+ ? decodeFn(packet, out, found, pos, pos + lenNum)
1066
+ : decodeFn(packet.subarray(pos, pos + lenNum), out, found);
1067
+ }
1068
+ catch {
1069
+ return [null, false];
1070
+ }
881
1071
  found = true;
882
1072
  pos += lenNum;
883
1073
  continue;
884
1074
  }
885
- try {
886
- pos = skipValue(packet, pos, wt);
887
- }
888
- catch {
1075
+ const nextPos = skipValidatedValue(packet, pos, wt);
1076
+ if (nextPos < 0) {
889
1077
  return [null, false];
890
1078
  }
1079
+ pos = nextPos;
891
1080
  }
892
1081
  return [out ?? null, found];
893
1082
  }
@@ -911,7 +1100,12 @@ export function readSubMsgArrDecoded(packet, fieldNum, decodeFn) {
911
1100
  const [el, en] = decodeVarintBigInt(body, p);
912
1101
  p += en;
913
1102
  const elNum = Number(el);
914
- out.push(decodeFn(body.subarray(p, p + elNum)));
1103
+ try {
1104
+ out.push(decodeFn(body.subarray(p, p + elNum)));
1105
+ }
1106
+ catch {
1107
+ return [[], false];
1108
+ }
915
1109
  p += elNum;
916
1110
  }
917
1111
  }
@@ -957,7 +1151,7 @@ function readMapEntryPayload(body, p, wt, scalarKind, decodeSubMsg) {
957
1151
  throw new Error('omnibuf: truncated map sint');
958
1152
  const s = decodeZigZagAuto(v);
959
1153
  if (scalarKind === 'int32' || scalarKind === 'sint32' || scalarKind === 'enum') {
960
- return [Number(s) | 0, p + vn];
1154
+ return [typeof s === 'bigint' ? Number(BigInt.asIntN(32, s)) : s | 0, p + vn];
961
1155
  }
962
1156
  return [s, p + vn];
963
1157
  }
@@ -1019,7 +1213,47 @@ function readMapEntryPayload(body, p, wt, scalarKind, decodeSubMsg) {
1019
1213
  throw new Error(`omnibuf: unsupported map wire type ${wt}`);
1020
1214
  }
1021
1215
  }
1216
+ function expectedMapEntryWireType(kind) {
1217
+ switch (kind) {
1218
+ case 'uint32':
1219
+ case 'uint64':
1220
+ return WireUint;
1221
+ case 'int32':
1222
+ case 'int64':
1223
+ case 'sint32':
1224
+ case 'sint64':
1225
+ case 'enum':
1226
+ return WireSint;
1227
+ case 'bool':
1228
+ return WireBoolFalse;
1229
+ case 'fixed32':
1230
+ return WireFixed32;
1231
+ case 'fixed64':
1232
+ return WireFixed64;
1233
+ case 'sfixed32':
1234
+ return WireSfixed32;
1235
+ case 'sfixed64':
1236
+ return WireSfixed64;
1237
+ case 'float32':
1238
+ return WireFloat32;
1239
+ case 'float64':
1240
+ return WireFloat64;
1241
+ case 'string':
1242
+ return WireString;
1243
+ case 'bytes':
1244
+ return WireBytes;
1245
+ case 'submsg':
1246
+ return WireSubMsg;
1247
+ default:
1248
+ return -1;
1249
+ }
1250
+ }
1022
1251
  export function readTypedMap(packet, fieldNum, keyType, valType, decodeSubMsg) {
1252
+ const expKeyWT = expectedMapEntryWireType(keyType);
1253
+ const expValWT = expectedMapEntryWireType(valType);
1254
+ if (expKeyWT < 0 || expValWT < 0) {
1255
+ return [{}, false];
1256
+ }
1023
1257
  const out = {};
1024
1258
  let found = false;
1025
1259
  let pos = 0;
@@ -1031,21 +1265,26 @@ export function readTypedMap(packet, fieldNum, keyType, valType, decodeSubMsg) {
1031
1265
  return [out, found];
1032
1266
  pos = nextPos;
1033
1267
  const [keyWT, valWT, c, startP, ok] = parseMapHeader(body);
1034
- if (!ok) {
1268
+ if (!ok || keyWT !== expKeyWT || valWT !== expValWT) {
1035
1269
  return [{}, false];
1036
1270
  }
1037
1271
  found = true;
1038
1272
  let p = startP;
1039
- for (let i = 0; i < c; i++) {
1040
- const [kVal, p1] = readMapEntryPayload(body, p, keyWT, keyType);
1041
- const [vVal, p2] = readMapEntryPayload(body, p1, valWT, valType, decodeSubMsg);
1042
- p = p2;
1043
- Object.defineProperty(out, String(kVal), {
1044
- value: vVal,
1045
- writable: true,
1046
- enumerable: true,
1047
- configurable: true,
1048
- });
1273
+ try {
1274
+ for (let i = 0; i < c; i++) {
1275
+ const [kVal, p1] = readMapEntryPayload(body, p, keyWT, keyType);
1276
+ const [vVal, p2] = readMapEntryPayload(body, p1, valWT, valType, decodeSubMsg);
1277
+ p = p2;
1278
+ Object.defineProperty(out, String(kVal), {
1279
+ value: vVal,
1280
+ writable: true,
1281
+ enumerable: true,
1282
+ configurable: true,
1283
+ });
1284
+ }
1285
+ }
1286
+ catch {
1287
+ return [{}, false];
1049
1288
  }
1050
1289
  if (p !== body.length) {
1051
1290
  return [{}, false];
@@ -1078,7 +1317,13 @@ export function lookupMapStringUint(packet, fieldNum, targetKey) {
1078
1317
  }
1079
1318
  p += kn;
1080
1319
  const kLenNum = Number(kLen);
1081
- const k = decodeUTF8(body, p, p + kLenNum);
1320
+ let k;
1321
+ try {
1322
+ k = decodeUTF8(body, p, p + kLenNum);
1323
+ }
1324
+ catch {
1325
+ return [0, false];
1326
+ }
1082
1327
  p += kLenNum;
1083
1328
  const [v, vn] = decodeVarintAuto(body, p);
1084
1329
  if (vn === 0)
@@ -1135,6 +1380,14 @@ export function activeOneofSlice(packet, targetField, oneofSpecs) {
1135
1380
  wt = Number(tag & 0x0fn);
1136
1381
  pos += tn;
1137
1382
  }
1383
+ if (fnum === 0) {
1384
+ const f0End = validateField0At(packet, pos, wt, 0);
1385
+ if (f0End < 0) {
1386
+ return null;
1387
+ }
1388
+ pos = f0End;
1389
+ continue;
1390
+ }
1138
1391
  for (let i = 0; i < oneofSpecs.length; i++) {
1139
1392
  const spec = oneofSpecs[i];
1140
1393
  if (spec[0] === fnum && ((spec[1] >>> wt) & 1) !== 0) {
@@ -1145,7 +1398,7 @@ export function activeOneofSlice(packet, targetField, oneofSpecs) {
1145
1398
  break;
1146
1399
  }
1147
1400
  }
1148
- const nextPos = fastSkipValue(packet, pos, wt);
1401
+ const nextPos = skipValidatedValue(packet, pos, wt);
1149
1402
  if (nextPos < 0) {
1150
1403
  return null;
1151
1404
  }
@@ -1158,14 +1411,53 @@ export function activeOneofSlice(packet, targetField, oneofSpecs) {
1158
1411
  }
1159
1412
  const sharedSpliceEncoder = new Encoder(8192);
1160
1413
  let sharedSpliceInUse = false;
1414
+ function extractDirtySetFromBigInt(mask, maxField) {
1415
+ const dirtySet = new Set();
1416
+ let remMask = mask & ~1n;
1417
+ let bitBase = 0;
1418
+ while (remMask !== 0n && bitBase <= maxField) {
1419
+ if ((remMask & 0xffffffffffffffffn) === 0n) {
1420
+ let shift = 64;
1421
+ while ((remMask & ((1n << BigInt(shift)) - 1n)) === 0n && bitBase + shift <= maxField) {
1422
+ shift *= 2;
1423
+ }
1424
+ if (shift > 64)
1425
+ shift >>= 1;
1426
+ remMask >>= BigInt(shift);
1427
+ bitBase += shift;
1428
+ continue;
1429
+ }
1430
+ let wordLo = Number(remMask & 0xffffffffn) >>> 0;
1431
+ let wordHi = Number((remMask >> 32n) & 0xffffffffn) >>> 0;
1432
+ while (wordLo !== 0) {
1433
+ const tz = 31 - Math.clz32(wordLo & -wordLo);
1434
+ const fn = bitBase + tz;
1435
+ if (fn >= 1 && fn <= maxField)
1436
+ dirtySet.add(fn);
1437
+ wordLo &= wordLo - 1;
1438
+ }
1439
+ while (wordHi !== 0) {
1440
+ const tz = 31 - Math.clz32(wordHi & -wordHi);
1441
+ const fn = bitBase + 32 + tz;
1442
+ if (fn >= 1 && fn <= maxField)
1443
+ dirtySet.add(fn);
1444
+ wordHi &= wordHi - 1;
1445
+ }
1446
+ remMask >>= 64n;
1447
+ bitBase += 64;
1448
+ }
1449
+ return dirtySet;
1450
+ }
1161
1451
  /**
1162
1452
  * Splices unmodified contiguous byte ranges from `raw` and invokes `appendDirty`
1163
- * only for fields whose bit is set in `dirtyMask`, avoiding any decoding of untouched fields.
1453
+ * only for fields whose bit is set in `dirtyMask` (or present in a `ReadonlySet<number>`),
1454
+ * avoiding any decoding of untouched fields.
1164
1455
  */
1165
1456
  export function spliceFields(raw, dirtyMask, maxField, appendDirty, enc) {
1166
- if (maxField <= 30) {
1457
+ if (maxField <= 30 && (typeof dirtyMask === 'number' || typeof dirtyMask === 'bigint')) {
1167
1458
  const mask32 = typeof dirtyMask === 'bigint' ? Number(dirtyMask & 0x7fffffffn) : dirtyMask | 0;
1168
1459
  if (mask32 === 0) {
1460
+ validateMessage(raw, 0);
1169
1461
  if (enc !== undefined) {
1170
1462
  enc.detachIfOverlaps(raw, 0);
1171
1463
  enc.writeRaw(raw);
@@ -1215,7 +1507,17 @@ export function spliceFields(raw, dirtyMask, maxField, appendDirty, enc) {
1215
1507
  }
1216
1508
  const fnum = tag <= 0x7fffffff ? tag >>> 4 : Math.floor(tag / 16);
1217
1509
  const wt = tag & 0x0f;
1218
- pos = skipValue(raw, pos, wt);
1510
+ if (fnum === 0) {
1511
+ if (wt !== WireSubMsg) {
1512
+ throw new Error('omnibuf: invalid Field #0 wire type');
1513
+ }
1514
+ const [f0Body, f0End] = readLenSlice(raw, pos, 0);
1515
+ decodeSchemaMeta(f0Body, 1);
1516
+ pos = f0End;
1517
+ }
1518
+ else {
1519
+ pos = validateValue(raw, pos, wt, 0);
1520
+ }
1219
1521
  const fieldEnd = pos;
1220
1522
  const bit = fnum >= 1 && fnum <= maxField ? 1 << fnum : 0;
1221
1523
  if ((mask32 & bit) !== 0) {
@@ -1250,8 +1552,11 @@ export function spliceFields(raw, dirtyMask, maxField, appendDirty, enc) {
1250
1552
  sharedSpliceInUse = false;
1251
1553
  }
1252
1554
  }
1253
- const mask = typeof dirtyMask === 'bigint' ? dirtyMask : BigInt(dirtyMask >>> 0);
1254
- if (mask === 0n) {
1555
+ const dirtySet = typeof dirtyMask === 'object' && dirtyMask !== null
1556
+ ? dirtyMask
1557
+ : extractDirtySetFromBigInt(typeof dirtyMask === 'bigint' ? dirtyMask : BigInt(dirtyMask >>> 0), maxField);
1558
+ if (dirtySet.size === 0) {
1559
+ validateMessage(raw, 0);
1255
1560
  if (enc !== undefined) {
1256
1561
  enc.detachIfOverlaps(raw, 0);
1257
1562
  enc.writeRaw(raw);
@@ -1269,42 +1574,71 @@ export function spliceFields(raw, dirtyMask, maxField, appendDirty, enc) {
1269
1574
  const n = raw.length;
1270
1575
  let pos = 0;
1271
1576
  let cleanStart = 0;
1272
- let emitted = 0n;
1577
+ const emittedSet = new Set();
1273
1578
  while (pos < n) {
1274
1579
  const fieldStart = pos;
1275
- const [tag, width] = decodeVarintBigInt(raw, pos);
1276
- if (!width || tag >> 4n > 536870911n)
1277
- throw new Error('omnibuf: invalid tag while splicing');
1278
- const fnum = Number(tag >> 4n);
1279
- const wt = Number(tag & 15n);
1280
- pos = skipValue(raw, pos + width, wt);
1580
+ let tag = 0;
1581
+ const b0 = raw[pos];
1582
+ if ((b0 & 1) !== 0) {
1583
+ tag = b0 >>> 1;
1584
+ pos += 1;
1585
+ }
1586
+ else if ((b0 & 2) !== 0) {
1587
+ if (pos + 2 > n)
1588
+ throw new Error('omnibuf: invalid tag while splicing');
1589
+ tag = ((b0 | (raw[pos + 1] << 8)) >>> 2) >>> 0;
1590
+ pos += 2;
1591
+ }
1592
+ else {
1593
+ const [t64, width] = decodeVarintBigInt(raw, pos);
1594
+ if (!width || t64 >> 4n > 536870911n)
1595
+ throw new Error('omnibuf: invalid tag while splicing');
1596
+ tag = Number(t64);
1597
+ pos += width;
1598
+ }
1599
+ const fnum = tag <= 0x7fffffff ? tag >>> 4 : Math.floor(tag / 16);
1600
+ const wt = tag & 0x0f;
1601
+ if (fnum === 0) {
1602
+ if (wt !== WireSubMsg) {
1603
+ throw new Error('omnibuf: invalid Field #0 wire type');
1604
+ }
1605
+ const [f0Body, f0End] = readLenSlice(raw, pos, 0);
1606
+ decodeSchemaMeta(f0Body, 1);
1607
+ pos = f0End;
1608
+ }
1609
+ else {
1610
+ pos = validateValue(raw, pos, wt, 0);
1611
+ }
1281
1612
  const fieldEnd = pos;
1282
- const bit = fnum >= 1 && fnum <= maxField ? 1n << BigInt(fnum) : 0n;
1283
- if ((mask & bit) !== 0n) {
1613
+ if (fnum >= 1 && fnum <= maxField && dirtySet.has(fnum)) {
1284
1614
  if (cleanStart < fieldStart) {
1285
1615
  activeEnc.writeRaw(raw.subarray(cleanStart, fieldStart));
1286
1616
  }
1287
- if ((emitted & bit) === 0n) {
1617
+ if (!emittedSet.has(fnum)) {
1288
1618
  appendDirty(activeEnc, fnum);
1289
- emitted |= bit;
1619
+ emittedSet.add(fnum);
1290
1620
  }
1291
1621
  cleanStart = fieldEnd;
1292
1622
  }
1293
- else {
1294
- emitted |= bit;
1623
+ else if (fnum >= 1) {
1624
+ emittedSet.add(fnum);
1295
1625
  }
1296
1626
  }
1297
1627
  if (cleanStart < n) {
1298
1628
  activeEnc.writeRaw(raw.subarray(cleanStart, n));
1299
1629
  }
1300
- const rem = mask & ~emitted;
1301
- if (rem !== 0n) {
1302
- for (let fnum = 1; fnum <= maxField; fnum++) {
1303
- if ((rem & (1n << BigInt(fnum))) !== 0n) {
1304
- appendDirty(activeEnc, fnum);
1305
- }
1630
+ const remaining = [];
1631
+ for (const fn of dirtySet) {
1632
+ if (fn >= 1 && fn <= maxField && !emittedSet.has(fn)) {
1633
+ remaining.push(fn);
1306
1634
  }
1307
1635
  }
1636
+ if (remaining.length > 1) {
1637
+ remaining.sort((a, b) => a - b);
1638
+ }
1639
+ for (let i = 0; i < remaining.length; i++) {
1640
+ appendDirty(activeEnc, remaining[i]);
1641
+ }
1308
1642
  return activeEnc.finish();
1309
1643
  }
1310
1644
  //# sourceMappingURL=readers.js.map