encoding.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420
  1. // If we're in node require encoding-indexes and attach it to the global.
  2. if (typeof module !== "undefined" && module.exports) {
  3. this["encoding-indexes"] = require("./encoding-indexes.js")["encoding-indexes"];
  4. }
  5. (function(global) {
  6. 'use strict';
  7. //
  8. // Utilities
  9. //
  10. /**
  11. * @param {number} a The number to test.
  12. * @param {number} min The minimum value in the range, inclusive.
  13. * @param {number} max The maximum value in the range, inclusive.
  14. * @return {boolean} True if a >= min and a <= max.
  15. */
  16. function inRange(a, min, max) {
  17. return min <= a && a <= max;
  18. }
  19. /**
  20. * @param {number} n The numerator.
  21. * @param {number} d The denominator.
  22. * @return {number} The result of the integer division of n by d.
  23. */
  24. function div(n, d) {
  25. return Math.floor(n / d);
  26. }
  27. //
  28. // Implementation of Encoding specification
  29. // http://dvcs.w3.org/hg/encoding/raw-file/tip/Overview.html
  30. //
  31. //
  32. // 3. Terminology
  33. //
  34. //
  35. // 4. Encodings
  36. //
  37. /** @const */ var EOF_byte = -1;
  38. /** @const */ var EOF_code_point = -1;
  39. /**
  40. * @constructor
  41. * @param {Uint8Array} bytes Array of bytes that provide the stream.
  42. */
  43. function ByteInputStream(bytes) {
  44. /** @type {number} */
  45. var pos = 0;
  46. /**
  47. * @this {ByteInputStream}
  48. * @return {number} Get the next byte from the stream.
  49. */
  50. this.get = function() {
  51. return (pos >= bytes.length) ? EOF_byte : Number(bytes[pos]);
  52. };
  53. /** @param {number} n Number (positive or negative) by which to
  54. * offset the byte pointer. */
  55. this.offset = function(n) {
  56. pos += n;
  57. if (pos < 0) {
  58. throw new Error('Seeking past start of the buffer');
  59. }
  60. if (pos > bytes.length) {
  61. throw new Error('Seeking past EOF');
  62. }
  63. };
  64. /**
  65. * @param {Array.<number>} test Array of bytes to compare against.
  66. * @return {boolean} True if the start of the stream matches the test
  67. * bytes.
  68. */
  69. this.match = function(test) {
  70. if (test.length > pos + bytes.length) {
  71. return false;
  72. }
  73. var i;
  74. for (i = 0; i < test.length; i += 1) {
  75. if (Number(bytes[pos + i]) !== test[i]) {
  76. return false;
  77. }
  78. }
  79. return true;
  80. };
  81. }
  82. /**
  83. * @constructor
  84. * @param {Array.<number>} bytes The array to write bytes into.
  85. */
  86. function ByteOutputStream(bytes) {
  87. /** @type {number} */
  88. var pos = 0;
  89. /**
  90. * @param {...number} var_args The byte or bytes to emit into the stream.
  91. * @return {number} The last byte emitted.
  92. */
  93. this.emit = function(var_args) {
  94. /** @type {number} */
  95. var last = EOF_byte;
  96. var i;
  97. for (i = 0; i < arguments.length; ++i) {
  98. last = Number(arguments[i]);
  99. bytes[pos++] = last;
  100. }
  101. return last;
  102. };
  103. }
  104. /**
  105. * @constructor
  106. * @param {string} string The source of code units for the stream.
  107. */
  108. function CodePointInputStream(string) {
  109. /**
  110. * @param {string} string Input string of UTF-16 code units.
  111. * @return {Array.<number>} Code points.
  112. */
  113. function stringToCodePoints(string) {
  114. /** @type {Array.<number>} */
  115. var cps = [];
  116. // Based on http://www.w3.org/TR/WebIDL/#idl-DOMString
  117. var i = 0, n = string.length;
  118. while (i < string.length) {
  119. var c = string.charCodeAt(i);
  120. if (!inRange(c, 0xD800, 0xDFFF)) {
  121. cps.push(c);
  122. } else if (inRange(c, 0xDC00, 0xDFFF)) {
  123. cps.push(0xFFFD);
  124. } else { // (inRange(cu, 0xD800, 0xDBFF))
  125. if (i === n - 1) {
  126. cps.push(0xFFFD);
  127. } else {
  128. var d = string.charCodeAt(i + 1);
  129. if (inRange(d, 0xDC00, 0xDFFF)) {
  130. var a = c & 0x3FF;
  131. var b = d & 0x3FF;
  132. i += 1;
  133. cps.push(0x10000 + (a << 10) + b);
  134. } else {
  135. cps.push(0xFFFD);
  136. }
  137. }
  138. }
  139. i += 1;
  140. }
  141. return cps;
  142. }
  143. /** @type {number} */
  144. var pos = 0;
  145. /** @type {Array.<number>} */
  146. var cps = stringToCodePoints(string);
  147. /** @param {number} n The number of bytes (positive or negative)
  148. * to advance the code point pointer by.*/
  149. this.offset = function(n) {
  150. pos += n;
  151. if (pos < 0) {
  152. throw new Error('Seeking past start of the buffer');
  153. }
  154. if (pos > cps.length) {
  155. throw new Error('Seeking past EOF');
  156. }
  157. };
  158. /** @return {number} Get the next code point from the stream. */
  159. this.get = function() {
  160. if (pos >= cps.length) {
  161. return EOF_code_point;
  162. }
  163. return cps[pos];
  164. };
  165. }
  166. /**
  167. * @constructor
  168. */
  169. function CodePointOutputStream() {
  170. /** @type {string} */
  171. var string = '';
  172. /** @return {string} The accumulated string. */
  173. this.string = function() {
  174. return string;
  175. };
  176. /** @param {number} c The code point to encode into the stream. */
  177. this.emit = function(c) {
  178. if (c <= 0xFFFF) {
  179. string += String.fromCharCode(c);
  180. } else {
  181. c -= 0x10000;
  182. string += String.fromCharCode(0xD800 + ((c >> 10) & 0x3ff));
  183. string += String.fromCharCode(0xDC00 + (c & 0x3ff));
  184. }
  185. };
  186. }
  187. /**
  188. * @constructor
  189. * @param {string} message Description of the error.
  190. */
  191. function EncodingError(message) {
  192. this.name = 'EncodingError';
  193. this.message = message;
  194. this.code = 0;
  195. }
  196. EncodingError.prototype = Error.prototype;
  197. /**
  198. * @param {boolean} fatal If true, decoding errors raise an exception.
  199. * @param {number=} opt_code_point Override the standard fallback code point.
  200. * @return {number} The code point to insert on a decoding error.
  201. */
  202. function decoderError(fatal, opt_code_point) {
  203. if (fatal) {
  204. throw new EncodingError('Decoder error');
  205. }
  206. return opt_code_point || 0xFFFD;
  207. }
  208. /**
  209. * @param {number} code_point The code point that could not be encoded.
  210. * @return {number} Always throws, no value is actually returned.
  211. */
  212. function encoderError(code_point) {
  213. throw new EncodingError('The code point ' + code_point +
  214. ' could not be encoded.');
  215. }
  216. /**
  217. * @param {string} label The encoding label.
  218. * @return {?{name:string,labels:Array.<string>}}
  219. */
  220. function getEncoding(label) {
  221. label = String(label).trim().toLowerCase();
  222. if (Object.prototype.hasOwnProperty.call(label_to_encoding, label)) {
  223. return label_to_encoding[label];
  224. }
  225. return null;
  226. }
  227. /** @type {Array.<{encodings: Array.<{name:string,labels:Array.<string>}>,
  228. * heading: string}>} */
  229. var encodings = [
  230. {
  231. "encodings": [
  232. {
  233. "labels": [
  234. "unicode-1-1-utf-8",
  235. "utf-8",
  236. "utf8"
  237. ],
  238. "name": "utf-8"
  239. }
  240. ],
  241. "heading": "The Encoding"
  242. },
  243. {
  244. "encodings": [
  245. {
  246. "labels": [
  247. "866",
  248. "cp866",
  249. "csibm866",
  250. "ibm866"
  251. ],
  252. "name": "ibm866"
  253. },
  254. {
  255. "labels": [
  256. "csisolatin2",
  257. "iso-8859-2",
  258. "iso-ir-101",
  259. "iso8859-2",
  260. "iso88592",
  261. "iso_8859-2",
  262. "iso_8859-2:1987",
  263. "l2",
  264. "latin2"
  265. ],
  266. "name": "iso-8859-2"
  267. },
  268. {
  269. "labels": [
  270. "csisolatin3",
  271. "iso-8859-3",
  272. "iso-ir-109",
  273. "iso8859-3",
  274. "iso88593",
  275. "iso_8859-3",
  276. "iso_8859-3:1988",
  277. "l3",
  278. "latin3"
  279. ],
  280. "name": "iso-8859-3"
  281. },
  282. {
  283. "labels": [
  284. "csisolatin4",
  285. "iso-8859-4",
  286. "iso-ir-110",
  287. "iso8859-4",
  288. "iso88594",
  289. "iso_8859-4",
  290. "iso_8859-4:1988",
  291. "l4",
  292. "latin4"
  293. ],
  294. "name": "iso-8859-4"
  295. },
  296. {
  297. "labels": [
  298. "csisolatincyrillic",
  299. "cyrillic",
  300. "iso-8859-5",
  301. "iso-ir-144",
  302. "iso8859-5",
  303. "iso88595",
  304. "iso_8859-5",
  305. "iso_8859-5:1988"
  306. ],
  307. "name": "iso-8859-5"
  308. },
  309. {
  310. "labels": [
  311. "arabic",
  312. "asmo-708",
  313. "csiso88596e",
  314. "csiso88596i",
  315. "csisolatinarabic",
  316. "ecma-114",
  317. "iso-8859-6",
  318. "iso-8859-6-e",
  319. "iso-8859-6-i",
  320. "iso-ir-127",
  321. "iso8859-6",
  322. "iso88596",
  323. "iso_8859-6",
  324. "iso_8859-6:1987"
  325. ],
  326. "name": "iso-8859-6"
  327. },
  328. {
  329. "labels": [
  330. "csisolatingreek",
  331. "ecma-118",
  332. "elot_928",
  333. "greek",
  334. "greek8",
  335. "iso-8859-7",
  336. "iso-ir-126",
  337. "iso8859-7",
  338. "iso88597",
  339. "iso_8859-7",
  340. "iso_8859-7:1987",
  341. "sun_eu_greek"
  342. ],
  343. "name": "iso-8859-7"
  344. },
  345. {
  346. "labels": [
  347. "csiso88598e",
  348. "csisolatinhebrew",
  349. "hebrew",
  350. "iso-8859-8",
  351. "iso-8859-8-e",
  352. "iso-ir-138",
  353. "iso8859-8",
  354. "iso88598",
  355. "iso_8859-8",
  356. "iso_8859-8:1988",
  357. "visual"
  358. ],
  359. "name": "iso-8859-8"
  360. },
  361. {
  362. "labels": [
  363. "csiso88598i",
  364. "iso-8859-8-i",
  365. "logical"
  366. ],
  367. "name": "iso-8859-8-i"
  368. },
  369. {
  370. "labels": [
  371. "csisolatin6",
  372. "iso-8859-10",
  373. "iso-ir-157",
  374. "iso8859-10",
  375. "iso885910",
  376. "l6",
  377. "latin6"
  378. ],
  379. "name": "iso-8859-10"
  380. },
  381. {
  382. "labels": [
  383. "iso-8859-13",
  384. "iso8859-13",
  385. "iso885913"
  386. ],
  387. "name": "iso-8859-13"
  388. },
  389. {
  390. "labels": [
  391. "iso-8859-14",
  392. "iso8859-14",
  393. "iso885914"
  394. ],
  395. "name": "iso-8859-14"
  396. },
  397. {
  398. "labels": [
  399. "csisolatin9",
  400. "iso-8859-15",
  401. "iso8859-15",
  402. "iso885915",
  403. "iso_8859-15",
  404. "l9"
  405. ],
  406. "name": "iso-8859-15"
  407. },
  408. {
  409. "labels": [
  410. "iso-8859-16"
  411. ],
  412. "name": "iso-8859-16"
  413. },
  414. {
  415. "labels": [
  416. "cskoi8r",
  417. "koi",
  418. "koi8",
  419. "koi8-r",
  420. "koi8_r"
  421. ],
  422. "name": "koi8-r"
  423. },
  424. {
  425. "labels": [
  426. "koi8-u"
  427. ],
  428. "name": "koi8-u"
  429. },
  430. {
  431. "labels": [
  432. "csmacintosh",
  433. "mac",
  434. "macintosh",
  435. "x-mac-roman"
  436. ],
  437. "name": "macintosh"
  438. },
  439. {
  440. "labels": [
  441. "dos-874",
  442. "iso-8859-11",
  443. "iso8859-11",
  444. "iso885911",
  445. "tis-620",
  446. "windows-874"
  447. ],
  448. "name": "windows-874"
  449. },
  450. {
  451. "labels": [
  452. "cp1250",
  453. "windows-1250",
  454. "x-cp1250"
  455. ],
  456. "name": "windows-1250"
  457. },
  458. {
  459. "labels": [
  460. "cp1251",
  461. "windows-1251",
  462. "x-cp1251"
  463. ],
  464. "name": "windows-1251"
  465. },
  466. {
  467. "labels": [
  468. "ansi_x3.4-1968",
  469. "ascii",
  470. "cp1252",
  471. "cp819",
  472. "csisolatin1",
  473. "ibm819",
  474. "iso-8859-1",
  475. "iso-ir-100",
  476. "iso8859-1",
  477. "iso88591",
  478. "iso_8859-1",
  479. "iso_8859-1:1987",
  480. "l1",
  481. "latin1",
  482. "us-ascii",
  483. "windows-1252",
  484. "x-cp1252"
  485. ],
  486. "name": "windows-1252"
  487. },
  488. {
  489. "labels": [
  490. "cp1253",
  491. "windows-1253",
  492. "x-cp1253"
  493. ],
  494. "name": "windows-1253"
  495. },
  496. {
  497. "labels": [
  498. "cp1254",
  499. "csisolatin5",
  500. "iso-8859-9",
  501. "iso-ir-148",
  502. "iso8859-9",
  503. "iso88599",
  504. "iso_8859-9",
  505. "iso_8859-9:1989",
  506. "l5",
  507. "latin5",
  508. "windows-1254",
  509. "x-cp1254"
  510. ],
  511. "name": "windows-1254"
  512. },
  513. {
  514. "labels": [
  515. "cp1255",
  516. "windows-1255",
  517. "x-cp1255"
  518. ],
  519. "name": "windows-1255"
  520. },
  521. {
  522. "labels": [
  523. "cp1256",
  524. "windows-1256",
  525. "x-cp1256"
  526. ],
  527. "name": "windows-1256"
  528. },
  529. {
  530. "labels": [
  531. "cp1257",
  532. "windows-1257",
  533. "x-cp1257"
  534. ],
  535. "name": "windows-1257"
  536. },
  537. {
  538. "labels": [
  539. "cp1258",
  540. "windows-1258",
  541. "x-cp1258"
  542. ],
  543. "name": "windows-1258"
  544. },
  545. {
  546. "labels": [
  547. "x-mac-cyrillic",
  548. "x-mac-ukrainian"
  549. ],
  550. "name": "x-mac-cyrillic"
  551. }
  552. ],
  553. "heading": "Legacy single-byte encodings"
  554. },
  555. {
  556. "encodings": [
  557. {
  558. "labels": [
  559. "chinese",
  560. "csgb2312",
  561. "csiso58gb231280",
  562. "gb18030",
  563. "gb2312",
  564. "gb_2312",
  565. "gb_2312-80",
  566. "gbk",
  567. "iso-ir-58",
  568. "x-gbk"
  569. ],
  570. "name": "gb18030"
  571. },
  572. {
  573. "labels": [
  574. "hz-gb-2312"
  575. ],
  576. "name": "hz-gb-2312"
  577. }
  578. ],
  579. "heading": "Legacy multi-byte Chinese (simplified) encodings"
  580. },
  581. {
  582. "encodings": [
  583. {
  584. "labels": [
  585. "big5",
  586. "big5-hkscs",
  587. "cn-big5",
  588. "csbig5",
  589. "x-x-big5"
  590. ],
  591. "name": "big5"
  592. }
  593. ],
  594. "heading": "Legacy multi-byte Chinese (traditional) encodings"
  595. },
  596. {
  597. "encodings": [
  598. {
  599. "labels": [
  600. "cseucpkdfmtjapanese",
  601. "euc-jp",
  602. "x-euc-jp"
  603. ],
  604. "name": "euc-jp"
  605. },
  606. {
  607. "labels": [
  608. "csiso2022jp",
  609. "iso-2022-jp"
  610. ],
  611. "name": "iso-2022-jp"
  612. },
  613. {
  614. "labels": [
  615. "csshiftjis",
  616. "ms_kanji",
  617. "shift-jis",
  618. "shift_jis",
  619. "sjis",
  620. "windows-31j",
  621. "x-sjis"
  622. ],
  623. "name": "shift_jis"
  624. }
  625. ],
  626. "heading": "Legacy multi-byte Japanese encodings"
  627. },
  628. {
  629. "encodings": [
  630. {
  631. "labels": [
  632. "cseuckr",
  633. "csksc56011987",
  634. "euc-kr",
  635. "iso-ir-149",
  636. "korean",
  637. "ks_c_5601-1987",
  638. "ks_c_5601-1989",
  639. "ksc5601",
  640. "ksc_5601",
  641. "windows-949"
  642. ],
  643. "name": "euc-kr"
  644. }
  645. ],
  646. "heading": "Legacy multi-byte Korean encodings"
  647. },
  648. {
  649. "encodings": [
  650. {
  651. "labels": [
  652. "csiso2022kr",
  653. "iso-2022-cn",
  654. "iso-2022-cn-ext",
  655. "iso-2022-kr"
  656. ],
  657. "name": "replacement"
  658. },
  659. {
  660. "labels": [
  661. "utf-16be"
  662. ],
  663. "name": "utf-16be"
  664. },
  665. {
  666. "labels": [
  667. "utf-16",
  668. "utf-16le"
  669. ],
  670. "name": "utf-16le"
  671. },
  672. {
  673. "labels": [
  674. "x-user-defined"
  675. ],
  676. "name": "x-user-defined"
  677. }
  678. ],
  679. "heading": "Legacy miscellaneous encodings"
  680. }
  681. ];
  682. var name_to_encoding = {};
  683. var label_to_encoding = {};
  684. encodings.forEach(function(category) {
  685. category['encodings'].forEach(function(encoding) {
  686. name_to_encoding[encoding['name']] = encoding;
  687. encoding['labels'].forEach(function(label) {
  688. label_to_encoding[label] = encoding;
  689. });
  690. });
  691. });
  692. //
  693. // 5. Indexes
  694. //
  695. /**
  696. * @param {number} pointer The |pointer| to search for.
  697. * @param {Array.<?number>|undefined} index The |index| to search within.
  698. * @return {?number} The code point corresponding to |pointer| in |index|,
  699. * or null if |code point| is not in |index|.
  700. */
  701. function indexCodePointFor(pointer, index) {
  702. if (!index) return null;
  703. return index[pointer] || null;
  704. }
  705. /**
  706. * @param {number} code_point The |code point| to search for.
  707. * @param {Array.<?number>} index The |index| to search within.
  708. * @return {?number} The first pointer corresponding to |code point| in
  709. * |index|, or null if |code point| is not in |index|.
  710. */
  711. function indexPointerFor(code_point, index) {
  712. var pointer = index.indexOf(code_point);
  713. return pointer === -1 ? null : pointer;
  714. }
  715. /**
  716. * @param {string} name Name of the index.
  717. * @return {(Array.<number>|Array.<Array.<number>>)}
  718. * */
  719. function index(name) {
  720. if (!('encoding-indexes' in global))
  721. throw new Error("Indexes missing. Did you forget to include encoding-indexes.js?");
  722. return global['encoding-indexes'][name];
  723. }
  724. /**
  725. * @param {number} pointer The |pointer| to search for in the gb18030 index.
  726. * @return {?number} The code point corresponding to |pointer| in |index|,
  727. * or null if |code point| is not in the gb18030 index.
  728. */
  729. function indexGB18030CodePointFor(pointer) {
  730. if ((pointer > 39419 && pointer < 189000) || (pointer > 1237575)) {
  731. return null;
  732. }
  733. var /** @type {number} */ offset = 0,
  734. /** @type {number} */ code_point_offset = 0,
  735. /** @type {Array.<Array.<number>>} */ idx = index('gb18030');
  736. var i;
  737. for (i = 0; i < idx.length; ++i) {
  738. var entry = idx[i];
  739. if (entry[0] <= pointer) {
  740. offset = entry[0];
  741. code_point_offset = entry[1];
  742. } else {
  743. break;
  744. }
  745. }
  746. return code_point_offset + pointer - offset;
  747. }
  748. /**
  749. * @param {number} code_point The |code point| to locate in the gb18030 index.
  750. * @return {number} The first pointer corresponding to |code point| in the
  751. * gb18030 index.
  752. */
  753. function indexGB18030PointerFor(code_point) {
  754. var /** @type {number} */ offset = 0,
  755. /** @type {number} */ pointer_offset = 0,
  756. /** @type {Array.<Array.<number>>} */ idx = index('gb18030');
  757. var i;
  758. for (i = 0; i < idx.length; ++i) {
  759. var entry = idx[i];
  760. if (entry[1] <= code_point) {
  761. offset = entry[1];
  762. pointer_offset = entry[0];
  763. } else {
  764. break;
  765. }
  766. }
  767. return pointer_offset + code_point - offset;
  768. }
  769. //
  770. // 7. API
  771. //
  772. /** @const */ var DEFAULT_ENCODING = 'utf-8';
  773. // 7.1 Interface TextDecoder
  774. /**
  775. * @constructor
  776. * @param {string=} opt_encoding The label of the encoding;
  777. * defaults to 'utf-8'.
  778. * @param {{fatal: boolean}=} options
  779. */
  780. function TextDecoder(opt_encoding, options) {
  781. if (!(this instanceof TextDecoder)) {
  782. return new TextDecoder(opt_encoding, options);
  783. }
  784. opt_encoding = opt_encoding ? String(opt_encoding) : DEFAULT_ENCODING;
  785. options = Object(options);
  786. /** @private */
  787. this._encoding = getEncoding(opt_encoding);
  788. if (this._encoding === null || this._encoding.name === 'replacement')
  789. throw new TypeError('Unknown encoding: ' + opt_encoding);
  790. if (!this._encoding.getDecoder)
  791. throw new Error('Decoder not present. Did you forget to include encoding-indexes.js?');
  792. /** @private @type {boolean} */
  793. this._streaming = false;
  794. /** @private @type {boolean} */
  795. this._BOMseen = false;
  796. /** @private */
  797. this._decoder = null;
  798. /** @private @type {{fatal: boolean}=} */
  799. this._options = { fatal: Boolean(options.fatal) };
  800. if (Object.defineProperty) {
  801. Object.defineProperty(
  802. this, 'encoding',
  803. { get: function() { return this._encoding.name; } });
  804. } else {
  805. this.encoding = this._encoding.name;
  806. }
  807. return this;
  808. }
  809. // TODO: Issue if input byte stream is offset by decoder
  810. // TODO: BOM detection will not work if stream header spans multiple calls
  811. // (last N bytes of previous stream may need to be retained?)
  812. TextDecoder.prototype = {
  813. /**
  814. * @param {ArrayBufferView=} opt_view The buffer of bytes to decode.
  815. * @param {{stream: boolean}=} options
  816. */
  817. decode: function decode(opt_view, options) {
  818. if (opt_view && !('buffer' in opt_view && 'byteOffset' in opt_view &&
  819. 'byteLength' in opt_view)) {
  820. throw new TypeError('Expected ArrayBufferView');
  821. } else if (!opt_view) {
  822. opt_view = new Uint8Array(0);
  823. }
  824. options = Object(options);
  825. if (!this._streaming) {
  826. this._decoder = this._encoding.getDecoder(this._options);
  827. this._BOMseen = false;
  828. }
  829. this._streaming = Boolean(options.stream);
  830. var bytes = new Uint8Array(opt_view.buffer,
  831. opt_view.byteOffset,
  832. opt_view.byteLength);
  833. var input_stream = new ByteInputStream(bytes);
  834. var output_stream = new CodePointOutputStream();
  835. /** @type {number} */
  836. var code_point;
  837. while (input_stream.get() !== EOF_byte) {
  838. code_point = this._decoder.decode(input_stream);
  839. if (code_point !== null && code_point !== EOF_code_point) {
  840. output_stream.emit(code_point);
  841. }
  842. }
  843. if (!this._streaming) {
  844. do {
  845. code_point = this._decoder.decode(input_stream);
  846. if (code_point !== null && code_point !== EOF_code_point) {
  847. output_stream.emit(code_point);
  848. }
  849. } while (code_point !== EOF_code_point &&
  850. input_stream.get() != EOF_byte);
  851. this._decoder = null;
  852. }
  853. var result = output_stream.string();
  854. if (!this._BOMseen && result.length) {
  855. this._BOMseen = true;
  856. if (['utf-8', 'utf-16le', 'utf-16be'].indexOf(this.encoding) !== -1 &&
  857. result.charCodeAt(0) === 0xFEFF) {
  858. result = result.substring(1);
  859. }
  860. }
  861. return result;
  862. }
  863. };
  864. // 7.2 Interface TextEncoder
  865. /**
  866. * @constructor
  867. * @param {string=} opt_encoding The label of the encoding;
  868. * defaults to 'utf-8'.
  869. * @param {{fatal: boolean}=} options
  870. */
  871. function TextEncoder(opt_encoding, options) {
  872. if (!(this instanceof TextEncoder)) {
  873. return new TextEncoder(opt_encoding, options);
  874. }
  875. opt_encoding = opt_encoding ? String(opt_encoding) : DEFAULT_ENCODING;
  876. options = Object(options);
  877. /** @private */
  878. this._encoding = getEncoding(opt_encoding);
  879. var allowLegacyEncoding = options.NONSTANDARD_allowLegacyEncoding;
  880. var isLegacyEncoding = (this._encoding.name !== 'utf-8' &&
  881. this._encoding.name !== 'utf-16le' &&
  882. this._encoding.name !== 'utf-16be');
  883. if (this._encoding === null || (isLegacyEncoding && !allowLegacyEncoding))
  884. throw new TypeError('Unknown encoding: ' + opt_encoding);
  885. if (!this._encoding.getEncoder)
  886. throw new Error('Encoder not present. Did you forget to include encoding-indexes.js?');
  887. /** @private @type {boolean} */
  888. this._streaming = false;
  889. /** @private */
  890. this._encoder = null;
  891. /** @private @type {{fatal: boolean}=} */
  892. this._options = { fatal: Boolean(options.fatal) };
  893. if (Object.defineProperty) {
  894. Object.defineProperty(
  895. this, 'encoding',
  896. { get: function() { return this._encoding.name; } });
  897. } else {
  898. this.encoding = this._encoding.name;
  899. }
  900. return this;
  901. }
  902. TextEncoder.prototype = {
  903. /**
  904. * @param {string=} opt_string The string to encode.
  905. * @param {{stream: boolean}=} options
  906. */
  907. encode: function encode(opt_string, options) {
  908. opt_string = opt_string ? String(opt_string) : '';
  909. options = Object(options);
  910. // TODO: any options?
  911. if (!this._streaming) {
  912. this._encoder = this._encoding.getEncoder(this._options);
  913. }
  914. this._streaming = Boolean(options.stream);
  915. var bytes = [];
  916. var output_stream = new ByteOutputStream(bytes);
  917. var input_stream = new CodePointInputStream(opt_string);
  918. while (input_stream.get() !== EOF_code_point) {
  919. this._encoder.encode(output_stream, input_stream);
  920. }
  921. if (!this._streaming) {
  922. /** @type {number} */
  923. var last_byte;
  924. do {
  925. last_byte = this._encoder.encode(output_stream, input_stream);
  926. } while (last_byte !== EOF_byte);
  927. this._encoder = null;
  928. }
  929. return new Uint8Array(bytes);
  930. }
  931. };
  932. //
  933. // 8. The encoding
  934. //
  935. // 8.1 utf-8
  936. /**
  937. * @constructor
  938. * @param {{fatal: boolean}} options
  939. */
  940. function UTF8Decoder(options) {
  941. var fatal = options.fatal;
  942. var /** @type {number} */ utf8_code_point = 0,
  943. /** @type {number} */ utf8_bytes_needed = 0,
  944. /** @type {number} */ utf8_bytes_seen = 0,
  945. /** @type {number} */ utf8_lower_boundary = 0;
  946. /**
  947. * @param {ByteInputStream} byte_pointer The byte stream to decode.
  948. * @return {?number} The next code point decoded, or null if not enough
  949. * data exists in the input stream to decode a complete code point.
  950. */
  951. this.decode = function(byte_pointer) {
  952. var bite = byte_pointer.get();
  953. if (bite === EOF_byte) {
  954. if (utf8_bytes_needed !== 0) {
  955. return decoderError(fatal);
  956. }
  957. return EOF_code_point;
  958. }
  959. byte_pointer.offset(1);
  960. if (utf8_bytes_needed === 0) {
  961. if (inRange(bite, 0x00, 0x7F)) {
  962. return bite;
  963. }
  964. if (inRange(bite, 0xC2, 0xDF)) {
  965. utf8_bytes_needed = 1;
  966. utf8_lower_boundary = 0x80;
  967. utf8_code_point = bite - 0xC0;
  968. } else if (inRange(bite, 0xE0, 0xEF)) {
  969. utf8_bytes_needed = 2;
  970. utf8_lower_boundary = 0x800;
  971. utf8_code_point = bite - 0xE0;
  972. } else if (inRange(bite, 0xF0, 0xF4)) {
  973. utf8_bytes_needed = 3;
  974. utf8_lower_boundary = 0x10000;
  975. utf8_code_point = bite - 0xF0;
  976. } else {
  977. return decoderError(fatal);
  978. }
  979. utf8_code_point = utf8_code_point * Math.pow(64, utf8_bytes_needed);
  980. return null;
  981. }
  982. if (!inRange(bite, 0x80, 0xBF)) {
  983. utf8_code_point = 0;
  984. utf8_bytes_needed = 0;
  985. utf8_bytes_seen = 0;
  986. utf8_lower_boundary = 0;
  987. byte_pointer.offset(-1);
  988. return decoderError(fatal);
  989. }
  990. utf8_bytes_seen += 1;
  991. utf8_code_point = utf8_code_point + (bite - 0x80) *
  992. Math.pow(64, utf8_bytes_needed - utf8_bytes_seen);
  993. if (utf8_bytes_seen !== utf8_bytes_needed) {
  994. return null;
  995. }
  996. var code_point = utf8_code_point;
  997. var lower_boundary = utf8_lower_boundary;
  998. utf8_code_point = 0;
  999. utf8_bytes_needed = 0;
  1000. utf8_bytes_seen = 0;
  1001. utf8_lower_boundary = 0;
  1002. if (inRange(code_point, lower_boundary, 0x10FFFF) &&
  1003. !inRange(code_point, 0xD800, 0xDFFF)) {
  1004. return code_point;
  1005. }
  1006. return decoderError(fatal);
  1007. };
  1008. }
  1009. /**
  1010. * @constructor
  1011. * @param {{fatal: boolean}} options
  1012. */
  1013. function UTF8Encoder(options) {
  1014. var fatal = options.fatal;
  1015. /**
  1016. * @param {ByteOutputStream} output_byte_stream Output byte stream.
  1017. * @param {CodePointInputStream} code_point_pointer Input stream.
  1018. * @return {number} The last byte emitted.
  1019. */
  1020. this.encode = function(output_byte_stream, code_point_pointer) {
  1021. /** @type {number} */
  1022. var code_point = code_point_pointer.get();
  1023. if (code_point === EOF_code_point) {
  1024. return EOF_byte;
  1025. }
  1026. code_point_pointer.offset(1);
  1027. if (inRange(code_point, 0xD800, 0xDFFF)) {
  1028. return encoderError(code_point);
  1029. }
  1030. if (inRange(code_point, 0x0000, 0x007f)) {
  1031. return output_byte_stream.emit(code_point);
  1032. }
  1033. var count, offset;
  1034. if (inRange(code_point, 0x0080, 0x07FF)) {
  1035. count = 1;
  1036. offset = 0xC0;
  1037. } else if (inRange(code_point, 0x0800, 0xFFFF)) {
  1038. count = 2;
  1039. offset = 0xE0;
  1040. } else if (inRange(code_point, 0x10000, 0x10FFFF)) {
  1041. count = 3;
  1042. offset = 0xF0;
  1043. }
  1044. var result = output_byte_stream.emit(
  1045. div(code_point, Math.pow(64, count)) + offset);
  1046. while (count > 0) {
  1047. var temp = div(code_point, Math.pow(64, count - 1));
  1048. result = output_byte_stream.emit(0x80 + (temp % 64));
  1049. count -= 1;
  1050. }
  1051. return result;
  1052. };
  1053. }
  1054. /** @param {{fatal: boolean}} options */
  1055. name_to_encoding['utf-8'].getEncoder = function(options) {
  1056. return new UTF8Encoder(options);
  1057. };
  1058. /** @param {{fatal: boolean}} options */
  1059. name_to_encoding['utf-8'].getDecoder = function(options) {
  1060. return new UTF8Decoder(options);
  1061. };
  1062. //
  1063. // 9. Legacy single-byte encodings
  1064. //
  1065. /**
  1066. * @constructor
  1067. * @param {Array.<number>} index The encoding index.
  1068. * @param {{fatal: boolean}} options
  1069. */
  1070. function SingleByteDecoder(index, options) {
  1071. var fatal = options.fatal;
  1072. /**
  1073. * @param {ByteInputStream} byte_pointer The byte stream to decode.
  1074. * @return {?number} The next code point decoded, or null if not enough
  1075. * data exists in the input stream to decode a complete code point.
  1076. */
  1077. this.decode = function(byte_pointer) {
  1078. var bite = byte_pointer.get();
  1079. if (bite === EOF_byte) {
  1080. return EOF_code_point;
  1081. }
  1082. byte_pointer.offset(1);
  1083. if (inRange(bite, 0x00, 0x7F)) {
  1084. return bite;
  1085. }
  1086. var code_point = index[bite - 0x80];
  1087. if (code_point === null) {
  1088. return decoderError(fatal);
  1089. }
  1090. return code_point;
  1091. };
  1092. }
  1093. /**
  1094. * @constructor
  1095. * @param {Array.<?number>} index The encoding index.
  1096. * @param {{fatal: boolean}} options
  1097. */
  1098. function SingleByteEncoder(index, options) {
  1099. var fatal = options.fatal;
  1100. /**
  1101. * @param {ByteOutputStream} output_byte_stream Output byte stream.
  1102. * @param {CodePointInputStream} code_point_pointer Input stream.
  1103. * @return {number} The last byte emitted.
  1104. */
  1105. this.encode = function(output_byte_stream, code_point_pointer) {
  1106. var code_point = code_point_pointer.get();
  1107. if (code_point === EOF_code_point) {
  1108. return EOF_byte;
  1109. }
  1110. code_point_pointer.offset(1);
  1111. if (inRange(code_point, 0x0000, 0x007F)) {
  1112. return output_byte_stream.emit(code_point);
  1113. }
  1114. var pointer = indexPointerFor(code_point, index);
  1115. if (pointer === null) {
  1116. encoderError(code_point);
  1117. }
  1118. return output_byte_stream.emit(pointer + 0x80);
  1119. };
  1120. }
  1121. (function() {
  1122. if (!('encoding-indexes' in global))
  1123. return;
  1124. encodings.forEach(function(category) {
  1125. if (category['heading'] !== 'Legacy single-byte encodings')
  1126. return;
  1127. category['encodings'].forEach(function(encoding) {
  1128. var idx = index(encoding['name']);
  1129. /** @param {{fatal: boolean}} options */
  1130. encoding.getDecoder = function(options) {
  1131. return new SingleByteDecoder(idx, options);
  1132. };
  1133. /** @param {{fatal: boolean}} options */
  1134. encoding.getEncoder = function(options) {
  1135. return new SingleByteEncoder(idx, options);
  1136. };
  1137. });
  1138. });
  1139. }());
  1140. //
  1141. // 10. Legacy multi-byte Chinese (simplified) encodings
  1142. //
  1143. // 9.1 gb18030
  1144. /**
  1145. * @constructor
  1146. * @param {{fatal: boolean}} options
  1147. */
  1148. function GB18030Decoder(options) {
  1149. var fatal = options.fatal;
  1150. var /** @type {number} */ gb18030_first = 0x00,
  1151. /** @type {number} */ gb18030_second = 0x00,
  1152. /** @type {number} */ gb18030_third = 0x00;
  1153. /**
  1154. * @param {ByteInputStream} byte_pointer The byte stream to decode.
  1155. * @return {?number} The next code point decoded, or null if not enough
  1156. * data exists in the input stream to decode a complete code point.
  1157. */
  1158. this.decode = function(byte_pointer) {
  1159. var bite = byte_pointer.get();
  1160. if (bite === EOF_byte && gb18030_first === 0x00 &&
  1161. gb18030_second === 0x00 && gb18030_third === 0x00) {
  1162. return EOF_code_point;
  1163. }
  1164. if (bite === EOF_byte &&
  1165. (gb18030_first !== 0x00 || gb18030_second !== 0x00 || gb18030_third !== 0x00)) {
  1166. gb18030_first = 0x00;
  1167. gb18030_second = 0x00;
  1168. gb18030_third = 0x00;
  1169. decoderError(fatal);
  1170. }
  1171. byte_pointer.offset(1);
  1172. var code_point;
  1173. if (gb18030_third !== 0x00) {
  1174. code_point = null;
  1175. if (inRange(bite, 0x30, 0x39)) {
  1176. code_point = indexGB18030CodePointFor(
  1177. (((gb18030_first - 0x81) * 10 + (gb18030_second - 0x30)) * 126 +
  1178. (gb18030_third - 0x81)) * 10 + bite - 0x30);
  1179. }
  1180. gb18030_first = 0x00;
  1181. gb18030_second = 0x00;
  1182. gb18030_third = 0x00;
  1183. if (code_point === null) {
  1184. byte_pointer.offset(-3);
  1185. return decoderError(fatal);
  1186. }
  1187. return code_point;
  1188. }
  1189. if (gb18030_second !== 0x00) {
  1190. if (inRange(bite, 0x81, 0xFE)) {
  1191. gb18030_third = bite;
  1192. return null;
  1193. }
  1194. byte_pointer.offset(-2);
  1195. gb18030_first = 0x00;
  1196. gb18030_second = 0x00;
  1197. return decoderError(fatal);
  1198. }
  1199. if (gb18030_first !== 0x00) {
  1200. if (inRange(bite, 0x30, 0x39)) {
  1201. gb18030_second = bite;
  1202. return null;
  1203. }
  1204. var lead = gb18030_first;
  1205. var pointer = null;
  1206. gb18030_first = 0x00;
  1207. var offset = bite < 0x7F ? 0x40 : 0x41;
  1208. if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFE)) {
  1209. pointer = (lead - 0x81) * 190 + (bite - offset);
  1210. }
  1211. code_point = pointer === null ? null :
  1212. indexCodePointFor(pointer, index('gb18030'));
  1213. if (pointer === null) {
  1214. byte_pointer.offset(-1);
  1215. }
  1216. if (code_point === null) {
  1217. return decoderError(fatal);
  1218. }
  1219. return code_point;
  1220. }
  1221. if (inRange(bite, 0x00, 0x7F)) {
  1222. return bite;
  1223. }
  1224. if (bite === 0x80) {
  1225. return 0x20AC;
  1226. }
  1227. if (inRange(bite, 0x81, 0xFE)) {
  1228. gb18030_first = bite;
  1229. return null;
  1230. }
  1231. return decoderError(fatal);
  1232. };
  1233. }
  1234. /**
  1235. * @constructor
  1236. * @param {{fatal: boolean}} options
  1237. */
  1238. function GB18030Encoder(options) {
  1239. var fatal = options.fatal;
  1240. /**
  1241. * @param {ByteOutputStream} output_byte_stream Output byte stream.
  1242. * @param {CodePointInputStream} code_point_pointer Input stream.
  1243. * @return {number} The last byte emitted.
  1244. */
  1245. this.encode = function(output_byte_stream, code_point_pointer) {
  1246. var code_point = code_point_pointer.get();
  1247. if (code_point === EOF_code_point) {
  1248. return EOF_byte;
  1249. }
  1250. code_point_pointer.offset(1);
  1251. if (inRange(code_point, 0x0000, 0x007F)) {
  1252. return output_byte_stream.emit(code_point);
  1253. }
  1254. var pointer = indexPointerFor(code_point, index('gb18030'));
  1255. if (pointer !== null) {
  1256. var lead = div(pointer, 190) + 0x81;
  1257. var trail = pointer % 190;
  1258. var offset = trail < 0x3F ? 0x40 : 0x41;
  1259. return output_byte_stream.emit(lead, trail + offset);
  1260. }
  1261. pointer = indexGB18030PointerFor(code_point);
  1262. var byte1 = div(div(div(pointer, 10), 126), 10);
  1263. pointer = pointer - byte1 * 10 * 126 * 10;
  1264. var byte2 = div(div(pointer, 10), 126);
  1265. pointer = pointer - byte2 * 10 * 126;
  1266. var byte3 = div(pointer, 10);
  1267. var byte4 = pointer - byte3 * 10;
  1268. return output_byte_stream.emit(byte1 + 0x81,
  1269. byte2 + 0x30,
  1270. byte3 + 0x81,
  1271. byte4 + 0x30);
  1272. };
  1273. }
  1274. /** @param {{fatal: boolean}} options */
  1275. name_to_encoding['gb18030'].getEncoder = function(options) {
  1276. return new GB18030Encoder(options);
  1277. };
  1278. /** @param {{fatal: boolean}} options */
  1279. name_to_encoding['gb18030'].getDecoder = function(options) {
  1280. return new GB18030Decoder(options);
  1281. };
  1282. // 10.2 hz-gb-2312
  1283. /**
  1284. * @constructor
  1285. * @param {{fatal: boolean}} options
  1286. */
  1287. function HZGB2312Decoder(options) {
  1288. var fatal = options.fatal;
  1289. var /** @type {boolean} */ hzgb2312 = false,
  1290. /** @type {number} */ hzgb2312_lead = 0x00;
  1291. /**
  1292. * @param {ByteInputStream} byte_pointer The byte stream to decode.
  1293. * @return {?number} The next code point decoded, or null if not enough
  1294. * data exists in the input stream to decode a complete code point.
  1295. */
  1296. this.decode = function(byte_pointer) {
  1297. var bite = byte_pointer.get();
  1298. if (bite === EOF_byte && hzgb2312_lead === 0x00) {
  1299. return EOF_code_point;
  1300. }
  1301. if (bite === EOF_byte && hzgb2312_lead !== 0x00) {
  1302. hzgb2312_lead = 0x00;
  1303. return decoderError(fatal);
  1304. }
  1305. byte_pointer.offset(1);
  1306. if (hzgb2312_lead === 0x7E) {
  1307. hzgb2312_lead = 0x00;
  1308. if (bite === 0x7B) {
  1309. hzgb2312 = true;
  1310. return null;
  1311. }
  1312. if (bite === 0x7D) {
  1313. hzgb2312 = false;
  1314. return null;
  1315. }
  1316. if (bite === 0x7E) {
  1317. return 0x007E;
  1318. }
  1319. if (bite === 0x0A) {
  1320. return null;
  1321. }
  1322. byte_pointer.offset(-1);
  1323. return decoderError(fatal);
  1324. }
  1325. if (hzgb2312_lead !== 0x00) {
  1326. var lead = hzgb2312_lead;
  1327. hzgb2312_lead = 0x00;
  1328. var code_point = null;
  1329. if (inRange(bite, 0x21, 0x7E)) {
  1330. code_point = indexCodePointFor((lead - 1) * 190 +
  1331. (bite + 0x3F), index('gb18030'));
  1332. }
  1333. if (bite === 0x0A) {
  1334. hzgb2312 = false;
  1335. }
  1336. if (code_point === null) {
  1337. return decoderError(fatal);
  1338. }
  1339. return code_point;
  1340. }
  1341. if (bite === 0x7E) {
  1342. hzgb2312_lead = 0x7E;
  1343. return null;
  1344. }
  1345. if (hzgb2312) {
  1346. if (inRange(bite, 0x20, 0x7F)) {
  1347. hzgb2312_lead = bite;
  1348. return null;
  1349. }
  1350. if (bite === 0x0A) {
  1351. hzgb2312 = false;
  1352. }
  1353. return decoderError(fatal);
  1354. }
  1355. if (inRange(bite, 0x00, 0x7F)) {
  1356. return bite;
  1357. }
  1358. return decoderError(fatal);
  1359. };
  1360. }
  1361. /**
  1362. * @constructor
  1363. * @param {{fatal: boolean}} options
  1364. */
  1365. function HZGB2312Encoder(options) {
  1366. var fatal = options.fatal;
  1367. /** @type {boolean} */
  1368. var hzgb2312 = false;
  1369. /**
  1370. * @param {ByteOutputStream} output_byte_stream Output byte stream.
  1371. * @param {CodePointInputStream} code_point_pointer Input stream.
  1372. * @return {number} The last byte emitted.
  1373. */
  1374. this.encode = function(output_byte_stream, code_point_pointer) {
  1375. var code_point = code_point_pointer.get();
  1376. if (code_point === EOF_code_point) {
  1377. return EOF_byte;
  1378. }
  1379. code_point_pointer.offset(1);
  1380. if (inRange(code_point, 0x0000, 0x007F) && hzgb2312) {
  1381. code_point_pointer.offset(-1);
  1382. hzgb2312 = false;
  1383. return output_byte_stream.emit(0x7E, 0x7D);
  1384. }
  1385. if (code_point === 0x007E) {
  1386. return output_byte_stream.emit(0x7E, 0x7E);
  1387. }
  1388. if (inRange(code_point, 0x0000, 0x007F)) {
  1389. return output_byte_stream.emit(code_point);
  1390. }
  1391. if (!hzgb2312) {
  1392. code_point_pointer.offset(-1);
  1393. hzgb2312 = true;
  1394. return output_byte_stream.emit(0x7E, 0x7B);
  1395. }
  1396. var pointer = indexPointerFor(code_point, index('gb18030'));
  1397. if (pointer === null) {
  1398. return encoderError(code_point);
  1399. }
  1400. var lead = div(pointer, 190) + 1;
  1401. var trail = pointer % 190 - 0x3F;
  1402. if (!inRange(lead, 0x21, 0x7E) || !inRange(trail, 0x21, 0x7E)) {
  1403. return encoderError(code_point);
  1404. }
  1405. return output_byte_stream.emit(lead, trail);
  1406. };
  1407. }
  1408. /** @param {{fatal: boolean}} options */
  1409. name_to_encoding['hz-gb-2312'].getEncoder = function(options) {
  1410. return new HZGB2312Encoder(options);
  1411. };
  1412. /** @param {{fatal: boolean}} options */
  1413. name_to_encoding['hz-gb-2312'].getDecoder = function(options) {
  1414. return new HZGB2312Decoder(options);
  1415. };
  1416. //
  1417. // 11. Legacy multi-byte Chinese (traditional) encodings
  1418. //
  1419. // 11.1 big5
  1420. /**
  1421. * @constructor
  1422. * @param {{fatal: boolean}} options
  1423. */
  1424. function Big5Decoder(options) {
  1425. var fatal = options.fatal;
  1426. var /** @type {number} */ big5_lead = 0x00,
  1427. /** @type {?number} */ big5_pending = null;
  1428. /**
  1429. * @param {ByteInputStream} byte_pointer The byte steram to decode.
  1430. * @return {?number} The next code point decoded, or null if not enough
  1431. * data exists in the input stream to decode a complete code point.
  1432. */
  1433. this.decode = function(byte_pointer) {
  1434. // NOTE: Hack to support emitting two code points
  1435. if (big5_pending !== null) {
  1436. var pending = big5_pending;
  1437. big5_pending = null;
  1438. return pending;
  1439. }
  1440. var bite = byte_pointer.get();
  1441. if (bite === EOF_byte && big5_lead === 0x00) {
  1442. return EOF_code_point;
  1443. }
  1444. if (bite === EOF_byte && big5_lead !== 0x00) {
  1445. big5_lead = 0x00;
  1446. return decoderError(fatal);
  1447. }
  1448. byte_pointer.offset(1);
  1449. if (big5_lead !== 0x00) {
  1450. var lead = big5_lead;
  1451. var pointer = null;
  1452. big5_lead = 0x00;
  1453. var offset = bite < 0x7F ? 0x40 : 0x62;
  1454. if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0xA1, 0xFE)) {
  1455. pointer = (lead - 0x81) * 157 + (bite - offset);
  1456. }
  1457. if (pointer === 1133) {
  1458. big5_pending = 0x0304;
  1459. return 0x00CA;
  1460. }
  1461. if (pointer === 1135) {
  1462. big5_pending = 0x030C;
  1463. return 0x00CA;
  1464. }
  1465. if (pointer === 1164) {
  1466. big5_pending = 0x0304;
  1467. return 0x00EA;
  1468. }
  1469. if (pointer === 1166) {
  1470. big5_pending = 0x030C;
  1471. return 0x00EA;
  1472. }
  1473. var code_point = (pointer === null) ? null :
  1474. indexCodePointFor(pointer, index('big5'));
  1475. if (pointer === null) {
  1476. byte_pointer.offset(-1);
  1477. }
  1478. if (code_point === null) {
  1479. return decoderError(fatal);
  1480. }
  1481. return code_point;
  1482. }
  1483. if (inRange(bite, 0x00, 0x7F)) {
  1484. return bite;
  1485. }
  1486. if (inRange(bite, 0x81, 0xFE)) {
  1487. big5_lead = bite;
  1488. return null;
  1489. }
  1490. return decoderError(fatal);
  1491. };
  1492. }
  1493. /**
  1494. * @constructor
  1495. * @param {{fatal: boolean}} options
  1496. */
  1497. function Big5Encoder(options) {
  1498. var fatal = options.fatal;
  1499. /**
  1500. * @param {ByteOutputStream} output_byte_stream Output byte stream.
  1501. * @param {CodePointInputStream} code_point_pointer Input stream.
  1502. * @return {number} The last byte emitted.
  1503. */
  1504. this.encode = function(output_byte_stream, code_point_pointer) {
  1505. var code_point = code_point_pointer.get();
  1506. if (code_point === EOF_code_point) {
  1507. return EOF_byte;
  1508. }
  1509. code_point_pointer.offset(1);
  1510. if (inRange(code_point, 0x0000, 0x007F)) {
  1511. return output_byte_stream.emit(code_point);
  1512. }
  1513. var pointer = indexPointerFor(code_point, index('big5'));
  1514. if (pointer === null) {
  1515. return encoderError(code_point);
  1516. }
  1517. var lead = div(pointer, 157) + 0x81;
  1518. //if (lead < 0xA1) {
  1519. // return encoderError(code_point);
  1520. //}
  1521. var trail = pointer % 157;
  1522. var offset = trail < 0x3F ? 0x40 : 0x62;
  1523. return output_byte_stream.emit(lead, trail + offset);
  1524. };
  1525. }
  1526. /** @param {{fatal: boolean}} options */
  1527. name_to_encoding['big5'].getEncoder = function(options) {
  1528. return new Big5Encoder(options);
  1529. };
  1530. /** @param {{fatal: boolean}} options */
  1531. name_to_encoding['big5'].getDecoder = function(options) {
  1532. return new Big5Decoder(options);
  1533. };
  1534. //
  1535. // 12. Legacy multi-byte Japanese encodings
  1536. //
  1537. // 12.1 euc.jp
  1538. /**
  1539. * @constructor
  1540. * @param {{fatal: boolean}} options
  1541. */
  1542. function EUCJPDecoder(options) {
  1543. var fatal = options.fatal;
  1544. var /** @type {number} */ eucjp_first = 0x00,
  1545. /** @type {number} */ eucjp_second = 0x00;
  1546. /**
  1547. * @param {ByteInputStream} byte_pointer The byte stream to decode.
  1548. * @return {?number} The next code point decoded, or null if not enough
  1549. * data exists in the input stream to decode a complete code point.
  1550. */
  1551. this.decode = function(byte_pointer) {
  1552. var bite = byte_pointer.get();
  1553. if (bite === EOF_byte) {
  1554. if (eucjp_first === 0x00 && eucjp_second === 0x00) {
  1555. return EOF_code_point;
  1556. }
  1557. eucjp_first = 0x00;
  1558. eucjp_second = 0x00;
  1559. return decoderError(fatal);
  1560. }
  1561. byte_pointer.offset(1);
  1562. var lead, code_point;
  1563. if (eucjp_second !== 0x00) {
  1564. lead = eucjp_second;
  1565. eucjp_second = 0x00;
  1566. code_point = null;
  1567. if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
  1568. code_point = indexCodePointFor((lead - 0xA1) * 94 + bite - 0xA1,
  1569. index('jis0212'));
  1570. }
  1571. if (!inRange(bite, 0xA1, 0xFE)) {
  1572. byte_pointer.offset(-1);
  1573. }
  1574. if (code_point === null) {
  1575. return decoderError(fatal);
  1576. }
  1577. return code_point;
  1578. }
  1579. if (eucjp_first === 0x8E && inRange(bite, 0xA1, 0xDF)) {
  1580. eucjp_first = 0x00;
  1581. return 0xFF61 + bite - 0xA1;
  1582. }
  1583. if (eucjp_first === 0x8F && inRange(bite, 0xA1, 0xFE)) {
  1584. eucjp_first = 0x00;
  1585. eucjp_second = bite;
  1586. return null;
  1587. }
  1588. if (eucjp_first !== 0x00) {
  1589. lead = eucjp_first;
  1590. eucjp_first = 0x00;
  1591. code_point = null;
  1592. if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
  1593. code_point = indexCodePointFor((lead - 0xA1) * 94 + bite - 0xA1,
  1594. index('jis0208'));
  1595. }
  1596. if (!inRange(bite, 0xA1, 0xFE)) {
  1597. byte_pointer.offset(-1);
  1598. }
  1599. if (code_point === null) {
  1600. return decoderError(fatal);
  1601. }
  1602. return code_point;
  1603. }
  1604. if (inRange(bite, 0x00, 0x7F)) {
  1605. return bite;
  1606. }
  1607. if (bite === 0x8E || bite === 0x8F || (inRange(bite, 0xA1, 0xFE))) {
  1608. eucjp_first = bite;
  1609. return null;
  1610. }
  1611. return decoderError(fatal);
  1612. };
  1613. }
  1614. /**
  1615. * @constructor
  1616. * @param {{fatal: boolean}} options
  1617. */
  1618. function EUCJPEncoder(options) {
  1619. var fatal = options.fatal;
  1620. /**
  1621. * @param {ByteOutputStream} output_byte_stream Output byte stream.
  1622. * @param {CodePointInputStream} code_point_pointer Input stream.
  1623. * @return {number} The last byte emitted.
  1624. */
  1625. this.encode = function(output_byte_stream, code_point_pointer) {
  1626. var code_point = code_point_pointer.get();
  1627. if (code_point === EOF_code_point) {
  1628. return EOF_byte;
  1629. }
  1630. code_point_pointer.offset(1);
  1631. if (inRange(code_point, 0x0000, 0x007F)) {
  1632. return output_byte_stream.emit(code_point);
  1633. }
  1634. if (code_point === 0x00A5) {
  1635. return output_byte_stream.emit(0x5C);
  1636. }
  1637. if (code_point === 0x203E) {
  1638. return output_byte_stream.emit(0x7E);
  1639. }
  1640. if (inRange(code_point, 0xFF61, 0xFF9F)) {
  1641. return output_byte_stream.emit(0x8E, code_point - 0xFF61 + 0xA1);
  1642. }
  1643. var pointer = indexPointerFor(code_point, index('jis0208'));
  1644. if (pointer === null) {
  1645. return encoderError(code_point);
  1646. }
  1647. var lead = div(pointer, 94) + 0xA1;
  1648. var trail = pointer % 94 + 0xA1;
  1649. return output_byte_stream.emit(lead, trail);
  1650. };
  1651. }
  1652. /** @param {{fatal: boolean}} options */
  1653. name_to_encoding['euc-jp'].getEncoder = function(options) {
  1654. return new EUCJPEncoder(options);
  1655. };
  1656. /** @param {{fatal: boolean}} options */
  1657. name_to_encoding['euc-jp'].getDecoder = function(options) {
  1658. return new EUCJPDecoder(options);
  1659. };
  1660. // 12.2 iso-2022-jp
  1661. /**
  1662. * @constructor
  1663. * @param {{fatal: boolean}} options
  1664. */
  1665. function ISO2022JPDecoder(options) {
  1666. var fatal = options.fatal;
  1667. /** @enum */
  1668. var state = {
  1669. ASCII: 0,
  1670. escape_start: 1,
  1671. escape_middle: 2,
  1672. escape_final: 3,
  1673. lead: 4,
  1674. trail: 5,
  1675. Katakana: 6
  1676. };
  1677. var /** @type {number} */ iso2022jp_state = state.ASCII,
  1678. /** @type {boolean} */ iso2022jp_jis0212 = false,
  1679. /** @type {number} */ iso2022jp_lead = 0x00;
  1680. /**
  1681. * @param {ByteInputStream} byte_pointer The byte stream to decode.
  1682. * @return {?number} The next code point decoded, or null if not enough
  1683. * data exists in the input stream to decode a complete code point.
  1684. */
  1685. this.decode = function(byte_pointer) {
  1686. var bite = byte_pointer.get();
  1687. if (bite !== EOF_byte) {
  1688. byte_pointer.offset(1);
  1689. }
  1690. switch (iso2022jp_state) {
  1691. default:
  1692. case state.ASCII:
  1693. if (bite === 0x1B) {
  1694. iso2022jp_state = state.escape_start;
  1695. return null;
  1696. }
  1697. if (inRange(bite, 0x00, 0x7F)) {
  1698. return bite;
  1699. }
  1700. if (bite === EOF_byte) {
  1701. return EOF_code_point;
  1702. }
  1703. return decoderError(fatal);
  1704. case state.escape_start:
  1705. if (bite === 0x24 || bite === 0x28) {
  1706. iso2022jp_lead = bite;
  1707. iso2022jp_state = state.escape_middle;
  1708. return null;
  1709. }
  1710. if (bite !== EOF_byte) {
  1711. byte_pointer.offset(-1);
  1712. }
  1713. iso2022jp_state = state.ASCII;
  1714. return decoderError(fatal);
  1715. case state.escape_middle:
  1716. var lead = iso2022jp_lead;
  1717. iso2022jp_lead = 0x00;
  1718. if (lead === 0x24 && (bite === 0x40 || bite === 0x42)) {
  1719. iso2022jp_jis0212 = false;
  1720. iso2022jp_state = state.lead;
  1721. return null;
  1722. }
  1723. if (lead === 0x24 && bite === 0x28) {
  1724. iso2022jp_state = state.escape_final;
  1725. return null;
  1726. }
  1727. if (lead === 0x28 && (bite === 0x42 || bite === 0x4A)) {
  1728. iso2022jp_state = state.ASCII;
  1729. return null;
  1730. }
  1731. if (lead === 0x28 && bite === 0x49) {
  1732. iso2022jp_state = state.Katakana;
  1733. return null;
  1734. }
  1735. if (bite === EOF_byte) {
  1736. byte_pointer.offset(-1);
  1737. } else {
  1738. byte_pointer.offset(-2);
  1739. }
  1740. iso2022jp_state = state.ASCII;
  1741. return decoderError(fatal);
  1742. case state.escape_final:
  1743. if (bite === 0x44) {
  1744. iso2022jp_jis0212 = true;
  1745. iso2022jp_state = state.lead;
  1746. return null;
  1747. }
  1748. if (bite === EOF_byte) {
  1749. byte_pointer.offset(-2);
  1750. } else {
  1751. byte_pointer.offset(-3);
  1752. }
  1753. iso2022jp_state = state.ASCII;
  1754. return decoderError(fatal);
  1755. case state.lead:
  1756. if (bite === 0x0A) {
  1757. iso2022jp_state = state.ASCII;
  1758. return decoderError(fatal, 0x000A);
  1759. }
  1760. if (bite === 0x1B) {
  1761. iso2022jp_state = state.escape_start;
  1762. return null;
  1763. }
  1764. if (bite === EOF_byte) {
  1765. return EOF_code_point;
  1766. }
  1767. iso2022jp_lead = bite;
  1768. iso2022jp_state = state.trail;
  1769. return null;
  1770. case state.trail:
  1771. iso2022jp_state = state.lead;
  1772. if (bite === EOF_byte) {
  1773. return decoderError(fatal);
  1774. }
  1775. var code_point = null;
  1776. var pointer = (iso2022jp_lead - 0x21) * 94 + bite - 0x21;
  1777. if (inRange(iso2022jp_lead, 0x21, 0x7E) &&
  1778. inRange(bite, 0x21, 0x7E)) {
  1779. code_point = (iso2022jp_jis0212 === false) ?
  1780. indexCodePointFor(pointer, index('jis0208')) :
  1781. indexCodePointFor(pointer, index('jis0212'));
  1782. }
  1783. if (code_point === null) {
  1784. return decoderError(fatal);
  1785. }
  1786. return code_point;
  1787. case state.Katakana:
  1788. if (bite === 0x1B) {
  1789. iso2022jp_state = state.escape_start;
  1790. return null;
  1791. }
  1792. if (inRange(bite, 0x21, 0x5F)) {
  1793. return 0xFF61 + bite - 0x21;
  1794. }
  1795. if (bite === EOF_byte) {
  1796. return EOF_code_point;
  1797. }
  1798. return decoderError(fatal);
  1799. }
  1800. };
  1801. }
  1802. /**
  1803. * @constructor
  1804. * @param {{fatal: boolean}} options
  1805. */
  1806. function ISO2022JPEncoder(options) {
  1807. var fatal = options.fatal;
  1808. /** @enum */
  1809. var state = {
  1810. ASCII: 0,
  1811. lead: 1,
  1812. Katakana: 2
  1813. };
  1814. var /** @type {number} */ iso2022jp_state = state.ASCII;
  1815. /**
  1816. * @param {ByteOutputStream} output_byte_stream Output byte stream.
  1817. * @param {CodePointInputStream} code_point_pointer Input stream.
  1818. * @return {number} The last byte emitted.
  1819. */
  1820. this.encode = function(output_byte_stream, code_point_pointer) {
  1821. var code_point = code_point_pointer.get();
  1822. if (code_point === EOF_code_point) {
  1823. return EOF_byte;
  1824. }
  1825. code_point_pointer.offset(1);
  1826. if ((inRange(code_point, 0x0000, 0x007F) ||
  1827. code_point === 0x00A5 || code_point === 0x203E) &&
  1828. iso2022jp_state !== state.ASCII) {
  1829. code_point_pointer.offset(-1);
  1830. iso2022jp_state = state.ASCII;
  1831. return output_byte_stream.emit(0x1B, 0x28, 0x42);
  1832. }
  1833. if (inRange(code_point, 0x0000, 0x007F)) {
  1834. return output_byte_stream.emit(code_point);
  1835. }
  1836. if (code_point === 0x00A5) {
  1837. return output_byte_stream.emit(0x5C);
  1838. }
  1839. if (code_point === 0x203E) {
  1840. return output_byte_stream.emit(0x7E);
  1841. }
  1842. if (inRange(code_point, 0xFF61, 0xFF9F) &&
  1843. iso2022jp_state !== state.Katakana) {
  1844. code_point_pointer.offset(-1);
  1845. iso2022jp_state = state.Katakana;
  1846. return output_byte_stream.emit(0x1B, 0x28, 0x49);
  1847. }
  1848. if (inRange(code_point, 0xFF61, 0xFF9F)) {
  1849. return output_byte_stream.emit(code_point - 0xFF61 - 0x21);
  1850. }
  1851. if (iso2022jp_state !== state.lead) {
  1852. code_point_pointer.offset(-1);
  1853. iso2022jp_state = state.lead;
  1854. return output_byte_stream.emit(0x1B, 0x24, 0x42);
  1855. }
  1856. var pointer = indexPointerFor(code_point, index('jis0208'));
  1857. if (pointer === null) {
  1858. return encoderError(code_point);
  1859. }
  1860. var lead = div(pointer, 94) + 0x21;
  1861. var trail = pointer % 94 + 0x21;
  1862. return output_byte_stream.emit(lead, trail);
  1863. };
  1864. }
  1865. /** @param {{fatal: boolean}} options */
  1866. name_to_encoding['iso-2022-jp'].getEncoder = function(options) {
  1867. return new ISO2022JPEncoder(options);
  1868. };
  1869. /** @param {{fatal: boolean}} options */
  1870. name_to_encoding['iso-2022-jp'].getDecoder = function(options) {
  1871. return new ISO2022JPDecoder(options);
  1872. };
  1873. // 12.3 shift_jis
  1874. /**
  1875. * @constructor
  1876. * @param {{fatal: boolean}} options
  1877. */
  1878. function ShiftJISDecoder(options) {
  1879. var fatal = options.fatal;
  1880. var /** @type {number} */ shiftjis_lead = 0x00;
  1881. /**
  1882. * @param {ByteInputStream} byte_pointer The byte stream to decode.
  1883. * @return {?number} The next code point decoded, or null if not enough
  1884. * data exists in the input stream to decode a complete code point.
  1885. */
  1886. this.decode = function(byte_pointer) {
  1887. var bite = byte_pointer.get();
  1888. if (bite === EOF_byte && shiftjis_lead === 0x00) {
  1889. return EOF_code_point;
  1890. }
  1891. if (bite === EOF_byte && shiftjis_lead !== 0x00) {
  1892. shiftjis_lead = 0x00;
  1893. return decoderError(fatal);
  1894. }
  1895. byte_pointer.offset(1);
  1896. if (shiftjis_lead !== 0x00) {
  1897. var lead = shiftjis_lead;
  1898. shiftjis_lead = 0x00;
  1899. if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFC)) {
  1900. var offset = (bite < 0x7F) ? 0x40 : 0x41;
  1901. var lead_offset = (lead < 0xA0) ? 0x81 : 0xC1;
  1902. var code_point = indexCodePointFor((lead - lead_offset) * 188 +
  1903. bite - offset, index('jis0208'));
  1904. if (code_point === null) {
  1905. return decoderError(fatal);
  1906. }
  1907. return code_point;
  1908. }
  1909. byte_pointer.offset(-1);
  1910. return decoderError(fatal);
  1911. }
  1912. if (inRange(bite, 0x00, 0x80)) {
  1913. return bite;
  1914. }
  1915. if (inRange(bite, 0xA1, 0xDF)) {
  1916. return 0xFF61 + bite - 0xA1;
  1917. }
  1918. if (inRange(bite, 0x81, 0x9F) || inRange(bite, 0xE0, 0xFC)) {
  1919. shiftjis_lead = bite;
  1920. return null;
  1921. }
  1922. return decoderError(fatal);
  1923. };
  1924. }
  1925. /**
  1926. * @constructor
  1927. * @param {{fatal: boolean}} options
  1928. */
  1929. function ShiftJISEncoder(options) {
  1930. var fatal = options.fatal;
  1931. /**
  1932. * @param {ByteOutputStream} output_byte_stream Output byte stream.
  1933. * @param {CodePointInputStream} code_point_pointer Input stream.
  1934. * @return {number} The last byte emitted.
  1935. */
  1936. this.encode = function(output_byte_stream, code_point_pointer) {
  1937. var code_point = code_point_pointer.get();
  1938. if (code_point === EOF_code_point) {
  1939. return EOF_byte;
  1940. }
  1941. code_point_pointer.offset(1);
  1942. if (inRange(code_point, 0x0000, 0x0080)) {
  1943. return output_byte_stream.emit(code_point);
  1944. }
  1945. if (code_point === 0x00A5) {
  1946. return output_byte_stream.emit(0x5C);
  1947. }
  1948. if (code_point === 0x203E) {
  1949. return output_byte_stream.emit(0x7E);
  1950. }
  1951. if (inRange(code_point, 0xFF61, 0xFF9F)) {
  1952. return output_byte_stream.emit(code_point - 0xFF61 + 0xA1);
  1953. }
  1954. var pointer = indexPointerFor(code_point, index('jis0208'));
  1955. if (pointer === null) {
  1956. return encoderError(code_point);
  1957. }
  1958. var lead = div(pointer, 188);
  1959. var lead_offset = lead < 0x1F ? 0x81 : 0xC1;
  1960. var trail = pointer % 188;
  1961. var offset = trail < 0x3F ? 0x40 : 0x41;
  1962. return output_byte_stream.emit(lead + lead_offset, trail + offset);
  1963. };
  1964. }
  1965. /** @param {{fatal: boolean}} options */
  1966. name_to_encoding['shift_jis'].getEncoder = function(options) {
  1967. return new ShiftJISEncoder(options);
  1968. };
  1969. /** @param {{fatal: boolean}} options */
  1970. name_to_encoding['shift_jis'].getDecoder = function(options) {
  1971. return new ShiftJISDecoder(options);
  1972. };
  1973. //
  1974. // 13. Legacy multi-byte Korean encodings
  1975. //
  1976. // 13.1 euc-kr
  1977. /**
  1978. * @constructor
  1979. * @param {{fatal: boolean}} options
  1980. */
  1981. function EUCKRDecoder(options) {
  1982. var fatal = options.fatal;
  1983. var /** @type {number} */ euckr_lead = 0x00;
  1984. /**
  1985. * @param {ByteInputStream} byte_pointer The byte stream to decode.
  1986. * @return {?number} The next code point decoded, or null if not enough
  1987. * data exists in the input stream to decode a complete code point.
  1988. */
  1989. this.decode = function(byte_pointer) {
  1990. var bite = byte_pointer.get();
  1991. if (bite === EOF_byte && euckr_lead === 0) {
  1992. return EOF_code_point;
  1993. }
  1994. if (bite === EOF_byte && euckr_lead !== 0) {
  1995. euckr_lead = 0x00;
  1996. return decoderError(fatal);
  1997. }
  1998. byte_pointer.offset(1);
  1999. if (euckr_lead !== 0x00) {
  2000. var lead = euckr_lead;
  2001. var pointer = null;
  2002. euckr_lead = 0x00;
  2003. if (inRange(lead, 0x81, 0xC6)) {
  2004. var temp = (26 + 26 + 126) * (lead - 0x81);
  2005. if (inRange(bite, 0x41, 0x5A)) {
  2006. pointer = temp + bite - 0x41;
  2007. } else if (inRange(bite, 0x61, 0x7A)) {
  2008. pointer = temp + 26 + bite - 0x61;
  2009. } else if (inRange(bite, 0x81, 0xFE)) {
  2010. pointer = temp + 26 + 26 + bite - 0x81;
  2011. }
  2012. }
  2013. if (inRange(lead, 0xC7, 0xFD) && inRange(bite, 0xA1, 0xFE)) {
  2014. pointer = (26 + 26 + 126) * (0xC7 - 0x81) + (lead - 0xC7) * 94 +
  2015. (bite - 0xA1);
  2016. }
  2017. var code_point = (pointer === null) ? null :
  2018. indexCodePointFor(pointer, index('euc-kr'));
  2019. if (pointer === null) {
  2020. byte_pointer.offset(-1);
  2021. }
  2022. if (code_point === null) {
  2023. return decoderError(fatal);
  2024. }
  2025. return code_point;
  2026. }
  2027. if (inRange(bite, 0x00, 0x7F)) {
  2028. return bite;
  2029. }
  2030. if (inRange(bite, 0x81, 0xFD)) {
  2031. euckr_lead = bite;
  2032. return null;
  2033. }
  2034. return decoderError(fatal);
  2035. };
  2036. }
  2037. /**
  2038. * @constructor
  2039. * @param {{fatal: boolean}} options
  2040. */
  2041. function EUCKREncoder(options) {
  2042. var fatal = options.fatal;
  2043. /**
  2044. * @param {ByteOutputStream} output_byte_stream Output byte stream.
  2045. * @param {CodePointInputStream} code_point_pointer Input stream.
  2046. * @return {number} The last byte emitted.
  2047. */
  2048. this.encode = function(output_byte_stream, code_point_pointer) {
  2049. var code_point = code_point_pointer.get();
  2050. if (code_point === EOF_code_point) {
  2051. return EOF_byte;
  2052. }
  2053. code_point_pointer.offset(1);
  2054. if (inRange(code_point, 0x0000, 0x007F)) {
  2055. return output_byte_stream.emit(code_point);
  2056. }
  2057. var pointer = indexPointerFor(code_point, index('euc-kr'));
  2058. if (pointer === null) {
  2059. return encoderError(code_point);
  2060. }
  2061. var lead, trail;
  2062. if (pointer < ((26 + 26 + 126) * (0xC7 - 0x81))) {
  2063. lead = div(pointer, (26 + 26 + 126)) + 0x81;
  2064. trail = pointer % (26 + 26 + 126);
  2065. var offset = trail < 26 ? 0x41 : trail < 26 + 26 ? 0x47 : 0x4D;
  2066. return output_byte_stream.emit(lead, trail + offset);
  2067. }
  2068. pointer = pointer - (26 + 26 + 126) * (0xC7 - 0x81);
  2069. lead = div(pointer, 94) + 0xC7;
  2070. trail = pointer % 94 + 0xA1;
  2071. return output_byte_stream.emit(lead, trail);
  2072. };
  2073. }
  2074. /** @param {{fatal: boolean}} options */
  2075. name_to_encoding['euc-kr'].getEncoder = function(options) {
  2076. return new EUCKREncoder(options);
  2077. };
  2078. /** @param {{fatal: boolean}} options */
  2079. name_to_encoding['euc-kr'].getDecoder = function(options) {
  2080. return new EUCKRDecoder(options);
  2081. };
  2082. //
  2083. // 14. Legacy miscellaneous encodings
  2084. //
  2085. // 14.1 replacement
  2086. // Not needed - API throws TypeError
  2087. // 14.2 utf-16
  2088. /**
  2089. * @constructor
  2090. * @param {boolean} utf16_be True if big-endian, false if little-endian.
  2091. * @param {{fatal: boolean}} options
  2092. */
  2093. function UTF16Decoder(utf16_be, options) {
  2094. var fatal = options.fatal;
  2095. var /** @type {?number} */ utf16_lead_byte = null,
  2096. /** @type {?number} */ utf16_lead_surrogate = null;
  2097. /**
  2098. * @param {ByteInputStream} byte_pointer The byte stream to decode.
  2099. * @return {?number} The next code point decoded, or null if not enough
  2100. * data exists in the input stream to decode a complete code point.
  2101. */
  2102. this.decode = function(byte_pointer) {
  2103. var bite = byte_pointer.get();
  2104. if (bite === EOF_byte && utf16_lead_byte === null &&
  2105. utf16_lead_surrogate === null) {
  2106. return EOF_code_point;
  2107. }
  2108. if (bite === EOF_byte && (utf16_lead_byte !== null ||
  2109. utf16_lead_surrogate !== null)) {
  2110. return decoderError(fatal);
  2111. }
  2112. byte_pointer.offset(1);
  2113. if (utf16_lead_byte === null) {
  2114. utf16_lead_byte = bite;
  2115. return null;
  2116. }
  2117. var code_point;
  2118. if (utf16_be) {
  2119. code_point = (utf16_lead_byte << 8) + bite;
  2120. } else {
  2121. code_point = (bite << 8) + utf16_lead_byte;
  2122. }
  2123. utf16_lead_byte = null;
  2124. if (utf16_lead_surrogate !== null) {
  2125. var lead_surrogate = utf16_lead_surrogate;
  2126. utf16_lead_surrogate = null;
  2127. if (inRange(code_point, 0xDC00, 0xDFFF)) {
  2128. return 0x10000 + (lead_surrogate - 0xD800) * 0x400 +
  2129. (code_point - 0xDC00);
  2130. }
  2131. byte_pointer.offset(-2);
  2132. return decoderError(fatal);
  2133. }
  2134. if (inRange(code_point, 0xD800, 0xDBFF)) {
  2135. utf16_lead_surrogate = code_point;
  2136. return null;
  2137. }
  2138. if (inRange(code_point, 0xDC00, 0xDFFF)) {
  2139. return decoderError(fatal);
  2140. }
  2141. return code_point;
  2142. };
  2143. }
  2144. /**
  2145. * @constructor
  2146. * @param {boolean} utf16_be True if big-endian, false if little-endian.
  2147. * @param {{fatal: boolean}} options
  2148. */
  2149. function UTF16Encoder(utf16_be, options) {
  2150. var fatal = options.fatal;
  2151. /**
  2152. * @param {ByteOutputStream} output_byte_stream Output byte stream.
  2153. * @param {CodePointInputStream} code_point_pointer Input stream.
  2154. * @return {number} The last byte emitted.
  2155. */
  2156. this.encode = function(output_byte_stream, code_point_pointer) {
  2157. /**
  2158. * @param {number} code_unit
  2159. * @return {number} last byte emitted
  2160. */
  2161. function convert_to_bytes(code_unit) {
  2162. var byte1 = code_unit >> 8;
  2163. var byte2 = code_unit & 0x00FF;
  2164. if (utf16_be) {
  2165. return output_byte_stream.emit(byte1, byte2);
  2166. }
  2167. return output_byte_stream.emit(byte2, byte1);
  2168. }
  2169. var code_point = code_point_pointer.get();
  2170. if (code_point === EOF_code_point) {
  2171. return EOF_byte;
  2172. }
  2173. code_point_pointer.offset(1);
  2174. if (inRange(code_point, 0xD800, 0xDFFF)) {
  2175. encoderError(code_point);
  2176. }
  2177. if (code_point <= 0xFFFF) {
  2178. return convert_to_bytes(code_point);
  2179. }
  2180. var lead = div((code_point - 0x10000), 0x400) + 0xD800;
  2181. var trail = ((code_point - 0x10000) % 0x400) + 0xDC00;
  2182. convert_to_bytes(lead);
  2183. return convert_to_bytes(trail);
  2184. };
  2185. }
  2186. // 14.3 utf-16be
  2187. /** @param {{fatal: boolean}} options */
  2188. name_to_encoding['utf-16be'].getEncoder = function(options) {
  2189. return new UTF16Encoder(true, options);
  2190. };
  2191. /** @param {{fatal: boolean}} options */
  2192. name_to_encoding['utf-16be'].getDecoder = function(options) {
  2193. return new UTF16Decoder(true, options);
  2194. };
  2195. // 14.4 utf-16le
  2196. /** @param {{fatal: boolean}} options */
  2197. name_to_encoding['utf-16le'].getEncoder = function(options) {
  2198. return new UTF16Encoder(false, options);
  2199. };
  2200. /** @param {{fatal: boolean}} options */
  2201. name_to_encoding['utf-16le'].getDecoder = function(options) {
  2202. return new UTF16Decoder(false, options);
  2203. };
  2204. // 14.5 x-user-defined
  2205. /**
  2206. * @constructor
  2207. * @param {{fatal: boolean}} options
  2208. */
  2209. function XUserDefinedDecoder(options) {
  2210. var fatal = options.fatal;
  2211. /**
  2212. * @param {ByteInputStream} byte_pointer The byte stream to decode.
  2213. * @return {?number} The next code point decoded, or null if not enough
  2214. * data exists in the input stream to decode a complete code point.
  2215. */
  2216. this.decode = function(byte_pointer) {
  2217. var bite = byte_pointer.get();
  2218. if (bite === EOF_byte) {
  2219. return EOF_code_point;
  2220. }
  2221. byte_pointer.offset(1);
  2222. if (inRange(bite, 0x00, 0x7F)) {
  2223. return bite;
  2224. }
  2225. return 0xF780 + bite - 0x80;
  2226. };
  2227. }
  2228. /**
  2229. * @constructor
  2230. * @param {{fatal: boolean}} options
  2231. */
  2232. function XUserDefinedEncoder(index, options) {
  2233. var fatal = options.fatal;
  2234. /**
  2235. * @param {ByteOutputStream} output_byte_stream Output byte stream.
  2236. * @param {CodePointInputStream} code_point_pointer Input stream.
  2237. * @return {number} The last byte emitted.
  2238. */
  2239. this.encode = function(output_byte_stream, code_point_pointer) {
  2240. var code_point = code_point_pointer.get();
  2241. if (code_point === EOF_code_point) {
  2242. return EOF_byte;
  2243. }
  2244. code_point_pointer.offset(1);
  2245. if (inRange(code_point, 0x0000, 0x007F)) {
  2246. return output_byte_stream.emit(code_point);
  2247. }
  2248. if (inRange(code_point, 0xF780, 0xF7FF)) {
  2249. return output_byte_stream.emit(code_point - 0xF780 + 0x80);
  2250. }
  2251. encoderError(code_point);
  2252. };
  2253. }
  2254. /** @param {{fatal: boolean}} options */
  2255. name_to_encoding['x-user-defined'].getEncoder = function(options) {
  2256. return new XUserDefinedEncoder(false, options);
  2257. };
  2258. /** @param {{fatal: boolean}} options */
  2259. name_to_encoding['x-user-defined'].getDecoder = function(options) {
  2260. return new XUserDefinedDecoder(false, options);
  2261. };
  2262. // NOTE: currently unused
  2263. /**
  2264. * @param {string} label The encoding label.
  2265. * @param {ByteInputStream} input_stream The byte stream to test.
  2266. */
  2267. function detectEncoding(label, input_stream) {
  2268. if (input_stream.match([0xFF, 0xFE])) {
  2269. input_stream.offset(2);
  2270. return 'utf-16le';
  2271. }
  2272. if (input_stream.match([0xFE, 0xFF])) {
  2273. input_stream.offset(2);
  2274. return 'utf-16be';
  2275. }
  2276. if (input_stream.match([0xEF, 0xBB, 0xBF])) {
  2277. input_stream.offset(3);
  2278. return 'utf-8';
  2279. }
  2280. return label;
  2281. }
  2282. if (!('TextEncoder' in global)) global['TextEncoder'] = TextEncoder;
  2283. if (!('TextDecoder' in global)) global['TextDecoder'] = TextDecoder;
  2284. }(this));