Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

encoding.js 99KB

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