EUCJPDecoder.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { decoderError } from "../../encoding/encodings";
  2. import { finished } from "../../encoding/finished";
  3. import { index, indexCodePointFor } from "../../encoding/indexes";
  4. import { end_of_stream, isASCIIByte } from "../../encoding/terminology";
  5. import { inRange } from "../../encoding/utilities";
  6. /**
  7. * @constructor
  8. * @implements {Decoder}
  9. * @param {{fatal: boolean}} options
  10. */
  11. export class EUCJPDecoder {
  12. constructor(options) {
  13. this.fatal = options.fatal;
  14. // euc-jp's decoder has an associated euc-jp jis0212 flag
  15. // (initially unset) and euc-jp lead (initially 0x00).
  16. /** @type {boolean} */ this.eucjp_jis0212_flag = false,
  17. /** @type {number} */ this.eucjp_lead = 0x00;
  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 euc-jp lead is not 0x00, set
  28. // euc-jp lead to 0x00, and return error.
  29. if (bite === end_of_stream && this.eucjp_lead !== 0x00) {
  30. this.eucjp_lead = 0x00;
  31. return decoderError(this.fatal);
  32. }
  33. // 2. If byte is end-of-stream and euc-jp lead is 0x00, return
  34. // finished.
  35. if (bite === end_of_stream && this.eucjp_lead === 0x00)
  36. return finished;
  37. // 3. If euc-jp lead is 0x8E and byte is in the range 0xA1 to
  38. // 0xDF, inclusive, set euc-jp lead to 0x00 and return a code
  39. // point whose value is 0xFF61 − 0xA1 + byte.
  40. if (this.eucjp_lead === 0x8E && inRange(bite, 0xA1, 0xDF)) {
  41. this.eucjp_lead = 0x00;
  42. return 0xFF61 - 0xA1 + bite;
  43. }
  44. // 4. If euc-jp lead is 0x8F and byte is in the range 0xA1 to
  45. // 0xFE, inclusive, set the euc-jp jis0212 flag, set euc-jp lead
  46. // to byte, and return continue.
  47. if (this.eucjp_lead === 0x8F && inRange(bite, 0xA1, 0xFE)) {
  48. this.eucjp_jis0212_flag = true;
  49. this.eucjp_lead = bite;
  50. return null;
  51. }
  52. // 5. If euc-jp lead is not 0x00, let lead be euc-jp lead, set
  53. // euc-jp lead to 0x00, and run these substeps:
  54. if (this.eucjp_lead !== 0x00) {
  55. const lead = this.eucjp_lead;
  56. this.eucjp_lead = 0x00;
  57. // 1. Let code point be null.
  58. let code_point = null;
  59. // 2. If lead and byte are both in the range 0xA1 to 0xFE,
  60. // inclusive, set code point to the index code point for (lead
  61. // − 0xA1) × 94 + byte − 0xA1 in index jis0208 if the euc-jp
  62. // jis0212 flag is unset and in index jis0212 otherwise.
  63. if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
  64. code_point = indexCodePointFor((lead - 0xA1) * 94 + (bite - 0xA1), index(!this.eucjp_jis0212_flag ? 'jis0208' : 'jis0212'));
  65. }
  66. // 3. Unset the euc-jp jis0212 flag.
  67. this.eucjp_jis0212_flag = false;
  68. // 4. If byte is not in the range 0xA1 to 0xFE, inclusive,
  69. // prepend byte to stream.
  70. if (!inRange(bite, 0xA1, 0xFE))
  71. stream.prepend(bite);
  72. // 5. If code point is null, return error.
  73. if (code_point === null)
  74. return decoderError(this.fatal);
  75. // 6. Return a code point whose value is code point.
  76. return code_point;
  77. }
  78. // 6. If byte is an ASCII byte, return a code point whose value
  79. // is byte.
  80. if (isASCIIByte(bite))
  81. return bite;
  82. // 7. If byte is 0x8E, 0x8F, or in the range 0xA1 to 0xFE,
  83. // inclusive, set euc-jp lead to byte and return continue.
  84. if (bite === 0x8E || bite === 0x8F || inRange(bite, 0xA1, 0xFE)) {
  85. this.eucjp_lead = bite;
  86. return null;
  87. }
  88. // 8. Return error.
  89. return decoderError(this.fatal);
  90. }
  91. }
  92. //# sourceMappingURL=EUCJPDecoder.js.map