ailink ble wifi 小程序配网演示demo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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