cbor-x 1.6.4 → 1.6.5

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.
@@ -561,43 +561,49 @@
561
561
  units.push(byte1);
562
562
  } else if ((byte1 & 0xe0) === 0xc0) {
563
563
  // 2 bytes
564
- const byte2 = src[position$1++] & 0x3f;
565
- const codePoint = ((byte1 & 0x1f) << 6) | byte2;
566
- // Reject overlong encoding: 2-byte sequences must encode values >= 0x80
567
- if (codePoint < 0x80) {
568
- units.push(0xFFFD); // replacement character
564
+ if (byte1 < 0xc2 || position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
565
+ units.push(0xFFFD);
569
566
  } else {
570
- units.push(codePoint);
567
+ const byte2 = src[position$1++] & 0x3f;
568
+ units.push(((byte1 & 0x1f) << 6) | byte2);
571
569
  }
572
570
  } else if ((byte1 & 0xf0) === 0xe0) {
573
571
  // 3 bytes
574
- const byte2 = src[position$1++] & 0x3f;
575
- const byte3 = src[position$1++] & 0x3f;
576
- const codePoint = ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3;
577
- // Reject overlong encoding: 3-byte sequences must encode values >= 0x800
578
- // Also reject surrogates (0xD800-0xDFFF)
579
- if (codePoint < 0x800 || (codePoint >= 0xD800 && codePoint <= 0xDFFF)) {
580
- units.push(0xFFFD); // replacement character
572
+ const byte2 = position$1 < end ? src[position$1] : 0;
573
+ if (position$1 >= end || (byte2 & 0xc0) !== 0x80 ||
574
+ (byte1 === 0xe0 && byte2 < 0xa0) || (byte1 === 0xed && byte2 >= 0xa0)) {
575
+ units.push(0xFFFD);
581
576
  } else {
582
- units.push(codePoint);
577
+ position$1++;
578
+ if (position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
579
+ units.push(0xFFFD);
580
+ } else {
581
+ const byte3 = src[position$1++] & 0x3f;
582
+ units.push(((byte1 & 0x1f) << 12) | ((byte2 & 0x3f) << 6) | byte3);
583
+ }
583
584
  }
584
585
  } else if ((byte1 & 0xf8) === 0xf0) {
585
586
  // 4 bytes
586
- const byte2 = src[position$1++] & 0x3f;
587
- const byte3 = src[position$1++] & 0x3f;
588
- const byte4 = src[position$1++] & 0x3f;
589
- let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
590
- // Reject overlong encoding: 4-byte sequences must encode values >= 0x10000
591
- // Also reject values > 0x10FFFF (maximum valid Unicode)
592
- if (unit < 0x10000 || unit > 0x10FFFF) {
593
- units.push(0xFFFD); // replacement character
594
- } else if (unit > 0xffff) {
595
- unit -= 0x10000;
596
- units.push(((unit >>> 10) & 0x3ff) | 0xd800);
597
- unit = 0xdc00 | (unit & 0x3ff);
598
- units.push(unit);
587
+ const byte2 = position$1 < end ? src[position$1] : 0;
588
+ if (byte1 > 0xf4 || position$1 >= end || (byte2 & 0xc0) !== 0x80 ||
589
+ (byte1 === 0xf0 && byte2 < 0x90) || (byte1 === 0xf4 && byte2 >= 0x90)) {
590
+ units.push(0xFFFD);
599
591
  } else {
600
- units.push(unit);
592
+ position$1++;
593
+ if (position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
594
+ units.push(0xFFFD);
595
+ } else {
596
+ const byte3 = src[position$1++] & 0x3f;
597
+ if (position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
598
+ units.push(0xFFFD);
599
+ } else {
600
+ const byte4 = src[position$1++] & 0x3f;
601
+ let unit = ((byte1 & 0x07) << 0x12) | ((byte2 & 0x3f) << 0x0c) | (byte3 << 0x06) | byte4;
602
+ unit -= 0x10000;
603
+ units.push(((unit >>> 10) & 0x3ff) | 0xd800);
604
+ units.push(0xdc00 | (unit & 0x3ff));
605
+ }
606
+ }
601
607
  }
602
608
  } else {
603
609
  units.push(0xFFFD); // replacement character for invalid lead byte