ISO2022JPDecoder.js 12 KB

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