@omnibuf/core 0.0.2 → 0.0.4

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/wire.d.ts CHANGED
@@ -3,26 +3,40 @@
3
3
  * Tag = (FieldNumber << 4) | WireType
4
4
  */
5
5
  export declare const WireUint: 0;
6
- export declare const WireBoolFalse: 1;
7
- export declare const WireBoolTrue: 2;
8
- export declare const WireString: 3;
9
- export declare const WireBytes: 4;
10
- export declare const WireSubMsg: 5;
11
- export declare const WireStringArr: 6;
12
- export declare const WireUintArr: 7;
13
- export declare const WireSubMsgArr: 8;
14
- export declare const WireMap: 9;
15
- export declare const WireFixed32: 10;
16
- export declare const WireFixed64: 11;
17
- export declare const WireSint: 12;
18
- export declare const WireSintArr: 13;
19
- export declare const WireFixedArr: 14;
6
+ export declare const WireSint: 1;
7
+ export declare const WireBoolFalse: 2;
8
+ export declare const WireBoolTrue: 3;
9
+ export declare const WireFixed32: 4;
10
+ export declare const WireFixed64: 5;
11
+ export declare const WireSfixed32: 6;
12
+ export declare const WireSfixed64: 7;
13
+ export declare const WireFloat32: 8;
14
+ export declare const WireFloat64: 9;
15
+ export declare const WireString: 10;
16
+ export declare const WireBytes: 11;
17
+ export declare const WireSubMsg: 12;
18
+ export declare const WireArray: 13;
19
+ export declare const WireMap: 14;
20
20
  export declare const WireExtended: 15;
21
- export type WireType = typeof WireUint | typeof WireBoolFalse | typeof WireBoolTrue | typeof WireString | typeof WireBytes | typeof WireSubMsg | typeof WireStringArr | typeof WireUintArr | typeof WireSubMsgArr | typeof WireMap | typeof WireFixed32 | typeof WireFixed64 | typeof WireSint | typeof WireSintArr | typeof WireFixedArr | typeof WireExtended;
21
+ export type WireType = typeof WireUint | typeof WireSint | typeof WireBoolFalse | typeof WireBoolTrue | typeof WireFixed32 | typeof WireFixed64 | typeof WireSfixed32 | typeof WireSfixed64 | typeof WireFloat32 | typeof WireFloat64 | typeof WireString | typeof WireBytes | typeof WireSubMsg | typeof WireArray | typeof WireMap | typeof WireExtended;
22
22
  /**
23
23
  * Maximum allowed sub-message / array nesting depth (prevents stack-overflow bombs).
24
24
  */
25
25
  export declare const MaxNestingDepth = 32;
26
+ /**
27
+ * Packs a 4-bit key wire type and 4-bit value wire type into a byte (legacy helper).
28
+ */
29
+ export declare function mapHeaderByte(keyType: number, valType: number): number;
30
+ /**
31
+ * Parses and validates the `[ElementType (1B)][Count (Prefix-Varint)]` header at the start of a
32
+ * `WireArray` body. Returns `[elemType, count, pos, ok]`.
33
+ */
34
+ export declare function parseArrayHeader(body: Uint8Array, start?: number, end?: number): [elemType: number, count: number, pos: number, ok: boolean];
35
+ /**
36
+ * Parses and validates the `[KeyType (1B)][ValueType (1B)][Count (Prefix-Varint)]` header
37
+ * at the start of a `WireMap` body. Returns `[keyWT, valWT, count, pos, ok]`.
38
+ */
39
+ export declare function parseMapHeader(body: Uint8Array, start?: number, end?: number): [keyWT: number, valWT: number, count: number, pos: number, ok: boolean];
26
40
  export interface StringCodec {
27
41
  encode(str: string): Uint8Array;
28
42
  decode(bytes: Uint8Array, start?: number, end?: number): string;
@@ -79,17 +93,17 @@ export declare function appendVarint(dst: Uint8Array, v: number | bigint): Uint8
79
93
  * Decodes a Prefix-Varint from `b` starting at `pos` (default 0).
80
94
  * Returns `[value, bytesRead]`. If truncated, returns `[0, 0]`.
81
95
  */
82
- export declare function decodeVarint(b: Uint8Array, pos?: number): [value: number, bytesRead: number];
96
+ export declare function decodeVarint(b: Uint8Array, pos?: number, end?: number): [value: number, bytesRead: number];
83
97
  /**
84
98
  * Decodes a Prefix-Varint from `b` starting at `pos` as an exact 64-bit unsigned `bigint`.
85
99
  * Returns `[value, bytesRead]`. If truncated, returns `[0n, 0]`.
86
100
  */
87
- export declare function decodeVarintBigInt(b: Uint8Array, pos?: number): [value: bigint, bytesRead: number];
101
+ export declare function decodeVarintBigInt(b: Uint8Array, pos?: number, end?: number): [value: bigint, bytesRead: number];
88
102
  /**
89
103
  * Decodes a Prefix-Varint returning a JS `number` when within safe integer bounds (`<= 2^53 - 1`),
90
104
  * or a `bigint` when larger. Returns `[value, bytesRead]`.
91
105
  */
92
- export declare function decodeVarintAuto(b: Uint8Array, pos?: number): [value: number | bigint, bytesRead: number];
106
+ export declare function decodeVarintAuto(b: Uint8Array, pos?: number, end?: number): [value: number | bigint, bytesRead: number];
93
107
  /**
94
108
  * Decodes a Prefix-Varint at `b[pos]` and returns `[value, nextPos]`.
95
109
  */
@@ -110,10 +124,6 @@ export declare function backpatchContainerLen(dst: Uint8Array, lenPos: number, e
110
124
  * Returns the next position, or `-1` if truncated or invalid.
111
125
  */
112
126
  export declare function fastSkipValue(packet: Uint8Array, pos: number, wt: number): number;
113
- /**
114
- * Advances `pos` past a single field payload of the given 4-bit `wireType` (0..15),
115
- * performing full bounds and structural alignment validation in O(1) time.
116
- */
117
127
  export declare function skipValue(b: Uint8Array, pos: number, wireType: number): number;
118
128
  export interface LocateFieldResult {
119
129
  valSlice: Uint8Array;
@@ -142,7 +152,7 @@ export declare function nextContainerField(packet: Uint8Array, pos: number, targ
142
152
  * Reads a length-prefixed slice `[ByteLen (Prefix-Varint)][Payload]` at `data[pos]`,
143
153
  * enforcing `byteLen >= minLen` and bounds within `data`.
144
154
  */
145
- export declare function readLenSlice(data: Uint8Array, pos: number, minLen?: number): [body: Uint8Array, nextPos: number];
155
+ export declare function readLenSlice(data: Uint8Array, pos: number, minLen?: number, end?: number): [body: Uint8Array, nextPos: number];
146
156
  export declare function float32Bits(v: number): number;
147
157
  export declare function float32FromBits(bits: number): number;
148
158
  export declare function float64Bits(v: number): bigint;
@@ -1 +1 @@
1
- {"version":3,"file":"wire.d.ts","sourceRoot":"","sources":["../src/wire.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,eAAO,MAAM,QAAQ,EAAG,CAAU,CAAC;AACnC,eAAO,MAAM,aAAa,EAAG,CAAU,CAAC;AACxC,eAAO,MAAM,YAAY,EAAG,CAAU,CAAC;AACvC,eAAO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MAAM,EACX,MAAM,SAAI,EACV,GAAG,SAAc,GAChB,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,CAAC,CAoCrC;AASD,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAG7C;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGpD;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAG7C;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGpD;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAKlE;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAQlE;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CA8BxD"}
package/dist/wire.js CHANGED
@@ -5,26 +5,147 @@
5
5
  * Explicit 4-bit Wire Types (0..15) encoded in the low 4 bits of the field tag:
6
6
  * Tag = (FieldNumber << 4) | WireType
7
7
  */
8
- export const WireUint = 0; // [Prefix-Varint] (unsigned int: uint32, uint64, enum)
9
- export const WireBoolFalse = 1; // 0 value bytes (false encoded directly in tag)
10
- export const WireBoolTrue = 2; // 0 value bytes (true encoded directly in tag)
11
- export const WireString = 3; // [ByteLen (Prefix-Varint)][UTF-8 Bytes]
12
- export const WireBytes = 4; // [ByteLen (Prefix-Varint)][Raw Bytes]
13
- export const WireSubMsg = 5; // [ByteLen (Prefix-Varint)][Sub-Packet Bytes]
14
- export const WireStringArr = 6; // [ByteLen (Prefix-Varint)][Count (Prefix-Varint)][Len + UTF-8 Bytes]...
15
- export const WireUintArr = 7; // [ByteLen (Prefix-Varint)][Count (Prefix-Varint)][Prefix-Varint]...
16
- export const WireSubMsgArr = 8; // [ByteLen (Prefix-Varint)][ElemLen (Prefix-Varint) + Elem Bytes]...
17
- export const WireMap = 9; // [ByteLen (Prefix-Varint)][MapType (1B: KeyWT<<4|ValWT)][Count (Prefix-Varint)][Key + Value]...
18
- export const WireFixed32 = 10; // [4 Bytes Little-Endian] (float32 / fixed32 / sfixed32)
19
- export const WireFixed64 = 11; // [8 Bytes Little-Endian] (float64 / fixed64 / sfixed64)
20
- export const WireSint = 12; // [Prefix-Varint] (ZigZag signed int: int32, int64, sint32, sint64)
21
- export const WireSintArr = 13; // [ByteLen (Prefix-Varint)][Count (Prefix-Varint)][ZigZag Prefix-Varint]...
22
- export const WireFixedArr = 14; // [ByteLen (Prefix-Varint)][ElemType (1B: WireFixed32=10 or WireFixed64=11)][Fixed Elements...]
23
- export const WireExtended = 15; // [ByteLen (Prefix-Varint)][ExtType (1B: 0..255)][Extended Payload...] (Reserved escape hatch)
8
+ export const WireUint = 0; // [Prefix-Varint] (unsigned int: uint32, uint64)
9
+ export const WireSint = 1; // [Prefix-Varint] (ZigZag signed int: int32, int64, sint32, sint64, enum)
10
+ export const WireBoolFalse = 2; // 0 value bytes in field tags; in WireArray/WireMap headers, bool element/key/value type (1 raw byte 0x00 or 0x01)
11
+ export const WireBoolTrue = 3; // 0 value bytes in field tags (true encoded directly in tag); invalid in WireArray/WireMap headers
12
+ export const WireFixed32 = 4; // [4 Bytes Little-Endian] (uint32 / fixed32)
13
+ export const WireFixed64 = 5; // [8 Bytes Little-Endian] (uint64 / fixed64)
14
+ export const WireSfixed32 = 6; // [4 Bytes Little-Endian] (int32 / sfixed32)
15
+ export const WireSfixed64 = 7; // [8 Bytes Little-Endian] (int64 / sfixed64)
16
+ export const WireFloat32 = 8; // [4 Bytes Little-Endian] (IEEE-754 float32 / float)
17
+ export const WireFloat64 = 9; // [8 Bytes Little-Endian] (IEEE-754 float64 / double)
18
+ export const WireString = 10; // [ByteLen (Prefix-Varint)][UTF-8 Bytes]
19
+ export const WireBytes = 11; // [ByteLen (Prefix-Varint)][Raw Bytes]
20
+ export const WireSubMsg = 12; // [ByteLen (Prefix-Varint)][Sub-Packet Bytes]
21
+ export const WireArray = 13; // [ByteLen (Prefix-Varint)][ElementType (1B)][Count (Prefix-Varint)][Elements...]
22
+ export const WireMap = 14; // [ByteLen (Prefix-Varint)][KeyType (1B)][ValueType (1B)][Count (Prefix-Varint)][Key + Value]...
23
+ export const WireExtended = 15; // [ByteLen (Prefix-Varint)][ExtType (1B: 0..255)][Extended Payload...]
24
24
  /**
25
25
  * Maximum allowed sub-message / array nesting depth (prevents stack-overflow bombs).
26
26
  */
27
27
  export const MaxNestingDepth = 32;
28
+ /**
29
+ * Packs a 4-bit key wire type and 4-bit value wire type into a byte (legacy helper).
30
+ */
31
+ export function mapHeaderByte(keyType, valType) {
32
+ return ((keyType & 0x0f) << 4) | (valType & 0x0f);
33
+ }
34
+ /**
35
+ * Parses and validates the `[ElementType (1B)][Count (Prefix-Varint)]` header at the start of a
36
+ * `WireArray` body. Returns `[elemType, count, pos, ok]`.
37
+ */
38
+ export function parseArrayHeader(body, start = 0, end = body.length) {
39
+ if (end - start < 2) {
40
+ return [0, 0, 0, false];
41
+ }
42
+ const elemType = body[start];
43
+ if (elemType > WireExtended || elemType === WireBoolTrue) {
44
+ return [0, 0, 0, false];
45
+ }
46
+ let countNum = 0;
47
+ let cn = 0;
48
+ const cb = body[start + 1];
49
+ if ((cb & 1) !== 0) {
50
+ countNum = cb >>> 1;
51
+ cn = 1;
52
+ }
53
+ else if ((cb & 2) !== 0) {
54
+ if (start + 3 > end) {
55
+ return [0, 0, 0, false];
56
+ }
57
+ countNum = (cb | (body[start + 2] << 8)) >>> 2;
58
+ cn = 2;
59
+ }
60
+ else {
61
+ const [count64, cBytes] = decodeVarintBigInt(body, start + 1);
62
+ if (cBytes === 0 || count64 > BigInt(end - (start + 1 + cBytes))) {
63
+ return [0, 0, 0, false];
64
+ }
65
+ countNum = Number(count64);
66
+ cn = cBytes;
67
+ }
68
+ const pos = start + 1 + cn;
69
+ if (pos > end) {
70
+ return [0, 0, 0, false];
71
+ }
72
+ const remaining = end - pos;
73
+ let width = 0;
74
+ switch (elemType) {
75
+ case WireBoolFalse:
76
+ width = 1;
77
+ break;
78
+ case WireFixed32:
79
+ case WireFloat32:
80
+ case WireSfixed32:
81
+ width = 4;
82
+ break;
83
+ case WireFixed64:
84
+ case WireFloat64:
85
+ case WireSfixed64:
86
+ width = 8;
87
+ break;
88
+ }
89
+ if (width > 0) {
90
+ if (remaining % width !== 0 || countNum !== remaining / width) {
91
+ return [0, 0, 0, false];
92
+ }
93
+ }
94
+ else if (countNum > remaining) {
95
+ return [0, 0, 0, false];
96
+ }
97
+ return [elemType, countNum, pos, true];
98
+ }
99
+ /**
100
+ * Parses and validates the `[KeyType (1B)][ValueType (1B)][Count (Prefix-Varint)]` header
101
+ * at the start of a `WireMap` body. Returns `[keyWT, valWT, count, pos, ok]`.
102
+ */
103
+ export function parseMapHeader(body, start = 0, end = body.length) {
104
+ if (end - start < 3) {
105
+ return [0, 0, 0, 0, false];
106
+ }
107
+ const keyWT = body[start];
108
+ const valWT = body[start + 1];
109
+ const validKey = keyWT === WireUint ||
110
+ keyWT === WireSint ||
111
+ keyWT === WireBoolFalse ||
112
+ keyWT === WireFixed32 ||
113
+ keyWT === WireFixed64 ||
114
+ keyWT === WireSfixed32 ||
115
+ keyWT === WireSfixed64 ||
116
+ keyWT === WireString;
117
+ const validVal = valWT <= WireExtended && valWT !== WireBoolTrue;
118
+ if (!validKey || !validVal) {
119
+ return [0, 0, 0, 0, false];
120
+ }
121
+ let countNum = 0;
122
+ let cn = 0;
123
+ const cb = body[start + 2];
124
+ if ((cb & 1) !== 0) {
125
+ countNum = cb >>> 1;
126
+ cn = 1;
127
+ }
128
+ else if ((cb & 2) !== 0) {
129
+ if (start + 4 > end) {
130
+ return [0, 0, 0, 0, false];
131
+ }
132
+ countNum = (cb | (body[start + 3] << 8)) >>> 2;
133
+ cn = 2;
134
+ }
135
+ else {
136
+ const [count64, cBytes] = decodeVarintBigInt(body, start + 2);
137
+ if (cBytes === 0 || count64 > BigInt(Math.floor((end - (start + 2 + cBytes)) / 2))) {
138
+ return [0, 0, 0, 0, false];
139
+ }
140
+ countNum = Number(count64);
141
+ cn = cBytes;
142
+ }
143
+ const pos = start + 2 + cn;
144
+ if (pos > end || countNum > (end - pos) >>> 1) {
145
+ return [0, 0, 0, 0, false];
146
+ }
147
+ return [keyWT, valWT, countNum, pos, true];
148
+ }
28
149
  const sharedTextEncoder = new TextEncoder();
29
150
  const sharedTextDecoder = new TextDecoder('utf-8', { fatal: false });
30
151
  /**
@@ -376,8 +497,8 @@ export function appendVarint(dst, v) {
376
497
  * Decodes a Prefix-Varint from `b` starting at `pos` (default 0).
377
498
  * Returns `[value, bytesRead]`. If truncated, returns `[0, 0]`.
378
499
  */
379
- export function decodeVarint(b, pos = 0) {
380
- const rem = b.length - pos;
500
+ export function decodeVarint(b, pos = 0, end = b.length) {
501
+ const rem = end - pos;
381
502
  if (rem <= 0)
382
503
  return [0, 0];
383
504
  const b0 = b[pos];
@@ -396,7 +517,7 @@ export function decodeVarint(b, pos = 0) {
396
517
  const u32 = (b0 | (b[pos + 1] << 8) | (b[pos + 2] << 16) | (b[pos + 3] << 24)) >>> 0;
397
518
  return [u32 >>> 3, 4];
398
519
  }
399
- if (rem < 9)
520
+ if (b0 !== 0 || rem < 9)
400
521
  return [0, 0];
401
522
  const lo = (b[pos + 1] | (b[pos + 2] << 8) | (b[pos + 3] << 16) | (b[pos + 4] << 24)) >>> 0;
402
523
  const hi = (b[pos + 5] | (b[pos + 6] << 8) | (b[pos + 7] << 16) | (b[pos + 8] << 24)) >>> 0;
@@ -406,8 +527,8 @@ export function decodeVarint(b, pos = 0) {
406
527
  * Decodes a Prefix-Varint from `b` starting at `pos` as an exact 64-bit unsigned `bigint`.
407
528
  * Returns `[value, bytesRead]`. If truncated, returns `[0n, 0]`.
408
529
  */
409
- export function decodeVarintBigInt(b, pos = 0) {
410
- const rem = b.length - pos;
530
+ export function decodeVarintBigInt(b, pos = 0, end = b.length) {
531
+ const rem = end - pos;
411
532
  if (rem <= 0)
412
533
  return [0n, 0];
413
534
  const b0 = b[pos];
@@ -426,7 +547,7 @@ export function decodeVarintBigInt(b, pos = 0) {
426
547
  const u32 = (b0 | (b[pos + 1] << 8) | (b[pos + 2] << 16) | (b[pos + 3] << 24)) >>> 0;
427
548
  return [BigInt(u32 >>> 3), 4];
428
549
  }
429
- if (rem < 9)
550
+ if (b0 !== 0 || rem < 9)
430
551
  return [0n, 0];
431
552
  let u64 = 0n;
432
553
  for (let i = 8; i >= 1; i--) {
@@ -438,8 +559,8 @@ export function decodeVarintBigInt(b, pos = 0) {
438
559
  * Decodes a Prefix-Varint returning a JS `number` when within safe integer bounds (`<= 2^53 - 1`),
439
560
  * or a `bigint` when larger. Returns `[value, bytesRead]`.
440
561
  */
441
- export function decodeVarintAuto(b, pos = 0) {
442
- const rem = b.length - pos;
562
+ export function decodeVarintAuto(b, pos = 0, end = b.length) {
563
+ const rem = end - pos;
443
564
  if (rem <= 0)
444
565
  return [0, 0];
445
566
  const b0 = b[pos];
@@ -457,7 +578,7 @@ export function decodeVarintAuto(b, pos = 0) {
457
578
  const u32 = (b0 | (b[pos + 1] << 8) | (b[pos + 2] << 16) | (b[pos + 3] << 24)) >>> 0;
458
579
  return [u32 >>> 3, 4];
459
580
  }
460
- if (rem < 9)
581
+ if (b0 !== 0 || rem < 9)
461
582
  return [0, 0];
462
583
  const lo = (b[pos + 1] | (b[pos + 2] << 8) | (b[pos + 3] << 16) | (b[pos + 4] << 24)) >>> 0;
463
584
  const hi = (b[pos + 5] | (b[pos + 6] << 8) | (b[pos + 7] << 16) | (b[pos + 8] << 24)) >>> 0;
@@ -536,9 +657,12 @@ export function fastSkipValue(packet, pos, wt) {
536
657
  else if ((vb & 4) !== 0) {
537
658
  pos += 4;
538
659
  }
539
- else {
660
+ else if (vb === 0) {
540
661
  pos += 9;
541
662
  }
663
+ else {
664
+ return -1;
665
+ }
542
666
  return pos <= n ? pos : -1;
543
667
  }
544
668
  case WireBoolFalse:
@@ -547,29 +671,32 @@ export function fastSkipValue(packet, pos, wt) {
547
671
  case WireString:
548
672
  case WireBytes:
549
673
  case WireSubMsg:
550
- case WireStringArr:
551
- case WireUintArr:
552
- case WireSubMsgArr:
674
+ case WireArray:
553
675
  case WireMap:
554
- case WireSintArr:
555
- case WireFixedArr:
556
676
  case WireExtended: {
557
677
  if (pos >= n)
558
678
  return -1;
679
+ const minLen = wt === WireMap ? 3 : wt === WireArray ? 2 : wt === WireExtended ? 1 : 0;
559
680
  const lb = packet[pos];
560
681
  if ((lb & 1) !== 0) {
561
- pos += 1 + (lb >>> 1);
682
+ const lenNum = lb >>> 1;
683
+ if (lenNum < minLen)
684
+ return -1;
685
+ pos += 1 + lenNum;
562
686
  return pos <= n ? pos : -1;
563
687
  }
564
688
  if ((lb & 2) !== 0) {
565
689
  if (pos + 2 > n)
566
690
  return -1;
567
691
  const u16 = lb | (packet[pos + 1] << 8);
568
- pos += 2 + (u16 >>> 2);
692
+ const lenNum = u16 >>> 2;
693
+ if (lenNum < minLen)
694
+ return -1;
695
+ pos += 2 + lenNum;
569
696
  return pos <= n ? pos : -1;
570
697
  }
571
698
  const [bLen, bn] = decodeVarintBigInt(packet, pos);
572
- if (bn === 0)
699
+ if (bn === 0 || bLen < BigInt(minLen))
573
700
  return -1;
574
701
  const rem = BigInt(n - (pos + bn));
575
702
  if (bLen > rem)
@@ -578,9 +705,13 @@ export function fastSkipValue(packet, pos, wt) {
578
705
  return pos <= n ? pos : -1;
579
706
  }
580
707
  case WireFixed32:
708
+ case WireSfixed32:
709
+ case WireFloat32:
581
710
  pos += 4;
582
711
  return pos <= n ? pos : -1;
583
712
  case WireFixed64:
713
+ case WireSfixed64:
714
+ case WireFloat64:
584
715
  pos += 8;
585
716
  return pos <= n ? pos : -1;
586
717
  default:
@@ -591,35 +722,136 @@ export function fastSkipValue(packet, pos, wt) {
591
722
  * Advances `pos` past a single field payload of the given 4-bit `wireType` (0..15),
592
723
  * performing full bounds and structural alignment validation in O(1) time.
593
724
  */
725
+ function isValidArrayHeaderAt(body, start, end) {
726
+ if (end - start < 2)
727
+ return false;
728
+ const elemType = body[start];
729
+ if (elemType > WireExtended || elemType === WireBoolTrue)
730
+ return false;
731
+ let countNum = 0;
732
+ let cn = 0;
733
+ const cb = body[start + 1];
734
+ if ((cb & 1) !== 0) {
735
+ countNum = cb >>> 1;
736
+ cn = 1;
737
+ }
738
+ else if ((cb & 2) !== 0) {
739
+ if (start + 3 > end)
740
+ return false;
741
+ countNum = (cb | (body[start + 2] << 8)) >>> 2;
742
+ cn = 2;
743
+ }
744
+ else {
745
+ const [count64, cBytes] = decodeVarintBigInt(body, start + 1, end);
746
+ if (cBytes === 0 || count64 > BigInt(end - (start + 1 + cBytes)))
747
+ return false;
748
+ countNum = Number(count64);
749
+ cn = cBytes;
750
+ }
751
+ const pos = start + 1 + cn;
752
+ if (pos > end)
753
+ return false;
754
+ const remaining = end - pos;
755
+ switch (elemType) {
756
+ case WireBoolFalse:
757
+ return countNum === remaining;
758
+ case WireFixed32:
759
+ case WireFloat32:
760
+ case WireSfixed32:
761
+ return (remaining & 3) === 0 && countNum === remaining >>> 2;
762
+ case WireFixed64:
763
+ case WireFloat64:
764
+ case WireSfixed64:
765
+ return (remaining & 7) === 0 && countNum === remaining >>> 3;
766
+ default:
767
+ return countNum <= remaining;
768
+ }
769
+ }
770
+ function isValidMapHeaderAt(body, start, end) {
771
+ if (end - start < 3)
772
+ return false;
773
+ const keyWT = body[start];
774
+ const valWT = body[start + 1];
775
+ const validKey = keyWT === WireUint ||
776
+ keyWT === WireSint ||
777
+ keyWT === WireBoolFalse ||
778
+ keyWT === WireFixed32 ||
779
+ keyWT === WireFixed64 ||
780
+ keyWT === WireSfixed32 ||
781
+ keyWT === WireSfixed64 ||
782
+ keyWT === WireString;
783
+ const validVal = valWT <= WireExtended && valWT !== WireBoolTrue;
784
+ if (!validKey || !validVal)
785
+ return false;
786
+ let countNum = 0;
787
+ let cn = 0;
788
+ const cb = body[start + 2];
789
+ if ((cb & 1) !== 0) {
790
+ countNum = cb >>> 1;
791
+ cn = 1;
792
+ }
793
+ else if ((cb & 2) !== 0) {
794
+ if (start + 4 > end)
795
+ return false;
796
+ countNum = (cb | (body[start + 3] << 8)) >>> 2;
797
+ cn = 2;
798
+ }
799
+ else {
800
+ const [count64, cBytes] = decodeVarintBigInt(body, start + 2, end);
801
+ if (cBytes === 0 || count64 > BigInt(Math.floor((end - (start + 2 + cBytes)) / 2)))
802
+ return false;
803
+ countNum = Number(count64);
804
+ cn = cBytes;
805
+ }
806
+ const pos = start + 2 + cn;
807
+ return pos <= end && countNum <= (end - pos) >>> 1;
808
+ }
594
809
  export function skipValue(b, pos, wireType) {
595
- if (pos < 0 || pos > b.length) {
810
+ const n = b.length;
811
+ if (pos < 0 || pos > n) {
596
812
  throw new Error(`omnibuf: position ${pos} out of bounds`);
597
813
  }
598
- if (wireType === WireFixedArr || wireType === WireExtended) {
599
- const [bLen, bn] = decodeVarintBigInt(b, pos);
600
- const rem = BigInt(b.length - (pos + bn));
601
- if (bn === 0 || bLen < 1n || bLen > rem) {
602
- throw new Error(`omnibuf: invalid container length at ${pos}`);
603
- }
604
- const lenNum = Number(bLen);
605
- pos += bn;
606
- if (wireType === WireFixedArr) {
607
- const elemType = b[pos];
608
- if (elemType === WireFixed32) {
609
- if ((lenNum - 1) % 4 !== 0) {
610
- throw new Error(`omnibuf: WireFixedArr(Fixed32) payload ${lenNum - 1} is not a multiple of 4 at ${pos}`);
611
- }
814
+ if (wireType === WireArray || wireType === WireMap || wireType === WireExtended) {
815
+ if (pos >= n) {
816
+ throw new Error('omnibuf: invalid container length');
817
+ }
818
+ let lenNum = 0;
819
+ let ln = 0;
820
+ const lb = b[pos];
821
+ if ((lb & 1) !== 0) {
822
+ lenNum = lb >>> 1;
823
+ ln = 1;
824
+ }
825
+ else if ((lb & 2) !== 0) {
826
+ if (pos + 2 > n) {
827
+ throw new Error('omnibuf: invalid container length');
612
828
  }
613
- else if (elemType === WireFixed64) {
614
- if ((lenNum - 1) % 8 !== 0) {
615
- throw new Error(`omnibuf: WireFixedArr(Fixed64) payload ${lenNum - 1} is not a multiple of 8 at ${pos}`);
616
- }
829
+ lenNum = (lb | (b[pos + 1] << 8)) >>> 2;
830
+ ln = 2;
831
+ }
832
+ else {
833
+ const [bLen, lBytes] = decodeVarintBigInt(b, pos);
834
+ if (lBytes === 0 || bLen < 1n || bLen > BigInt(n - pos - lBytes)) {
835
+ throw new Error('omnibuf: invalid container length');
617
836
  }
618
- else {
619
- throw new Error(`omnibuf: invalid WireFixedArr ElemType ${elemType} at ${pos}`);
837
+ lenNum = Number(bLen);
838
+ ln = lBytes;
839
+ }
840
+ if (lenNum < 1 || pos + ln + lenNum > n) {
841
+ throw new Error('omnibuf: invalid container length');
842
+ }
843
+ const end = pos + ln + lenNum;
844
+ if (wireType === WireArray) {
845
+ if (!isValidArrayHeaderAt(b, pos + ln, end)) {
846
+ throw new Error('omnibuf: invalid array header');
847
+ }
848
+ }
849
+ else if (wireType === WireMap) {
850
+ if (!isValidMapHeaderAt(b, pos + ln, end)) {
851
+ throw new Error('omnibuf: invalid map header');
620
852
  }
621
853
  }
622
- return pos + lenNum;
854
+ return end;
623
855
  }
624
856
  const nextPos = fastSkipValue(b, pos, wireType);
625
857
  if (nextPos < 0) {
@@ -662,7 +894,7 @@ export function locateField(packet, targetField) {
662
894
  }
663
895
  else {
664
896
  const [tag, tn] = decodeVarintBigInt(packet, pos);
665
- if (tn === 0 || tag >> 4n > 0xffffffffn) {
897
+ if (tn === 0 || tag >> 4n > 536870911n) {
666
898
  return NOT_FOUND;
667
899
  }
668
900
  fnum = Number(tag >> 4n);
@@ -715,7 +947,7 @@ export function nextContainerField(packet, pos, targetField, expWT, minLen) {
715
947
  }
716
948
  else {
717
949
  const [tag, tn] = decodeVarintBigInt(packet, pos);
718
- if (tn === 0 || tag >> 4n > 0xffffffffn) {
950
+ if (tn === 0 || tag >> 4n > 536870911n) {
719
951
  return { body: new Uint8Array(0), nextPos: 0, matched: false, valid: false };
720
952
  }
721
953
  fnum = Number(tag >> 4n);
@@ -723,15 +955,35 @@ export function nextContainerField(packet, pos, targetField, expWT, minLen) {
723
955
  pos += tn;
724
956
  }
725
957
  if (fnum === targetField) {
726
- if (wt !== expWT) {
958
+ if (wt !== expWT || pos >= n) {
727
959
  return { body: new Uint8Array(0), nextPos: 0, matched: false, valid: false };
728
960
  }
729
- const [bLen, bn] = decodeVarintBigInt(packet, pos);
730
- const rem = BigInt(n - (pos + bn));
731
- if (bn === 0 || bLen < BigInt(minLen) || bLen > rem) {
961
+ let lenNum = 0;
962
+ let bn = 0;
963
+ const lb = packet[pos];
964
+ if ((lb & 1) !== 0) {
965
+ lenNum = lb >>> 1;
966
+ bn = 1;
967
+ }
968
+ else if ((lb & 2) !== 0) {
969
+ if (pos + 2 > n) {
970
+ return { body: new Uint8Array(0), nextPos: 0, matched: false, valid: false };
971
+ }
972
+ lenNum = (lb | (packet[pos + 1] << 8)) >>> 2;
973
+ bn = 2;
974
+ }
975
+ else {
976
+ const [bLen, bBytes] = decodeVarintBigInt(packet, pos);
977
+ const rem = BigInt(n - (pos + bBytes));
978
+ if (bBytes === 0 || bLen < BigInt(minLen) || bLen > rem) {
979
+ return { body: new Uint8Array(0), nextPos: 0, matched: false, valid: false };
980
+ }
981
+ lenNum = Number(bLen);
982
+ bn = bBytes;
983
+ }
984
+ if (lenNum < minLen || pos + bn + lenNum > n) {
732
985
  return { body: new Uint8Array(0), nextPos: 0, matched: false, valid: false };
733
986
  }
734
- const lenNum = Number(bLen);
735
987
  pos += bn;
736
988
  return {
737
989
  body: packet.subarray(pos, pos + lenNum),
@@ -753,8 +1005,8 @@ export function nextContainerField(packet, pos, targetField, expWT, minLen) {
753
1005
  * Reads a length-prefixed slice `[ByteLen (Prefix-Varint)][Payload]` at `data[pos]`,
754
1006
  * enforcing `byteLen >= minLen` and bounds within `data`.
755
1007
  */
756
- export function readLenSlice(data, pos, minLen = 0) {
757
- const n = data.length;
1008
+ export function readLenSlice(data, pos, minLen = 0, end = data.length) {
1009
+ const n = end;
758
1010
  if (pos >= n) {
759
1011
  throw new Error(`omnibuf: truncated length varint at ${pos}`);
760
1012
  }
@@ -778,7 +1030,7 @@ export function readLenSlice(data, pos, minLen = 0) {
778
1030
  }
779
1031
  return [data.subarray(pos, pos + lenNum), pos + lenNum];
780
1032
  }
781
- const [bLen, bn] = decodeVarintBigInt(data, pos);
1033
+ const [bLen, bn] = decodeVarintBigInt(data, pos, n);
782
1034
  if (bn === 0) {
783
1035
  throw new Error(`omnibuf: truncated length varint at ${pos}`);
784
1036
  }