ISO2022JPDecoder.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. var states;
  9. (function (states) {
  10. states[states["ASCII"] = 0] = "ASCII";
  11. states[states["Roman"] = 1] = "Roman";
  12. states[states["Katakana"] = 2] = "Katakana";
  13. states[states["LeadByte"] = 3] = "LeadByte";
  14. states[states["TrailByte"] = 4] = "TrailByte";
  15. states[states["EscapeStart"] = 5] = "EscapeStart";
  16. states[states["Escape"] = 6] = "Escape";
  17. })(states || (states = {}));
  18. var ISO2022JPDecoder = /** @class */ (function () {
  19. /**
  20. * @constructor
  21. * @implements {Decoder}
  22. * @param {{fatal: boolean}} options
  23. */
  24. function ISO2022JPDecoder(options) {
  25. this.fatal = options.fatal;
  26. // iso-2022-jp's decoder has an associated iso-2022-jp decoder
  27. // state (initially ASCII), iso-2022-jp decoder output state
  28. // (initially ASCII), iso-2022-jp lead (initially 0x00), and
  29. // iso-2022-jp output flag (initially unset).
  30. /** @type {number} */ this.iso2022jp_decoder_state = states.ASCII,
  31. /** @type {number} */ this.iso2022jp_decoder_output_state = states.ASCII,
  32. /** @type {number} */ this.iso2022jp_lead = 0x00,
  33. /** @type {boolean} */ this.iso2022jp_output_flag = false;
  34. }
  35. /**
  36. * @param {Stream} stream The stream of bytes being decoded.
  37. * @param {number} bite The next byte read from the stream.
  38. * @return {?(number|!Array.<number>)} The next code point(s)
  39. * decoded, or null if not enough data exists in the input
  40. * stream to decode a complete code point.
  41. */
  42. ISO2022JPDecoder.prototype.handler = function (stream, bite) {
  43. // switching on iso-2022-jp decoder state:
  44. switch (this.iso2022jp_decoder_state) {
  45. default:
  46. case states.ASCII:
  47. // ASCII
  48. // Based on byte:
  49. // 0x1B
  50. if (bite === 0x1B) {
  51. // Set iso-2022-jp decoder state to escape start and return
  52. // continue.
  53. this.iso2022jp_decoder_state = states.EscapeStart;
  54. return null;
  55. }
  56. // 0x00 to 0x7F, excluding 0x0E, 0x0F, and 0x1B
  57. if (utilities_1.inRange(bite, 0x00, 0x7F) && bite !== 0x0E
  58. && bite !== 0x0F && bite !== 0x1B) {
  59. // Unset the iso-2022-jp output flag and return a code point
  60. // whose value is byte.
  61. this.iso2022jp_output_flag = false;
  62. return bite;
  63. }
  64. // end-of-stream
  65. if (bite === terminology_1.end_of_stream) {
  66. // Return finished.
  67. return finished_1.finished;
  68. }
  69. // Otherwise
  70. // Unset the iso-2022-jp output flag and return error.
  71. this.iso2022jp_output_flag = false;
  72. return encodings_1.decoderError(this.fatal);
  73. case states.Roman:
  74. // Roman
  75. // Based on byte:
  76. // 0x1B
  77. if (bite === 0x1B) {
  78. // Set iso-2022-jp decoder state to escape start and return
  79. // continue.
  80. this.iso2022jp_decoder_state = states.EscapeStart;
  81. return null;
  82. }
  83. // 0x5C
  84. if (bite === 0x5C) {
  85. // Unset the iso-2022-jp output flag and return code point
  86. // U+00A5.
  87. this.iso2022jp_output_flag = false;
  88. return 0x00A5;
  89. }
  90. // 0x7E
  91. if (bite === 0x7E) {
  92. // Unset the iso-2022-jp output flag and return code point
  93. // U+203E.
  94. this.iso2022jp_output_flag = false;
  95. return 0x203E;
  96. }
  97. // 0x00 to 0x7F, excluding 0x0E, 0x0F, 0x1B, 0x5C, and 0x7E
  98. if (utilities_1.inRange(bite, 0x00, 0x7F) && bite !== 0x0E && bite !== 0x0F
  99. && bite !== 0x1B && bite !== 0x5C && bite !== 0x7E) {
  100. // Unset the iso-2022-jp output flag and return a code point
  101. // whose value is byte.
  102. this.iso2022jp_output_flag = false;
  103. return bite;
  104. }
  105. // end-of-stream
  106. if (bite === terminology_1.end_of_stream) {
  107. // Return finished.
  108. return finished_1.finished;
  109. }
  110. // Otherwise
  111. // Unset the iso-2022-jp output flag and return error.
  112. this.iso2022jp_output_flag = false;
  113. return encodings_1.decoderError(this.fatal);
  114. case states.Katakana:
  115. // Katakana
  116. // Based on byte:
  117. // 0x1B
  118. if (bite === 0x1B) {
  119. // Set iso-2022-jp decoder state to escape start and return
  120. // continue.
  121. this.iso2022jp_decoder_state = states.EscapeStart;
  122. return null;
  123. }
  124. // 0x21 to 0x5F
  125. if (utilities_1.inRange(bite, 0x21, 0x5F)) {
  126. // Unset the iso-2022-jp output flag and return a code point
  127. // whose value is 0xFF61 − 0x21 + byte.
  128. this.iso2022jp_output_flag = false;
  129. return 0xFF61 - 0x21 + bite;
  130. }
  131. // end-of-stream
  132. if (bite === terminology_1.end_of_stream) {
  133. // Return finished.
  134. return finished_1.finished;
  135. }
  136. // Otherwise
  137. // Unset the iso-2022-jp output flag and return error.
  138. this.iso2022jp_output_flag = false;
  139. return encodings_1.decoderError(this.fatal);
  140. case states.LeadByte:
  141. // Lead byte
  142. // Based on byte:
  143. // 0x1B
  144. if (bite === 0x1B) {
  145. // Set iso-2022-jp decoder state to escape start and return
  146. // continue.
  147. this.iso2022jp_decoder_state = states.EscapeStart;
  148. return null;
  149. }
  150. // 0x21 to 0x7E
  151. if (utilities_1.inRange(bite, 0x21, 0x7E)) {
  152. // Unset the iso-2022-jp output flag, set iso-2022-jp lead
  153. // to byte, iso-2022-jp decoder state to trail byte, and
  154. // return continue.
  155. this.iso2022jp_output_flag = false;
  156. this.iso2022jp_lead = bite;
  157. this.iso2022jp_decoder_state = states.TrailByte;
  158. return null;
  159. }
  160. // end-of-stream
  161. if (bite === terminology_1.end_of_stream) {
  162. // Return finished.
  163. return finished_1.finished;
  164. }
  165. // Otherwise
  166. // Unset the iso-2022-jp output flag and return error.
  167. this.iso2022jp_output_flag = false;
  168. return encodings_1.decoderError(this.fatal);
  169. case states.TrailByte:
  170. // Trail byte
  171. // Based on byte:
  172. // 0x1B
  173. if (bite === 0x1B) {
  174. // Set iso-2022-jp decoder state to escape start and return
  175. // continue.
  176. this.iso2022jp_decoder_state = states.EscapeStart;
  177. return encodings_1.decoderError(this.fatal);
  178. }
  179. // 0x21 to 0x7E
  180. if (utilities_1.inRange(bite, 0x21, 0x7E)) {
  181. // 1. Set the iso-2022-jp decoder state to lead byte.
  182. this.iso2022jp_decoder_state = states.LeadByte;
  183. // 2. Let pointer be (iso-2022-jp lead − 0x21) × 94 + byte − 0x21.
  184. var pointer = (this.iso2022jp_lead - 0x21) * 94 + bite - 0x21;
  185. // 3. Let code point be the index code point for pointer in
  186. // index jis0208.
  187. var code_point = indexes_1.indexCodePointFor(pointer, indexes_1.index('jis0208'));
  188. // 4. If code point is null, return error.
  189. if (code_point === null)
  190. return encodings_1.decoderError(this.fatal);
  191. // 5. Return a code point whose value is code point.
  192. return code_point;
  193. }
  194. // end-of-stream
  195. if (bite === terminology_1.end_of_stream) {
  196. // Set the iso-2022-jp decoder state to lead byte, prepend
  197. // byte to stream, and return error.
  198. this.iso2022jp_decoder_state = states.LeadByte;
  199. stream.prepend(bite);
  200. return encodings_1.decoderError(this.fatal);
  201. }
  202. // Otherwise
  203. // Set iso-2022-jp decoder state to lead byte and return
  204. // error.
  205. this.iso2022jp_decoder_state = states.LeadByte;
  206. return encodings_1.decoderError(this.fatal);
  207. case states.EscapeStart:
  208. // Escape start
  209. // 1. If byte is either 0x24 or 0x28, set iso-2022-jp lead to
  210. // byte, iso-2022-jp decoder state to escape, and return
  211. // continue.
  212. if (bite === 0x24 || bite === 0x28) {
  213. this.iso2022jp_lead = bite;
  214. this.iso2022jp_decoder_state = states.Escape;
  215. return null;
  216. }
  217. // 2. Prepend byte to stream.
  218. stream.prepend(bite);
  219. // 3. Unset the iso-2022-jp output flag, set iso-2022-jp
  220. // decoder state to iso-2022-jp decoder output state, and
  221. // return error.
  222. this.iso2022jp_output_flag = false;
  223. this.iso2022jp_decoder_state = this.iso2022jp_decoder_output_state;
  224. return encodings_1.decoderError(this.fatal);
  225. case states.Escape:
  226. // Escape
  227. // 1. Let lead be iso-2022-jp lead and set iso-2022-jp lead to
  228. // 0x00.
  229. var lead = this.iso2022jp_lead;
  230. this.iso2022jp_lead = 0x00;
  231. // 2. Let state be null.
  232. var state = null;
  233. // 3. If lead is 0x28 and byte is 0x42, set state to ASCII.
  234. if (lead === 0x28 && bite === 0x42)
  235. state = states.ASCII;
  236. // 4. If lead is 0x28 and byte is 0x4A, set state to Roman.
  237. if (lead === 0x28 && bite === 0x4A)
  238. state = states.Roman;
  239. // 5. If lead is 0x28 and byte is 0x49, set state to Katakana.
  240. if (lead === 0x28 && bite === 0x49)
  241. state = states.Katakana;
  242. // 6. If lead is 0x24 and byte is either 0x40 or 0x42, set
  243. // state to lead byte.
  244. if (lead === 0x24 && (bite === 0x40 || bite === 0x42))
  245. state = states.LeadByte;
  246. // 7. If state is non-null, run these substeps:
  247. if (state !== null) {
  248. // 1. Set iso-2022-jp decoder state and iso-2022-jp decoder
  249. // output state to states.
  250. this.iso2022jp_decoder_state = this.iso2022jp_decoder_state = state;
  251. // 2. Let output flag be the iso-2022-jp output flag.
  252. var output_flag = this.iso2022jp_output_flag;
  253. // 3. Set the iso-2022-jp output flag.
  254. this.iso2022jp_output_flag = true;
  255. // 4. Return continue, if output flag is unset, and error
  256. // otherwise.
  257. return !output_flag ? null : encodings_1.decoderError(this.fatal);
  258. }
  259. // 8. Prepend lead and byte to stream.
  260. stream.prepend([lead, bite]);
  261. // 9. Unset the iso-2022-jp output flag, set iso-2022-jp
  262. // decoder state to iso-2022-jp decoder output state and
  263. // return error.
  264. this.iso2022jp_output_flag = false;
  265. this.iso2022jp_decoder_state = this.iso2022jp_decoder_output_state;
  266. return encodings_1.decoderError(this.fatal);
  267. }
  268. };
  269. return ISO2022JPDecoder;
  270. }());
  271. exports.ISO2022JPDecoder = ISO2022JPDecoder;
  272. //# sourceMappingURL=ISO2022JPDecoder.js.map