encoding.js 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309
  1. // This is free and unencumbered software released into the public domain.
  2. // See LICENSE.md for more information.
  3. // If we're in node require encoding-indexes and attach it to the global.
  4. /**
  5. * @fileoverview Global |this| required for resolving indexes in node.
  6. * @suppress {globalThis}
  7. */
  8. if (typeof module !== "undefined" && module.exports &&
  9. !this["encoding-indexes"]) {
  10. require("./encoding-indexes.js");
  11. }
  12. (function(global) {
  13. 'use strict';
  14. //
  15. // Utilities
  16. //
  17. /**
  18. * @param {number} a The number to test.
  19. * @param {number} min The minimum value in the range, inclusive.
  20. * @param {number} max The maximum value in the range, inclusive.
  21. * @return {boolean} True if a >= min and a <= max.
  22. */
  23. function inRange(a, min, max) {
  24. return min <= a && a <= max;
  25. }
  26. /**
  27. * @param {!Array.<*>} array The array to check.
  28. * @param {*} item The item to look for in the array.
  29. * @return {boolean} True if the item appears in the array.
  30. */
  31. function includes(array, item) {
  32. return array.indexOf(item) !== -1;
  33. }
  34. var floor = Math.floor;
  35. /**
  36. * @param {*} o
  37. * @return {Object}
  38. */
  39. function ToDictionary(o) {
  40. if (o === undefined) return {};
  41. if (o === Object(o)) return o;
  42. throw TypeError('Could not convert argument to dictionary');
  43. }
  44. /**
  45. * @param {string} string Input string of UTF-16 code units.
  46. * @return {!Array.<number>} Code points.
  47. */
  48. function stringToCodePoints(string) {
  49. // https://heycam.github.io/webidl/#dfn-obtain-unicode
  50. // 1. Let S be the DOMString value.
  51. var s = String(string);
  52. // 2. Let n be the length of S.
  53. var n = s.length;
  54. // 3. Initialize i to 0.
  55. var i = 0;
  56. // 4. Initialize U to be an empty sequence of Unicode characters.
  57. var u = [];
  58. // 5. While i < n:
  59. while (i < n) {
  60. // 1. Let c be the code unit in S at index i.
  61. var c = s.charCodeAt(i);
  62. // 2. Depending on the value of c:
  63. // c < 0xD800 or c > 0xDFFF
  64. if (c < 0xD800 || c > 0xDFFF) {
  65. // Append to U the Unicode character with code point c.
  66. u.push(c);
  67. }
  68. // 0xDC00 ≤ c ≤ 0xDFFF
  69. else if (0xDC00 <= c && c <= 0xDFFF) {
  70. // Append to U a U+FFFD REPLACEMENT CHARACTER.
  71. u.push(0xFFFD);
  72. }
  73. // 0xD800 ≤ c ≤ 0xDBFF
  74. else if (0xD800 <= c && c <= 0xDBFF) {
  75. // 1. If i = n−1, then append to U a U+FFFD REPLACEMENT
  76. // CHARACTER.
  77. if (i === n - 1) {
  78. u.push(0xFFFD);
  79. }
  80. // 2. Otherwise, i < n−1:
  81. else {
  82. // 1. Let d be the code unit in S at index i+1.
  83. var d = s.charCodeAt(i + 1);
  84. // 2. If 0xDC00 ≤ d ≤ 0xDFFF, then:
  85. if (0xDC00 <= d && d <= 0xDFFF) {
  86. // 1. Let a be c & 0x3FF.
  87. var a = c & 0x3FF;
  88. // 2. Let b be d & 0x3FF.
  89. var b = d & 0x3FF;
  90. // 3. Append to U the Unicode character with code point
  91. // 2^16+2^10*a+b.
  92. u.push(0x10000 + (a << 10) + b);
  93. // 4. Set i to i+1.
  94. i += 1;
  95. }
  96. // 3. Otherwise, d < 0xDC00 or d > 0xDFFF. Append to U a
  97. // U+FFFD REPLACEMENT CHARACTER.
  98. else {
  99. u.push(0xFFFD);
  100. }
  101. }
  102. }
  103. // 3. Set i to i+1.
  104. i += 1;
  105. }
  106. // 6. Return U.
  107. return u;
  108. }
  109. /**
  110. * @param {!Array.<number>} code_points Array of code points.
  111. * @return {string} string String of UTF-16 code units.
  112. */
  113. function codePointsToString(code_points) {
  114. var s = '';
  115. for (var i = 0; i < code_points.length; ++i) {
  116. var cp = code_points[i];
  117. if (cp <= 0xFFFF) {
  118. s += String.fromCharCode(cp);
  119. } else {
  120. cp -= 0x10000;
  121. s += String.fromCharCode((cp >> 10) + 0xD800,
  122. (cp & 0x3FF) + 0xDC00);
  123. }
  124. }
  125. return s;
  126. }
  127. //
  128. // Implementation of Encoding specification
  129. // https://encoding.spec.whatwg.org/
  130. //
  131. //
  132. // 4. Terminology
  133. //
  134. /**
  135. * An ASCII byte is a byte in the range 0x00 to 0x7F, inclusive.
  136. * @param {number} a The number to test.
  137. * @return {boolean} True if a is in the range 0x00 to 0x7F, inclusive.
  138. */
  139. function isASCIIByte(a) {
  140. return 0x00 <= a && a <= 0x7F;
  141. }
  142. /**
  143. * An ASCII code point is a code point in the range U+0000 to
  144. * U+007F, inclusive.
  145. */
  146. var isASCIICodePoint = isASCIIByte;
  147. /**
  148. * End-of-stream is a special token that signifies no more tokens
  149. * are in the stream.
  150. * @const
  151. */ var end_of_stream = -1;
  152. /**
  153. * A stream represents an ordered sequence of tokens.
  154. *
  155. * @constructor
  156. * @param {!(Array.<number>|Uint8Array)} tokens Array of tokens that provide
  157. * the stream.
  158. */
  159. function Stream(tokens) {
  160. /** @type {!Array.<number>} */
  161. this.tokens = [].slice.call(tokens);
  162. // Reversed as push/pop is more efficient than shift/unshift.
  163. this.tokens.reverse();
  164. }
  165. Stream.prototype = {
  166. /**
  167. * @return {boolean} True if end-of-stream has been hit.
  168. */
  169. endOfStream: function() {
  170. return !this.tokens.length;
  171. },
  172. /**
  173. * When a token is read from a stream, the first token in the
  174. * stream must be returned and subsequently removed, and
  175. * end-of-stream must be returned otherwise.
  176. *
  177. * @return {number} Get the next token from the stream, or
  178. * end_of_stream.
  179. */
  180. read: function() {
  181. if (!this.tokens.length)
  182. return end_of_stream;
  183. return this.tokens.pop();
  184. },
  185. /**
  186. * When one or more tokens are prepended to a stream, those tokens
  187. * must be inserted, in given order, before the first token in the
  188. * stream.
  189. *
  190. * @param {(number|!Array.<number>)} token The token(s) to prepend to the
  191. * stream.
  192. */
  193. prepend: function(token) {
  194. if (Array.isArray(token)) {
  195. var tokens = /**@type {!Array.<number>}*/(token);
  196. while (tokens.length)
  197. this.tokens.push(tokens.pop());
  198. } else {
  199. this.tokens.push(token);
  200. }
  201. },
  202. /**
  203. * When one or more tokens are pushed to a stream, those tokens
  204. * must be inserted, in given order, after the last token in the
  205. * stream.
  206. *
  207. * @param {(number|!Array.<number>)} token The tokens(s) to push to the
  208. * stream.
  209. */
  210. push: function(token) {
  211. if (Array.isArray(token)) {
  212. var tokens = /**@type {!Array.<number>}*/(token);
  213. while (tokens.length)
  214. this.tokens.unshift(tokens.shift());
  215. } else {
  216. this.tokens.unshift(token);
  217. }
  218. }
  219. };
  220. //
  221. // 5. Encodings
  222. //
  223. // 5.1 Encoders and decoders
  224. /** @const */
  225. var finished = -1;
  226. /**
  227. * @param {boolean} fatal If true, decoding errors raise an exception.
  228. * @param {number=} opt_code_point Override the standard fallback code point.
  229. * @return {number} The code point to insert on a decoding error.
  230. */
  231. function decoderError(fatal, opt_code_point) {
  232. if (fatal)
  233. throw TypeError('Decoder error');
  234. return opt_code_point || 0xFFFD;
  235. }
  236. /**
  237. * @param {number} code_point The code point that could not be encoded.
  238. * @return {number} Always throws, no value is actually returned.
  239. */
  240. function encoderError(code_point) {
  241. throw TypeError('The code point ' + code_point + ' could not be encoded.');
  242. }
  243. /** @interface */
  244. function Decoder() {}
  245. Decoder.prototype = {
  246. /**
  247. * @param {Stream} stream The stream of bytes being decoded.
  248. * @param {number} bite The next byte read from the stream.
  249. * @return {?(number|!Array.<number>)} The next code point(s)
  250. * decoded, or null if not enough data exists in the input
  251. * stream to decode a complete code point, or |finished|.
  252. */
  253. handler: function(stream, bite) {}
  254. };
  255. /** @interface */
  256. function Encoder() {}
  257. Encoder.prototype = {
  258. /**
  259. * @param {Stream} stream The stream of code points being encoded.
  260. * @param {number} code_point Next code point read from the stream.
  261. * @return {(number|!Array.<number>)} Byte(s) to emit, or |finished|.
  262. */
  263. handler: function(stream, code_point) {}
  264. };
  265. // 5.2 Names and labels
  266. // TODO: Define @typedef for Encoding: {name:string,labels:Array.<string>}
  267. // https://github.com/google/closure-compiler/issues/247
  268. /**
  269. * @param {string} label The encoding label.
  270. * @return {?{name:string,labels:Array.<string>}}
  271. */
  272. function getEncoding(label) {
  273. // 1. Remove any leading and trailing ASCII whitespace from label.
  274. label = String(label).trim().toLowerCase();
  275. // 2. If label is an ASCII case-insensitive match for any of the
  276. // labels listed in the table below, return the corresponding
  277. // encoding, and failure otherwise.
  278. if (Object.prototype.hasOwnProperty.call(label_to_encoding, label)) {
  279. return label_to_encoding[label];
  280. }
  281. return null;
  282. }
  283. /**
  284. * Encodings table: https://encoding.spec.whatwg.org/encodings.json
  285. * @const
  286. * @type {!Array.<{
  287. * heading: string,
  288. * encodings: Array.<{name:string,labels:Array.<string>}>
  289. * }>}
  290. */
  291. var encodings = [
  292. {
  293. "encodings": [
  294. {
  295. "labels": [
  296. "unicode-1-1-utf-8",
  297. "utf-8",
  298. "utf8"
  299. ],
  300. "name": "UTF-8"
  301. }
  302. ],
  303. "heading": "The Encoding"
  304. },
  305. {
  306. "encodings": [
  307. {
  308. "labels": [
  309. "866",
  310. "cp866",
  311. "csibm866",
  312. "ibm866"
  313. ],
  314. "name": "IBM866"
  315. },
  316. {
  317. "labels": [
  318. "csisolatin2",
  319. "iso-8859-2",
  320. "iso-ir-101",
  321. "iso8859-2",
  322. "iso88592",
  323. "iso_8859-2",
  324. "iso_8859-2:1987",
  325. "l2",
  326. "latin2"
  327. ],
  328. "name": "ISO-8859-2"
  329. },
  330. {
  331. "labels": [
  332. "csisolatin3",
  333. "iso-8859-3",
  334. "iso-ir-109",
  335. "iso8859-3",
  336. "iso88593",
  337. "iso_8859-3",
  338. "iso_8859-3:1988",
  339. "l3",
  340. "latin3"
  341. ],
  342. "name": "ISO-8859-3"
  343. },
  344. {
  345. "labels": [
  346. "csisolatin4",
  347. "iso-8859-4",
  348. "iso-ir-110",
  349. "iso8859-4",
  350. "iso88594",
  351. "iso_8859-4",
  352. "iso_8859-4:1988",
  353. "l4",
  354. "latin4"
  355. ],
  356. "name": "ISO-8859-4"
  357. },
  358. {
  359. "labels": [
  360. "csisolatincyrillic",
  361. "cyrillic",
  362. "iso-8859-5",
  363. "iso-ir-144",
  364. "iso8859-5",
  365. "iso88595",
  366. "iso_8859-5",
  367. "iso_8859-5:1988"
  368. ],
  369. "name": "ISO-8859-5"
  370. },
  371. {
  372. "labels": [
  373. "arabic",
  374. "asmo-708",
  375. "csiso88596e",
  376. "csiso88596i",
  377. "csisolatinarabic",
  378. "ecma-114",
  379. "iso-8859-6",
  380. "iso-8859-6-e",
  381. "iso-8859-6-i",
  382. "iso-ir-127",
  383. "iso8859-6",
  384. "iso88596",
  385. "iso_8859-6",
  386. "iso_8859-6:1987"
  387. ],
  388. "name": "ISO-8859-6"
  389. },
  390. {
  391. "labels": [
  392. "csisolatingreek",
  393. "ecma-118",
  394. "elot_928",
  395. "greek",
  396. "greek8",
  397. "iso-8859-7",
  398. "iso-ir-126",
  399. "iso8859-7",
  400. "iso88597",
  401. "iso_8859-7",
  402. "iso_8859-7:1987",
  403. "sun_eu_greek"
  404. ],
  405. "name": "ISO-8859-7"
  406. },
  407. {
  408. "labels": [
  409. "csiso88598e",
  410. "csisolatinhebrew",
  411. "hebrew",
  412. "iso-8859-8",
  413. "iso-8859-8-e",
  414. "iso-ir-138",
  415. "iso8859-8",
  416. "iso88598",
  417. "iso_8859-8",
  418. "iso_8859-8:1988",
  419. "visual"
  420. ],
  421. "name": "ISO-8859-8"
  422. },
  423. {
  424. "labels": [
  425. "csiso88598i",
  426. "iso-8859-8-i",
  427. "logical"
  428. ],
  429. "name": "ISO-8859-8-I"
  430. },
  431. {
  432. "labels": [
  433. "csisolatin6",
  434. "iso-8859-10",
  435. "iso-ir-157",
  436. "iso8859-10",
  437. "iso885910",
  438. "l6",
  439. "latin6"
  440. ],
  441. "name": "ISO-8859-10"
  442. },
  443. {
  444. "labels": [
  445. "iso-8859-13",
  446. "iso8859-13",
  447. "iso885913"
  448. ],
  449. "name": "ISO-8859-13"
  450. },
  451. {
  452. "labels": [
  453. "iso-8859-14",
  454. "iso8859-14",
  455. "iso885914"
  456. ],
  457. "name": "ISO-8859-14"
  458. },
  459. {
  460. "labels": [
  461. "csisolatin9",
  462. "iso-8859-15",
  463. "iso8859-15",
  464. "iso885915",
  465. "iso_8859-15",
  466. "l9"
  467. ],
  468. "name": "ISO-8859-15"
  469. },
  470. {
  471. "labels": [
  472. "iso-8859-16"
  473. ],
  474. "name": "ISO-8859-16"
  475. },
  476. {
  477. "labels": [
  478. "cskoi8r",
  479. "koi",
  480. "koi8",
  481. "koi8-r",
  482. "koi8_r"
  483. ],
  484. "name": "KOI8-R"
  485. },
  486. {
  487. "labels": [
  488. "koi8-ru",
  489. "koi8-u"
  490. ],
  491. "name": "KOI8-U"
  492. },
  493. {
  494. "labels": [
  495. "csmacintosh",
  496. "mac",
  497. "macintosh",
  498. "x-mac-roman"
  499. ],
  500. "name": "macintosh"
  501. },
  502. {
  503. "labels": [
  504. "dos-874",
  505. "iso-8859-11",
  506. "iso8859-11",
  507. "iso885911",
  508. "tis-620",
  509. "windows-874"
  510. ],
  511. "name": "windows-874"
  512. },
  513. {
  514. "labels": [
  515. "cp1250",
  516. "windows-1250",
  517. "x-cp1250"
  518. ],
  519. "name": "windows-1250"
  520. },
  521. {
  522. "labels": [
  523. "cp1251",
  524. "windows-1251",
  525. "x-cp1251"
  526. ],
  527. "name": "windows-1251"
  528. },
  529. {
  530. "labels": [
  531. "ansi_x3.4-1968",
  532. "ascii",
  533. "cp1252",
  534. "cp819",
  535. "csisolatin1",
  536. "ibm819",
  537. "iso-8859-1",
  538. "iso-ir-100",
  539. "iso8859-1",
  540. "iso88591",
  541. "iso_8859-1",
  542. "iso_8859-1:1987",
  543. "l1",
  544. "latin1",
  545. "us-ascii",
  546. "windows-1252",
  547. "x-cp1252"
  548. ],
  549. "name": "windows-1252"
  550. },
  551. {
  552. "labels": [
  553. "cp1253",
  554. "windows-1253",
  555. "x-cp1253"
  556. ],
  557. "name": "windows-1253"
  558. },
  559. {
  560. "labels": [
  561. "cp1254",
  562. "csisolatin5",
  563. "iso-8859-9",
  564. "iso-ir-148",
  565. "iso8859-9",
  566. "iso88599",
  567. "iso_8859-9",
  568. "iso_8859-9:1989",
  569. "l5",
  570. "latin5",
  571. "windows-1254",
  572. "x-cp1254"
  573. ],
  574. "name": "windows-1254"
  575. },
  576. {
  577. "labels": [
  578. "cp1255",
  579. "windows-1255",
  580. "x-cp1255"
  581. ],
  582. "name": "windows-1255"
  583. },
  584. {
  585. "labels": [
  586. "cp1256",
  587. "windows-1256",
  588. "x-cp1256"
  589. ],
  590. "name": "windows-1256"
  591. },
  592. {
  593. "labels": [
  594. "cp1257",
  595. "windows-1257",
  596. "x-cp1257"
  597. ],
  598. "name": "windows-1257"
  599. },
  600. {
  601. "labels": [
  602. "cp1258",
  603. "windows-1258",
  604. "x-cp1258"
  605. ],
  606. "name": "windows-1258"
  607. },
  608. {
  609. "labels": [
  610. "x-mac-cyrillic",
  611. "x-mac-ukrainian"
  612. ],
  613. "name": "x-mac-cyrillic"
  614. }
  615. ],
  616. "heading": "Legacy single-byte encodings"
  617. },
  618. {
  619. "encodings": [
  620. {
  621. "labels": [
  622. "chinese",
  623. "csgb2312",
  624. "csiso58gb231280",
  625. "gb2312",
  626. "gb_2312",
  627. "gb_2312-80",
  628. "gbk",
  629. "iso-ir-58",
  630. "x-gbk"
  631. ],
  632. "name": "GBK"
  633. },
  634. {
  635. "labels": [
  636. "gb18030"
  637. ],
  638. "name": "gb18030"
  639. }
  640. ],
  641. "heading": "Legacy multi-byte Chinese (simplified) encodings"
  642. },
  643. {
  644. "encodings": [
  645. {
  646. "labels": [
  647. "big5",
  648. "big5-hkscs",
  649. "cn-big5",
  650. "csbig5",
  651. "x-x-big5"
  652. ],
  653. "name": "Big5"
  654. }
  655. ],
  656. "heading": "Legacy multi-byte Chinese (traditional) encodings"
  657. },
  658. {
  659. "encodings": [
  660. {
  661. "labels": [
  662. "cseucpkdfmtjapanese",
  663. "euc-jp",
  664. "x-euc-jp"
  665. ],
  666. "name": "EUC-JP"
  667. },
  668. {
  669. "labels": [
  670. "csiso2022jp",
  671. "iso-2022-jp"
  672. ],
  673. "name": "ISO-2022-JP"
  674. },
  675. {
  676. "labels": [
  677. "csshiftjis",
  678. "ms932",
  679. "ms_kanji",
  680. "shift-jis",
  681. "shift_jis",
  682. "sjis",
  683. "windows-31j",
  684. "x-sjis"
  685. ],
  686. "name": "Shift_JIS"
  687. }
  688. ],
  689. "heading": "Legacy multi-byte Japanese encodings"
  690. },
  691. {
  692. "encodings": [
  693. {
  694. "labels": [
  695. "cseuckr",
  696. "csksc56011987",
  697. "euc-kr",
  698. "iso-ir-149",
  699. "korean",
  700. "ks_c_5601-1987",
  701. "ks_c_5601-1989",
  702. "ksc5601",
  703. "ksc_5601",
  704. "windows-949"
  705. ],
  706. "name": "EUC-KR"
  707. }
  708. ],
  709. "heading": "Legacy multi-byte Korean encodings"
  710. },
  711. {
  712. "encodings": [
  713. {
  714. "labels": [
  715. "csiso2022kr",
  716. "hz-gb-2312",
  717. "iso-2022-cn",
  718. "iso-2022-cn-ext",
  719. "iso-2022-kr"
  720. ],
  721. "name": "replacement"
  722. },
  723. {
  724. "labels": [
  725. "utf-16be"
  726. ],
  727. "name": "UTF-16BE"
  728. },
  729. {
  730. "labels": [
  731. "utf-16",
  732. "utf-16le"
  733. ],
  734. "name": "UTF-16LE"
  735. },
  736. {
  737. "labels": [
  738. "x-user-defined"
  739. ],
  740. "name": "x-user-defined"
  741. }
  742. ],
  743. "heading": "Legacy miscellaneous encodings"
  744. }
  745. ];
  746. // Label to encoding registry.
  747. /** @type {Object.<string,{name:string,labels:Array.<string>}>} */
  748. var label_to_encoding = {};
  749. encodings.forEach(function(category) {
  750. category.encodings.forEach(function(encoding) {
  751. encoding.labels.forEach(function(label) {
  752. label_to_encoding[label] = encoding;
  753. });
  754. });
  755. });
  756. // Registry of of encoder/decoder factories, by encoding name.
  757. /** @type {Object.<string, function({fatal:boolean}): Encoder>} */
  758. var encoders = {};
  759. /** @type {Object.<string, function({fatal:boolean}): Decoder>} */
  760. var decoders = {};
  761. //
  762. // 6. Indexes
  763. //
  764. /**
  765. * @param {number} pointer The |pointer| to search for.
  766. * @param {(!Array.<?number>|undefined)} index The |index| to search within.
  767. * @return {?number} The code point corresponding to |pointer| in |index|,
  768. * or null if |code point| is not in |index|.
  769. */
  770. function indexCodePointFor(pointer, index) {
  771. if (!index) return null;
  772. return index[pointer] || null;
  773. }
  774. /**
  775. * @param {number} code_point The |code point| to search for.
  776. * @param {!Array.<?number>} index The |index| to search within.
  777. * @return {?number} The first pointer corresponding to |code point| in
  778. * |index|, or null if |code point| is not in |index|.
  779. */
  780. function indexPointerFor(code_point, index) {
  781. var pointer = index.indexOf(code_point);
  782. return pointer === -1 ? null : pointer;
  783. }
  784. /**
  785. * @param {string} name Name of the index.
  786. * @return {(!Array.<number>|!Array.<Array.<number>>)}
  787. * */
  788. function index(name) {
  789. if (!('encoding-indexes' in global)) {
  790. throw Error("Indexes missing." +
  791. " Did you forget to include encoding-indexes.js first?");
  792. }
  793. return global['encoding-indexes'][name];
  794. }
  795. /**
  796. * @param {number} pointer The |pointer| to search for in the gb18030 index.
  797. * @return {?number} The code point corresponding to |pointer| in |index|,
  798. * or null if |code point| is not in the gb18030 index.
  799. */
  800. function indexGB18030RangesCodePointFor(pointer) {
  801. // 1. If pointer is greater than 39419 and less than 189000, or
  802. // pointer is greater than 1237575, return null.
  803. if ((pointer > 39419 && pointer < 189000) || (pointer > 1237575))
  804. return null;
  805. // 2. If pointer is 7457, return code point U+E7C7.
  806. if (pointer === 7457) return 0xE7C7;
  807. // 3. Let offset be the last pointer in index gb18030 ranges that
  808. // is equal to or less than pointer and let code point offset be
  809. // its corresponding code point.
  810. var offset = 0;
  811. var code_point_offset = 0;
  812. var idx = index('gb18030-ranges');
  813. var i;
  814. for (i = 0; i < idx.length; ++i) {
  815. /** @type {!Array.<number>} */
  816. var entry = idx[i];
  817. if (entry[0] <= pointer) {
  818. offset = entry[0];
  819. code_point_offset = entry[1];
  820. } else {
  821. break;
  822. }
  823. }
  824. // 4. Return a code point whose value is code point offset +
  825. // pointer − offset.
  826. return code_point_offset + pointer - offset;
  827. }
  828. /**
  829. * @param {number} code_point The |code point| to locate in the gb18030 index.
  830. * @return {number} The first pointer corresponding to |code point| in the
  831. * gb18030 index.
  832. */
  833. function indexGB18030RangesPointerFor(code_point) {
  834. // 1. If code point is U+E7C7, return pointer 7457.
  835. if (code_point === 0xE7C7) return 7457;
  836. // 2. Let offset be the last code point in index gb18030 ranges
  837. // that is equal to or less than code point and let pointer offset
  838. // be its corresponding pointer.
  839. var offset = 0;
  840. var pointer_offset = 0;
  841. var idx = index('gb18030-ranges');
  842. var i;
  843. for (i = 0; i < idx.length; ++i) {
  844. /** @type {!Array.<number>} */
  845. var entry = idx[i];
  846. if (entry[1] <= code_point) {
  847. offset = entry[1];
  848. pointer_offset = entry[0];
  849. } else {
  850. break;
  851. }
  852. }
  853. // 3. Return a pointer whose value is pointer offset + code point
  854. // − offset.
  855. return pointer_offset + code_point - offset;
  856. }
  857. /**
  858. * @param {number} code_point The |code_point| to search for in the Shift_JIS
  859. * index.
  860. * @return {?number} The code point corresponding to |pointer| in |index|,
  861. * or null if |code point| is not in the Shift_JIS index.
  862. */
  863. function indexShiftJISPointerFor(code_point) {
  864. // 1. Let index be index jis0208 excluding all entries whose
  865. // pointer is in the range 8272 to 8835, inclusive.
  866. shift_jis_index = shift_jis_index ||
  867. index('jis0208').map(function(code_point, pointer) {
  868. return inRange(pointer, 8272, 8835) ? null : code_point;
  869. });
  870. var index_ = shift_jis_index;
  871. // 2. Return the index pointer for code point in index.
  872. return index_.indexOf(code_point);
  873. }
  874. var shift_jis_index;
  875. /**
  876. * @param {number} code_point The |code_point| to search for in the big5
  877. * index.
  878. * @return {?number} The code point corresponding to |pointer| in |index|,
  879. * or null if |code point| is not in the big5 index.
  880. */
  881. function indexBig5PointerFor(code_point) {
  882. // 1. Let index be index Big5 excluding all entries whose pointer
  883. big5_index_no_hkscs = big5_index_no_hkscs ||
  884. index('big5').map(function(code_point, pointer) {
  885. return (pointer < (0xA1 - 0x81) * 157) ? null : code_point;
  886. });
  887. var index_ = big5_index_no_hkscs;
  888. // 2. If code point is U+2550, U+255E, U+2561, U+256A, U+5341, or
  889. // U+5345, return the last pointer corresponding to code point in
  890. // index.
  891. if (code_point === 0x2550 || code_point === 0x255E ||
  892. code_point === 0x2561 || code_point === 0x256A ||
  893. code_point === 0x5341 || code_point === 0x5345) {
  894. return index_.lastIndexOf(code_point);
  895. }
  896. // 3. Return the index pointer for code point in index.
  897. return indexPointerFor(code_point, index_);
  898. }
  899. var big5_index_no_hkscs;
  900. //
  901. // 8. API
  902. //
  903. /** @const */ var DEFAULT_ENCODING = 'utf-8';
  904. // 8.1 Interface TextDecoder
  905. /**
  906. * @constructor
  907. * @param {string=} label The label of the encoding;
  908. * defaults to 'utf-8'.
  909. * @param {Object=} options
  910. */
  911. function TextDecoder(label, options) {
  912. // Web IDL conventions
  913. if (!(this instanceof TextDecoder))
  914. throw TypeError('Called as a function. Did you forget \'new\'?');
  915. label = label !== undefined ? String(label) : DEFAULT_ENCODING;
  916. options = ToDictionary(options);
  917. // A TextDecoder object has an associated encoding, decoder,
  918. // stream, ignore BOM flag (initially unset), BOM seen flag
  919. // (initially unset), error mode (initially replacement), and do
  920. // not flush flag (initially unset).
  921. /** @private */
  922. this._encoding = null;
  923. /** @private @type {?Decoder} */
  924. this._decoder = null;
  925. /** @private @type {boolean} */
  926. this._ignoreBOM = false;
  927. /** @private @type {boolean} */
  928. this._BOMseen = false;
  929. /** @private @type {string} */
  930. this._error_mode = 'replacement';
  931. /** @private @type {boolean} */
  932. this._do_not_flush = false;
  933. // 1. Let encoding be the result of getting an encoding from
  934. // label.
  935. var encoding = getEncoding(label);
  936. // 2. If encoding is failure or replacement, throw a RangeError.
  937. if (encoding === null || encoding.name === 'replacement')
  938. throw RangeError('Unknown encoding: ' + label);
  939. if (!decoders[encoding.name]) {
  940. throw Error('Decoder not present.' +
  941. ' Did you forget to include encoding-indexes.js first?');
  942. }
  943. // 3. Let dec be a new TextDecoder object.
  944. var dec = this;
  945. // 4. Set dec's encoding to encoding.
  946. dec._encoding = encoding;
  947. // 5. If options's fatal member is true, set dec's error mode to
  948. // fatal.
  949. if (Boolean(options['fatal']))
  950. dec._error_mode = 'fatal';
  951. // 6. If options's ignoreBOM member is true, set dec's ignore BOM
  952. // flag.
  953. if (Boolean(options['ignoreBOM']))
  954. dec._ignoreBOM = true;
  955. // For pre-ES5 runtimes:
  956. if (!Object.defineProperty) {
  957. this.encoding = dec._encoding.name.toLowerCase();
  958. this.fatal = dec._error_mode === 'fatal';
  959. this.ignoreBOM = dec._ignoreBOM;
  960. }
  961. // 7. Return dec.
  962. return dec;
  963. }
  964. if (Object.defineProperty) {
  965. // The encoding attribute's getter must return encoding's name.
  966. Object.defineProperty(TextDecoder.prototype, 'encoding', {
  967. /** @this {TextDecoder} */
  968. get: function() { return this._encoding.name.toLowerCase(); }
  969. });
  970. // The fatal attribute's getter must return true if error mode
  971. // is fatal, and false otherwise.
  972. Object.defineProperty(TextDecoder.prototype, 'fatal', {
  973. /** @this {TextDecoder} */
  974. get: function() { return this._error_mode === 'fatal'; }
  975. });
  976. // The ignoreBOM attribute's getter must return true if ignore
  977. // BOM flag is set, and false otherwise.
  978. Object.defineProperty(TextDecoder.prototype, 'ignoreBOM', {
  979. /** @this {TextDecoder} */
  980. get: function() { return this._ignoreBOM; }
  981. });
  982. }
  983. /**
  984. * @param {BufferSource=} input The buffer of bytes to decode.
  985. * @param {Object=} options
  986. * @return {string} The decoded string.
  987. */
  988. TextDecoder.prototype.decode = function decode(input, options) {
  989. var bytes;
  990. if (typeof input === 'object' && input instanceof ArrayBuffer) {
  991. bytes = new Uint8Array(input);
  992. } else if (typeof input === 'object' && 'buffer' in input &&
  993. input.buffer instanceof ArrayBuffer) {
  994. bytes = new Uint8Array(input.buffer,
  995. input.byteOffset,
  996. input.byteLength);
  997. } else {
  998. bytes = new Uint8Array(0);
  999. }
  1000. options = ToDictionary(options);
  1001. // 1. If the do not flush flag is unset, set decoder to a new
  1002. // encoding's decoder, set stream to a new stream, and unset the
  1003. // BOM seen flag.
  1004. if (!this._do_not_flush) {
  1005. this._decoder = decoders[this._encoding.name]({
  1006. fatal: this._error_mode === 'fatal'});
  1007. this._BOMseen = false;
  1008. }
  1009. // 2. If options's stream is true, set the do not flush flag, and
  1010. // unset the do not flush flag otherwise.
  1011. this._do_not_flush = Boolean(options['stream']);
  1012. // 3. If input is given, push a copy of input to stream.
  1013. // TODO: Align with spec algorithm - maintain stream on instance.
  1014. var input_stream = new Stream(bytes);
  1015. // 4. Let output be a new stream.
  1016. var output = [];
  1017. /** @type {?(number|!Array.<number>)} */
  1018. var result;
  1019. // 5. While true:
  1020. while (true) {
  1021. // 1. Let token be the result of reading from stream.
  1022. var token = input_stream.read();
  1023. // 2. If token is end-of-stream and the do not flush flag is
  1024. // set, return output, serialized.
  1025. // TODO: Align with spec algorithm.
  1026. if (token === end_of_stream)
  1027. break;
  1028. // 3. Otherwise, run these subsubsteps:
  1029. // 1. Let result be the result of processing token for decoder,
  1030. // stream, output, and error mode.
  1031. result = this._decoder.handler(input_stream, token);
  1032. // 2. If result is finished, return output, serialized.
  1033. if (result === finished)
  1034. break;
  1035. if (result !== null) {
  1036. if (Array.isArray(result))
  1037. output.push.apply(output, /**@type {!Array.<number>}*/(result));
  1038. else
  1039. output.push(result);
  1040. }
  1041. // 3. Otherwise, if result is error, throw a TypeError.
  1042. // (Thrown in handler)
  1043. // 4. Otherwise, do nothing.
  1044. }
  1045. // TODO: Align with spec algorithm.
  1046. if (!this._do_not_flush) {
  1047. do {
  1048. result = this._decoder.handler(input_stream, input_stream.read());
  1049. if (result === finished)
  1050. break;
  1051. if (result === null)
  1052. continue;
  1053. if (Array.isArray(result))
  1054. output.push.apply(output, /**@type {!Array.<number>}*/(result));
  1055. else
  1056. output.push(result);
  1057. } while (!input_stream.endOfStream());
  1058. this._decoder = null;
  1059. }
  1060. // A TextDecoder object also has an associated serialize stream
  1061. // algorithm...
  1062. /**
  1063. * @param {!Array.<number>} stream
  1064. * @return {string}
  1065. * @this {TextDecoder}
  1066. */
  1067. function serializeStream(stream) {
  1068. // 1. Let token be the result of reading from stream.
  1069. // (Done in-place on array, rather than as a stream)
  1070. // 2. If encoding is UTF-8, UTF-16BE, or UTF-16LE, and ignore
  1071. // BOM flag and BOM seen flag are unset, run these subsubsteps:
  1072. if (includes(['UTF-8', 'UTF-16LE', 'UTF-16BE'], this._encoding.name) &&
  1073. !this._ignoreBOM && !this._BOMseen) {
  1074. if (stream.length > 0 && stream[0] === 0xFEFF) {
  1075. // 1. If token is U+FEFF, set BOM seen flag.
  1076. this._BOMseen = true;
  1077. stream.shift();
  1078. } else if (stream.length > 0) {
  1079. // 2. Otherwise, if token is not end-of-stream, set BOM seen
  1080. // flag and append token to stream.
  1081. this._BOMseen = true;
  1082. } else {
  1083. // 3. Otherwise, if token is not end-of-stream, append token
  1084. // to output.
  1085. // (no-op)
  1086. }
  1087. }
  1088. // 4. Otherwise, return output.
  1089. return codePointsToString(stream);
  1090. }
  1091. return serializeStream.call(this, output);
  1092. };
  1093. // 8.2 Interface TextEncoder
  1094. /**
  1095. * @constructor
  1096. * @param {string=} label The label of the encoding. NONSTANDARD.
  1097. * @param {Object=} options NONSTANDARD.
  1098. */
  1099. function TextEncoder(label, options) {
  1100. // Web IDL conventions
  1101. if (!(this instanceof TextEncoder))
  1102. throw TypeError('Called as a function. Did you forget \'new\'?');
  1103. options = ToDictionary(options);
  1104. // A TextEncoder object has an associated encoding and encoder.
  1105. /** @private */
  1106. this._encoding = null;
  1107. /** @private @type {?Encoder} */
  1108. this._encoder = null;
  1109. // Non-standard
  1110. /** @private @type {boolean} */
  1111. this._do_not_flush = false;
  1112. /** @private @type {string} */
  1113. this._fatal = Boolean(options['fatal']) ? 'fatal' : 'replacement';
  1114. // 1. Let enc be a new TextEncoder object.
  1115. var enc = this;
  1116. // 2. Set enc's encoding to UTF-8's encoder.
  1117. if (Boolean(options['NONSTANDARD_allowLegacyEncoding'])) {
  1118. // NONSTANDARD behavior.
  1119. label = label !== undefined ? String(label) : DEFAULT_ENCODING;
  1120. var encoding = getEncoding(label);
  1121. if (encoding === null || encoding.name === 'replacement')
  1122. throw RangeError('Unknown encoding: ' + label);
  1123. if (!encoders[encoding.name]) {
  1124. throw Error('Encoder not present.' +
  1125. ' Did you forget to include encoding-indexes.js first?');
  1126. }
  1127. enc._encoding = encoding;
  1128. } else {
  1129. // Standard behavior.
  1130. enc._encoding = getEncoding('utf-8');
  1131. if (label !== undefined && 'console' in global) {
  1132. console.warn('TextEncoder constructor called with encoding label, '
  1133. + 'which is ignored.');
  1134. }
  1135. }
  1136. // For pre-ES5 runtimes:
  1137. if (!Object.defineProperty)
  1138. this.encoding = enc._encoding.name.toLowerCase();
  1139. // 3. Return enc.
  1140. return enc;
  1141. }
  1142. if (Object.defineProperty) {
  1143. // The encoding attribute's getter must return encoding's name.
  1144. Object.defineProperty(TextEncoder.prototype, 'encoding', {
  1145. /** @this {TextEncoder} */
  1146. get: function() { return this._encoding.name.toLowerCase(); }
  1147. });
  1148. }
  1149. /**
  1150. * @param {string=} opt_string The string to encode.
  1151. * @param {Object=} options
  1152. * @return {!Uint8Array} Encoded bytes, as a Uint8Array.
  1153. */
  1154. TextEncoder.prototype.encode = function encode(opt_string, options) {
  1155. opt_string = opt_string === undefined ? '' : String(opt_string);
  1156. options = ToDictionary(options);
  1157. // NOTE: This option is nonstandard. None of the encodings
  1158. // permitted for encoding (i.e. UTF-8, UTF-16) are stateful when
  1159. // the input is a USVString so streaming is not necessary.
  1160. if (!this._do_not_flush)
  1161. this._encoder = encoders[this._encoding.name]({
  1162. fatal: this._fatal === 'fatal'});
  1163. this._do_not_flush = Boolean(options['stream']);
  1164. // 1. Convert input to a stream.
  1165. var input = new Stream(stringToCodePoints(opt_string));
  1166. // 2. Let output be a new stream
  1167. var output = [];
  1168. /** @type {?(number|!Array.<number>)} */
  1169. var result;
  1170. // 3. While true, run these substeps:
  1171. while (true) {
  1172. // 1. Let token be the result of reading from input.
  1173. var token = input.read();
  1174. if (token === end_of_stream)
  1175. break;
  1176. // 2. Let result be the result of processing token for encoder,
  1177. // input, output.
  1178. result = this._encoder.handler(input, token);
  1179. if (result === finished)
  1180. break;
  1181. if (Array.isArray(result))
  1182. output.push.apply(output, /**@type {!Array.<number>}*/(result));
  1183. else
  1184. output.push(result);
  1185. }
  1186. // TODO: Align with spec algorithm.
  1187. if (!this._do_not_flush) {
  1188. while (true) {
  1189. result = this._encoder.handler(input, input.read());
  1190. if (result === finished)
  1191. break;
  1192. if (Array.isArray(result))
  1193. output.push.apply(output, /**@type {!Array.<number>}*/(result));
  1194. else
  1195. output.push(result);
  1196. }
  1197. this._encoder = null;
  1198. }
  1199. // 3. If result is finished, convert output into a byte sequence,
  1200. // and then return a Uint8Array object wrapping an ArrayBuffer
  1201. // containing output.
  1202. return new Uint8Array(output);
  1203. };
  1204. //
  1205. // 9. The encoding
  1206. //
  1207. // 9.1 utf-8
  1208. // 9.1.1 utf-8 decoder
  1209. /**
  1210. * @constructor
  1211. * @implements {Decoder}
  1212. * @param {{fatal: boolean}} options
  1213. */
  1214. function UTF8Decoder(options) {
  1215. var fatal = options.fatal;
  1216. // utf-8's decoder's has an associated utf-8 code point, utf-8
  1217. // bytes seen, and utf-8 bytes needed (all initially 0), a utf-8
  1218. // lower boundary (initially 0x80), and a utf-8 upper boundary
  1219. // (initially 0xBF).
  1220. var /** @type {number} */ utf8_code_point = 0,
  1221. /** @type {number} */ utf8_bytes_seen = 0,
  1222. /** @type {number} */ utf8_bytes_needed = 0,
  1223. /** @type {number} */ utf8_lower_boundary = 0x80,
  1224. /** @type {number} */ utf8_upper_boundary = 0xBF;
  1225. /**
  1226. * @param {Stream} stream The stream of bytes being decoded.
  1227. * @param {number} bite The next byte read from the stream.
  1228. * @return {?(number|!Array.<number>)} The next code point(s)
  1229. * decoded, or null if not enough data exists in the input
  1230. * stream to decode a complete code point.
  1231. */
  1232. this.handler = function(stream, bite) {
  1233. // 1. If byte is end-of-stream and utf-8 bytes needed is not 0,
  1234. // set utf-8 bytes needed to 0 and return error.
  1235. if (bite === end_of_stream && utf8_bytes_needed !== 0) {
  1236. utf8_bytes_needed = 0;
  1237. return decoderError(fatal);
  1238. }
  1239. // 2. If byte is end-of-stream, return finished.
  1240. if (bite === end_of_stream)
  1241. return finished;
  1242. // 3. If utf-8 bytes needed is 0, based on byte:
  1243. if (utf8_bytes_needed === 0) {
  1244. // 0x00 to 0x7F
  1245. if (inRange(bite, 0x00, 0x7F)) {
  1246. // Return a code point whose value is byte.
  1247. return bite;
  1248. }
  1249. // 0xC2 to 0xDF
  1250. else if (inRange(bite, 0xC2, 0xDF)) {
  1251. // 1. Set utf-8 bytes needed to 1.
  1252. utf8_bytes_needed = 1;
  1253. // 2. Set UTF-8 code point to byte & 0x1F.
  1254. utf8_code_point = bite & 0x1F;
  1255. }
  1256. // 0xE0 to 0xEF
  1257. else if (inRange(bite, 0xE0, 0xEF)) {
  1258. // 1. If byte is 0xE0, set utf-8 lower boundary to 0xA0.
  1259. if (bite === 0xE0)
  1260. utf8_lower_boundary = 0xA0;
  1261. // 2. If byte is 0xED, set utf-8 upper boundary to 0x9F.
  1262. if (bite === 0xED)
  1263. utf8_upper_boundary = 0x9F;
  1264. // 3. Set utf-8 bytes needed to 2.
  1265. utf8_bytes_needed = 2;
  1266. // 4. Set UTF-8 code point to byte & 0xF.
  1267. utf8_code_point = bite & 0xF;
  1268. }
  1269. // 0xF0 to 0xF4
  1270. else if (inRange(bite, 0xF0, 0xF4)) {
  1271. // 1. If byte is 0xF0, set utf-8 lower boundary to 0x90.
  1272. if (bite === 0xF0)
  1273. utf8_lower_boundary = 0x90;
  1274. // 2. If byte is 0xF4, set utf-8 upper boundary to 0x8F.
  1275. if (bite === 0xF4)
  1276. utf8_upper_boundary = 0x8F;
  1277. // 3. Set utf-8 bytes needed to 3.
  1278. utf8_bytes_needed = 3;
  1279. // 4. Set UTF-8 code point to byte & 0x7.
  1280. utf8_code_point = bite & 0x7;
  1281. }
  1282. // Otherwise
  1283. else {
  1284. // Return error.
  1285. return decoderError(fatal);
  1286. }
  1287. // Return continue.
  1288. return null;
  1289. }
  1290. // 4. If byte is not in the range utf-8 lower boundary to utf-8
  1291. // upper boundary, inclusive, run these substeps:
  1292. if (!inRange(bite, utf8_lower_boundary, utf8_upper_boundary)) {
  1293. // 1. Set utf-8 code point, utf-8 bytes needed, and utf-8
  1294. // bytes seen to 0, set utf-8 lower boundary to 0x80, and set
  1295. // utf-8 upper boundary to 0xBF.
  1296. utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
  1297. utf8_lower_boundary = 0x80;
  1298. utf8_upper_boundary = 0xBF;
  1299. // 2. Prepend byte to stream.
  1300. stream.prepend(bite);
  1301. // 3. Return error.
  1302. return decoderError(fatal);
  1303. }
  1304. // 5. Set utf-8 lower boundary to 0x80 and utf-8 upper boundary
  1305. // to 0xBF.
  1306. utf8_lower_boundary = 0x80;
  1307. utf8_upper_boundary = 0xBF;
  1308. // 6. Set UTF-8 code point to (UTF-8 code point << 6) | (byte &
  1309. // 0x3F)
  1310. utf8_code_point = (utf8_code_point << 6) | (bite & 0x3F);
  1311. // 7. Increase utf-8 bytes seen by one.
  1312. utf8_bytes_seen += 1;
  1313. // 8. If utf-8 bytes seen is not equal to utf-8 bytes needed,
  1314. // continue.
  1315. if (utf8_bytes_seen !== utf8_bytes_needed)
  1316. return null;
  1317. // 9. Let code point be utf-8 code point.
  1318. var code_point = utf8_code_point;
  1319. // 10. Set utf-8 code point, utf-8 bytes needed, and utf-8 bytes
  1320. // seen to 0.
  1321. utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
  1322. // 11. Return a code point whose value is code point.
  1323. return code_point;
  1324. };
  1325. }
  1326. // 9.1.2 utf-8 encoder
  1327. /**
  1328. * @constructor
  1329. * @implements {Encoder}
  1330. * @param {{fatal: boolean}} options
  1331. */
  1332. function UTF8Encoder(options) {
  1333. var fatal = options.fatal;
  1334. /**
  1335. * @param {Stream} stream Input stream.
  1336. * @param {number} code_point Next code point read from the stream.
  1337. * @return {(number|!Array.<number>)} Byte(s) to emit.
  1338. */
  1339. this.handler = function(stream, code_point) {
  1340. // 1. If code point is end-of-stream, return finished.
  1341. if (code_point === end_of_stream)
  1342. return finished;
  1343. // 2. If code point is an ASCII code point, return a byte whose
  1344. // value is code point.
  1345. if (isASCIICodePoint(code_point))
  1346. return code_point;
  1347. // 3. Set count and offset based on the range code point is in:
  1348. var count, offset;
  1349. // U+0080 to U+07FF, inclusive:
  1350. if (inRange(code_point, 0x0080, 0x07FF)) {
  1351. // 1 and 0xC0
  1352. count = 1;
  1353. offset = 0xC0;
  1354. }
  1355. // U+0800 to U+FFFF, inclusive:
  1356. else if (inRange(code_point, 0x0800, 0xFFFF)) {
  1357. // 2 and 0xE0
  1358. count = 2;
  1359. offset = 0xE0;
  1360. }
  1361. // U+10000 to U+10FFFF, inclusive:
  1362. else if (inRange(code_point, 0x10000, 0x10FFFF)) {
  1363. // 3 and 0xF0
  1364. count = 3;
  1365. offset = 0xF0;
  1366. }
  1367. // 4. Let bytes be a byte sequence whose first byte is (code
  1368. // point >> (6 × count)) + offset.
  1369. var bytes = [(code_point >> (6 * count)) + offset];
  1370. // 5. Run these substeps while count is greater than 0:
  1371. while (count > 0) {
  1372. // 1. Set temp to code point >> (6 × (count − 1)).
  1373. var temp = code_point >> (6 * (count - 1));
  1374. // 2. Append to bytes 0x80 | (temp & 0x3F).
  1375. bytes.push(0x80 | (temp & 0x3F));
  1376. // 3. Decrease count by one.
  1377. count -= 1;
  1378. }
  1379. // 6. Return bytes bytes, in order.
  1380. return bytes;
  1381. };
  1382. }
  1383. /** @param {{fatal: boolean}} options */
  1384. encoders['UTF-8'] = function(options) {
  1385. return new UTF8Encoder(options);
  1386. };
  1387. /** @param {{fatal: boolean}} options */
  1388. decoders['UTF-8'] = function(options) {
  1389. return new UTF8Decoder(options);
  1390. };
  1391. //
  1392. // 10. Legacy single-byte encodings
  1393. //
  1394. // 10.1 single-byte decoder
  1395. /**
  1396. * @constructor
  1397. * @implements {Decoder}
  1398. * @param {!Array.<number>} index The encoding index.
  1399. * @param {{fatal: boolean}} options
  1400. */
  1401. function SingleByteDecoder(index, options) {
  1402. var fatal = options.fatal;
  1403. /**
  1404. * @param {Stream} stream The stream of bytes being decoded.
  1405. * @param {number} bite The next byte read from the stream.
  1406. * @return {?(number|!Array.<number>)} The next code point(s)
  1407. * decoded, or null if not enough data exists in the input
  1408. * stream to decode a complete code point.
  1409. */
  1410. this.handler = function(stream, bite) {
  1411. // 1. If byte is end-of-stream, return finished.
  1412. if (bite === end_of_stream)
  1413. return finished;
  1414. // 2. If byte is an ASCII byte, return a code point whose value
  1415. // is byte.
  1416. if (isASCIIByte(bite))
  1417. return bite;
  1418. // 3. Let code point be the index code point for byte − 0x80 in
  1419. // index single-byte.
  1420. var code_point = index[bite - 0x80];
  1421. // 4. If code point is null, return error.
  1422. if (code_point === null)
  1423. return decoderError(fatal);
  1424. // 5. Return a code point whose value is code point.
  1425. return code_point;
  1426. };
  1427. }
  1428. // 10.2 single-byte encoder
  1429. /**
  1430. * @constructor
  1431. * @implements {Encoder}
  1432. * @param {!Array.<?number>} index The encoding index.
  1433. * @param {{fatal: boolean}} options
  1434. */
  1435. function SingleByteEncoder(index, options) {
  1436. var fatal = options.fatal;
  1437. /**
  1438. * @param {Stream} stream Input stream.
  1439. * @param {number} code_point Next code point read from the stream.
  1440. * @return {(number|!Array.<number>)} Byte(s) to emit.
  1441. */
  1442. this.handler = function(stream, code_point) {
  1443. // 1. If code point is end-of-stream, return finished.
  1444. if (code_point === end_of_stream)
  1445. return finished;
  1446. // 2. If code point is an ASCII code point, return a byte whose
  1447. // value is code point.
  1448. if (isASCIICodePoint(code_point))
  1449. return code_point;
  1450. // 3. Let pointer be the index pointer for code point in index
  1451. // single-byte.
  1452. var pointer = indexPointerFor(code_point, index);
  1453. // 4. If pointer is null, return error with code point.
  1454. if (pointer === null)
  1455. encoderError(code_point);
  1456. // 5. Return a byte whose value is pointer + 0x80.
  1457. return pointer + 0x80;
  1458. };
  1459. }
  1460. (function() {
  1461. if (!('encoding-indexes' in global))
  1462. return;
  1463. encodings.forEach(function(category) {
  1464. if (category.heading !== 'Legacy single-byte encodings')
  1465. return;
  1466. category.encodings.forEach(function(encoding) {
  1467. var name = encoding.name;
  1468. var idx = index(name.toLowerCase());
  1469. /** @param {{fatal: boolean}} options */
  1470. decoders[name] = function(options) {
  1471. return new SingleByteDecoder(idx, options);
  1472. };
  1473. /** @param {{fatal: boolean}} options */
  1474. encoders[name] = function(options) {
  1475. return new SingleByteEncoder(idx, options);
  1476. };
  1477. });
  1478. });
  1479. }());
  1480. //
  1481. // 11. Legacy multi-byte Chinese (simplified) encodings
  1482. //
  1483. // 11.1 gbk
  1484. // 11.1.1 gbk decoder
  1485. // gbk's decoder is gb18030's decoder.
  1486. /** @param {{fatal: boolean}} options */
  1487. decoders['GBK'] = function(options) {
  1488. return new GB18030Decoder(options);
  1489. };
  1490. // 11.1.2 gbk encoder
  1491. // gbk's encoder is gb18030's encoder with its gbk flag set.
  1492. /** @param {{fatal: boolean}} options */
  1493. encoders['GBK'] = function(options) {
  1494. return new GB18030Encoder(options, true);
  1495. };
  1496. // 11.2 gb18030
  1497. // 11.2.1 gb18030 decoder
  1498. /**
  1499. * @constructor
  1500. * @implements {Decoder}
  1501. * @param {{fatal: boolean}} options
  1502. */
  1503. function GB18030Decoder(options) {
  1504. var fatal = options.fatal;
  1505. // gb18030's decoder has an associated gb18030 first, gb18030
  1506. // second, and gb18030 third (all initially 0x00).
  1507. var /** @type {number} */ gb18030_first = 0x00,
  1508. /** @type {number} */ gb18030_second = 0x00,
  1509. /** @type {number} */ gb18030_third = 0x00;
  1510. /**
  1511. * @param {Stream} stream The stream of bytes being decoded.
  1512. * @param {number} bite The next byte read from the stream.
  1513. * @return {?(number|!Array.<number>)} The next code point(s)
  1514. * decoded, or null if not enough data exists in the input
  1515. * stream to decode a complete code point.
  1516. */
  1517. this.handler = function(stream, bite) {
  1518. // 1. If byte is end-of-stream and gb18030 first, gb18030
  1519. // second, and gb18030 third are 0x00, return finished.
  1520. if (bite === end_of_stream && gb18030_first === 0x00 &&
  1521. gb18030_second === 0x00 && gb18030_third === 0x00) {
  1522. return finished;
  1523. }
  1524. // 2. If byte is end-of-stream, and gb18030 first, gb18030
  1525. // second, or gb18030 third is not 0x00, set gb18030 first,
  1526. // gb18030 second, and gb18030 third to 0x00, and return error.
  1527. if (bite === end_of_stream &&
  1528. (gb18030_first !== 0x00 || gb18030_second !== 0x00 ||
  1529. gb18030_third !== 0x00)) {
  1530. gb18030_first = 0x00;
  1531. gb18030_second = 0x00;
  1532. gb18030_third = 0x00;
  1533. decoderError(fatal);
  1534. }
  1535. var code_point;
  1536. // 3. If gb18030 third is not 0x00, run these substeps:
  1537. if (gb18030_third !== 0x00) {
  1538. // 1. Let code point be null.
  1539. code_point = null;
  1540. // 2. If byte is in the range 0x30 to 0x39, inclusive, set
  1541. // code point to the index gb18030 ranges code point for
  1542. // (((gb18030 first − 0x81) × 10 + gb18030 second − 0x30) ×
  1543. // 126 + gb18030 third − 0x81) × 10 + byte − 0x30.
  1544. if (inRange(bite, 0x30, 0x39)) {
  1545. code_point = indexGB18030RangesCodePointFor(
  1546. (((gb18030_first - 0x81) * 10 + gb18030_second - 0x30) * 126 +
  1547. gb18030_third - 0x81) * 10 + bite - 0x30);
  1548. }
  1549. // 3. Let buffer be a byte sequence consisting of gb18030
  1550. // second, gb18030 third, and byte, in order.
  1551. var buffer = [gb18030_second, gb18030_third, bite];
  1552. // 4. Set gb18030 first, gb18030 second, and gb18030 third to
  1553. // 0x00.
  1554. gb18030_first = 0x00;
  1555. gb18030_second = 0x00;
  1556. gb18030_third = 0x00;
  1557. // 5. If code point is null, prepend buffer to stream and
  1558. // return error.
  1559. if (code_point === null) {
  1560. stream.prepend(buffer);
  1561. return decoderError(fatal);
  1562. }
  1563. // 6. Return a code point whose value is code point.
  1564. return code_point;
  1565. }
  1566. // 4. If gb18030 second is not 0x00, run these substeps:
  1567. if (gb18030_second !== 0x00) {
  1568. // 1. If byte is in the range 0x81 to 0xFE, inclusive, set
  1569. // gb18030 third to byte and return continue.
  1570. if (inRange(bite, 0x81, 0xFE)) {
  1571. gb18030_third = bite;
  1572. return null;
  1573. }
  1574. // 2. Prepend gb18030 second followed by byte to stream, set
  1575. // gb18030 first and gb18030 second to 0x00, and return error.
  1576. stream.prepend([gb18030_second, bite]);
  1577. gb18030_first = 0x00;
  1578. gb18030_second = 0x00;
  1579. return decoderError(fatal);
  1580. }
  1581. // 5. If gb18030 first is not 0x00, run these substeps:
  1582. if (gb18030_first !== 0x00) {
  1583. // 1. If byte is in the range 0x30 to 0x39, inclusive, set
  1584. // gb18030 second to byte and return continue.
  1585. if (inRange(bite, 0x30, 0x39)) {
  1586. gb18030_second = bite;
  1587. return null;
  1588. }
  1589. // 2. Let lead be gb18030 first, let pointer be null, and set
  1590. // gb18030 first to 0x00.
  1591. var lead = gb18030_first;
  1592. var pointer = null;
  1593. gb18030_first = 0x00;
  1594. // 3. Let offset be 0x40 if byte is less than 0x7F and 0x41
  1595. // otherwise.
  1596. var offset = bite < 0x7F ? 0x40 : 0x41;
  1597. // 4. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
  1598. // to 0xFE, inclusive, set pointer to (lead − 0x81) × 190 +
  1599. // (byte − offset).
  1600. if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFE))
  1601. pointer = (lead - 0x81) * 190 + (bite - offset);
  1602. // 5. Let code point be null if pointer is null and the index
  1603. // code point for pointer in index gb18030 otherwise.
  1604. code_point = pointer === null ? null :
  1605. indexCodePointFor(pointer, index('gb18030'));
  1606. // 6. If code point is null and byte is an ASCII byte, prepend
  1607. // byte to stream.
  1608. if (code_point === null && isASCIIByte(bite))
  1609. stream.prepend(bite);
  1610. // 7. If code point is null, return error.
  1611. if (code_point === null)
  1612. return decoderError(fatal);
  1613. // 8. Return a code point whose value is code point.
  1614. return code_point;
  1615. }
  1616. // 6. If byte is an ASCII byte, return a code point whose value
  1617. // is byte.
  1618. if (isASCIIByte(bite))
  1619. return bite;
  1620. // 7. If byte is 0x80, return code point U+20AC.
  1621. if (bite === 0x80)
  1622. return 0x20AC;
  1623. // 8. If byte is in the range 0x81 to 0xFE, inclusive, set
  1624. // gb18030 first to byte and return continue.
  1625. if (inRange(bite, 0x81, 0xFE)) {
  1626. gb18030_first = bite;
  1627. return null;
  1628. }
  1629. // 9. Return error.
  1630. return decoderError(fatal);
  1631. };
  1632. }
  1633. // 11.2.2 gb18030 encoder
  1634. /**
  1635. * @constructor
  1636. * @implements {Encoder}
  1637. * @param {{fatal: boolean}} options
  1638. * @param {boolean=} gbk_flag
  1639. */
  1640. function GB18030Encoder(options, gbk_flag) {
  1641. var fatal = options.fatal;
  1642. // gb18030's decoder has an associated gbk flag (initially unset).
  1643. /**
  1644. * @param {Stream} stream Input stream.
  1645. * @param {number} code_point Next code point read from the stream.
  1646. * @return {(number|!Array.<number>)} Byte(s) to emit.
  1647. */
  1648. this.handler = function(stream, code_point) {
  1649. // 1. If code point is end-of-stream, return finished.
  1650. if (code_point === end_of_stream)
  1651. return finished;
  1652. // 2. If code point is an ASCII code point, return a byte whose
  1653. // value is code point.
  1654. if (isASCIICodePoint(code_point))
  1655. return code_point;
  1656. // 3. If code point is U+E5E5, return error with code point.
  1657. if (code_point === 0xE5E5)
  1658. return encoderError(code_point);
  1659. // 4. If the gbk flag is set and code point is U+20AC, return
  1660. // byte 0x80.
  1661. if (gbk_flag && code_point === 0x20AC)
  1662. return 0x80;
  1663. // 5. Let pointer be the index pointer for code point in index
  1664. // gb18030.
  1665. var pointer = indexPointerFor(code_point, index('gb18030'));
  1666. // 6. If pointer is not null, run these substeps:
  1667. if (pointer !== null) {
  1668. // 1. Let lead be floor(pointer / 190) + 0x81.
  1669. var lead = floor(pointer / 190) + 0x81;
  1670. // 2. Let trail be pointer % 190.
  1671. var trail = pointer % 190;
  1672. // 3. Let offset be 0x40 if trail is less than 0x3F and 0x41 otherwise.
  1673. var offset = trail < 0x3F ? 0x40 : 0x41;
  1674. // 4. Return two bytes whose values are lead and trail + offset.
  1675. return [lead, trail + offset];
  1676. }
  1677. // 7. If gbk flag is set, return error with code point.
  1678. if (gbk_flag)
  1679. return encoderError(code_point);
  1680. // 8. Set pointer to the index gb18030 ranges pointer for code
  1681. // point.
  1682. pointer = indexGB18030RangesPointerFor(code_point);
  1683. // 9. Let byte1 be floor(pointer / 10 / 126 / 10).
  1684. var byte1 = floor(pointer / 10 / 126 / 10);
  1685. // 10. Set pointer to pointer − byte1 × 10 × 126 × 10.
  1686. pointer = pointer - byte1 * 10 * 126 * 10;
  1687. // 11. Let byte2 be floor(pointer / 10 / 126).
  1688. var byte2 = floor(pointer / 10 / 126);
  1689. // 12. Set pointer to pointer − byte2 × 10 × 126.
  1690. pointer = pointer - byte2 * 10 * 126;
  1691. // 13. Let byte3 be floor(pointer / 10).
  1692. var byte3 = floor(pointer / 10);
  1693. // 14. Let byte4 be pointer − byte3 × 10.
  1694. var byte4 = pointer - byte3 * 10;
  1695. // 15. Return four bytes whose values are byte1 + 0x81, byte2 +
  1696. // 0x30, byte3 + 0x81, byte4 + 0x30.
  1697. return [byte1 + 0x81,
  1698. byte2 + 0x30,
  1699. byte3 + 0x81,
  1700. byte4 + 0x30];
  1701. };
  1702. }
  1703. /** @param {{fatal: boolean}} options */
  1704. encoders['gb18030'] = function(options) {
  1705. return new GB18030Encoder(options);
  1706. };
  1707. /** @param {{fatal: boolean}} options */
  1708. decoders['gb18030'] = function(options) {
  1709. return new GB18030Decoder(options);
  1710. };
  1711. //
  1712. // 12. Legacy multi-byte Chinese (traditional) encodings
  1713. //
  1714. // 12.1 Big5
  1715. // 12.1.1 Big5 decoder
  1716. /**
  1717. * @constructor
  1718. * @implements {Decoder}
  1719. * @param {{fatal: boolean}} options
  1720. */
  1721. function Big5Decoder(options) {
  1722. var fatal = options.fatal;
  1723. // Big5's decoder has an associated Big5 lead (initially 0x00).
  1724. var /** @type {number} */ Big5_lead = 0x00;
  1725. /**
  1726. * @param {Stream} stream The stream of bytes being decoded.
  1727. * @param {number} bite The next byte read from the stream.
  1728. * @return {?(number|!Array.<number>)} The next code point(s)
  1729. * decoded, or null if not enough data exists in the input
  1730. * stream to decode a complete code point.
  1731. */
  1732. this.handler = function(stream, bite) {
  1733. // 1. If byte is end-of-stream and Big5 lead is not 0x00, set
  1734. // Big5 lead to 0x00 and return error.
  1735. if (bite === end_of_stream && Big5_lead !== 0x00) {
  1736. Big5_lead = 0x00;
  1737. return decoderError(fatal);
  1738. }
  1739. // 2. If byte is end-of-stream and Big5 lead is 0x00, return
  1740. // finished.
  1741. if (bite === end_of_stream && Big5_lead === 0x00)
  1742. return finished;
  1743. // 3. If Big5 lead is not 0x00, let lead be Big5 lead, let
  1744. // pointer be null, set Big5 lead to 0x00, and then run these
  1745. // substeps:
  1746. if (Big5_lead !== 0x00) {
  1747. var lead = Big5_lead;
  1748. var pointer = null;
  1749. Big5_lead = 0x00;
  1750. // 1. Let offset be 0x40 if byte is less than 0x7F and 0x62
  1751. // otherwise.
  1752. var offset = bite < 0x7F ? 0x40 : 0x62;
  1753. // 2. If byte is in the range 0x40 to 0x7E, inclusive, or 0xA1
  1754. // to 0xFE, inclusive, set pointer to (lead − 0x81) × 157 +
  1755. // (byte − offset).
  1756. if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0xA1, 0xFE))
  1757. pointer = (lead - 0x81) * 157 + (bite - offset);
  1758. // 3. If there is a row in the table below whose first column
  1759. // is pointer, return the two code points listed in its second
  1760. // column
  1761. // Pointer | Code points
  1762. // --------+--------------
  1763. // 1133 | U+00CA U+0304
  1764. // 1135 | U+00CA U+030C
  1765. // 1164 | U+00EA U+0304
  1766. // 1166 | U+00EA U+030C
  1767. switch (pointer) {
  1768. case 1133: return [0x00CA, 0x0304];
  1769. case 1135: return [0x00CA, 0x030C];
  1770. case 1164: return [0x00EA, 0x0304];
  1771. case 1166: return [0x00EA, 0x030C];
  1772. }
  1773. // 4. Let code point be null if pointer is null and the index
  1774. // code point for pointer in index Big5 otherwise.
  1775. var code_point = (pointer === null) ? null :
  1776. indexCodePointFor(pointer, index('big5'));
  1777. // 5. If code point is null and byte is an ASCII byte, prepend
  1778. // byte to stream.
  1779. if (code_point === null && isASCIIByte(bite))
  1780. stream.prepend(bite);
  1781. // 6. If code point is null, return error.
  1782. if (code_point === null)
  1783. return decoderError(fatal);
  1784. // 7. Return a code point whose value is code point.
  1785. return code_point;
  1786. }
  1787. // 4. If byte is an ASCII byte, return a code point whose value
  1788. // is byte.
  1789. if (isASCIIByte(bite))
  1790. return bite;
  1791. // 5. If byte is in the range 0x81 to 0xFE, inclusive, set Big5
  1792. // lead to byte and return continue.
  1793. if (inRange(bite, 0x81, 0xFE)) {
  1794. Big5_lead = bite;
  1795. return null;
  1796. }
  1797. // 6. Return error.
  1798. return decoderError(fatal);
  1799. };
  1800. }
  1801. // 12.1.2 Big5 encoder
  1802. /**
  1803. * @constructor
  1804. * @implements {Encoder}
  1805. * @param {{fatal: boolean}} options
  1806. */
  1807. function Big5Encoder(options) {
  1808. var fatal = options.fatal;
  1809. /**
  1810. * @param {Stream} stream Input stream.
  1811. * @param {number} code_point Next code point read from the stream.
  1812. * @return {(number|!Array.<number>)} Byte(s) to emit.
  1813. */
  1814. this.handler = function(stream, code_point) {
  1815. // 1. If code point is end-of-stream, return finished.
  1816. if (code_point === end_of_stream)
  1817. return finished;
  1818. // 2. If code point is an ASCII code point, return a byte whose
  1819. // value is code point.
  1820. if (isASCIICodePoint(code_point))
  1821. return code_point;
  1822. // 3. Let pointer be the index Big5 pointer for code point.
  1823. var pointer = indexBig5PointerFor(code_point);
  1824. // 4. If pointer is null, return error with code point.
  1825. if (pointer === null)
  1826. return encoderError(code_point);
  1827. // 5. Let lead be floor(pointer / 157) + 0x81.
  1828. var lead = floor(pointer / 157) + 0x81;
  1829. // 6. If lead is less than 0xA1, return error with code point.
  1830. if (lead < 0xA1)
  1831. return encoderError(code_point);
  1832. // 7. Let trail be pointer % 157.
  1833. var trail = pointer % 157;
  1834. // 8. Let offset be 0x40 if trail is less than 0x3F and 0x62
  1835. // otherwise.
  1836. var offset = trail < 0x3F ? 0x40 : 0x62;
  1837. // Return two bytes whose values are lead and trail + offset.
  1838. return [lead, trail + offset];
  1839. };
  1840. }
  1841. /** @param {{fatal: boolean}} options */
  1842. encoders['Big5'] = function(options) {
  1843. return new Big5Encoder(options);
  1844. };
  1845. /** @param {{fatal: boolean}} options */
  1846. decoders['Big5'] = function(options) {
  1847. return new Big5Decoder(options);
  1848. };
  1849. //
  1850. // 13. Legacy multi-byte Japanese encodings
  1851. //
  1852. // 13.1 euc-jp
  1853. // 13.1.1 euc-jp decoder
  1854. /**
  1855. * @constructor
  1856. * @implements {Decoder}
  1857. * @param {{fatal: boolean}} options
  1858. */
  1859. function EUCJPDecoder(options) {
  1860. var fatal = options.fatal;
  1861. // euc-jp's decoder has an associated euc-jp jis0212 flag
  1862. // (initially unset) and euc-jp lead (initially 0x00).
  1863. var /** @type {boolean} */ eucjp_jis0212_flag = false,
  1864. /** @type {number} */ eucjp_lead = 0x00;
  1865. /**
  1866. * @param {Stream} stream The stream of bytes being decoded.
  1867. * @param {number} bite The next byte read from the stream.
  1868. * @return {?(number|!Array.<number>)} The next code point(s)
  1869. * decoded, or null if not enough data exists in the input
  1870. * stream to decode a complete code point.
  1871. */
  1872. this.handler = function(stream, bite) {
  1873. // 1. If byte is end-of-stream and euc-jp lead is not 0x00, set
  1874. // euc-jp lead to 0x00, and return error.
  1875. if (bite === end_of_stream && eucjp_lead !== 0x00) {
  1876. eucjp_lead = 0x00;
  1877. return decoderError(fatal);
  1878. }
  1879. // 2. If byte is end-of-stream and euc-jp lead is 0x00, return
  1880. // finished.
  1881. if (bite === end_of_stream && eucjp_lead === 0x00)
  1882. return finished;
  1883. // 3. If euc-jp lead is 0x8E and byte is in the range 0xA1 to
  1884. // 0xDF, inclusive, set euc-jp lead to 0x00 and return a code
  1885. // point whose value is 0xFF61 − 0xA1 + byte.
  1886. if (eucjp_lead === 0x8E && inRange(bite, 0xA1, 0xDF)) {
  1887. eucjp_lead = 0x00;
  1888. return 0xFF61 - 0xA1 + bite;
  1889. }
  1890. // 4. If euc-jp lead is 0x8F and byte is in the range 0xA1 to
  1891. // 0xFE, inclusive, set the euc-jp jis0212 flag, set euc-jp lead
  1892. // to byte, and return continue.
  1893. if (eucjp_lead === 0x8F && inRange(bite, 0xA1, 0xFE)) {
  1894. eucjp_jis0212_flag = true;
  1895. eucjp_lead = bite;
  1896. return null;
  1897. }
  1898. // 5. If euc-jp lead is not 0x00, let lead be euc-jp lead, set
  1899. // euc-jp lead to 0x00, and run these substeps:
  1900. if (eucjp_lead !== 0x00) {
  1901. var lead = eucjp_lead;
  1902. eucjp_lead = 0x00;
  1903. // 1. Let code point be null.
  1904. var code_point = null;
  1905. // 2. If lead and byte are both in the range 0xA1 to 0xFE,
  1906. // inclusive, set code point to the index code point for (lead
  1907. // − 0xA1) × 94 + byte − 0xA1 in index jis0208 if the euc-jp
  1908. // jis0212 flag is unset and in index jis0212 otherwise.
  1909. if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
  1910. code_point = indexCodePointFor(
  1911. (lead - 0xA1) * 94 + (bite - 0xA1),
  1912. index(!eucjp_jis0212_flag ? 'jis0208' : 'jis0212'));
  1913. }
  1914. // 3. Unset the euc-jp jis0212 flag.
  1915. eucjp_jis0212_flag = false;
  1916. // 4. If byte is not in the range 0xA1 to 0xFE, inclusive,
  1917. // prepend byte to stream.
  1918. if (!inRange(bite, 0xA1, 0xFE))
  1919. stream.prepend(bite);
  1920. // 5. If code point is null, return error.
  1921. if (code_point === null)
  1922. return decoderError(fatal);
  1923. // 6. Return a code point whose value is code point.
  1924. return code_point;
  1925. }
  1926. // 6. If byte is an ASCII byte, return a code point whose value
  1927. // is byte.
  1928. if (isASCIIByte(bite))
  1929. return bite;
  1930. // 7. If byte is 0x8E, 0x8F, or in the range 0xA1 to 0xFE,
  1931. // inclusive, set euc-jp lead to byte and return continue.
  1932. if (bite === 0x8E || bite === 0x8F || inRange(bite, 0xA1, 0xFE)) {
  1933. eucjp_lead = bite;
  1934. return null;
  1935. }
  1936. // 8. Return error.
  1937. return decoderError(fatal);
  1938. };
  1939. }
  1940. // 13.1.2 euc-jp encoder
  1941. /**
  1942. * @constructor
  1943. * @implements {Encoder}
  1944. * @param {{fatal: boolean}} options
  1945. */
  1946. function EUCJPEncoder(options) {
  1947. var fatal = options.fatal;
  1948. /**
  1949. * @param {Stream} stream Input stream.
  1950. * @param {number} code_point Next code point read from the stream.
  1951. * @return {(number|!Array.<number>)} Byte(s) to emit.
  1952. */
  1953. this.handler = function(stream, code_point) {
  1954. // 1. If code point is end-of-stream, return finished.
  1955. if (code_point === end_of_stream)
  1956. return finished;
  1957. // 2. If code point is an ASCII code point, return a byte whose
  1958. // value is code point.
  1959. if (isASCIICodePoint(code_point))
  1960. return code_point;
  1961. // 3. If code point is U+00A5, return byte 0x5C.
  1962. if (code_point === 0x00A5)
  1963. return 0x5C;
  1964. // 4. If code point is U+203E, return byte 0x7E.
  1965. if (code_point === 0x203E)
  1966. return 0x7E;
  1967. // 5. If code point is in the range U+FF61 to U+FF9F, inclusive,
  1968. // return two bytes whose values are 0x8E and code point −
  1969. // 0xFF61 + 0xA1.
  1970. if (inRange(code_point, 0xFF61, 0xFF9F))
  1971. return [0x8E, code_point - 0xFF61 + 0xA1];
  1972. // 6. If code point is U+2212, set it to U+FF0D.
  1973. if (code_point === 0x2212)
  1974. code_point = 0xFF0D;
  1975. // 7. Let pointer be the index pointer for code point in index
  1976. // jis0208.
  1977. var pointer = indexPointerFor(code_point, index('jis0208'));
  1978. // 8. If pointer is null, return error with code point.
  1979. if (pointer === null)
  1980. return encoderError(code_point);
  1981. // 9. Let lead be floor(pointer / 94) + 0xA1.
  1982. var lead = floor(pointer / 94) + 0xA1;
  1983. // 10. Let trail be pointer % 94 + 0xA1.
  1984. var trail = pointer % 94 + 0xA1;
  1985. // 11. Return two bytes whose values are lead and trail.
  1986. return [lead, trail];
  1987. };
  1988. }
  1989. /** @param {{fatal: boolean}} options */
  1990. encoders['EUC-JP'] = function(options) {
  1991. return new EUCJPEncoder(options);
  1992. };
  1993. /** @param {{fatal: boolean}} options */
  1994. decoders['EUC-JP'] = function(options) {
  1995. return new EUCJPDecoder(options);
  1996. };
  1997. // 13.2 iso-2022-jp
  1998. // 13.2.1 iso-2022-jp decoder
  1999. /**
  2000. * @constructor
  2001. * @implements {Decoder}
  2002. * @param {{fatal: boolean}} options
  2003. */
  2004. function ISO2022JPDecoder(options) {
  2005. var fatal = options.fatal;
  2006. /** @enum */
  2007. var states = {
  2008. ASCII: 0,
  2009. Roman: 1,
  2010. Katakana: 2,
  2011. LeadByte: 3,
  2012. TrailByte: 4,
  2013. EscapeStart: 5,
  2014. Escape: 6
  2015. };
  2016. // iso-2022-jp's decoder has an associated iso-2022-jp decoder
  2017. // state (initially ASCII), iso-2022-jp decoder output state
  2018. // (initially ASCII), iso-2022-jp lead (initially 0x00), and
  2019. // iso-2022-jp output flag (initially unset).
  2020. var /** @type {number} */ iso2022jp_decoder_state = states.ASCII,
  2021. /** @type {number} */ iso2022jp_decoder_output_state = states.ASCII,
  2022. /** @type {number} */ iso2022jp_lead = 0x00,
  2023. /** @type {boolean} */ iso2022jp_output_flag = false;
  2024. /**
  2025. * @param {Stream} stream The stream of bytes being decoded.
  2026. * @param {number} bite The next byte read from the stream.
  2027. * @return {?(number|!Array.<number>)} The next code point(s)
  2028. * decoded, or null if not enough data exists in the input
  2029. * stream to decode a complete code point.
  2030. */
  2031. this.handler = function(stream, bite) {
  2032. // switching on iso-2022-jp decoder state:
  2033. switch (iso2022jp_decoder_state) {
  2034. default:
  2035. case states.ASCII:
  2036. // ASCII
  2037. // Based on byte:
  2038. // 0x1B
  2039. if (bite === 0x1B) {
  2040. // Set iso-2022-jp decoder state to escape start and return
  2041. // continue.
  2042. iso2022jp_decoder_state = states.EscapeStart;
  2043. return null;
  2044. }
  2045. // 0x00 to 0x7F, excluding 0x0E, 0x0F, and 0x1B
  2046. if (inRange(bite, 0x00, 0x7F) && bite !== 0x0E
  2047. && bite !== 0x0F && bite !== 0x1B) {
  2048. // Unset the iso-2022-jp output flag and return a code point
  2049. // whose value is byte.
  2050. iso2022jp_output_flag = false;
  2051. return bite;
  2052. }
  2053. // end-of-stream
  2054. if (bite === end_of_stream) {
  2055. // Return finished.
  2056. return finished;
  2057. }
  2058. // Otherwise
  2059. // Unset the iso-2022-jp output flag and return error.
  2060. iso2022jp_output_flag = false;
  2061. return decoderError(fatal);
  2062. case states.Roman:
  2063. // Roman
  2064. // Based on byte:
  2065. // 0x1B
  2066. if (bite === 0x1B) {
  2067. // Set iso-2022-jp decoder state to escape start and return
  2068. // continue.
  2069. iso2022jp_decoder_state = states.EscapeStart;
  2070. return null;
  2071. }
  2072. // 0x5C
  2073. if (bite === 0x5C) {
  2074. // Unset the iso-2022-jp output flag and return code point
  2075. // U+00A5.
  2076. iso2022jp_output_flag = false;
  2077. return 0x00A5;
  2078. }
  2079. // 0x7E
  2080. if (bite === 0x7E) {
  2081. // Unset the iso-2022-jp output flag and return code point
  2082. // U+203E.
  2083. iso2022jp_output_flag = false;
  2084. return 0x203E;
  2085. }
  2086. // 0x00 to 0x7F, excluding 0x0E, 0x0F, 0x1B, 0x5C, and 0x7E
  2087. if (inRange(bite, 0x00, 0x7F) && bite !== 0x0E && bite !== 0x0F
  2088. && bite !== 0x1B && bite !== 0x5C && bite !== 0x7E) {
  2089. // Unset the iso-2022-jp output flag and return a code point
  2090. // whose value is byte.
  2091. iso2022jp_output_flag = false;
  2092. return bite;
  2093. }
  2094. // end-of-stream
  2095. if (bite === end_of_stream) {
  2096. // Return finished.
  2097. return finished;
  2098. }
  2099. // Otherwise
  2100. // Unset the iso-2022-jp output flag and return error.
  2101. iso2022jp_output_flag = false;
  2102. return decoderError(fatal);
  2103. case states.Katakana:
  2104. // Katakana
  2105. // Based on byte:
  2106. // 0x1B
  2107. if (bite === 0x1B) {
  2108. // Set iso-2022-jp decoder state to escape start and return
  2109. // continue.
  2110. iso2022jp_decoder_state = states.EscapeStart;
  2111. return null;
  2112. }
  2113. // 0x21 to 0x5F
  2114. if (inRange(bite, 0x21, 0x5F)) {
  2115. // Unset the iso-2022-jp output flag and return a code point
  2116. // whose value is 0xFF61 − 0x21 + byte.
  2117. iso2022jp_output_flag = false;
  2118. return 0xFF61 - 0x21 + bite;
  2119. }
  2120. // end-of-stream
  2121. if (bite === end_of_stream) {
  2122. // Return finished.
  2123. return finished;
  2124. }
  2125. // Otherwise
  2126. // Unset the iso-2022-jp output flag and return error.
  2127. iso2022jp_output_flag = false;
  2128. return decoderError(fatal);
  2129. case states.LeadByte:
  2130. // Lead byte
  2131. // Based on byte:
  2132. // 0x1B
  2133. if (bite === 0x1B) {
  2134. // Set iso-2022-jp decoder state to escape start and return
  2135. // continue.
  2136. iso2022jp_decoder_state = states.EscapeStart;
  2137. return null;
  2138. }
  2139. // 0x21 to 0x7E
  2140. if (inRange(bite, 0x21, 0x7E)) {
  2141. // Unset the iso-2022-jp output flag, set iso-2022-jp lead
  2142. // to byte, iso-2022-jp decoder state to trail byte, and
  2143. // return continue.
  2144. iso2022jp_output_flag = false;
  2145. iso2022jp_lead = bite;
  2146. iso2022jp_decoder_state = states.TrailByte;
  2147. return null;
  2148. }
  2149. // end-of-stream
  2150. if (bite === end_of_stream) {
  2151. // Return finished.
  2152. return finished;
  2153. }
  2154. // Otherwise
  2155. // Unset the iso-2022-jp output flag and return error.
  2156. iso2022jp_output_flag = false;
  2157. return decoderError(fatal);
  2158. case states.TrailByte:
  2159. // Trail byte
  2160. // Based on byte:
  2161. // 0x1B
  2162. if (bite === 0x1B) {
  2163. // Set iso-2022-jp decoder state to escape start and return
  2164. // continue.
  2165. iso2022jp_decoder_state = states.EscapeStart;
  2166. return decoderError(fatal);
  2167. }
  2168. // 0x21 to 0x7E
  2169. if (inRange(bite, 0x21, 0x7E)) {
  2170. // 1. Set the iso-2022-jp decoder state to lead byte.
  2171. iso2022jp_decoder_state = states.LeadByte;
  2172. // 2. Let pointer be (iso-2022-jp lead − 0x21) × 94 + byte − 0x21.
  2173. var pointer = (iso2022jp_lead - 0x21) * 94 + bite - 0x21;
  2174. // 3. Let code point be the index code point for pointer in
  2175. // index jis0208.
  2176. var code_point = indexCodePointFor(pointer, index('jis0208'));
  2177. // 4. If code point is null, return error.
  2178. if (code_point === null)
  2179. return decoderError(fatal);
  2180. // 5. Return a code point whose value is code point.
  2181. return code_point;
  2182. }
  2183. // end-of-stream
  2184. if (bite === end_of_stream) {
  2185. // Set the iso-2022-jp decoder state to lead byte, prepend
  2186. // byte to stream, and return error.
  2187. iso2022jp_decoder_state = states.LeadByte;
  2188. stream.prepend(bite);
  2189. return decoderError(fatal);
  2190. }
  2191. // Otherwise
  2192. // Set iso-2022-jp decoder state to lead byte and return
  2193. // error.
  2194. iso2022jp_decoder_state = states.LeadByte;
  2195. return decoderError(fatal);
  2196. case states.EscapeStart:
  2197. // Escape start
  2198. // 1. If byte is either 0x24 or 0x28, set iso-2022-jp lead to
  2199. // byte, iso-2022-jp decoder state to escape, and return
  2200. // continue.
  2201. if (bite === 0x24 || bite === 0x28) {
  2202. iso2022jp_lead = bite;
  2203. iso2022jp_decoder_state = states.Escape;
  2204. return null;
  2205. }
  2206. // 2. Prepend byte to stream.
  2207. stream.prepend(bite);
  2208. // 3. Unset the iso-2022-jp output flag, set iso-2022-jp
  2209. // decoder state to iso-2022-jp decoder output state, and
  2210. // return error.
  2211. iso2022jp_output_flag = false;
  2212. iso2022jp_decoder_state = iso2022jp_decoder_output_state;
  2213. return decoderError(fatal);
  2214. case states.Escape:
  2215. // Escape
  2216. // 1. Let lead be iso-2022-jp lead and set iso-2022-jp lead to
  2217. // 0x00.
  2218. var lead = iso2022jp_lead;
  2219. iso2022jp_lead = 0x00;
  2220. // 2. Let state be null.
  2221. var state = null;
  2222. // 3. If lead is 0x28 and byte is 0x42, set state to ASCII.
  2223. if (lead === 0x28 && bite === 0x42)
  2224. state = states.ASCII;
  2225. // 4. If lead is 0x28 and byte is 0x4A, set state to Roman.
  2226. if (lead === 0x28 && bite === 0x4A)
  2227. state = states.Roman;
  2228. // 5. If lead is 0x28 and byte is 0x49, set state to Katakana.
  2229. if (lead === 0x28 && bite === 0x49)
  2230. state = states.Katakana;
  2231. // 6. If lead is 0x24 and byte is either 0x40 or 0x42, set
  2232. // state to lead byte.
  2233. if (lead === 0x24 && (bite === 0x40 || bite === 0x42))
  2234. state = states.LeadByte;
  2235. // 7. If state is non-null, run these substeps:
  2236. if (state !== null) {
  2237. // 1. Set iso-2022-jp decoder state and iso-2022-jp decoder
  2238. // output state to states.
  2239. iso2022jp_decoder_state = iso2022jp_decoder_state = state;
  2240. // 2. Let output flag be the iso-2022-jp output flag.
  2241. var output_flag = iso2022jp_output_flag;
  2242. // 3. Set the iso-2022-jp output flag.
  2243. iso2022jp_output_flag = true;
  2244. // 4. Return continue, if output flag is unset, and error
  2245. // otherwise.
  2246. return !output_flag ? null : decoderError(fatal);
  2247. }
  2248. // 8. Prepend lead and byte to stream.
  2249. stream.prepend([lead, bite]);
  2250. // 9. Unset the iso-2022-jp output flag, set iso-2022-jp
  2251. // decoder state to iso-2022-jp decoder output state and
  2252. // return error.
  2253. iso2022jp_output_flag = false;
  2254. iso2022jp_decoder_state = iso2022jp_decoder_output_state;
  2255. return decoderError(fatal);
  2256. }
  2257. };
  2258. }
  2259. // 13.2.2 iso-2022-jp encoder
  2260. /**
  2261. * @constructor
  2262. * @implements {Encoder}
  2263. * @param {{fatal: boolean}} options
  2264. */
  2265. function ISO2022JPEncoder(options) {
  2266. var fatal = options.fatal;
  2267. // iso-2022-jp's encoder has an associated iso-2022-jp encoder
  2268. // state which is one of ASCII, Roman, and jis0208 (initially
  2269. // ASCII).
  2270. /** @enum */
  2271. var states = {
  2272. ASCII: 0,
  2273. Roman: 1,
  2274. jis0208: 2
  2275. };
  2276. var /** @type {number} */ iso2022jp_state = states.ASCII;
  2277. /**
  2278. * @param {Stream} stream Input stream.
  2279. * @param {number} code_point Next code point read from the stream.
  2280. * @return {(number|!Array.<number>)} Byte(s) to emit.
  2281. */
  2282. this.handler = function(stream, code_point) {
  2283. // 1. If code point is end-of-stream and iso-2022-jp encoder
  2284. // state is not ASCII, prepend code point to stream, set
  2285. // iso-2022-jp encoder state to ASCII, and return three bytes
  2286. // 0x1B 0x28 0x42.
  2287. if (code_point === end_of_stream &&
  2288. iso2022jp_state !== states.ASCII) {
  2289. stream.prepend(code_point);
  2290. iso2022jp_state = states.ASCII;
  2291. return [0x1B, 0x28, 0x42];
  2292. }
  2293. // 2. If code point is end-of-stream and iso-2022-jp encoder
  2294. // state is ASCII, return finished.
  2295. if (code_point === end_of_stream && iso2022jp_state === states.ASCII)
  2296. return finished;
  2297. // 3. If ISO-2022-JP encoder state is ASCII or Roman, and code
  2298. // point is U+000E, U+000F, or U+001B, return error with U+FFFD.
  2299. if ((iso2022jp_state === states.ASCII ||
  2300. iso2022jp_state === states.Roman) &&
  2301. (code_point === 0x000E || code_point === 0x000F ||
  2302. code_point === 0x001B)) {
  2303. return encoderError(0xFFFD);
  2304. }
  2305. // 4. If iso-2022-jp encoder state is ASCII and code point is an
  2306. // ASCII code point, return a byte whose value is code point.
  2307. if (iso2022jp_state === states.ASCII &&
  2308. isASCIICodePoint(code_point))
  2309. return code_point;
  2310. // 5. If iso-2022-jp encoder state is Roman and code point is an
  2311. // ASCII code point, excluding U+005C and U+007E, or is U+00A5
  2312. // or U+203E, run these substeps:
  2313. if (iso2022jp_state === states.Roman &&
  2314. ((isASCIICodePoint(code_point) &&
  2315. code_point !== 0x005C && code_point !== 0x007E) ||
  2316. (code_point == 0x00A5 || code_point == 0x203E))) {
  2317. // 1. If code point is an ASCII code point, return a byte
  2318. // whose value is code point.
  2319. if (isASCIICodePoint(code_point))
  2320. return code_point;
  2321. // 2. If code point is U+00A5, return byte 0x5C.
  2322. if (code_point === 0x00A5)
  2323. return 0x5C;
  2324. // 3. If code point is U+203E, return byte 0x7E.
  2325. if (code_point === 0x203E)
  2326. return 0x7E;
  2327. }
  2328. // 6. If code point is an ASCII code point, and iso-2022-jp
  2329. // encoder state is not ASCII, prepend code point to stream, set
  2330. // iso-2022-jp encoder state to ASCII, and return three bytes
  2331. // 0x1B 0x28 0x42.
  2332. if (isASCIICodePoint(code_point) &&
  2333. iso2022jp_state !== states.ASCII) {
  2334. stream.prepend(code_point);
  2335. iso2022jp_state = states.ASCII;
  2336. return [0x1B, 0x28, 0x42];
  2337. }
  2338. // 7. If code point is either U+00A5 or U+203E, and iso-2022-jp
  2339. // encoder state is not Roman, prepend code point to stream, set
  2340. // iso-2022-jp encoder state to Roman, and return three bytes
  2341. // 0x1B 0x28 0x4A.
  2342. if ((code_point === 0x00A5 || code_point === 0x203E) &&
  2343. iso2022jp_state !== states.Roman) {
  2344. stream.prepend(code_point);
  2345. iso2022jp_state = states.Roman;
  2346. return [0x1B, 0x28, 0x4A];
  2347. }
  2348. // 8. If code point is U+2212, set it to U+FF0D.
  2349. if (code_point === 0x2212)
  2350. code_point = 0xFF0D;
  2351. // 9. Let pointer be the index pointer for code point in index
  2352. // jis0208.
  2353. var pointer = indexPointerFor(code_point, index('jis0208'));
  2354. // 10. If pointer is null, return error with code point.
  2355. if (pointer === null)
  2356. return encoderError(code_point);
  2357. // 11. If iso-2022-jp encoder state is not jis0208, prepend code
  2358. // point to stream, set iso-2022-jp encoder state to jis0208,
  2359. // and return three bytes 0x1B 0x24 0x42.
  2360. if (iso2022jp_state !== states.jis0208) {
  2361. stream.prepend(code_point);
  2362. iso2022jp_state = states.jis0208;
  2363. return [0x1B, 0x24, 0x42];
  2364. }
  2365. // 12. Let lead be floor(pointer / 94) + 0x21.
  2366. var lead = floor(pointer / 94) + 0x21;
  2367. // 13. Let trail be pointer % 94 + 0x21.
  2368. var trail = pointer % 94 + 0x21;
  2369. // 14. Return two bytes whose values are lead and trail.
  2370. return [lead, trail];
  2371. };
  2372. }
  2373. /** @param {{fatal: boolean}} options */
  2374. encoders['ISO-2022-JP'] = function(options) {
  2375. return new ISO2022JPEncoder(options);
  2376. };
  2377. /** @param {{fatal: boolean}} options */
  2378. decoders['ISO-2022-JP'] = function(options) {
  2379. return new ISO2022JPDecoder(options);
  2380. };
  2381. // 13.3 Shift_JIS
  2382. // 13.3.1 Shift_JIS decoder
  2383. /**
  2384. * @constructor
  2385. * @implements {Decoder}
  2386. * @param {{fatal: boolean}} options
  2387. */
  2388. function ShiftJISDecoder(options) {
  2389. var fatal = options.fatal;
  2390. // Shift_JIS's decoder has an associated Shift_JIS lead (initially
  2391. // 0x00).
  2392. var /** @type {number} */ Shift_JIS_lead = 0x00;
  2393. /**
  2394. * @param {Stream} stream The stream of bytes being decoded.
  2395. * @param {number} bite The next byte read from the stream.
  2396. * @return {?(number|!Array.<number>)} The next code point(s)
  2397. * decoded, or null if not enough data exists in the input
  2398. * stream to decode a complete code point.
  2399. */
  2400. this.handler = function(stream, bite) {
  2401. // 1. If byte is end-of-stream and Shift_JIS lead is not 0x00,
  2402. // set Shift_JIS lead to 0x00 and return error.
  2403. if (bite === end_of_stream && Shift_JIS_lead !== 0x00) {
  2404. Shift_JIS_lead = 0x00;
  2405. return decoderError(fatal);
  2406. }
  2407. // 2. If byte is end-of-stream and Shift_JIS lead is 0x00,
  2408. // return finished.
  2409. if (bite === end_of_stream && Shift_JIS_lead === 0x00)
  2410. return finished;
  2411. // 3. If Shift_JIS lead is not 0x00, let lead be Shift_JIS lead,
  2412. // let pointer be null, set Shift_JIS lead to 0x00, and then run
  2413. // these substeps:
  2414. if (Shift_JIS_lead !== 0x00) {
  2415. var lead = Shift_JIS_lead;
  2416. var pointer = null;
  2417. Shift_JIS_lead = 0x00;
  2418. // 1. Let offset be 0x40, if byte is less than 0x7F, and 0x41
  2419. // otherwise.
  2420. var offset = (bite < 0x7F) ? 0x40 : 0x41;
  2421. // 2. Let lead offset be 0x81, if lead is less than 0xA0, and
  2422. // 0xC1 otherwise.
  2423. var lead_offset = (lead < 0xA0) ? 0x81 : 0xC1;
  2424. // 3. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
  2425. // to 0xFC, inclusive, set pointer to (lead − lead offset) ×
  2426. // 188 + byte − offset.
  2427. if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFC))
  2428. pointer = (lead - lead_offset) * 188 + bite - offset;
  2429. // 4. If pointer is in the range 8836 to 10715, inclusive,
  2430. // return a code point whose value is 0xE000 − 8836 + pointer.
  2431. if (inRange(pointer, 8836, 10715))
  2432. return 0xE000 - 8836 + pointer;
  2433. // 5. Let code point be null, if pointer is null, and the
  2434. // index code point for pointer in index jis0208 otherwise.
  2435. var code_point = (pointer === null) ? null :
  2436. indexCodePointFor(pointer, index('jis0208'));
  2437. // 6. If code point is null and byte is an ASCII byte, prepend
  2438. // byte to stream.
  2439. if (code_point === null && isASCIIByte(bite))
  2440. stream.prepend(bite);
  2441. // 7. If code point is null, return error.
  2442. if (code_point === null)
  2443. return decoderError(fatal);
  2444. // 8. Return a code point whose value is code point.
  2445. return code_point;
  2446. }
  2447. // 4. If byte is an ASCII byte or 0x80, return a code point
  2448. // whose value is byte.
  2449. if (isASCIIByte(bite) || bite === 0x80)
  2450. return bite;
  2451. // 5. If byte is in the range 0xA1 to 0xDF, inclusive, return a
  2452. // code point whose value is 0xFF61 − 0xA1 + byte.
  2453. if (inRange(bite, 0xA1, 0xDF))
  2454. return 0xFF61 - 0xA1 + bite;
  2455. // 6. If byte is in the range 0x81 to 0x9F, inclusive, or 0xE0
  2456. // to 0xFC, inclusive, set Shift_JIS lead to byte and return
  2457. // continue.
  2458. if (inRange(bite, 0x81, 0x9F) || inRange(bite, 0xE0, 0xFC)) {
  2459. Shift_JIS_lead = bite;
  2460. return null;
  2461. }
  2462. // 7. Return error.
  2463. return decoderError(fatal);
  2464. };
  2465. }
  2466. // 13.3.2 Shift_JIS encoder
  2467. /**
  2468. * @constructor
  2469. * @implements {Encoder}
  2470. * @param {{fatal: boolean}} options
  2471. */
  2472. function ShiftJISEncoder(options) {
  2473. var fatal = options.fatal;
  2474. /**
  2475. * @param {Stream} stream Input stream.
  2476. * @param {number} code_point Next code point read from the stream.
  2477. * @return {(number|!Array.<number>)} Byte(s) to emit.
  2478. */
  2479. this.handler = function(stream, code_point) {
  2480. // 1. If code point is end-of-stream, return finished.
  2481. if (code_point === end_of_stream)
  2482. return finished;
  2483. // 2. If code point is an ASCII code point or U+0080, return a
  2484. // byte whose value is code point.
  2485. if (isASCIICodePoint(code_point) || code_point === 0x0080)
  2486. return code_point;
  2487. // 3. If code point is U+00A5, return byte 0x5C.
  2488. if (code_point === 0x00A5)
  2489. return 0x5C;
  2490. // 4. If code point is U+203E, return byte 0x7E.
  2491. if (code_point === 0x203E)
  2492. return 0x7E;
  2493. // 5. If code point is in the range U+FF61 to U+FF9F, inclusive,
  2494. // return a byte whose value is code point − 0xFF61 + 0xA1.
  2495. if (inRange(code_point, 0xFF61, 0xFF9F))
  2496. return code_point - 0xFF61 + 0xA1;
  2497. // 6. If code point is U+2212, set it to U+FF0D.
  2498. if (code_point === 0x2212)
  2499. code_point = 0xFF0D;
  2500. // 7. Let pointer be the index Shift_JIS pointer for code point.
  2501. var pointer = indexShiftJISPointerFor(code_point);
  2502. // 8. If pointer is null, return error with code point.
  2503. if (pointer === null)
  2504. return encoderError(code_point);
  2505. // 9. Let lead be floor(pointer / 188).
  2506. var lead = floor(pointer / 188);
  2507. // 10. Let lead offset be 0x81, if lead is less than 0x1F, and
  2508. // 0xC1 otherwise.
  2509. var lead_offset = (lead < 0x1F) ? 0x81 : 0xC1;
  2510. // 11. Let trail be pointer % 188.
  2511. var trail = pointer % 188;
  2512. // 12. Let offset be 0x40, if trail is less than 0x3F, and 0x41
  2513. // otherwise.
  2514. var offset = (trail < 0x3F) ? 0x40 : 0x41;
  2515. // 13. Return two bytes whose values are lead + lead offset and
  2516. // trail + offset.
  2517. return [lead + lead_offset, trail + offset];
  2518. };
  2519. }
  2520. /** @param {{fatal: boolean}} options */
  2521. encoders['Shift_JIS'] = function(options) {
  2522. return new ShiftJISEncoder(options);
  2523. };
  2524. /** @param {{fatal: boolean}} options */
  2525. decoders['Shift_JIS'] = function(options) {
  2526. return new ShiftJISDecoder(options);
  2527. };
  2528. //
  2529. // 14. Legacy multi-byte Korean encodings
  2530. //
  2531. // 14.1 euc-kr
  2532. // 14.1.1 euc-kr decoder
  2533. /**
  2534. * @constructor
  2535. * @implements {Decoder}
  2536. * @param {{fatal: boolean}} options
  2537. */
  2538. function EUCKRDecoder(options) {
  2539. var fatal = options.fatal;
  2540. // euc-kr's decoder has an associated euc-kr lead (initially 0x00).
  2541. var /** @type {number} */ euckr_lead = 0x00;
  2542. /**
  2543. * @param {Stream} stream The stream of bytes being decoded.
  2544. * @param {number} bite The next byte read from the stream.
  2545. * @return {?(number|!Array.<number>)} The next code point(s)
  2546. * decoded, or null if not enough data exists in the input
  2547. * stream to decode a complete code point.
  2548. */
  2549. this.handler = function(stream, bite) {
  2550. // 1. If byte is end-of-stream and euc-kr lead is not 0x00, set
  2551. // euc-kr lead to 0x00 and return error.
  2552. if (bite === end_of_stream && euckr_lead !== 0) {
  2553. euckr_lead = 0x00;
  2554. return decoderError(fatal);
  2555. }
  2556. // 2. If byte is end-of-stream and euc-kr lead is 0x00, return
  2557. // finished.
  2558. if (bite === end_of_stream && euckr_lead === 0)
  2559. return finished;
  2560. // 3. If euc-kr lead is not 0x00, let lead be euc-kr lead, let
  2561. // pointer be null, set euc-kr lead to 0x00, and then run these
  2562. // substeps:
  2563. if (euckr_lead !== 0x00) {
  2564. var lead = euckr_lead;
  2565. var pointer = null;
  2566. euckr_lead = 0x00;
  2567. // 1. If byte is in the range 0x41 to 0xFE, inclusive, set
  2568. // pointer to (lead − 0x81) × 190 + (byte − 0x41).
  2569. if (inRange(bite, 0x41, 0xFE))
  2570. pointer = (lead - 0x81) * 190 + (bite - 0x41);
  2571. // 2. Let code point be null, if pointer is null, and the
  2572. // index code point for pointer in index euc-kr otherwise.
  2573. var code_point = (pointer === null)
  2574. ? null : indexCodePointFor(pointer, index('euc-kr'));
  2575. // 3. If code point is null and byte is an ASCII byte, prepend
  2576. // byte to stream.
  2577. if (pointer === null && isASCIIByte(bite))
  2578. stream.prepend(bite);
  2579. // 4. If code point is null, return error.
  2580. if (code_point === null)
  2581. return decoderError(fatal);
  2582. // 5. Return a code point whose value is code point.
  2583. return code_point;
  2584. }
  2585. // 4. If byte is an ASCII byte, return a code point whose value
  2586. // is byte.
  2587. if (isASCIIByte(bite))
  2588. return bite;
  2589. // 5. If byte is in the range 0x81 to 0xFE, inclusive, set
  2590. // euc-kr lead to byte and return continue.
  2591. if (inRange(bite, 0x81, 0xFE)) {
  2592. euckr_lead = bite;
  2593. return null;
  2594. }
  2595. // 6. Return error.
  2596. return decoderError(fatal);
  2597. };
  2598. }
  2599. // 14.1.2 euc-kr encoder
  2600. /**
  2601. * @constructor
  2602. * @implements {Encoder}
  2603. * @param {{fatal: boolean}} options
  2604. */
  2605. function EUCKREncoder(options) {
  2606. var fatal = options.fatal;
  2607. /**
  2608. * @param {Stream} stream Input stream.
  2609. * @param {number} code_point Next code point read from the stream.
  2610. * @return {(number|!Array.<number>)} Byte(s) to emit.
  2611. */
  2612. this.handler = function(stream, code_point) {
  2613. // 1. If code point is end-of-stream, return finished.
  2614. if (code_point === end_of_stream)
  2615. return finished;
  2616. // 2. If code point is an ASCII code point, return a byte whose
  2617. // value is code point.
  2618. if (isASCIICodePoint(code_point))
  2619. return code_point;
  2620. // 3. Let pointer be the index pointer for code point in index
  2621. // euc-kr.
  2622. var pointer = indexPointerFor(code_point, index('euc-kr'));
  2623. // 4. If pointer is null, return error with code point.
  2624. if (pointer === null)
  2625. return encoderError(code_point);
  2626. // 5. Let lead be floor(pointer / 190) + 0x81.
  2627. var lead = floor(pointer / 190) + 0x81;
  2628. // 6. Let trail be pointer % 190 + 0x41.
  2629. var trail = (pointer % 190) + 0x41;
  2630. // 7. Return two bytes whose values are lead and trail.
  2631. return [lead, trail];
  2632. };
  2633. }
  2634. /** @param {{fatal: boolean}} options */
  2635. encoders['EUC-KR'] = function(options) {
  2636. return new EUCKREncoder(options);
  2637. };
  2638. /** @param {{fatal: boolean}} options */
  2639. decoders['EUC-KR'] = function(options) {
  2640. return new EUCKRDecoder(options);
  2641. };
  2642. //
  2643. // 15. Legacy miscellaneous encodings
  2644. //
  2645. // 15.1 replacement
  2646. // Not needed - API throws RangeError
  2647. // 15.2 Common infrastructure for utf-16be and utf-16le
  2648. /**
  2649. * @param {number} code_unit
  2650. * @param {boolean} utf16be
  2651. * @return {!Array.<number>} bytes
  2652. */
  2653. function convertCodeUnitToBytes(code_unit, utf16be) {
  2654. // 1. Let byte1 be code unit >> 8.
  2655. var byte1 = code_unit >> 8;
  2656. // 2. Let byte2 be code unit & 0x00FF.
  2657. var byte2 = code_unit & 0x00FF;
  2658. // 3. Then return the bytes in order:
  2659. // utf-16be flag is set: byte1, then byte2.
  2660. if (utf16be)
  2661. return [byte1, byte2];
  2662. // utf-16be flag is unset: byte2, then byte1.
  2663. return [byte2, byte1];
  2664. }
  2665. // 15.2.1 shared utf-16 decoder
  2666. /**
  2667. * @constructor
  2668. * @implements {Decoder}
  2669. * @param {boolean} utf16_be True if big-endian, false if little-endian.
  2670. * @param {{fatal: boolean}} options
  2671. */
  2672. function UTF16Decoder(utf16_be, options) {
  2673. var fatal = options.fatal;
  2674. var /** @type {?number} */ utf16_lead_byte = null,
  2675. /** @type {?number} */ utf16_lead_surrogate = null;
  2676. /**
  2677. * @param {Stream} stream The stream of bytes being decoded.
  2678. * @param {number} bite The next byte read from the stream.
  2679. * @return {?(number|!Array.<number>)} The next code point(s)
  2680. * decoded, or null if not enough data exists in the input
  2681. * stream to decode a complete code point.
  2682. */
  2683. this.handler = function(stream, bite) {
  2684. // 1. If byte is end-of-stream and either utf-16 lead byte or
  2685. // utf-16 lead surrogate is not null, set utf-16 lead byte and
  2686. // utf-16 lead surrogate to null, and return error.
  2687. if (bite === end_of_stream && (utf16_lead_byte !== null ||
  2688. utf16_lead_surrogate !== null)) {
  2689. return decoderError(fatal);
  2690. }
  2691. // 2. If byte is end-of-stream and utf-16 lead byte and utf-16
  2692. // lead surrogate are null, return finished.
  2693. if (bite === end_of_stream && utf16_lead_byte === null &&
  2694. utf16_lead_surrogate === null) {
  2695. return finished;
  2696. }
  2697. // 3. If utf-16 lead byte is null, set utf-16 lead byte to byte
  2698. // and return continue.
  2699. if (utf16_lead_byte === null) {
  2700. utf16_lead_byte = bite;
  2701. return null;
  2702. }
  2703. // 4. Let code unit be the result of:
  2704. var code_unit;
  2705. if (utf16_be) {
  2706. // utf-16be decoder flag is set
  2707. // (utf-16 lead byte << 8) + byte.
  2708. code_unit = (utf16_lead_byte << 8) + bite;
  2709. } else {
  2710. // utf-16be decoder flag is unset
  2711. // (byte << 8) + utf-16 lead byte.
  2712. code_unit = (bite << 8) + utf16_lead_byte;
  2713. }
  2714. // Then set utf-16 lead byte to null.
  2715. utf16_lead_byte = null;
  2716. // 5. If utf-16 lead surrogate is not null, let lead surrogate
  2717. // be utf-16 lead surrogate, set utf-16 lead surrogate to null,
  2718. // and then run these substeps:
  2719. if (utf16_lead_surrogate !== null) {
  2720. var lead_surrogate = utf16_lead_surrogate;
  2721. utf16_lead_surrogate = null;
  2722. // 1. If code unit is in the range U+DC00 to U+DFFF,
  2723. // inclusive, return a code point whose value is 0x10000 +
  2724. // ((lead surrogate − 0xD800) << 10) + (code unit − 0xDC00).
  2725. if (inRange(code_unit, 0xDC00, 0xDFFF)) {
  2726. return 0x10000 + (lead_surrogate - 0xD800) * 0x400 +
  2727. (code_unit - 0xDC00);
  2728. }
  2729. // 2. Prepend the sequence resulting of converting code unit
  2730. // to bytes using utf-16be decoder flag to stream and return
  2731. // error.
  2732. stream.prepend(convertCodeUnitToBytes(code_unit, utf16_be));
  2733. return decoderError(fatal);
  2734. }
  2735. // 6. If code unit is in the range U+D800 to U+DBFF, inclusive,
  2736. // set utf-16 lead surrogate to code unit and return continue.
  2737. if (inRange(code_unit, 0xD800, 0xDBFF)) {
  2738. utf16_lead_surrogate = code_unit;
  2739. return null;
  2740. }
  2741. // 7. If code unit is in the range U+DC00 to U+DFFF, inclusive,
  2742. // return error.
  2743. if (inRange(code_unit, 0xDC00, 0xDFFF))
  2744. return decoderError(fatal);
  2745. // 8. Return code point code unit.
  2746. return code_unit;
  2747. };
  2748. }
  2749. // 15.2.2 shared utf-16 encoder
  2750. /**
  2751. * @constructor
  2752. * @implements {Encoder}
  2753. * @param {boolean} utf16_be True if big-endian, false if little-endian.
  2754. * @param {{fatal: boolean}} options
  2755. */
  2756. function UTF16Encoder(utf16_be, options) {
  2757. var fatal = options.fatal;
  2758. /**
  2759. * @param {Stream} stream Input stream.
  2760. * @param {number} code_point Next code point read from the stream.
  2761. * @return {(number|!Array.<number>)} Byte(s) to emit.
  2762. */
  2763. this.handler = function(stream, code_point) {
  2764. // 1. If code point is end-of-stream, return finished.
  2765. if (code_point === end_of_stream)
  2766. return finished;
  2767. // 2. If code point is in the range U+0000 to U+FFFF, inclusive,
  2768. // return the sequence resulting of converting code point to
  2769. // bytes using utf-16be encoder flag.
  2770. if (inRange(code_point, 0x0000, 0xFFFF))
  2771. return convertCodeUnitToBytes(code_point, utf16_be);
  2772. // 3. Let lead be ((code point − 0x10000) >> 10) + 0xD800,
  2773. // converted to bytes using utf-16be encoder flag.
  2774. var lead = convertCodeUnitToBytes(
  2775. ((code_point - 0x10000) >> 10) + 0xD800, utf16_be);
  2776. // 4. Let trail be ((code point − 0x10000) & 0x3FF) + 0xDC00,
  2777. // converted to bytes using utf-16be encoder flag.
  2778. var trail = convertCodeUnitToBytes(
  2779. ((code_point - 0x10000) & 0x3FF) + 0xDC00, utf16_be);
  2780. // 5. Return a byte sequence of lead followed by trail.
  2781. return lead.concat(trail);
  2782. };
  2783. }
  2784. // 15.3 utf-16be
  2785. // 15.3.1 utf-16be decoder
  2786. /** @param {{fatal: boolean}} options */
  2787. encoders['UTF-16BE'] = function(options) {
  2788. return new UTF16Encoder(true, options);
  2789. };
  2790. // 15.3.2 utf-16be encoder
  2791. /** @param {{fatal: boolean}} options */
  2792. decoders['UTF-16BE'] = function(options) {
  2793. return new UTF16Decoder(true, options);
  2794. };
  2795. // 15.4 utf-16le
  2796. // 15.4.1 utf-16le decoder
  2797. /** @param {{fatal: boolean}} options */
  2798. encoders['UTF-16LE'] = function(options) {
  2799. return new UTF16Encoder(false, options);
  2800. };
  2801. // 15.4.2 utf-16le encoder
  2802. /** @param {{fatal: boolean}} options */
  2803. decoders['UTF-16LE'] = function(options) {
  2804. return new UTF16Decoder(false, options);
  2805. };
  2806. // 15.5 x-user-defined
  2807. // 15.5.1 x-user-defined decoder
  2808. /**
  2809. * @constructor
  2810. * @implements {Decoder}
  2811. * @param {{fatal: boolean}} options
  2812. */
  2813. function XUserDefinedDecoder(options) {
  2814. var fatal = options.fatal;
  2815. /**
  2816. * @param {Stream} stream The stream of bytes being decoded.
  2817. * @param {number} bite The next byte read from the stream.
  2818. * @return {?(number|!Array.<number>)} The next code point(s)
  2819. * decoded, or null if not enough data exists in the input
  2820. * stream to decode a complete code point.
  2821. */
  2822. this.handler = function(stream, bite) {
  2823. // 1. If byte is end-of-stream, return finished.
  2824. if (bite === end_of_stream)
  2825. return finished;
  2826. // 2. If byte is an ASCII byte, return a code point whose value
  2827. // is byte.
  2828. if (isASCIIByte(bite))
  2829. return bite;
  2830. // 3. Return a code point whose value is 0xF780 + byte − 0x80.
  2831. return 0xF780 + bite - 0x80;
  2832. };
  2833. }
  2834. // 15.5.2 x-user-defined encoder
  2835. /**
  2836. * @constructor
  2837. * @implements {Encoder}
  2838. * @param {{fatal: boolean}} options
  2839. */
  2840. function XUserDefinedEncoder(options) {
  2841. var fatal = options.fatal;
  2842. /**
  2843. * @param {Stream} stream Input stream.
  2844. * @param {number} code_point Next code point read from the stream.
  2845. * @return {(number|!Array.<number>)} Byte(s) to emit.
  2846. */
  2847. this.handler = function(stream, code_point) {
  2848. // 1.If code point is end-of-stream, return finished.
  2849. if (code_point === end_of_stream)
  2850. return finished;
  2851. // 2. If code point is an ASCII code point, return a byte whose
  2852. // value is code point.
  2853. if (isASCIICodePoint(code_point))
  2854. return code_point;
  2855. // 3. If code point is in the range U+F780 to U+F7FF, inclusive,
  2856. // return a byte whose value is code point − 0xF780 + 0x80.
  2857. if (inRange(code_point, 0xF780, 0xF7FF))
  2858. return code_point - 0xF780 + 0x80;
  2859. // 4. Return error with code point.
  2860. return encoderError(code_point);
  2861. };
  2862. }
  2863. /** @param {{fatal: boolean}} options */
  2864. encoders['x-user-defined'] = function(options) {
  2865. return new XUserDefinedEncoder(options);
  2866. };
  2867. /** @param {{fatal: boolean}} options */
  2868. decoders['x-user-defined'] = function(options) {
  2869. return new XUserDefinedDecoder(options);
  2870. };
  2871. if (!global['TextEncoder'])
  2872. global['TextEncoder'] = TextEncoder;
  2873. if (!global['TextDecoder'])
  2874. global['TextDecoder'] = TextDecoder;
  2875. if (typeof module !== "undefined" && module.exports) {
  2876. module.exports = {
  2877. TextEncoder: global['TextEncoder'],
  2878. TextDecoder: global['TextDecoder'],
  2879. EncodingIndexes: global["encoding-indexes"]
  2880. };
  2881. }
  2882. }(this));