@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/wire.js CHANGED
@@ -58,7 +58,7 @@ export function parseArrayHeader(body, start = 0, end = body.length) {
58
58
  cn = 2;
59
59
  }
60
60
  else {
61
- const [count64, cBytes] = decodeVarintBigInt(body, start + 1);
61
+ const [count64, cBytes] = decodeVarintBigInt(body, start + 1, end);
62
62
  if (cBytes === 0 || count64 > BigInt(end - (start + 1 + cBytes))) {
63
63
  return [0, 0, 0, false];
64
64
  }
@@ -91,7 +91,7 @@ export function parseArrayHeader(body, start = 0, end = body.length) {
91
91
  return [0, 0, 0, false];
92
92
  }
93
93
  }
94
- else if (countNum > remaining) {
94
+ else if (countNum === 0 ? remaining !== 0 : countNum > remaining) {
95
95
  return [0, 0, 0, false];
96
96
  }
97
97
  return [elemType, countNum, pos, true];
@@ -133,7 +133,7 @@ export function parseMapHeader(body, start = 0, end = body.length) {
133
133
  cn = 2;
134
134
  }
135
135
  else {
136
- const [count64, cBytes] = decodeVarintBigInt(body, start + 2);
136
+ const [count64, cBytes] = decodeVarintBigInt(body, start + 2, end);
137
137
  if (cBytes === 0 || count64 > BigInt(Math.floor((end - (start + 2 + cBytes)) / 2))) {
138
138
  return [0, 0, 0, 0, false];
139
139
  }
@@ -141,7 +141,7 @@ export function parseMapHeader(body, start = 0, end = body.length) {
141
141
  cn = cBytes;
142
142
  }
143
143
  const pos = start + 2 + cn;
144
- if (pos > end || countNum > (end - pos) >>> 1) {
144
+ if (pos > end || (countNum === 0 ? end !== pos : countNum > (end - pos) >>> 1)) {
145
145
  return [0, 0, 0, 0, false];
146
146
  }
147
147
  return [keyWT, valWT, countNum, pos, true];
@@ -153,6 +153,10 @@ const sharedTextDecoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: tru
153
153
  * Returns `null` if any byte is non-ASCII (`>= 0x80`) or if `end - start > 64`.
154
154
  */
155
155
  export function fastDecodeShortASCII(bytes, start, end) {
156
+ if (start < 0)
157
+ start = 0;
158
+ if (end > bytes.length)
159
+ end = bytes.length;
156
160
  const len = end - start;
157
161
  if (len <= 0)
158
162
  return '';
@@ -364,15 +368,27 @@ export function decodeZigZagBigInt(u) {
364
368
  return BigInt.asIntN(64, (b >> 1n) ^ -(b & 1n));
365
369
  }
366
370
  /**
367
- * Maps a ZigZag-encoded unsigned integer back to a signed JS number (when safe)
368
- * or bigint (when outside Number.MAX_SAFE_INTEGER).
371
+ * Maps a ZigZag-encoded unsigned integer back to a signed JS number.
369
372
  */
370
373
  export function decodeZigZag(u) {
371
374
  if (typeof u === 'number' && u <= 0xffffffff) {
372
- return (u >>> 1) ^ -(u & 1);
375
+ const res = (u >>> 1) ^ -(u & 1);
376
+ return res === 0 ? 0 : res;
373
377
  }
374
- const b = decodeZigZagBigInt(u);
375
- return Number(b);
378
+ const n = Number(decodeZigZagBigInt(u));
379
+ return n === 0 ? 0 : n;
380
+ }
381
+ /**
382
+ * Maps a ZigZag-encoded unsigned integer back to a signed 32-bit JS number,
383
+ * retaining the exact low 32 bits even when the wire value exceeds 2^53.
384
+ */
385
+ export function decodeZigZag32(u) {
386
+ if (typeof u === 'number' && u <= 0xffffffff) {
387
+ const res = (u >>> 1) ^ -(u & 1);
388
+ return res === 0 ? 0 : res;
389
+ }
390
+ const n = Number(BigInt.asIntN(32, decodeZigZagBigInt(u)));
391
+ return n === 0 ? 0 : n;
376
392
  }
377
393
  /**
378
394
  * Decodes a ZigZag value returning a JS number when within safe integer bounds,
@@ -764,7 +780,7 @@ function isValidArrayHeaderAt(body, start, end) {
764
780
  case WireSfixed64:
765
781
  return (remaining & 7) === 0 && countNum === remaining >>> 3;
766
782
  default:
767
- return countNum <= remaining;
783
+ return countNum === 0 ? remaining === 0 : countNum <= remaining;
768
784
  }
769
785
  }
770
786
  function isValidMapHeaderAt(body, start, end) {
@@ -804,7 +820,56 @@ function isValidMapHeaderAt(body, start, end) {
804
820
  cn = cBytes;
805
821
  }
806
822
  const pos = start + 2 + cn;
807
- return pos <= end && countNum <= (end - pos) >>> 1;
823
+ return pos <= end && (countNum === 0 ? end === pos : countNum <= (end - pos) >>> 1);
824
+ }
825
+ export function skipValidatedValue(b, pos, wireType) {
826
+ const n = b.length;
827
+ if (pos < 0 || pos > n) {
828
+ return -1;
829
+ }
830
+ if (wireType === WireArray || wireType === WireMap || wireType === WireExtended) {
831
+ if (pos >= n) {
832
+ return -1;
833
+ }
834
+ let lenNum = 0;
835
+ let ln = 0;
836
+ const lb = b[pos];
837
+ if ((lb & 1) !== 0) {
838
+ lenNum = lb >>> 1;
839
+ ln = 1;
840
+ }
841
+ else if ((lb & 2) !== 0) {
842
+ if (pos + 2 > n) {
843
+ return -1;
844
+ }
845
+ lenNum = (lb | (b[pos + 1] << 8)) >>> 2;
846
+ ln = 2;
847
+ }
848
+ else {
849
+ const [bLen, lBytes] = decodeVarintBigInt(b, pos);
850
+ if (lBytes === 0 || bLen < 1n || bLen > BigInt(n - pos - lBytes)) {
851
+ return -1;
852
+ }
853
+ lenNum = Number(bLen);
854
+ ln = lBytes;
855
+ }
856
+ if (lenNum < 1 || pos + ln + lenNum > n) {
857
+ return -1;
858
+ }
859
+ const end = pos + ln + lenNum;
860
+ if (wireType === WireArray) {
861
+ if (!isValidArrayHeaderAt(b, pos + ln, end)) {
862
+ return -1;
863
+ }
864
+ }
865
+ else if (wireType === WireMap) {
866
+ if (!isValidMapHeaderAt(b, pos + ln, end)) {
867
+ return -1;
868
+ }
869
+ }
870
+ return end;
871
+ }
872
+ return fastSkipValue(b, pos, wireType);
808
873
  }
809
874
  export function skipValue(b, pos, wireType) {
810
875
  const n = b.length;
@@ -859,6 +924,257 @@ export function skipValue(b, pos, wireType) {
859
924
  }
860
925
  return nextPos;
861
926
  }
927
+ function validateUnknownEntryAt(body, p, wt, depth, limit) {
928
+ if (wt === WireBoolFalse) {
929
+ if (p >= body.length || (body[p] !== 0 && body[p] !== 1))
930
+ return -1;
931
+ return p + 1;
932
+ }
933
+ if (wt === WireSubMsg) {
934
+ if (p >= body.length)
935
+ return -1;
936
+ const [bLen, bn] = decodeVarintBigInt(body, p);
937
+ if (bn === 0 || bLen > BigInt(body.length - (p + bn)))
938
+ return -1;
939
+ const start = p + bn;
940
+ const end = start + Number(bLen);
941
+ if (!validateUnknownMessageBytes(body.subarray(start, end), depth + 1, limit))
942
+ return -1;
943
+ return end;
944
+ }
945
+ if (wt === WireArray || wt === WireMap) {
946
+ return validateUnknownValueAt(body, p, wt, depth, limit);
947
+ }
948
+ return skipValidatedValue(body, p, wt);
949
+ }
950
+ function validateUnknownValueAt(data, pos, wt, depth, limit) {
951
+ if (depth > limit)
952
+ return -1;
953
+ if (wt === WireSubMsg) {
954
+ if (pos >= data.length)
955
+ return -1;
956
+ const [bLen, bn] = decodeVarintBigInt(data, pos);
957
+ if (bn === 0 || bLen > BigInt(data.length - (pos + bn)))
958
+ return -1;
959
+ const start = pos + bn;
960
+ const end = start + Number(bLen);
961
+ if (!validateUnknownMessageBytes(data.subarray(start, end), depth + 1, limit))
962
+ return -1;
963
+ return end;
964
+ }
965
+ if (wt === WireArray) {
966
+ if (depth + 1 > limit || pos >= data.length)
967
+ return -1;
968
+ const [bLen, bn] = decodeVarintBigInt(data, pos);
969
+ if (bn === 0 || bLen < 2n || bLen > BigInt(data.length - (pos + bn)))
970
+ return -1;
971
+ const end = pos + bn + Number(bLen);
972
+ const body = data.subarray(pos + bn, end);
973
+ const [elemType, count, startP, ok] = parseArrayHeader(body);
974
+ if (!ok)
975
+ return -1;
976
+ let p = startP;
977
+ if (elemType === WireBoolFalse) {
978
+ if (body.length - p !== count)
979
+ return -1;
980
+ for (let i = 0; i < count; i++) {
981
+ const b = body[p + i];
982
+ if (b !== 0 && b !== 1)
983
+ return -1;
984
+ }
985
+ return end;
986
+ }
987
+ if (elemType === WireFixed32 || elemType === WireSfixed32 || elemType === WireFloat32) {
988
+ return body.length - p === count * 4 ? end : -1;
989
+ }
990
+ if (elemType === WireFixed64 || elemType === WireSfixed64 || elemType === WireFloat64) {
991
+ return body.length - p === count * 8 ? end : -1;
992
+ }
993
+ for (let i = 0; i < count; i++) {
994
+ p = validateUnknownEntryAt(body, p, elemType, depth + 1, limit);
995
+ if (p < 0)
996
+ return -1;
997
+ }
998
+ return p === body.length ? end : -1;
999
+ }
1000
+ if (wt === WireMap) {
1001
+ if (depth + 1 > limit || pos >= data.length)
1002
+ return -1;
1003
+ const [bLen, bn] = decodeVarintBigInt(data, pos);
1004
+ if (bn === 0 || bLen < 3n || bLen > BigInt(data.length - (pos + bn)))
1005
+ return -1;
1006
+ const end = pos + bn + Number(bLen);
1007
+ const body = data.subarray(pos + bn, end);
1008
+ const [keyWT, valWT, count, startP, ok] = parseMapHeader(body);
1009
+ if (!ok)
1010
+ return -1;
1011
+ let p = startP;
1012
+ for (let i = 0; i < count; i++) {
1013
+ p = validateUnknownEntryAt(body, p, keyWT, depth + 1, limit);
1014
+ if (p < 0)
1015
+ return -1;
1016
+ p = validateUnknownEntryAt(body, p, valWT, depth + 1, limit);
1017
+ if (p < 0)
1018
+ return -1;
1019
+ }
1020
+ return p === body.length ? end : -1;
1021
+ }
1022
+ return skipValidatedValue(data, pos, wt);
1023
+ }
1024
+ function validateUnknownMessageBytes(data, depth, limit = MaxNestingDepth) {
1025
+ if (depth > limit)
1026
+ return false;
1027
+ let pos = 0;
1028
+ while (pos < data.length) {
1029
+ const [tag, tn] = decodeVarintBigInt(data, pos);
1030
+ if (tn === 0 || tag >> 4n > 536870911n)
1031
+ return false;
1032
+ pos += tn;
1033
+ const fnum = Number(tag >> 4n);
1034
+ const wt = Number(tag & 0x0fn);
1035
+ if (fnum === 0) {
1036
+ const nextPos = validateField0At(data, pos, wt, depth, limit);
1037
+ if (nextPos < 0)
1038
+ return false;
1039
+ pos = nextPos;
1040
+ }
1041
+ else {
1042
+ const nextPos = validateUnknownValueAt(data, pos, wt, depth, limit);
1043
+ if (nextPos < 0)
1044
+ return false;
1045
+ pos = nextPos;
1046
+ }
1047
+ }
1048
+ return true;
1049
+ }
1050
+ /**
1051
+ * Validates an `omnibuf.v1.SchemaMeta` payload at `depth`.
1052
+ */
1053
+ export function validateSchemaMetaBytes(data, depth, limit = MaxNestingDepth) {
1054
+ if (depth > limit)
1055
+ return false;
1056
+ let pos = 0;
1057
+ const n = data.length;
1058
+ while (pos < n) {
1059
+ const [tag, tn] = decodeVarintBigInt(data, pos);
1060
+ if (tn === 0 || tag >> 4n > 536870911n)
1061
+ return false;
1062
+ pos += tn;
1063
+ const fnum = Number(tag >> 4n);
1064
+ const wt = Number(tag & 0x0fn);
1065
+ switch (fnum) {
1066
+ case 1: {
1067
+ if (wt !== WireString || pos >= n)
1068
+ return false;
1069
+ const [sLen, sn] = decodeVarintBigInt(data, pos);
1070
+ if (sn === 0 || sLen > BigInt(n - (pos + sn)))
1071
+ return false;
1072
+ pos += sn;
1073
+ const end = pos + Number(sLen);
1074
+ try {
1075
+ decodeUTF8(data, pos, end);
1076
+ }
1077
+ catch {
1078
+ return false;
1079
+ }
1080
+ pos = end;
1081
+ break;
1082
+ }
1083
+ case 2: {
1084
+ if (wt !== WireMap || depth + 1 > limit || pos >= n)
1085
+ return false;
1086
+ const [bLen, bn] = decodeVarintBigInt(data, pos);
1087
+ if (bn === 0 || bLen < 3n || bLen > BigInt(n - (pos + bn)))
1088
+ return false;
1089
+ const bodyEnd = pos + bn + Number(bLen);
1090
+ const body = data.subarray(pos + bn, bodyEnd);
1091
+ pos = bodyEnd;
1092
+ const [keyWT, valWT, count, startP, ok] = parseMapHeader(body);
1093
+ if (!ok || keyWT !== WireUint || valWT !== WireString)
1094
+ return false;
1095
+ let p = startP;
1096
+ for (let i = 0; i < count; i++) {
1097
+ const [k64, kn] = decodeVarintBigInt(body, p);
1098
+ if (kn === 0 || k64 === 0n || k64 > 536870911n)
1099
+ return false;
1100
+ p += kn;
1101
+ const [sLen, sn] = decodeVarintBigInt(body, p);
1102
+ if (sn === 0 || sLen > BigInt(body.length - (p + sn)))
1103
+ return false;
1104
+ p += sn;
1105
+ const sEnd = p + Number(sLen);
1106
+ try {
1107
+ decodeUTF8(body, p, sEnd);
1108
+ }
1109
+ catch {
1110
+ return false;
1111
+ }
1112
+ p = sEnd;
1113
+ }
1114
+ if (p !== body.length)
1115
+ return false;
1116
+ break;
1117
+ }
1118
+ case 3: {
1119
+ if (wt !== WireMap || depth + 1 > limit || pos >= n)
1120
+ return false;
1121
+ const [bLen, bn] = decodeVarintBigInt(data, pos);
1122
+ if (bn === 0 || bLen < 3n || bLen > BigInt(n - (pos + bn)))
1123
+ return false;
1124
+ const bodyEnd = pos + bn + Number(bLen);
1125
+ const body = data.subarray(pos + bn, bodyEnd);
1126
+ pos = bodyEnd;
1127
+ const [keyWT, valWT, count, startP, ok] = parseMapHeader(body);
1128
+ if (!ok || keyWT !== WireUint || valWT !== WireSubMsg)
1129
+ return false;
1130
+ let p = startP;
1131
+ for (let i = 0; i < count; i++) {
1132
+ const [k64, kn] = decodeVarintBigInt(body, p);
1133
+ if (kn === 0 || k64 === 0n || k64 > 536870911n)
1134
+ return false;
1135
+ p += kn;
1136
+ const [cLen, cn] = decodeVarintBigInt(body, p);
1137
+ if (cn === 0 || cLen > BigInt(body.length - (p + cn)))
1138
+ return false;
1139
+ p += cn;
1140
+ const cEnd = p + Number(cLen);
1141
+ if (!validateSchemaMetaBytes(body.subarray(p, cEnd), depth + 2, limit))
1142
+ return false;
1143
+ p = cEnd;
1144
+ }
1145
+ if (p !== body.length)
1146
+ return false;
1147
+ break;
1148
+ }
1149
+ default: {
1150
+ if (fnum === 0)
1151
+ return false;
1152
+ const nextPos = validateUnknownValueAt(data, pos, wt, depth, limit);
1153
+ if (nextPos < 0)
1154
+ return false;
1155
+ pos = nextPos;
1156
+ break;
1157
+ }
1158
+ }
1159
+ }
1160
+ return true;
1161
+ }
1162
+ /**
1163
+ * Validates a `Field #0` (`omnibuf.v1.SchemaMeta`) entry starting at value offset `pos`.
1164
+ * Returns the end offset after `Field #0`, or `-1` if invalid.
1165
+ */
1166
+ export function validateField0At(packet, pos, wt, depth = 0, limit = MaxNestingDepth) {
1167
+ if (wt !== WireSubMsg || pos < 0 || pos >= packet.length)
1168
+ return -1;
1169
+ const [bLen, bn] = decodeVarintBigInt(packet, pos);
1170
+ if (bn === 0 || bLen > BigInt(packet.length - (pos + bn)))
1171
+ return -1;
1172
+ const start = pos + bn;
1173
+ const end = start + Number(bLen);
1174
+ if (!validateSchemaMetaBytes(packet.subarray(start, end), depth + 1, limit))
1175
+ return -1;
1176
+ return end;
1177
+ }
862
1178
  const NOT_FOUND = {
863
1179
  valSlice: new Uint8Array(0),
864
1180
  valPos: 0,
@@ -870,7 +1186,7 @@ const NOT_FOUND = {
870
1186
  * using TZCNT length prefixes (without parsing inside preceding strings, sub-messages, or arrays).
871
1187
  * Scans all field headers in the message and returns the last matching occurrence ("last write wins").
872
1188
  */
873
- export function locateField(packet, targetField) {
1189
+ export function locateField(packet, targetField, expMask = 0xffff) {
874
1190
  let pos = 0;
875
1191
  const n = packet.length;
876
1192
  let lastPos = -1;
@@ -901,11 +1217,23 @@ export function locateField(packet, targetField) {
901
1217
  wt = Number(tag & 0x0fn);
902
1218
  pos += tn;
903
1219
  }
904
- if (fnum === targetField) {
1220
+ if (fnum === 0) {
1221
+ const f0End = validateField0At(packet, pos, wt, 0);
1222
+ if (f0End < 0) {
1223
+ return NOT_FOUND;
1224
+ }
1225
+ if (targetField === 0 && ((expMask >>> wt) & 1) !== 0) {
1226
+ lastPos = pos;
1227
+ lastWT = wt;
1228
+ }
1229
+ pos = f0End;
1230
+ continue;
1231
+ }
1232
+ if (fnum === targetField && ((expMask >>> wt) & 1) !== 0) {
905
1233
  lastPos = pos;
906
1234
  lastWT = wt;
907
1235
  }
908
- const nextPos = fastSkipValue(packet, pos, wt);
1236
+ const nextPos = skipValidatedValue(packet, pos, wt);
909
1237
  if (nextPos < 0) {
910
1238
  return NOT_FOUND;
911
1239
  }
@@ -954,8 +1282,19 @@ export function nextContainerField(packet, pos, targetField, expWT, minLen) {
954
1282
  wt = Number(tag & 0x0fn);
955
1283
  pos += tn;
956
1284
  }
957
- if (fnum === targetField) {
958
- if (wt !== expWT || pos >= n) {
1285
+ if (fnum === 0 && targetField !== 0) {
1286
+ const f0End = validateField0At(packet, pos, wt, 0);
1287
+ if (f0End < 0) {
1288
+ return { body: new Uint8Array(0), nextPos: 0, matched: false, valid: false };
1289
+ }
1290
+ pos = f0End;
1291
+ continue;
1292
+ }
1293
+ if (fnum === 0 && wt !== WireSubMsg) {
1294
+ return { body: new Uint8Array(0), nextPos: 0, matched: false, valid: false };
1295
+ }
1296
+ if (fnum === targetField && wt === expWT) {
1297
+ if (pos >= n) {
959
1298
  return { body: new Uint8Array(0), nextPos: 0, matched: false, valid: false };
960
1299
  }
961
1300
  let lenNum = 0;
@@ -992,12 +1331,11 @@ export function nextContainerField(packet, pos, targetField, expWT, minLen) {
992
1331
  valid: true,
993
1332
  };
994
1333
  }
995
- try {
996
- pos = skipValue(packet, pos, wt);
997
- }
998
- catch {
1334
+ const nextPos = skipValidatedValue(packet, pos, wt);
1335
+ if (nextPos < 0) {
999
1336
  return { body: new Uint8Array(0), nextPos: 0, matched: false, valid: false };
1000
1337
  }
1338
+ pos = nextPos;
1001
1339
  }
1002
1340
  return { body: new Uint8Array(0), nextPos: n, matched: false, valid: true };
1003
1341
  }