UTF16Decoder.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { decoderError } from "../../encoding/encodings";
  2. import { finished } from "../../encoding/finished";
  3. import { end_of_stream } from "../../encoding/terminology";
  4. import { inRange } from "../../encoding/utilities";
  5. import { convertCodeUnitToBytes } from "./converCodeUnitToBytes";
  6. /**
  7. * @constructor
  8. * @implements {Decoder}
  9. * @param {boolean} utf16_be True if big-endian, false if little-endian.
  10. * @param {{fatal: boolean}} options
  11. */
  12. export class UTF16Decoder {
  13. constructor(utf16_be, options) {
  14. this.utf16_be = utf16_be;
  15. this.fatal = options.fatal;
  16. /** @type {?number} */ this.utf16_lead_byte = null;
  17. /** @type {?number} */ this.utf16_lead_surrogate = null;
  18. }
  19. /**
  20. * @param {Stream} stream The stream of bytes being decoded.
  21. * @param {number} bite The next byte read from the stream.
  22. * @return {?(number|!Array.<number>)} The next code point(s)
  23. * decoded, or null if not enough data exists in the input
  24. * stream to decode a complete code point.
  25. */
  26. handler(stream, bite) {
  27. // 1. If byte is end-of-stream and either utf-16 lead byte or
  28. // utf-16 lead surrogate is not null, set utf-16 lead byte and
  29. // utf-16 lead surrogate to null, and return error.
  30. if (bite === end_of_stream && (this.utf16_lead_byte !== null ||
  31. this.utf16_lead_surrogate !== null)) {
  32. return decoderError(this.fatal);
  33. }
  34. // 2. If byte is end-of-stream and utf-16 lead byte and utf-16
  35. // lead surrogate are null, return finished.
  36. if (bite === end_of_stream && this.utf16_lead_byte === null &&
  37. this.utf16_lead_surrogate === null) {
  38. return finished;
  39. }
  40. // 3. If utf-16 lead byte is null, set utf-16 lead byte to byte
  41. // and return continue.
  42. if (this.utf16_lead_byte === null) {
  43. this.utf16_lead_byte = bite;
  44. return null;
  45. }
  46. // 4. Let code unit be the result of:
  47. let code_unit;
  48. if (this.utf16_be) {
  49. // utf-16be decoder flag is set
  50. // (utf-16 lead byte << 8) + byte.
  51. code_unit = (this.utf16_lead_byte << 8) + bite;
  52. }
  53. else {
  54. // utf-16be decoder flag is unset
  55. // (byte << 8) + utf-16 lead byte.
  56. code_unit = (bite << 8) + this.utf16_lead_byte;
  57. }
  58. // Then set utf-16 lead byte to null.
  59. this.utf16_lead_byte = null;
  60. // 5. If utf-16 lead surrogate is not null, let lead surrogate
  61. // be utf-16 lead surrogate, set utf-16 lead surrogate to null,
  62. // and then run these substeps:
  63. if (this.utf16_lead_surrogate !== null) {
  64. const lead_surrogate = this.utf16_lead_surrogate;
  65. this.utf16_lead_surrogate = null;
  66. // 1. If code unit is in the range U+DC00 to U+DFFF,
  67. // inclusive, return a code point whose value is 0x10000 +
  68. // ((lead surrogate − 0xD800) << 10) + (code unit − 0xDC00).
  69. if (inRange(code_unit, 0xDC00, 0xDFFF)) {
  70. return 0x10000 + (lead_surrogate - 0xD800) * 0x400 +
  71. (code_unit - 0xDC00);
  72. }
  73. // 2. Prepend the sequence resulting of converting code unit
  74. // to bytes using utf-16be decoder flag to stream and return
  75. // error.
  76. stream.prepend(convertCodeUnitToBytes(code_unit, this.utf16_be));
  77. return decoderError(this.fatal);
  78. }
  79. // 6. If code unit is in the range U+D800 to U+DBFF, inclusive,
  80. // set utf-16 lead surrogate to code unit and return continue.
  81. if (inRange(code_unit, 0xD800, 0xDBFF)) {
  82. this.utf16_lead_surrogate = code_unit;
  83. return null;
  84. }
  85. // 7. If code unit is in the range U+DC00 to U+DFFF, inclusive,
  86. // return error.
  87. if (inRange(code_unit, 0xDC00, 0xDFFF))
  88. return decoderError(this.fatal);
  89. // 8. Return code point code unit.
  90. return code_unit;
  91. }
  92. }
  93. //# sourceMappingURL=UTF16Decoder.js.map