encoding.js 99 KB

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