@omnibuf/core 0.0.8 → 0.1.2

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,21 +1,23 @@
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, float32FromBits, float64FromBits, locateField, nextContainerField, parseArrayHeader, parseMapHeader, roundFloat32Shortest, skipValidatedValue, 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
18
  export function readUint32(packet, fieldNum) {
17
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
18
- if (!ok || wireType !== WireUint || valSlice.length === 0)
19
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireUint);
20
+ if (!ok || valSlice.length === 0)
19
21
  return [0, false];
20
22
  const b0 = valSlice[0];
21
23
  if ((b0 & 7) !== 0) {
@@ -28,30 +30,32 @@ export function readUint32(packet, fieldNum) {
28
30
  return [lo, true];
29
31
  }
30
32
  export function readInt32(packet, fieldNum) {
31
- const [v, ok] = readInt(packet, fieldNum);
32
- return [v | 0, ok];
33
- }
34
- export function readEnum(packet, fieldNum) {
35
- const [v, ok] = readInt(packet, fieldNum);
36
- return [v | 0, ok];
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];
37
40
  }
41
+ export const readEnum = readInt32;
38
42
  export function readUintBigInt(packet, fieldNum) {
39
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
40
- if (!ok || wireType !== WireUint)
43
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireUint);
44
+ if (!ok)
41
45
  return [0n, false];
42
46
  const [v, n] = decodeVarintBigInt(valSlice, 0);
43
47
  return [v, n > 0];
44
48
  }
45
49
  export function readUintAuto(packet, fieldNum) {
46
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
47
- if (!ok || wireType !== WireUint)
50
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireUint);
51
+ if (!ok)
48
52
  return [0, false];
49
53
  const [v, n] = decodeVarintAuto(valSlice, 0);
50
54
  return [v, n > 0];
51
55
  }
52
56
  export function readInt(packet, fieldNum) {
53
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
54
- if (!ok || wireType !== WireSint)
57
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireSint);
58
+ if (!ok)
55
59
  return [0, false];
56
60
  const [u, n] = decodeVarintBigInt(valSlice, 0);
57
61
  if (n === 0)
@@ -59,8 +63,8 @@ export function readInt(packet, fieldNum) {
59
63
  return [decodeZigZag(u), true];
60
64
  }
61
65
  export function readIntBigInt(packet, fieldNum) {
62
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
63
- if (!ok || wireType !== WireSint)
66
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireSint);
67
+ if (!ok)
64
68
  return [0n, false];
65
69
  const [u, n] = decodeVarintBigInt(valSlice, 0);
66
70
  if (n === 0)
@@ -68,8 +72,8 @@ export function readIntBigInt(packet, fieldNum) {
68
72
  return [decodeZigZagBigInt(u), true];
69
73
  }
70
74
  export function readIntAuto(packet, fieldNum) {
71
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
72
- if (!ok || wireType !== WireSint)
75
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireSint);
76
+ if (!ok)
73
77
  return [0, false];
74
78
  const [u, n] = decodeVarintBigInt(valSlice, 0);
75
79
  if (n === 0)
@@ -77,39 +81,39 @@ export function readIntAuto(packet, fieldNum) {
77
81
  return [decodeZigZagAuto(u), true];
78
82
  }
79
83
  export function readBool(packet, fieldNum) {
80
- const { wireType, ok } = locateField(packet, fieldNum);
81
- if (!ok || (wireType !== WireBoolTrue && wireType !== WireBoolFalse)) {
84
+ const { wireType, ok } = locateField(packet, fieldNum, (1 << WireBoolFalse) | (1 << WireBoolTrue));
85
+ if (!ok) {
82
86
  return [false, false];
83
87
  }
84
88
  return [wireType === WireBoolTrue, true];
85
89
  }
86
90
  export function readFixed32(packet, fieldNum) {
87
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
88
- if (!ok || wireType !== WireFixed32 || valSlice.length < 4) {
91
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireFixed32);
92
+ if (!ok || valSlice.length < 4) {
89
93
  return [0, false];
90
94
  }
91
95
  const bits = (valSlice[0] | (valSlice[1] << 8) | (valSlice[2] << 16) | (valSlice[3] << 24)) >>> 0;
92
96
  return [bits, true];
93
97
  }
94
98
  export function readSfixed32(packet, fieldNum) {
95
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
96
- if (!ok || wireType !== WireSfixed32 || valSlice.length < 4) {
99
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireSfixed32);
100
+ if (!ok || valSlice.length < 4) {
97
101
  return [0, false];
98
102
  }
99
103
  const bits = valSlice[0] | (valSlice[1] << 8) | (valSlice[2] << 16) | (valSlice[3] << 24) | 0;
100
104
  return [bits, true];
101
105
  }
102
106
  export function readFloat32(packet, fieldNum) {
103
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
104
- if (!ok || wireType !== WireFloat32 || valSlice.length < 4) {
107
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireFloat32);
108
+ if (!ok || valSlice.length < 4) {
105
109
  return [0, false];
106
110
  }
107
111
  const bits = (valSlice[0] | (valSlice[1] << 8) | (valSlice[2] << 16) | (valSlice[3] << 24)) >>> 0;
108
112
  return [float32FromBits(bits), true];
109
113
  }
110
114
  export function readFixed64(packet, fieldNum) {
111
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
112
- if (!ok || wireType !== WireFixed64 || valSlice.length < 8) {
115
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireFixed64);
116
+ if (!ok || valSlice.length < 8) {
113
117
  return [0n, false];
114
118
  }
115
119
  let u64 = 0n;
@@ -125,8 +129,8 @@ export function readFixed64Auto(packet, fieldNum) {
125
129
  return [u64 <= 9007199254740991n ? Number(u64) : u64, true];
126
130
  }
127
131
  export function readSfixed64(packet, fieldNum) {
128
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
129
- if (!ok || wireType !== WireSfixed64 || valSlice.length < 8) {
132
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireSfixed64);
133
+ if (!ok || valSlice.length < 8) {
130
134
  return [0n, false];
131
135
  }
132
136
  let u64 = 0n;
@@ -142,8 +146,8 @@ export function readSfixed64Auto(packet, fieldNum) {
142
146
  return [s64 >= -9007199254740991n && s64 <= 9007199254740991n ? Number(s64) : s64, true];
143
147
  }
144
148
  export function readFloat64(packet, fieldNum) {
145
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
146
- if (!ok || wireType !== WireFloat64 || valSlice.length < 8) {
149
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireFloat64);
150
+ if (!ok || valSlice.length < 8) {
147
151
  return [0, false];
148
152
  }
149
153
  let u64 = 0n;
@@ -153,8 +157,8 @@ export function readFloat64(packet, fieldNum) {
153
157
  return [float64FromBits(u64), true];
154
158
  }
155
159
  function locateStringSlice(packet, fieldNum) {
156
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
157
- if (!ok || wireType !== WireString || valSlice.length === 0) {
160
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireString);
161
+ if (!ok || valSlice.length === 0) {
158
162
  return [new Uint8Array(0), false];
159
163
  }
160
164
  const b0 = valSlice[0];
@@ -195,8 +199,8 @@ export function readString(packet, fieldNum) {
195
199
  }
196
200
  }
197
201
  export function readBytes(packet, fieldNum) {
198
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
199
- if (!ok || wireType !== WireBytes) {
202
+ const { valSlice, ok } = locateField(packet, fieldNum, 1 << WireBytes);
203
+ if (!ok) {
200
204
  return [new Uint8Array(0), false];
201
205
  }
202
206
  const [bLen, bn] = decodeVarintBigInt(valSlice, 0);
@@ -242,6 +246,12 @@ export function readSubMsg(packet, fieldNum) {
242
246
  let off = 0;
243
247
  for (let i = 0; i < chunks.length; i++) {
244
248
  const c = chunks[i];
249
+ try {
250
+ validateMessage(c, 1);
251
+ }
252
+ catch {
253
+ return [new Uint8Array(0), false];
254
+ }
245
255
  merged.set(c, off);
246
256
  off += c.length;
247
257
  }
@@ -256,6 +266,7 @@ export function readElem(arrPayload, index) {
256
266
  }
257
267
  let pos = startP;
258
268
  const n = arrPayload.length;
269
+ let foundElem = new Uint8Array(0);
259
270
  for (let i = 0; i < count; i++) {
260
271
  if (pos >= n)
261
272
  return [new Uint8Array(0), false];
@@ -285,11 +296,14 @@ export function readElem(arrPayload, index) {
285
296
  return [new Uint8Array(0), false];
286
297
  }
287
298
  if (i === index) {
288
- return [arrPayload.subarray(pos, pos + elemLen), true];
299
+ foundElem = arrPayload.subarray(pos, pos + elemLen);
289
300
  }
290
301
  pos += elemLen;
291
302
  }
292
- return [new Uint8Array(0), false];
303
+ if (pos !== n) {
304
+ return [new Uint8Array(0), false];
305
+ }
306
+ return [foundElem, true];
293
307
  }
294
308
  export function countSubMsgArrElems(arrPayload) {
295
309
  const [elemType, count, startP, ok] = parseArrayHeader(arrPayload);
@@ -348,17 +362,26 @@ export function readSubMsgArrElem(packet, fieldNum, index) {
348
362
  return [new Uint8Array(0), false];
349
363
  let remIdx = index;
350
364
  let pos = 0;
365
+ let foundElem = new Uint8Array(0);
366
+ let found = false;
351
367
  while (true) {
352
368
  const { body, nextPos, matched, valid } = nextContainerField(packet, pos, fieldNum, WireArray, 2);
353
- if (!valid || !matched)
369
+ if (!valid)
354
370
  return [new Uint8Array(0), false];
371
+ if (!matched)
372
+ return [foundElem, found];
355
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
+ }
356
382
  const [c, validArr] = countSubMsgArrElems(body);
357
383
  if (!validArr)
358
384
  return [new Uint8Array(0), false];
359
- if (remIdx < c) {
360
- return readElem(body, remIdx);
361
- }
362
385
  remIdx -= c;
363
386
  }
364
387
  }
@@ -395,31 +418,136 @@ export function readArrNestedString(packet, arrField, index, targetField) {
395
418
  // ============================================================================
396
419
  // Packed & Counted Array Readers
397
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
+ }
398
517
  export function countedArrLen(packet, fieldNum, expElemWT) {
399
- const { valSlice, wireType, ok } = locateField(packet, fieldNum);
400
- if (!ok || wireType !== WireArray)
401
- return 0;
402
- const [bLen, bn] = decodeVarintBigInt(valSlice, 0);
403
- if (bn === 0 || bLen < 2n || bLen > BigInt(valSlice.length - bn))
404
- return 0;
405
- const body = valSlice.subarray(bn, bn + Number(bLen));
406
- const [elemType, c, , headerOk] = parseArrayHeader(body);
407
- if (!headerOk)
408
- return 0;
409
- if (expElemWT !== undefined && expElemWT !== WireArray && elemType !== expElemWT) {
410
- return 0;
411
- }
412
- 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
+ }
413
538
  }
414
539
  export function appendStringArrBody(dst, body) {
540
+ const origLen = dst.length;
415
541
  const n = body.length;
416
542
  const [elemType, c, startP, ok] = parseArrayHeader(body);
417
543
  if (!ok || elemType !== WireString)
418
544
  return [[], false];
419
545
  let p = startP;
420
546
  for (let i = 0; i < c; i++) {
421
- if (p >= n)
547
+ if (p >= n) {
548
+ dst.length = origLen;
422
549
  return [[], false];
550
+ }
423
551
  const sb = body[p];
424
552
  let lenNum = 0;
425
553
  if ((sb & 1) !== 0) {
@@ -427,25 +555,37 @@ export function appendStringArrBody(dst, body) {
427
555
  p += 1;
428
556
  }
429
557
  else if ((sb & 2) !== 0) {
430
- if (p + 2 > n)
558
+ if (p + 2 > n) {
559
+ dst.length = origLen;
431
560
  return [[], false];
561
+ }
432
562
  lenNum = (sb | (body[p + 1] << 8)) >>> 2;
433
563
  p += 2;
434
564
  }
435
565
  else {
436
566
  const [sLen, sn] = decodeVarintBigInt(body, p);
437
567
  if (sn === 0 || sLen > BigInt(n - (p + sn))) {
568
+ dst.length = origLen;
438
569
  return [[], false];
439
570
  }
440
571
  p += sn;
441
572
  lenNum = Number(sLen);
442
573
  }
443
- if (p + lenNum > n)
574
+ if (p + lenNum > n) {
575
+ dst.length = origLen;
444
576
  return [[], false];
445
- 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
+ }
446
585
  p += lenNum;
447
586
  }
448
587
  if (p !== n) {
588
+ dst.length = origLen;
449
589
  return [[], false];
450
590
  }
451
591
  return [dst, true];
@@ -535,7 +675,7 @@ export function appendEnumArrBody(dst, body) {
535
675
  if (vn === 0)
536
676
  return [[], false];
537
677
  p += vn;
538
- dst.push(decodeZigZag(v) | 0);
678
+ dst.push(decodeZigZag32(v));
539
679
  }
540
680
  if (p !== body.length) {
541
681
  return [[], false];
@@ -889,10 +1029,15 @@ export function readSubMsgMerged(packet, fieldNum, decodeFn) {
889
1029
  wt = Number(tag & 0x0fn);
890
1030
  pos += tn;
891
1031
  }
892
- if (fnum === 0 && wt !== WireSubMsg)
893
- return [null, false];
894
- if (fnum === fieldNum) {
895
- 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)
896
1041
  return [null, false];
897
1042
  let lenNum = 0;
898
1043
  const lb = packet[pos];
@@ -915,19 +1060,23 @@ export function readSubMsgMerged(packet, fieldNum, decodeFn) {
915
1060
  }
916
1061
  if (pos + lenNum > n)
917
1062
  return [null, false];
918
- out = useRange
919
- ? decodeFn(packet, out, found, pos, pos + lenNum)
920
- : 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
+ }
921
1071
  found = true;
922
1072
  pos += lenNum;
923
1073
  continue;
924
1074
  }
925
- try {
926
- pos = skipValue(packet, pos, wt);
927
- }
928
- catch {
1075
+ const nextPos = skipValidatedValue(packet, pos, wt);
1076
+ if (nextPos < 0) {
929
1077
  return [null, false];
930
1078
  }
1079
+ pos = nextPos;
931
1080
  }
932
1081
  return [out ?? null, found];
933
1082
  }
@@ -951,7 +1100,12 @@ export function readSubMsgArrDecoded(packet, fieldNum, decodeFn) {
951
1100
  const [el, en] = decodeVarintBigInt(body, p);
952
1101
  p += en;
953
1102
  const elNum = Number(el);
954
- 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
+ }
955
1109
  p += elNum;
956
1110
  }
957
1111
  }
@@ -997,7 +1151,7 @@ function readMapEntryPayload(body, p, wt, scalarKind, decodeSubMsg) {
997
1151
  throw new Error('omnibuf: truncated map sint');
998
1152
  const s = decodeZigZagAuto(v);
999
1153
  if (scalarKind === 'int32' || scalarKind === 'sint32' || scalarKind === 'enum') {
1000
- return [Number(s) | 0, p + vn];
1154
+ return [typeof s === 'bigint' ? Number(BigInt.asIntN(32, s)) : s | 0, p + vn];
1001
1155
  }
1002
1156
  return [s, p + vn];
1003
1157
  }
@@ -1059,7 +1213,47 @@ function readMapEntryPayload(body, p, wt, scalarKind, decodeSubMsg) {
1059
1213
  throw new Error(`omnibuf: unsupported map wire type ${wt}`);
1060
1214
  }
1061
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
+ }
1062
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
+ }
1063
1257
  const out = {};
1064
1258
  let found = false;
1065
1259
  let pos = 0;
@@ -1071,21 +1265,32 @@ export function readTypedMap(packet, fieldNum, keyType, valType, decodeSubMsg) {
1071
1265
  return [out, found];
1072
1266
  pos = nextPos;
1073
1267
  const [keyWT, valWT, c, startP, ok] = parseMapHeader(body);
1074
- if (!ok) {
1268
+ if (!ok || keyWT !== expKeyWT || valWT !== expValWT) {
1075
1269
  return [{}, false];
1076
1270
  }
1077
1271
  found = true;
1078
1272
  let p = startP;
1079
- for (let i = 0; i < c; i++) {
1080
- const [kVal, p1] = readMapEntryPayload(body, p, keyWT, keyType);
1081
- const [vVal, p2] = readMapEntryPayload(body, p1, valWT, valType, decodeSubMsg);
1082
- p = p2;
1083
- Object.defineProperty(out, String(kVal), {
1084
- value: vVal,
1085
- writable: true,
1086
- enumerable: true,
1087
- configurable: true,
1088
- });
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
+ const kStr = String(kVal);
1279
+ if (kStr === '__proto__') {
1280
+ Object.defineProperty(out, kStr, {
1281
+ value: vVal,
1282
+ writable: true,
1283
+ enumerable: true,
1284
+ configurable: true,
1285
+ });
1286
+ }
1287
+ else {
1288
+ out[kStr] = vVal;
1289
+ }
1290
+ }
1291
+ }
1292
+ catch {
1293
+ return [{}, false];
1089
1294
  }
1090
1295
  if (p !== body.length) {
1091
1296
  return [{}, false];
@@ -1118,7 +1323,13 @@ export function lookupMapStringUint(packet, fieldNum, targetKey) {
1118
1323
  }
1119
1324
  p += kn;
1120
1325
  const kLenNum = Number(kLen);
1121
- const k = decodeUTF8(body, p, p + kLenNum);
1326
+ let k;
1327
+ try {
1328
+ k = decodeUTF8(body, p, p + kLenNum);
1329
+ }
1330
+ catch {
1331
+ return [0, false];
1332
+ }
1122
1333
  p += kLenNum;
1123
1334
  const [v, vn] = decodeVarintAuto(body, p);
1124
1335
  if (vn === 0)
@@ -1175,8 +1386,13 @@ export function activeOneofSlice(packet, targetField, oneofSpecs) {
1175
1386
  wt = Number(tag & 0x0fn);
1176
1387
  pos += tn;
1177
1388
  }
1178
- if (fnum === 0 && wt !== WireSubMsg) {
1179
- return null;
1389
+ if (fnum === 0) {
1390
+ const f0End = validateField0At(packet, pos, wt, 0);
1391
+ if (f0End < 0) {
1392
+ return null;
1393
+ }
1394
+ pos = f0End;
1395
+ continue;
1180
1396
  }
1181
1397
  for (let i = 0; i < oneofSpecs.length; i++) {
1182
1398
  const spec = oneofSpecs[i];
@@ -1201,14 +1417,53 @@ export function activeOneofSlice(packet, targetField, oneofSpecs) {
1201
1417
  }
1202
1418
  const sharedSpliceEncoder = new Encoder(8192);
1203
1419
  let sharedSpliceInUse = false;
1420
+ function extractDirtySetFromBigInt(mask, maxField) {
1421
+ const dirtySet = new Set();
1422
+ let remMask = mask & ~1n;
1423
+ let bitBase = 0;
1424
+ while (remMask !== 0n && bitBase <= maxField) {
1425
+ if ((remMask & 0xffffffffffffffffn) === 0n) {
1426
+ let shift = 64;
1427
+ while ((remMask & ((1n << BigInt(shift)) - 1n)) === 0n && bitBase + shift <= maxField) {
1428
+ shift *= 2;
1429
+ }
1430
+ if (shift > 64)
1431
+ shift >>= 1;
1432
+ remMask >>= BigInt(shift);
1433
+ bitBase += shift;
1434
+ continue;
1435
+ }
1436
+ let wordLo = Number(remMask & 0xffffffffn) >>> 0;
1437
+ let wordHi = Number((remMask >> 32n) & 0xffffffffn) >>> 0;
1438
+ while (wordLo !== 0) {
1439
+ const tz = 31 - Math.clz32(wordLo & -wordLo);
1440
+ const fn = bitBase + tz;
1441
+ if (fn >= 1 && fn <= maxField)
1442
+ dirtySet.add(fn);
1443
+ wordLo &= wordLo - 1;
1444
+ }
1445
+ while (wordHi !== 0) {
1446
+ const tz = 31 - Math.clz32(wordHi & -wordHi);
1447
+ const fn = bitBase + 32 + tz;
1448
+ if (fn >= 1 && fn <= maxField)
1449
+ dirtySet.add(fn);
1450
+ wordHi &= wordHi - 1;
1451
+ }
1452
+ remMask >>= 64n;
1453
+ bitBase += 64;
1454
+ }
1455
+ return dirtySet;
1456
+ }
1204
1457
  /**
1205
1458
  * Splices unmodified contiguous byte ranges from `raw` and invokes `appendDirty`
1206
- * only for fields whose bit is set in `dirtyMask`, avoiding any decoding of untouched fields.
1459
+ * only for fields whose bit is set in `dirtyMask` (or present in a `ReadonlySet<number>`),
1460
+ * avoiding any decoding of untouched fields.
1207
1461
  */
1208
1462
  export function spliceFields(raw, dirtyMask, maxField, appendDirty, enc) {
1209
- if (maxField <= 30) {
1463
+ if (maxField <= 30 && (typeof dirtyMask === 'number' || typeof dirtyMask === 'bigint')) {
1210
1464
  const mask32 = typeof dirtyMask === 'bigint' ? Number(dirtyMask & 0x7fffffffn) : dirtyMask | 0;
1211
1465
  if (mask32 === 0) {
1466
+ validateMessage(raw, 0);
1212
1467
  if (enc !== undefined) {
1213
1468
  enc.detachIfOverlaps(raw, 0);
1214
1469
  enc.writeRaw(raw);
@@ -1258,10 +1513,17 @@ export function spliceFields(raw, dirtyMask, maxField, appendDirty, enc) {
1258
1513
  }
1259
1514
  const fnum = tag <= 0x7fffffff ? tag >>> 4 : Math.floor(tag / 16);
1260
1515
  const wt = tag & 0x0f;
1261
- if (fnum === 0 && wt !== WireSubMsg) {
1262
- throw new Error('omnibuf: invalid Field #0 wire type');
1516
+ if (fnum === 0) {
1517
+ if (wt !== WireSubMsg) {
1518
+ throw new Error('omnibuf: invalid Field #0 wire type');
1519
+ }
1520
+ const [f0Body, f0End] = readLenSlice(raw, pos, 0);
1521
+ decodeSchemaMeta(f0Body, 1);
1522
+ pos = f0End;
1523
+ }
1524
+ else {
1525
+ pos = validateValue(raw, pos, wt, 0);
1263
1526
  }
1264
- pos = skipValue(raw, pos, wt);
1265
1527
  const fieldEnd = pos;
1266
1528
  const bit = fnum >= 1 && fnum <= maxField ? 1 << fnum : 0;
1267
1529
  if ((mask32 & bit) !== 0) {
@@ -1296,8 +1558,11 @@ export function spliceFields(raw, dirtyMask, maxField, appendDirty, enc) {
1296
1558
  sharedSpliceInUse = false;
1297
1559
  }
1298
1560
  }
1299
- const mask = typeof dirtyMask === 'bigint' ? dirtyMask : BigInt(dirtyMask >>> 0);
1300
- if (mask === 0n) {
1561
+ const dirtySet = typeof dirtyMask === 'object' && dirtyMask !== null
1562
+ ? dirtyMask
1563
+ : extractDirtySetFromBigInt(typeof dirtyMask === 'bigint' ? dirtyMask : BigInt(dirtyMask >>> 0), maxField);
1564
+ if (dirtySet.size === 0) {
1565
+ validateMessage(raw, 0);
1301
1566
  if (enc !== undefined) {
1302
1567
  enc.detachIfOverlaps(raw, 0);
1303
1568
  enc.writeRaw(raw);
@@ -1315,45 +1580,71 @@ export function spliceFields(raw, dirtyMask, maxField, appendDirty, enc) {
1315
1580
  const n = raw.length;
1316
1581
  let pos = 0;
1317
1582
  let cleanStart = 0;
1318
- let emitted = 0n;
1583
+ const emittedSet = new Set();
1319
1584
  while (pos < n) {
1320
1585
  const fieldStart = pos;
1321
- const [tag, width] = decodeVarintBigInt(raw, pos);
1322
- if (!width || tag >> 4n > 536870911n)
1323
- throw new Error('omnibuf: invalid tag while splicing');
1324
- const fnum = Number(tag >> 4n);
1325
- const wt = Number(tag & 15n);
1326
- if (fnum === 0 && wt !== WireSubMsg) {
1327
- throw new Error('omnibuf: invalid Field #0 wire type');
1328
- }
1329
- pos = skipValue(raw, pos + width, wt);
1586
+ let tag = 0;
1587
+ const b0 = raw[pos];
1588
+ if ((b0 & 1) !== 0) {
1589
+ tag = b0 >>> 1;
1590
+ pos += 1;
1591
+ }
1592
+ else if ((b0 & 2) !== 0) {
1593
+ if (pos + 2 > n)
1594
+ throw new Error('omnibuf: invalid tag while splicing');
1595
+ tag = ((b0 | (raw[pos + 1] << 8)) >>> 2) >>> 0;
1596
+ pos += 2;
1597
+ }
1598
+ else {
1599
+ const [t64, width] = decodeVarintBigInt(raw, pos);
1600
+ if (!width || t64 >> 4n > 536870911n)
1601
+ throw new Error('omnibuf: invalid tag while splicing');
1602
+ tag = Number(t64);
1603
+ pos += width;
1604
+ }
1605
+ const fnum = tag <= 0x7fffffff ? tag >>> 4 : Math.floor(tag / 16);
1606
+ const wt = tag & 0x0f;
1607
+ if (fnum === 0) {
1608
+ if (wt !== WireSubMsg) {
1609
+ throw new Error('omnibuf: invalid Field #0 wire type');
1610
+ }
1611
+ const [f0Body, f0End] = readLenSlice(raw, pos, 0);
1612
+ decodeSchemaMeta(f0Body, 1);
1613
+ pos = f0End;
1614
+ }
1615
+ else {
1616
+ pos = validateValue(raw, pos, wt, 0);
1617
+ }
1330
1618
  const fieldEnd = pos;
1331
- const bit = fnum >= 1 && fnum <= maxField ? 1n << BigInt(fnum) : 0n;
1332
- if ((mask & bit) !== 0n) {
1619
+ if (fnum >= 1 && fnum <= maxField && dirtySet.has(fnum)) {
1333
1620
  if (cleanStart < fieldStart) {
1334
1621
  activeEnc.writeRaw(raw.subarray(cleanStart, fieldStart));
1335
1622
  }
1336
- if ((emitted & bit) === 0n) {
1623
+ if (!emittedSet.has(fnum)) {
1337
1624
  appendDirty(activeEnc, fnum);
1338
- emitted |= bit;
1625
+ emittedSet.add(fnum);
1339
1626
  }
1340
1627
  cleanStart = fieldEnd;
1341
1628
  }
1342
- else {
1343
- emitted |= bit;
1629
+ else if (fnum >= 1) {
1630
+ emittedSet.add(fnum);
1344
1631
  }
1345
1632
  }
1346
1633
  if (cleanStart < n) {
1347
1634
  activeEnc.writeRaw(raw.subarray(cleanStart, n));
1348
1635
  }
1349
- const rem = mask & ~emitted;
1350
- if (rem !== 0n) {
1351
- for (let fnum = 1; fnum <= maxField; fnum++) {
1352
- if ((rem & (1n << BigInt(fnum))) !== 0n) {
1353
- appendDirty(activeEnc, fnum);
1354
- }
1636
+ const remaining = [];
1637
+ for (const fn of dirtySet) {
1638
+ if (fn >= 1 && fn <= maxField && !emittedSet.has(fn)) {
1639
+ remaining.push(fn);
1355
1640
  }
1356
1641
  }
1642
+ if (remaining.length > 1) {
1643
+ remaining.sort((a, b) => a - b);
1644
+ }
1645
+ for (let i = 0; i < remaining.length; i++) {
1646
+ appendDirty(activeEnc, remaining[i]);
1647
+ }
1357
1648
  return activeEnc.finish();
1358
1649
  }
1359
1650
  //# sourceMappingURL=readers.js.map