EUCJPDecoder.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var encodings_1 = require("../../encoding/encodings");
  4. var finished_1 = require("../../encoding/finished");
  5. var indexes_1 = require("../../encoding/indexes");
  6. var terminology_1 = require("../../encoding/terminology");
  7. var utilities_1 = require("../../encoding/utilities");
  8. /**
  9. * @constructor
  10. * @implements {Decoder}
  11. * @param {{fatal: boolean}} options
  12. */
  13. var EUCJPDecoder = /** @class */ (function () {
  14. function EUCJPDecoder(options) {
  15. this.fatal = options.fatal;
  16. // euc-jp's decoder has an associated euc-jp jis0212 flag
  17. // (initially unset) and euc-jp lead (initially 0x00).
  18. /** @type {boolean} */ this.eucjp_jis0212_flag = false,
  19. /** @type {number} */ this.eucjp_lead = 0x00;
  20. }
  21. /**
  22. * @param {Stream} stream The stream of bytes being decoded.
  23. * @param {number} bite The next byte read from the stream.
  24. * @return {?(number|!Array.<number>)} The next code point(s)
  25. * decoded, or null if not enough data exists in the input
  26. * stream to decode a complete code point.
  27. */
  28. EUCJPDecoder.prototype.handler = function (stream, bite) {
  29. // 1. If byte is end-of-stream and euc-jp lead is not 0x00, set
  30. // euc-jp lead to 0x00, and return error.
  31. if (bite === terminology_1.end_of_stream && this.eucjp_lead !== 0x00) {
  32. this.eucjp_lead = 0x00;
  33. return encodings_1.decoderError(this.fatal);
  34. }
  35. // 2. If byte is end-of-stream and euc-jp lead is 0x00, return
  36. // finished.
  37. if (bite === terminology_1.end_of_stream && this.eucjp_lead === 0x00)
  38. return finished_1.finished;
  39. // 3. If euc-jp lead is 0x8E and byte is in the range 0xA1 to
  40. // 0xDF, inclusive, set euc-jp lead to 0x00 and return a code
  41. // point whose value is 0xFF61 − 0xA1 + byte.
  42. if (this.eucjp_lead === 0x8E && utilities_1.inRange(bite, 0xA1, 0xDF)) {
  43. this.eucjp_lead = 0x00;
  44. return 0xFF61 - 0xA1 + bite;
  45. }
  46. // 4. If euc-jp lead is 0x8F and byte is in the range 0xA1 to
  47. // 0xFE, inclusive, set the euc-jp jis0212 flag, set euc-jp lead
  48. // to byte, and return continue.
  49. if (this.eucjp_lead === 0x8F && utilities_1.inRange(bite, 0xA1, 0xFE)) {
  50. this.eucjp_jis0212_flag = true;
  51. this.eucjp_lead = bite;
  52. return null;
  53. }
  54. // 5. If euc-jp lead is not 0x00, let lead be euc-jp lead, set
  55. // euc-jp lead to 0x00, and run these substeps:
  56. if (this.eucjp_lead !== 0x00) {
  57. var lead = this.eucjp_lead;
  58. this.eucjp_lead = 0x00;
  59. // 1. Let code point be null.
  60. var code_point = null;
  61. // 2. If lead and byte are both in the range 0xA1 to 0xFE,
  62. // inclusive, set code point to the index code point for (lead
  63. // − 0xA1) × 94 + byte − 0xA1 in index jis0208 if the euc-jp
  64. // jis0212 flag is unset and in index jis0212 otherwise.
  65. if (utilities_1.inRange(lead, 0xA1, 0xFE) && utilities_1.inRange(bite, 0xA1, 0xFE)) {
  66. code_point = indexes_1.indexCodePointFor((lead - 0xA1) * 94 + (bite - 0xA1), indexes_1.index(!this.eucjp_jis0212_flag ? 'jis0208' : 'jis0212'));
  67. }
  68. // 3. Unset the euc-jp jis0212 flag.
  69. this.eucjp_jis0212_flag = false;
  70. // 4. If byte is not in the range 0xA1 to 0xFE, inclusive,
  71. // prepend byte to stream.
  72. if (!utilities_1.inRange(bite, 0xA1, 0xFE))
  73. stream.prepend(bite);
  74. // 5. If code point is null, return error.
  75. if (code_point === null)
  76. return encodings_1.decoderError(this.fatal);
  77. // 6. Return a code point whose value is code point.
  78. return code_point;
  79. }
  80. // 6. If byte is an ASCII byte, return a code point whose value
  81. // is byte.
  82. if (terminology_1.isASCIIByte(bite))
  83. return bite;
  84. // 7. If byte is 0x8E, 0x8F, or in the range 0xA1 to 0xFE,
  85. // inclusive, set euc-jp lead to byte and return continue.
  86. if (bite === 0x8E || bite === 0x8F || utilities_1.inRange(bite, 0xA1, 0xFE)) {
  87. this.eucjp_lead = bite;
  88. return null;
  89. }
  90. // 8. Return error.
  91. return encodings_1.decoderError(this.fatal);
  92. };
  93. return EUCJPDecoder;
  94. }());
  95. exports.EUCJPDecoder = EUCJPDecoder;
  96. //# sourceMappingURL=EUCJPDecoder.js.map