cbor-x 1.6.0 → 1.6.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/index.js CHANGED
@@ -264,6 +264,7 @@
264
264
  }
265
265
  token = dataView.getUint32(position$1);
266
266
  position$1 += 4;
267
+ if (majorType === 1) return -1 - token; // can't safely use negation operator here
267
268
  break
268
269
  case 0x1b:
269
270
  if (majorType == 7) {
@@ -278,8 +279,7 @@
278
279
  } else if (currentDecoder.int64AsNumber) {
279
280
  token = dataView.getUint32(position$1) * 0x100000000;
280
281
  token += dataView.getUint32(position$1 + 4);
281
- } else
282
- token = dataView.getBigUint64(position$1);
282
+ } else token = dataView.getBigUint64(position$1);
283
283
  position$1 += 8;
284
284
  break
285
285
  case 0x1f:
@@ -561,26 +561,45 @@
561
561
  } else if ((byte1 & 0xe0) === 0xc0) {
562
562
  // 2 bytes
563
563
  const byte2 = src[position$1++] & 0x3f;
564
- units.push(((byte1 & 0x1f) << 6) | byte2);
564
+ const codePoint = ((byte1 & 0x1f) << 6) | byte2;
565
+ // Reject overlong encoding: 2-byte sequences must encode values >= 0x80
566
+ if (codePoint < 0x80) {
567
+ units.push(0xFFFD); // replacement character
568
+ } else {
569
+ units.push(codePoint);
570
+ }
565
571
  } else if ((byte1 & 0xf0) === 0xe0) {
566
572
  // 3 bytes
567
573
  const byte2 = src[position$1++] & 0x3f;
568
574
  const byte3 = src[position$1++] & 0x3f;
569
- units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
575
+ const codePoint = ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3;
576
+ // Reject overlong encoding: 3-byte sequences must encode values >= 0x800
577
+ // Also reject surrogates (0xD800-0xDFFF)
578
+ if (codePoint < 0x800 || (codePoint >= 0xD800 && codePoint <= 0xDFFF)) {
579
+ units.push(0xFFFD); // replacement character
580
+ } else {
581
+ units.push(codePoint);
582
+ }
570
583
  } else if ((byte1 & 0xf8) === 0xf0) {
571
584
  // 4 bytes
572
585
  const byte2 = src[position$1++] & 0x3f;
573
586
  const byte3 = src[position$1++] & 0x3f;
574
587
  const byte4 = src[position$1++] & 0x3f;
575
588
  let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
576
- if (unit > 0xffff) {
589
+ // Reject overlong encoding: 4-byte sequences must encode values >= 0x10000
590
+ // Also reject values > 0x10FFFF (maximum valid Unicode)
591
+ if (unit < 0x10000 || unit > 0x10FFFF) {
592
+ units.push(0xFFFD); // replacement character
593
+ } else if (unit > 0xffff) {
577
594
  unit -= 0x10000;
578
595
  units.push(((unit >>> 10) & 0x3ff) | 0xd800);
579
596
  unit = 0xdc00 | (unit & 0x3ff);
597
+ units.push(unit);
598
+ } else {
599
+ units.push(unit);
580
600
  }
581
- units.push(unit);
582
601
  } else {
583
- units.push(byte1);
602
+ units.push(0xFFFD); // replacement character for invalid lead byte
584
603
  }
585
604
 
586
605
  if (units.length >= 0x1000) {
@@ -1205,8 +1224,8 @@
1205
1224
  let structures;
1206
1225
  let referenceMap;
1207
1226
  options = options || {};
1208
- let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) {
1209
- return target.utf8Write(string, position, maxBytes)
1227
+ let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1228
+ return target.utf8Write(string, position, target.byteLength - position)
1210
1229
  } : (textEncoder && textEncoder.encodeInto) ?
1211
1230
  function(string, position) {
1212
1231
  return textEncoder.encodeInto(string, target.subarray(position)).written
@@ -1587,7 +1606,7 @@
1587
1606
  targetView.setUint32(position, value);
1588
1607
  position += 4;
1589
1608
  }
1590
- } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer
1609
+ } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer, 31-bit or less
1591
1610
  if (value >= -0x18) {
1592
1611
  target[position++] = 0x1f - value;
1593
1612
  } else if (value >= -0x100) {
@@ -1602,6 +1621,11 @@
1602
1621
  targetView.setUint32(position, ~value);
1603
1622
  position += 4;
1604
1623
  }
1624
+ } else if (!this.alwaysUseFloat && value < 0 && value >= -0x100000000 && Math.floor(value) === value) {
1625
+ // negative integer, 32-bit or less
1626
+ target[position++] = 0x3a;
1627
+ targetView.setUint32(position, -1 - value);
1628
+ position += 4;
1605
1629
  } else {
1606
1630
  let useFloat32;
1607
1631
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
@@ -1643,6 +1667,9 @@
1643
1667
  }
1644
1668
  let constructor = value.constructor;
1645
1669
  if (constructor === Object) {
1670
+ if (this.skipFunction === true) {
1671
+ value = Object.fromEntries([...Object.keys(value).filter(x => typeof value[x] !== "function").map(x => [x, value[x]])]);
1672
+ }
1646
1673
  writeObject(value);
1647
1674
  } else if (constructor === Array) {
1648
1675
  length = value.length;