123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391 |
- /*
- Modifications for better node.js integration:
- Copyright 2014 Brian White. All rights reserved.
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to
- deal in the Software without restriction, including without limitation the
- rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- sell copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- IN THE SOFTWARE.
- */
- /*
- Original source code:
- Copyright 2014 Joshua Bell
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- //
- // Utilities
- //
- /**
- * @param {number} a The number to test.
- * @param {number} min The minimum value in the range, inclusive.
- * @param {number} max The maximum value in the range, inclusive.
- * @return {boolean} True if a >= min and a <= max.
- */
- function inRange(a, min, max) {
- return min <= a && a <= max;
- }
- /**
- * @param {number} n The numerator.
- * @param {number} d The denominator.
- * @return {number} The result of the integer division of n by d.
- */
- function div(n, d) {
- return Math.floor(n / d);
- }
- //
- // Implementation of Encoding specification
- // http://dvcs.w3.org/hg/encoding/raw-file/tip/Overview.html
- //
- //
- // 3. Terminology
- //
- //
- // 4. Encodings
- //
- /** @const */ var EOF_byte = -1;
- /** @const */ var EOF_code_point = -1;
- /**
- * @constructor
- * @param {Buffer} bytes Array of bytes that provide the stream.
- */
- function ByteInputStream(bytes) {
- /** @type {number} */
- var pos = 0;
- /**
- * @this {ByteInputStream}
- * @return {number} Get the next byte from the stream.
- */
- this.get = function() {
- return (pos >= bytes.length) ? EOF_byte : Number(bytes[pos]);
- };
- /** @param {number} n Number (positive or negative) by which to
- * offset the byte pointer. */
- this.offset = function(n) {
- pos += n;
- if (pos < 0) {
- throw new Error('Seeking past start of the buffer');
- }
- if (pos > bytes.length) {
- throw new Error('Seeking past EOF');
- }
- };
- /**
- * @param {Array.<number>} test Array of bytes to compare against.
- * @return {boolean} True if the start of the stream matches the test
- * bytes.
- */
- this.match = function(test) {
- if (test.length > pos + bytes.length) {
- return false;
- }
- var i;
- for (i = 0; i < test.length; i += 1) {
- if (Number(bytes[pos + i]) !== test[i]) {
- return false;
- }
- }
- return true;
- };
- }
- /**
- * @constructor
- * @param {Array.<number>} bytes The array to write bytes into.
- */
- function ByteOutputStream(bytes) {
- /** @type {number} */
- var pos = 0;
- /**
- * @param {...number} var_args The byte or bytes to emit into the stream.
- * @return {number} The last byte emitted.
- */
- this.emit = function(var_args) {
- /** @type {number} */
- var last = EOF_byte;
- var i;
- for (i = 0; i < arguments.length; ++i) {
- last = Number(arguments[i]);
- bytes[pos++] = last;
- }
- return last;
- };
- }
- /**
- * @constructor
- * @param {string} string The source of code units for the stream.
- */
- function CodePointInputStream(string) {
- /**
- * @param {string} string Input string of UTF-16 code units.
- * @return {Array.<number>} Code points.
- */
- function stringToCodePoints(string) {
- /** @type {Array.<number>} */
- var cps = [];
- // Based on http://www.w3.org/TR/WebIDL/#idl-DOMString
- var i = 0, n = string.length;
- while (i < string.length) {
- var c = string.charCodeAt(i);
- if (!inRange(c, 0xD800, 0xDFFF)) {
- cps.push(c);
- } else if (inRange(c, 0xDC00, 0xDFFF)) {
- cps.push(0xFFFD);
- } else { // (inRange(cu, 0xD800, 0xDBFF))
- if (i === n - 1) {
- cps.push(0xFFFD);
- } else {
- var d = string.charCodeAt(i + 1);
- if (inRange(d, 0xDC00, 0xDFFF)) {
- var a = c & 0x3FF;
- var b = d & 0x3FF;
- i += 1;
- cps.push(0x10000 + (a << 10) + b);
- } else {
- cps.push(0xFFFD);
- }
- }
- }
- i += 1;
- }
- return cps;
- }
- /** @type {number} */
- var pos = 0;
- /** @type {Array.<number>} */
- var cps = stringToCodePoints(string);
- /** @param {number} n The number of bytes (positive or negative)
- * to advance the code point pointer by.*/
- this.offset = function(n) {
- pos += n;
- if (pos < 0) {
- throw new Error('Seeking past start of the buffer');
- }
- if (pos > cps.length) {
- throw new Error('Seeking past EOF');
- }
- };
- /** @return {number} Get the next code point from the stream. */
- this.get = function() {
- if (pos >= cps.length) {
- return EOF_code_point;
- }
- return cps[pos];
- };
- }
- /**
- * @constructor
- */
- function CodePointOutputStream() {
- /** @type {string} */
- var string = '';
- /** @return {string} The accumulated string. */
- this.string = function() {
- return string;
- };
- /** @param {number} c The code point to encode into the stream. */
- this.emit = function(c) {
- if (c <= 0xFFFF) {
- string += String.fromCharCode(c);
- } else {
- c -= 0x10000;
- string += String.fromCharCode(0xD800 + ((c >> 10) & 0x3ff));
- string += String.fromCharCode(0xDC00 + (c & 0x3ff));
- }
- };
- }
- /**
- * @constructor
- * @param {string} message Description of the error.
- */
- function EncodingError(message) {
- this.name = 'EncodingError';
- this.message = message;
- this.code = 0;
- }
- EncodingError.prototype = Error.prototype;
- /**
- * @param {boolean} fatal If true, decoding errors raise an exception.
- * @param {number=} opt_code_point Override the standard fallback code point.
- * @return {number} The code point to insert on a decoding error.
- */
- function decoderError(fatal, opt_code_point) {
- if (fatal) {
- throw new EncodingError('Decoder error');
- }
- return opt_code_point || 0xFFFD;
- }
- /**
- * @param {number} code_point The code point that could not be encoded.
- * @return {number} Always throws, no value is actually returned.
- */
- function encoderError(code_point) {
- throw new EncodingError('The code point ' + code_point +
- ' could not be encoded.');
- }
- /**
- * @param {string} label The encoding label.
- * @return {?{name:string,labels:Array.<string>}}
- */
- function getEncoding(label) {
- label = String(label).trim().toLowerCase();
- if (Object.prototype.hasOwnProperty.call(label_to_encoding, label)) {
- return label_to_encoding[label];
- }
- return null;
- }
- /** @type {Array.<{encodings: Array.<{name:string,labels:Array.<string>}>,
- * heading: string}>} */
- var encodings = [
- {
- "encodings": [
- {
- "labels": [
- "unicode-1-1-utf-8",
- "utf-8",
- "utf8"
- ],
- "name": "utf-8"
- }
- ],
- "heading": "The Encoding"
- },
- {
- "encodings": [
- {
- "labels": [
- "864",
- "cp864",
- "csibm864",
- "ibm864"
- ],
- "name": "ibm864"
- },
- {
- "labels": [
- "866",
- "cp866",
- "csibm866",
- "ibm866"
- ],
- "name": "ibm866"
- },
- {
- "labels": [
- "csisolatin2",
- "iso-8859-2",
- "iso-ir-101",
- "iso8859-2",
- "iso88592",
- "iso_8859-2",
- "iso_8859-2:1987",
- "l2",
- "latin2"
- ],
- "name": "iso-8859-2"
- },
- {
- "labels": [
- "csisolatin3",
- "iso-8859-3",
- "iso-ir-109",
- "iso8859-3",
- "iso88593",
- "iso_8859-3",
- "iso_8859-3:1988",
- "l3",
- "latin3"
- ],
- "name": "iso-8859-3"
- },
- {
- "labels": [
- "csisolatin4",
- "iso-8859-4",
- "iso-ir-110",
- "iso8859-4",
- "iso88594",
- "iso_8859-4",
- "iso_8859-4:1988",
- "l4",
- "latin4"
- ],
- "name": "iso-8859-4"
- },
- {
- "labels": [
- "csisolatincyrillic",
- "cyrillic",
- "iso-8859-5",
- "iso-ir-144",
- "iso8859-5",
- "iso88595",
- "iso_8859-5",
- "iso_8859-5:1988"
- ],
- "name": "iso-8859-5"
- },
- {
- "labels": [
- "arabic",
- "asmo-708",
- "csiso88596e",
- "csiso88596i",
- "csisolatinarabic",
- "ecma-114",
- "iso-8859-6",
- "iso-8859-6-e",
- "iso-8859-6-i",
- "iso-ir-127",
- "iso8859-6",
- "iso88596",
- "iso_8859-6",
- "iso_8859-6:1987"
- ],
- "name": "iso-8859-6"
- },
- {
- "labels": [
- "csisolatingreek",
- "ecma-118",
- "elot_928",
- "greek",
- "greek8",
- "iso-8859-7",
- "iso-ir-126",
- "iso8859-7",
- "iso88597",
- "iso_8859-7",
- "iso_8859-7:1987",
- "sun_eu_greek"
- ],
- "name": "iso-8859-7"
- },
- {
- "labels": [
- "csiso88598e",
- "csisolatinhebrew",
- "hebrew",
- "iso-8859-8",
- "iso-8859-8-e",
- "iso-ir-138",
- "iso8859-8",
- "iso88598",
- "iso_8859-8",
- "iso_8859-8:1988",
- "visual"
- ],
- "name": "iso-8859-8"
- },
- {
- "labels": [
- "csiso88598i",
- "iso-8859-8-i",
- "logical"
- ],
- "name": "iso-8859-8-i"
- },
- {
- "labels": [
- "csisolatin6",
- "iso-8859-10",
- "iso-ir-157",
- "iso8859-10",
- "iso885910",
- "l6",
- "latin6"
- ],
- "name": "iso-8859-10"
- },
- {
- "labels": [
- "iso-8859-13",
- "iso8859-13",
- "iso885913"
- ],
- "name": "iso-8859-13"
- },
- {
- "labels": [
- "iso-8859-14",
- "iso8859-14",
- "iso885914"
- ],
- "name": "iso-8859-14"
- },
- {
- "labels": [
- "csisolatin9",
- "iso-8859-15",
- "iso8859-15",
- "iso885915",
- "iso_8859-15",
- "l9"
- ],
- "name": "iso-8859-15"
- },
- {
- "labels": [
- "iso-8859-16"
- ],
- "name": "iso-8859-16"
- },
- {
- "labels": [
- "cskoi8r",
- "koi",
- "koi8",
- "koi8-r",
- "koi8_r"
- ],
- "name": "koi8-r"
- },
- {
- "labels": [
- "koi8-u"
- ],
- "name": "koi8-u"
- },
- {
- "labels": [
- "csmacintosh",
- "mac",
- "macintosh",
- "x-mac-roman"
- ],
- "name": "macintosh"
- },
- {
- "labels": [
- "dos-874",
- "iso-8859-11",
- "iso8859-11",
- "iso885911",
- "tis-620",
- "windows-874"
- ],
- "name": "windows-874"
- },
- {
- "labels": [
- "cp1250",
- "windows-1250",
- "x-cp1250"
- ],
- "name": "windows-1250"
- },
- {
- "labels": [
- "cp1251",
- "windows-1251",
- "x-cp1251"
- ],
- "name": "windows-1251"
- },
- {
- "labels": [
- "ansi_x3.4-1968",
- "ascii",
- "cp1252",
- "cp819",
- "csisolatin1",
- "ibm819",
- "iso-8859-1",
- "iso-ir-100",
- "iso8859-1",
- "iso88591",
- "iso_8859-1",
- "iso_8859-1:1987",
- "l1",
- "latin1",
- "us-ascii",
- "windows-1252",
- "x-cp1252"
- ],
- "name": "windows-1252"
- },
- {
- "labels": [
- "cp1253",
- "windows-1253",
- "x-cp1253"
- ],
- "name": "windows-1253"
- },
- {
- "labels": [
- "cp1254",
- "csisolatin5",
- "iso-8859-9",
- "iso-ir-148",
- "iso8859-9",
- "iso88599",
- "iso_8859-9",
- "iso_8859-9:1989",
- "l5",
- "latin5",
- "windows-1254",
- "x-cp1254"
- ],
- "name": "windows-1254"
- },
- {
- "labels": [
- "cp1255",
- "windows-1255",
- "x-cp1255"
- ],
- "name": "windows-1255"
- },
- {
- "labels": [
- "cp1256",
- "windows-1256",
- "x-cp1256"
- ],
- "name": "windows-1256"
- },
- {
- "labels": [
- "cp1257",
- "windows-1257",
- "x-cp1257"
- ],
- "name": "windows-1257"
- },
- {
- "labels": [
- "cp1258",
- "windows-1258",
- "x-cp1258"
- ],
- "name": "windows-1258"
- },
- {
- "labels": [
- "x-mac-cyrillic",
- "x-mac-ukrainian"
- ],
- "name": "x-mac-cyrillic"
- }
- ],
- "heading": "Legacy single-byte encodings"
- },
- {
- "encodings": [
- {
- "labels": [
- "chinese",
- "csgb2312",
- "csiso58gb231280",
- "gb2312",
- "gb_2312",
- "gb_2312-80",
- "gbk",
- "iso-ir-58",
- "x-gbk"
- ],
- "name": "gbk"
- },
- {
- "labels": [
- "gb18030"
- ],
- "name": "gb18030"
- },
- {
- "labels": [
- "hz-gb-2312"
- ],
- "name": "hz-gb-2312"
- }
- ],
- "heading": "Legacy multi-byte Chinese (simplified) encodings"
- },
- {
- "encodings": [
- {
- "labels": [
- "big5",
- "big5-hkscs",
- "cn-big5",
- "csbig5",
- "x-x-big5"
- ],
- "name": "big5"
- }
- ],
- "heading": "Legacy multi-byte Chinese (traditional) encodings"
- },
- {
- "encodings": [
- {
- "labels": [
- "cseucpkdfmtjapanese",
- "euc-jp",
- "x-euc-jp"
- ],
- "name": "euc-jp"
- },
- {
- "labels": [
- "csiso2022jp",
- "iso-2022-jp"
- ],
- "name": "iso-2022-jp"
- },
- {
- "labels": [
- "csshiftjis",
- "ms_kanji",
- "shift-jis",
- "shift_jis",
- "sjis",
- "windows-31j",
- "x-sjis"
- ],
- "name": "shift_jis"
- }
- ],
- "heading": "Legacy multi-byte Japanese encodings"
- },
- {
- "encodings": [
- {
- "labels": [
- "cseuckr",
- "csksc56011987",
- "euc-kr",
- "iso-ir-149",
- "korean",
- "ks_c_5601-1987",
- "ks_c_5601-1989",
- "ksc5601",
- "ksc_5601",
- "windows-949"
- ],
- "name": "euc-kr"
- }
- ],
- "heading": "Legacy multi-byte Korean encodings"
- },
- {
- "encodings": [
- {
- "labels": [
- "csiso2022kr",
- "iso-2022-cn",
- "iso-2022-cn-ext",
- "iso-2022-kr"
- ],
- "name": "replacement"
- },
- {
- "labels": [
- "utf-16be"
- ],
- "name": "utf-16be"
- },
- {
- "labels": [
- "utf-16",
- "utf-16le"
- ],
- "name": "utf-16le"
- },
- {
- "labels": [
- "x-user-defined"
- ],
- "name": "x-user-defined"
- }
- ],
- "heading": "Legacy miscellaneous encodings"
- }
- ];
- var name_to_encoding = {};
- var label_to_encoding = {};
- encodings.forEach(function(category) {
- category.encodings.forEach(function(encoding) {
- name_to_encoding[encoding.name] = encoding;
- encoding.labels.forEach(function(label) {
- label_to_encoding[label] = encoding;
- });
- });
- });
- //
- // 5. Indexes
- //
- /**
- * @param {number} pointer The |pointer| to search for.
- * @param {Array.<?number>|undefined} index The |index| to search within.
- * @return {?number} The code point corresponding to |pointer| in |index|,
- * or null if |code point| is not in |index|.
- */
- function indexCodePointFor(pointer, index) {
- if (!index) return null;
- return index[pointer] || null;
- }
- /**
- * @param {number} code_point The |code point| to search for.
- * @param {Array.<?number>} index The |index| to search within.
- * @return {?number} The first pointer corresponding to |code point| in
- * |index|, or null if |code point| is not in |index|.
- */
- function indexPointerFor(code_point, index) {
- var pointer = index.indexOf(code_point);
- return pointer === -1 ? null : pointer;
- }
- /** @type {Object.<string, (Array.<number>|Array.<Array.<number>>)>} */
- var indexes = require('./encoding-indexes');
- /**
- * @param {number} pointer The |pointer| to search for in the gb18030 index.
- * @return {?number} The code point corresponding to |pointer| in |index|,
- * or null if |code point| is not in the gb18030 index.
- */
- function indexGB18030CodePointFor(pointer) {
- if ((pointer > 39419 && pointer < 189000) || (pointer > 1237575)) {
- return null;
- }
- var /** @type {number} */ offset = 0,
- /** @type {number} */ code_point_offset = 0,
- /** @type {Array.<Array.<number>>} */ idx = indexes['gb18030'];
- var i;
- for (i = 0; i < idx.length; ++i) {
- var entry = idx[i];
- if (entry[0] <= pointer) {
- offset = entry[0];
- code_point_offset = entry[1];
- } else {
- break;
- }
- }
- return code_point_offset + pointer - offset;
- }
- /**
- * @param {number} code_point The |code point| to locate in the gb18030 index.
- * @return {number} The first pointer corresponding to |code point| in the
- * gb18030 index.
- */
- function indexGB18030PointerFor(code_point) {
- var /** @type {number} */ offset = 0,
- /** @type {number} */ pointer_offset = 0,
- /** @type {Array.<Array.<number>>} */ idx = indexes['gb18030'];
- var i;
- for (i = 0; i < idx.length; ++i) {
- var entry = idx[i];
- if (entry[1] <= code_point) {
- offset = entry[1];
- pointer_offset = entry[0];
- } else {
- break;
- }
- }
- return pointer_offset + code_point - offset;
- }
- //
- // 7. API
- //
- /** @const */ var DEFAULT_ENCODING = 'utf-8';
- // 7.1 Interface TextDecoder
- /**
- * @constructor
- * @param {string=} opt_encoding The label of the encoding;
- * defaults to 'utf-8'.
- * @param {{fatal: boolean}=} options
- */
- function TextDecoder(opt_encoding, options) {
- if (!(this instanceof TextDecoder)) {
- return new TextDecoder(opt_encoding, options);
- }
- opt_encoding = opt_encoding ? String(opt_encoding) : DEFAULT_ENCODING;
- options = Object(options);
- /** @private */
- this._encoding = getEncoding(opt_encoding);
- if (this._encoding === null || this._encoding.name === 'replacement')
- throw new TypeError('Unknown encoding: ' + opt_encoding);
- /** @private @type {boolean} */
- this._streaming = false;
- /** @private @type {boolean} */
- this._BOMseen = false;
- /** @private */
- this._decoder = null;
- /** @private @type {{fatal: boolean}=} */
- this._options = { fatal: Boolean(options.fatal) };
- if (Object.defineProperty) {
- Object.defineProperty(
- this, 'encoding',
- { get: function() { return this._encoding.name; } });
- } else {
- this.encoding = this._encoding.name;
- }
- return this;
- }
- // TODO: Issue if input byte stream is offset by decoder
- // TODO: BOM detection will not work if stream header spans multiple calls
- // (last N bytes of previous stream may need to be retained?)
- TextDecoder.prototype = {
- /**
- * @param {Buffer=} bytes The buffer of bytes to decode.
- * @param {{stream: boolean}=} options
- */
- decode: function decode(bytes, options) {
- options = Object(options);
- if (!this._streaming) {
- this._decoder = this._encoding.getDecoder(this._options);
- this._BOMseen = false;
- }
- this._streaming = Boolean(options.stream);
- var input_stream = new ByteInputStream(bytes);
- var output_stream = new CodePointOutputStream();
- /** @type {number} */
- var code_point;
- while (input_stream.get() !== EOF_byte) {
- code_point = this._decoder.decode(input_stream);
- if (code_point !== null && code_point !== EOF_code_point) {
- output_stream.emit(code_point);
- }
- }
- if (!this._streaming) {
- do {
- code_point = this._decoder.decode(input_stream);
- if (code_point !== null && code_point !== EOF_code_point) {
- output_stream.emit(code_point);
- }
- } while (code_point !== EOF_code_point &&
- input_stream.get() != EOF_byte);
- this._decoder = null;
- }
- var result = output_stream.string();
- if (!this._BOMseen && result.length) {
- this._BOMseen = true;
- if (UTFs.indexOf(this.encoding) !== -1 &&
- result.charCodeAt(0) === 0xFEFF) {
- result = result.substring(1);
- }
- }
- return result;
- }
- };
- var UTFs = ['utf-8', 'utf-16le', 'utf-16be'];
- // 7.2 Interface TextEncoder
- /**
- * @constructor
- * @param {string=} opt_encoding The label of the encoding;
- * defaults to 'utf-8'.
- * @param {{fatal: boolean}=} options
- */
- function TextEncoder(opt_encoding, options) {
- if (!(this instanceof TextEncoder)) {
- return new TextEncoder(opt_encoding, options);
- }
- opt_encoding = opt_encoding ? String(opt_encoding) : DEFAULT_ENCODING;
- options = Object(options);
- /** @private */
- this._encoding = getEncoding(opt_encoding);
- if (this._encoding === null || (this._encoding.name !== 'utf-8' &&
- this._encoding.name !== 'utf-16le' &&
- this._encoding.name !== 'utf-16be'))
- throw new TypeError('Unknown encoding: ' + opt_encoding);
- /** @private @type {boolean} */
- this._streaming = false;
- /** @private */
- this._encoder = null;
- /** @private @type {{fatal: boolean}=} */
- this._options = { fatal: Boolean(options.fatal) };
- if (Object.defineProperty) {
- Object.defineProperty(
- this, 'encoding',
- { get: function() { return this._encoding.name; } });
- } else {
- this.encoding = this._encoding.name;
- }
- return this;
- }
- TextEncoder.prototype = {
- /**
- * @param {string=} opt_string The string to encode.
- * @param {{stream: boolean}=} options
- */
- encode: function encode(opt_string, options) {
- opt_string = opt_string ? String(opt_string) : '';
- options = Object(options);
- // TODO: any options?
- if (!this._streaming) {
- this._encoder = this._encoding.getEncoder(this._options);
- }
- this._streaming = Boolean(options.stream);
- var bytes = [];
- var output_stream = new ByteOutputStream(bytes);
- var input_stream = new CodePointInputStream(opt_string);
- while (input_stream.get() !== EOF_code_point) {
- this._encoder.encode(output_stream, input_stream);
- }
- if (!this._streaming) {
- /** @type {number} */
- var last_byte;
- do {
- last_byte = this._encoder.encode(output_stream, input_stream);
- } while (last_byte !== EOF_byte);
- this._encoder = null;
- }
- return new Buffer(bytes);
- }
- };
- //
- // 8. The encoding
- //
- // 8.1 utf-8
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function UTF8Decoder(options) {
- var fatal = options.fatal;
- var /** @type {number} */ utf8_code_point = 0,
- /** @type {number} */ utf8_bytes_needed = 0,
- /** @type {number} */ utf8_bytes_seen = 0,
- /** @type {number} */ utf8_lower_boundary = 0;
- /**
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
- * @return {?number} The next code point decoded, or null if not enough
- * data exists in the input stream to decode a complete code point.
- */
- this.decode = function(byte_pointer) {
- var bite = byte_pointer.get();
- if (bite === EOF_byte) {
- if (utf8_bytes_needed !== 0) {
- return decoderError(fatal);
- }
- return EOF_code_point;
- }
- byte_pointer.offset(1);
- if (utf8_bytes_needed === 0) {
- if (inRange(bite, 0x00, 0x7F)) {
- return bite;
- }
- if (inRange(bite, 0xC2, 0xDF)) {
- utf8_bytes_needed = 1;
- utf8_lower_boundary = 0x80;
- utf8_code_point = bite - 0xC0;
- } else if (inRange(bite, 0xE0, 0xEF)) {
- utf8_bytes_needed = 2;
- utf8_lower_boundary = 0x800;
- utf8_code_point = bite - 0xE0;
- } else if (inRange(bite, 0xF0, 0xF4)) {
- utf8_bytes_needed = 3;
- utf8_lower_boundary = 0x10000;
- utf8_code_point = bite - 0xF0;
- } else {
- return decoderError(fatal);
- }
- utf8_code_point = utf8_code_point * Math.pow(64, utf8_bytes_needed);
- return null;
- }
- if (!inRange(bite, 0x80, 0xBF)) {
- utf8_code_point = 0;
- utf8_bytes_needed = 0;
- utf8_bytes_seen = 0;
- utf8_lower_boundary = 0;
- byte_pointer.offset(-1);
- return decoderError(fatal);
- }
- utf8_bytes_seen += 1;
- utf8_code_point = utf8_code_point + (bite - 0x80) *
- Math.pow(64, utf8_bytes_needed - utf8_bytes_seen);
- if (utf8_bytes_seen !== utf8_bytes_needed) {
- return null;
- }
- var code_point = utf8_code_point;
- var lower_boundary = utf8_lower_boundary;
- utf8_code_point = 0;
- utf8_bytes_needed = 0;
- utf8_bytes_seen = 0;
- utf8_lower_boundary = 0;
- if (inRange(code_point, lower_boundary, 0x10FFFF) &&
- !inRange(code_point, 0xD800, 0xDFFF)) {
- return code_point;
- }
- return decoderError(fatal);
- };
- }
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function UTF8Encoder(options) {
- var fatal = options.fatal;
- /**
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
- * @param {CodePointInputStream} code_point_pointer Input stream.
- * @return {number} The last byte emitted.
- */
- this.encode = function(output_byte_stream, code_point_pointer) {
- /** @type {number} */
- var code_point = code_point_pointer.get();
- if (code_point === EOF_code_point) {
- return EOF_byte;
- }
- code_point_pointer.offset(1);
- if (inRange(code_point, 0xD800, 0xDFFF)) {
- return encoderError(code_point);
- }
- if (inRange(code_point, 0x0000, 0x007f)) {
- return output_byte_stream.emit(code_point);
- }
- var count, offset;
- if (inRange(code_point, 0x0080, 0x07FF)) {
- count = 1;
- offset = 0xC0;
- } else if (inRange(code_point, 0x0800, 0xFFFF)) {
- count = 2;
- offset = 0xE0;
- } else if (inRange(code_point, 0x10000, 0x10FFFF)) {
- count = 3;
- offset = 0xF0;
- }
- var result = output_byte_stream.emit(
- div(code_point, Math.pow(64, count)) + offset);
- while (count > 0) {
- var temp = div(code_point, Math.pow(64, count - 1));
- result = output_byte_stream.emit(0x80 + (temp % 64));
- count -= 1;
- }
- return result;
- };
- }
- /** @param {{fatal: boolean}} options */
- name_to_encoding['utf-8'].getEncoder = function(options) {
- return new UTF8Encoder(options);
- };
- /** @param {{fatal: boolean}} options */
- name_to_encoding['utf-8'].getDecoder = function(options) {
- return new UTF8Decoder(options);
- };
- //
- // 9. Legacy single-byte encodings
- //
- /**
- * @constructor
- * @param {Array.<number>} index The encoding index.
- * @param {{fatal: boolean}} options
- */
- function SingleByteDecoder(index, options) {
- var fatal = options.fatal;
- /**
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
- * @return {?number} The next code point decoded, or null if not enough
- * data exists in the input stream to decode a complete code point.
- */
- this.decode = function(byte_pointer) {
- var bite = byte_pointer.get();
- if (bite === EOF_byte) {
- return EOF_code_point;
- }
- byte_pointer.offset(1);
- if (inRange(bite, 0x00, 0x7F)) {
- return bite;
- }
- var code_point = index[bite - 0x80];
- if (code_point === null) {
- return decoderError(fatal);
- }
- return code_point;
- };
- }
- /**
- * @constructor
- * @param {Array.<?number>} index The encoding index.
- * @param {{fatal: boolean}} options
- */
- function SingleByteEncoder(index, options) {
- var fatal = options.fatal;
- /**
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
- * @param {CodePointInputStream} code_point_pointer Input stream.
- * @return {number} The last byte emitted.
- */
- this.encode = function(output_byte_stream, code_point_pointer) {
- var code_point = code_point_pointer.get();
- if (code_point === EOF_code_point) {
- return EOF_byte;
- }
- code_point_pointer.offset(1);
- if (inRange(code_point, 0x0000, 0x007F)) {
- return output_byte_stream.emit(code_point);
- }
- var pointer = indexPointerFor(code_point, index);
- if (pointer === null) {
- encoderError(code_point);
- }
- return output_byte_stream.emit(pointer + 0x80);
- };
- }
- (function() {
- encodings.forEach(function(category) {
- if (category.heading !== 'Legacy single-byte encodings')
- return;
- category.encodings.forEach(function(encoding) {
- var idx = indexes[encoding.name];
- /** @param {{fatal: boolean}} options */
- encoding.getDecoder = function(options) {
- return new SingleByteDecoder(idx, options);
- };
- /** @param {{fatal: boolean}} options */
- encoding.getEncoder = function(options) {
- return new SingleByteEncoder(idx, options);
- };
- });
- });
- }());
- //
- // 10. Legacy multi-byte Chinese (simplified) encodings
- //
- // 9.1 gbk
- /**
- * @constructor
- * @param {boolean} gb18030 True if decoding gb18030, false otherwise.
- * @param {{fatal: boolean}} options
- */
- function GBKDecoder(gb18030, options) {
- var fatal = options.fatal;
- var /** @type {number} */ gbk_first = 0x00,
- /** @type {number} */ gbk_second = 0x00,
- /** @type {number} */ gbk_third = 0x00;
- /**
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
- * @return {?number} The next code point decoded, or null if not enough
- * data exists in the input stream to decode a complete code point.
- */
- this.decode = function(byte_pointer) {
- var bite = byte_pointer.get();
- if (bite === EOF_byte && gbk_first === 0x00 &&
- gbk_second === 0x00 && gbk_third === 0x00) {
- return EOF_code_point;
- }
- if (bite === EOF_byte &&
- (gbk_first !== 0x00 || gbk_second !== 0x00 || gbk_third !== 0x00)) {
- gbk_first = 0x00;
- gbk_second = 0x00;
- gbk_third = 0x00;
- decoderError(fatal);
- }
- byte_pointer.offset(1);
- var code_point;
- if (gbk_third !== 0x00) {
- code_point = null;
- if (inRange(bite, 0x30, 0x39)) {
- code_point = indexGB18030CodePointFor(
- (((gbk_first - 0x81) * 10 + (gbk_second - 0x30)) * 126 +
- (gbk_third - 0x81)) * 10 + bite - 0x30);
- }
- gbk_first = 0x00;
- gbk_second = 0x00;
- gbk_third = 0x00;
- if (code_point === null) {
- byte_pointer.offset(-3);
- return decoderError(fatal);
- }
- return code_point;
- }
- if (gbk_second !== 0x00) {
- if (inRange(bite, 0x81, 0xFE)) {
- gbk_third = bite;
- return null;
- }
- byte_pointer.offset(-2);
- gbk_first = 0x00;
- gbk_second = 0x00;
- return decoderError(fatal);
- }
- if (gbk_first !== 0x00) {
- if (inRange(bite, 0x30, 0x39) && gb18030) {
- gbk_second = bite;
- return null;
- }
- var lead = gbk_first;
- var pointer = null;
- gbk_first = 0x00;
- var offset = bite < 0x7F ? 0x40 : 0x41;
- if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFE)) {
- pointer = (lead - 0x81) * 190 + (bite - offset);
- }
- code_point = pointer === null ? null :
- indexCodePointFor(pointer, indexes['gbk']);
- if (pointer === null) {
- byte_pointer.offset(-1);
- }
- if (code_point === null) {
- return decoderError(fatal);
- }
- return code_point;
- }
- if (inRange(bite, 0x00, 0x7F)) {
- return bite;
- }
- if (bite === 0x80) {
- return 0x20AC;
- }
- if (inRange(bite, 0x81, 0xFE)) {
- gbk_first = bite;
- return null;
- }
- return decoderError(fatal);
- };
- }
- /**
- * @constructor
- * @param {boolean} gb18030 True if decoding gb18030, false otherwise.
- * @param {{fatal: boolean}} options
- */
- function GBKEncoder(gb18030, options) {
- var fatal = options.fatal;
- /**
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
- * @param {CodePointInputStream} code_point_pointer Input stream.
- * @return {number} The last byte emitted.
- */
- this.encode = function(output_byte_stream, code_point_pointer) {
- var code_point = code_point_pointer.get();
- if (code_point === EOF_code_point) {
- return EOF_byte;
- }
- code_point_pointer.offset(1);
- if (inRange(code_point, 0x0000, 0x007F)) {
- return output_byte_stream.emit(code_point);
- }
- var pointer = indexPointerFor(code_point, indexes['gbk']);
- if (pointer !== null) {
- var lead = div(pointer, 190) + 0x81;
- var trail = pointer % 190;
- var offset = trail < 0x3F ? 0x40 : 0x41;
- return output_byte_stream.emit(lead, trail + offset);
- }
- if (pointer === null && !gb18030) {
- return encoderError(code_point);
- }
- pointer = indexGB18030PointerFor(code_point);
- var byte1 = div(div(div(pointer, 10), 126), 10);
- pointer = pointer - byte1 * 10 * 126 * 10;
- var byte2 = div(div(pointer, 10), 126);
- pointer = pointer - byte2 * 10 * 126;
- var byte3 = div(pointer, 10);
- var byte4 = pointer - byte3 * 10;
- return output_byte_stream.emit(byte1 + 0x81,
- byte2 + 0x30,
- byte3 + 0x81,
- byte4 + 0x30);
- };
- }
- name_to_encoding['gbk'].getEncoder = function(options) {
- return new GBKEncoder(false, options);
- };
- name_to_encoding['gbk'].getDecoder = function(options) {
- return new GBKDecoder(false, options);
- };
- // 9.2 gb18030
- name_to_encoding['gb18030'].getEncoder = function(options) {
- return new GBKEncoder(true, options);
- };
- name_to_encoding['gb18030'].getDecoder = function(options) {
- return new GBKDecoder(true, options);
- };
- // 10.2 hz-gb-2312
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function HZGB2312Decoder(options) {
- var fatal = options.fatal;
- var /** @type {boolean} */ hzgb2312 = false,
- /** @type {number} */ hzgb2312_lead = 0x00;
- /**
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
- * @return {?number} The next code point decoded, or null if not enough
- * data exists in the input stream to decode a complete code point.
- */
- this.decode = function(byte_pointer) {
- var bite = byte_pointer.get();
- if (bite === EOF_byte && hzgb2312_lead === 0x00) {
- return EOF_code_point;
- }
- if (bite === EOF_byte && hzgb2312_lead !== 0x00) {
- hzgb2312_lead = 0x00;
- return decoderError(fatal);
- }
- byte_pointer.offset(1);
- if (hzgb2312_lead === 0x7E) {
- hzgb2312_lead = 0x00;
- if (bite === 0x7B) {
- hzgb2312 = true;
- return null;
- }
- if (bite === 0x7D) {
- hzgb2312 = false;
- return null;
- }
- if (bite === 0x7E) {
- return 0x007E;
- }
- if (bite === 0x0A) {
- return null;
- }
- byte_pointer.offset(-1);
- return decoderError(fatal);
- }
- if (hzgb2312_lead !== 0x00) {
- var lead = hzgb2312_lead;
- hzgb2312_lead = 0x00;
- var code_point = null;
- if (inRange(bite, 0x21, 0x7E)) {
- code_point = indexCodePointFor((lead - 1) * 190 +
- (bite + 0x3F), indexes['gbk']);
- }
- if (bite === 0x0A) {
- hzgb2312 = false;
- }
- if (code_point === null) {
- return decoderError(fatal);
- }
- return code_point;
- }
- if (bite === 0x7E) {
- hzgb2312_lead = 0x7E;
- return null;
- }
- if (hzgb2312) {
- if (inRange(bite, 0x20, 0x7F)) {
- hzgb2312_lead = bite;
- return null;
- }
- if (bite === 0x0A) {
- hzgb2312 = false;
- }
- return decoderError(fatal);
- }
- if (inRange(bite, 0x00, 0x7F)) {
- return bite;
- }
- return decoderError(fatal);
- };
- }
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function HZGB2312Encoder(options) {
- var fatal = options.fatal;
- /** @type {boolean} */
- var hzgb2312 = false;
- /**
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
- * @param {CodePointInputStream} code_point_pointer Input stream.
- * @return {number} The last byte emitted.
- */
- this.encode = function(output_byte_stream, code_point_pointer) {
- var code_point = code_point_pointer.get();
- if (code_point === EOF_code_point) {
- return EOF_byte;
- }
- code_point_pointer.offset(1);
- if (inRange(code_point, 0x0000, 0x007F) && hzgb2312) {
- code_point_pointer.offset(-1);
- hzgb2312 = false;
- return output_byte_stream.emit(0x7E, 0x7D);
- }
- if (code_point === 0x007E) {
- return output_byte_stream.emit(0x7E, 0x7E);
- }
- if (inRange(code_point, 0x0000, 0x007F)) {
- return output_byte_stream.emit(code_point);
- }
- if (!hzgb2312) {
- code_point_pointer.offset(-1);
- hzgb2312 = true;
- return output_byte_stream.emit(0x7E, 0x7B);
- }
- var pointer = indexPointerFor(code_point, indexes['gbk']);
- if (pointer === null) {
- return encoderError(code_point);
- }
- var lead = div(pointer, 190) + 1;
- var trail = pointer % 190 - 0x3F;
- if (!inRange(lead, 0x21, 0x7E) || !inRange(trail, 0x21, 0x7E)) {
- return encoderError(code_point);
- }
- return output_byte_stream.emit(lead, trail);
- };
- }
- /** @param {{fatal: boolean}} options */
- name_to_encoding['hz-gb-2312'].getEncoder = function(options) {
- return new HZGB2312Encoder(options);
- };
- /** @param {{fatal: boolean}} options */
- name_to_encoding['hz-gb-2312'].getDecoder = function(options) {
- return new HZGB2312Decoder(options);
- };
- //
- // 11. Legacy multi-byte Chinese (traditional) encodings
- //
- // 11.1 big5
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function Big5Decoder(options) {
- var fatal = options.fatal;
- var /** @type {number} */ big5_lead = 0x00,
- /** @type {?number} */ big5_pending = null;
- /**
- * @param {ByteInputStream} byte_pointer The byte steram to decode.
- * @return {?number} The next code point decoded, or null if not enough
- * data exists in the input stream to decode a complete code point.
- */
- this.decode = function(byte_pointer) {
- // NOTE: Hack to support emitting two code points
- if (big5_pending !== null) {
- var pending = big5_pending;
- big5_pending = null;
- return pending;
- }
- var bite = byte_pointer.get();
- if (bite === EOF_byte && big5_lead === 0x00) {
- return EOF_code_point;
- }
- if (bite === EOF_byte && big5_lead !== 0x00) {
- big5_lead = 0x00;
- return decoderError(fatal);
- }
- byte_pointer.offset(1);
- if (big5_lead !== 0x00) {
- var lead = big5_lead;
- var pointer = null;
- big5_lead = 0x00;
- var offset = bite < 0x7F ? 0x40 : 0x62;
- if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0xA1, 0xFE)) {
- pointer = (lead - 0x81) * 157 + (bite - offset);
- }
- if (pointer === 1133) {
- big5_pending = 0x0304;
- return 0x00CA;
- }
- if (pointer === 1135) {
- big5_pending = 0x030C;
- return 0x00CA;
- }
- if (pointer === 1164) {
- big5_pending = 0x0304;
- return 0x00EA;
- }
- if (pointer === 1166) {
- big5_pending = 0x030C;
- return 0x00EA;
- }
- var code_point = (pointer === null) ? null :
- indexCodePointFor(pointer, indexes['big5']);
- if (pointer === null) {
- byte_pointer.offset(-1);
- }
- if (code_point === null) {
- return decoderError(fatal);
- }
- return code_point;
- }
- if (inRange(bite, 0x00, 0x7F)) {
- return bite;
- }
- if (inRange(bite, 0x81, 0xFE)) {
- big5_lead = bite;
- return null;
- }
- return decoderError(fatal);
- };
- }
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function Big5Encoder(options) {
- var fatal = options.fatal;
- /**
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
- * @param {CodePointInputStream} code_point_pointer Input stream.
- * @return {number} The last byte emitted.
- */
- this.encode = function(output_byte_stream, code_point_pointer) {
- var code_point = code_point_pointer.get();
- if (code_point === EOF_code_point) {
- return EOF_byte;
- }
- code_point_pointer.offset(1);
- if (inRange(code_point, 0x0000, 0x007F)) {
- return output_byte_stream.emit(code_point);
- }
- var pointer = indexPointerFor(code_point, indexes['big5']);
- if (pointer === null) {
- return encoderError(code_point);
- }
- var lead = div(pointer, 157) + 0x81;
- //if (lead < 0xA1) {
- // return encoderError(code_point);
- //}
- var trail = pointer % 157;
- var offset = trail < 0x3F ? 0x40 : 0x62;
- return output_byte_stream.emit(lead, trail + offset);
- };
- }
- /** @param {{fatal: boolean}} options */
- name_to_encoding['big5'].getEncoder = function(options) {
- return new Big5Encoder(options);
- };
- /** @param {{fatal: boolean}} options */
- name_to_encoding['big5'].getDecoder = function(options) {
- return new Big5Decoder(options);
- };
- //
- // 12. Legacy multi-byte Japanese encodings
- //
- // 12.1 euc.jp
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function EUCJPDecoder(options) {
- var fatal = options.fatal;
- var /** @type {number} */ eucjp_first = 0x00,
- /** @type {number} */ eucjp_second = 0x00;
- /**
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
- * @return {?number} The next code point decoded, or null if not enough
- * data exists in the input stream to decode a complete code point.
- */
- this.decode = function(byte_pointer) {
- var bite = byte_pointer.get();
- if (bite === EOF_byte) {
- if (eucjp_first === 0x00 && eucjp_second === 0x00) {
- return EOF_code_point;
- }
- eucjp_first = 0x00;
- eucjp_second = 0x00;
- return decoderError(fatal);
- }
- byte_pointer.offset(1);
- var lead, code_point;
- if (eucjp_second !== 0x00) {
- lead = eucjp_second;
- eucjp_second = 0x00;
- code_point = null;
- if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
- code_point = indexCodePointFor((lead - 0xA1) * 94 + bite - 0xA1,
- indexes['jis0212']);
- }
- if (!inRange(bite, 0xA1, 0xFE)) {
- byte_pointer.offset(-1);
- }
- if (code_point === null) {
- return decoderError(fatal);
- }
- return code_point;
- }
- if (eucjp_first === 0x8E && inRange(bite, 0xA1, 0xDF)) {
- eucjp_first = 0x00;
- return 0xFF61 + bite - 0xA1;
- }
- if (eucjp_first === 0x8F && inRange(bite, 0xA1, 0xFE)) {
- eucjp_first = 0x00;
- eucjp_second = bite;
- return null;
- }
- if (eucjp_first !== 0x00) {
- lead = eucjp_first;
- eucjp_first = 0x00;
- code_point = null;
- if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
- code_point = indexCodePointFor((lead - 0xA1) * 94 + bite - 0xA1,
- indexes['jis0208']);
- }
- if (!inRange(bite, 0xA1, 0xFE)) {
- byte_pointer.offset(-1);
- }
- if (code_point === null) {
- return decoderError(fatal);
- }
- return code_point;
- }
- if (inRange(bite, 0x00, 0x7F)) {
- return bite;
- }
- if (bite === 0x8E || bite === 0x8F || (inRange(bite, 0xA1, 0xFE))) {
- eucjp_first = bite;
- return null;
- }
- return decoderError(fatal);
- };
- }
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function EUCJPEncoder(options) {
- var fatal = options.fatal;
- /**
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
- * @param {CodePointInputStream} code_point_pointer Input stream.
- * @return {number} The last byte emitted.
- */
- this.encode = function(output_byte_stream, code_point_pointer) {
- var code_point = code_point_pointer.get();
- if (code_point === EOF_code_point) {
- return EOF_byte;
- }
- code_point_pointer.offset(1);
- if (inRange(code_point, 0x0000, 0x007F)) {
- return output_byte_stream.emit(code_point);
- }
- if (code_point === 0x00A5) {
- return output_byte_stream.emit(0x5C);
- }
- if (code_point === 0x203E) {
- return output_byte_stream.emit(0x7E);
- }
- if (inRange(code_point, 0xFF61, 0xFF9F)) {
- return output_byte_stream.emit(0x8E, code_point - 0xFF61 + 0xA1);
- }
- var pointer = indexPointerFor(code_point, indexes['jis0208']);
- if (pointer === null) {
- return encoderError(code_point);
- }
- var lead = div(pointer, 94) + 0xA1;
- var trail = pointer % 94 + 0xA1;
- return output_byte_stream.emit(lead, trail);
- };
- }
- /** @param {{fatal: boolean}} options */
- name_to_encoding['euc-jp'].getEncoder = function(options) {
- return new EUCJPEncoder(options);
- };
- /** @param {{fatal: boolean}} options */
- name_to_encoding['euc-jp'].getDecoder = function(options) {
- return new EUCJPDecoder(options);
- };
- // 12.2 iso-2022-jp
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function ISO2022JPDecoder(options) {
- var fatal = options.fatal;
- /** @enum */
- var state = {
- ASCII: 0,
- escape_start: 1,
- escape_middle: 2,
- escape_final: 3,
- lead: 4,
- trail: 5,
- Katakana: 6
- };
- var /** @type {number} */ iso2022jp_state = state.ASCII,
- /** @type {boolean} */ iso2022jp_jis0212 = false,
- /** @type {number} */ iso2022jp_lead = 0x00;
- /**
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
- * @return {?number} The next code point decoded, or null if not enough
- * data exists in the input stream to decode a complete code point.
- */
- this.decode = function(byte_pointer) {
- var bite = byte_pointer.get();
- if (bite !== EOF_byte) {
- byte_pointer.offset(1);
- }
- switch (iso2022jp_state) {
- default:
- case state.ASCII:
- if (bite === 0x1B) {
- iso2022jp_state = state.escape_start;
- return null;
- }
- if (inRange(bite, 0x00, 0x7F)) {
- return bite;
- }
- if (bite === EOF_byte) {
- return EOF_code_point;
- }
- return decoderError(fatal);
- case state.escape_start:
- if (bite === 0x24 || bite === 0x28) {
- iso2022jp_lead = bite;
- iso2022jp_state = state.escape_middle;
- return null;
- }
- if (bite !== EOF_byte) {
- byte_pointer.offset(-1);
- }
- iso2022jp_state = state.ASCII;
- return decoderError(fatal);
- case state.escape_middle:
- var lead = iso2022jp_lead;
- iso2022jp_lead = 0x00;
- if (lead === 0x24 && (bite === 0x40 || bite === 0x42)) {
- iso2022jp_jis0212 = false;
- iso2022jp_state = state.lead;
- return null;
- }
- if (lead === 0x24 && bite === 0x28) {
- iso2022jp_state = state.escape_final;
- return null;
- }
- if (lead === 0x28 && (bite === 0x42 || bite === 0x4A)) {
- iso2022jp_state = state.ASCII;
- return null;
- }
- if (lead === 0x28 && bite === 0x49) {
- iso2022jp_state = state.Katakana;
- return null;
- }
- if (bite === EOF_byte) {
- byte_pointer.offset(-1);
- } else {
- byte_pointer.offset(-2);
- }
- iso2022jp_state = state.ASCII;
- return decoderError(fatal);
- case state.escape_final:
- if (bite === 0x44) {
- iso2022jp_jis0212 = true;
- iso2022jp_state = state.lead;
- return null;
- }
- if (bite === EOF_byte) {
- byte_pointer.offset(-2);
- } else {
- byte_pointer.offset(-3);
- }
- iso2022jp_state = state.ASCII;
- return decoderError(fatal);
- case state.lead:
- if (bite === 0x0A) {
- iso2022jp_state = state.ASCII;
- return decoderError(fatal, 0x000A);
- }
- if (bite === 0x1B) {
- iso2022jp_state = state.escape_start;
- return null;
- }
- if (bite === EOF_byte) {
- return EOF_code_point;
- }
- iso2022jp_lead = bite;
- iso2022jp_state = state.trail;
- return null;
- case state.trail:
- iso2022jp_state = state.lead;
- if (bite === EOF_byte) {
- return decoderError(fatal);
- }
- var code_point = null;
- var pointer = (iso2022jp_lead - 0x21) * 94 + bite - 0x21;
- if (inRange(iso2022jp_lead, 0x21, 0x7E) &&
- inRange(bite, 0x21, 0x7E)) {
- code_point = (iso2022jp_jis0212 === false) ?
- indexCodePointFor(pointer, indexes['jis0208']) :
- indexCodePointFor(pointer, indexes['jis0212']);
- }
- if (code_point === null) {
- return decoderError(fatal);
- }
- return code_point;
- case state.Katakana:
- if (bite === 0x1B) {
- iso2022jp_state = state.escape_start;
- return null;
- }
- if (inRange(bite, 0x21, 0x5F)) {
- return 0xFF61 + bite - 0x21;
- }
- if (bite === EOF_byte) {
- return EOF_code_point;
- }
- return decoderError(fatal);
- }
- };
- }
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function ISO2022JPEncoder(options) {
- var fatal = options.fatal;
- /** @enum */
- var state = {
- ASCII: 0,
- lead: 1,
- Katakana: 2
- };
- var /** @type {number} */ iso2022jp_state = state.ASCII;
- /**
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
- * @param {CodePointInputStream} code_point_pointer Input stream.
- * @return {number} The last byte emitted.
- */
- this.encode = function(output_byte_stream, code_point_pointer) {
- var code_point = code_point_pointer.get();
- if (code_point === EOF_code_point) {
- return EOF_byte;
- }
- code_point_pointer.offset(1);
- if ((inRange(code_point, 0x0000, 0x007F) ||
- code_point === 0x00A5 || code_point === 0x203E) &&
- iso2022jp_state !== state.ASCII) {
- code_point_pointer.offset(-1);
- iso2022jp_state = state.ASCII;
- return output_byte_stream.emit(0x1B, 0x28, 0x42);
- }
- if (inRange(code_point, 0x0000, 0x007F)) {
- return output_byte_stream.emit(code_point);
- }
- if (code_point === 0x00A5) {
- return output_byte_stream.emit(0x5C);
- }
- if (code_point === 0x203E) {
- return output_byte_stream.emit(0x7E);
- }
- if (inRange(code_point, 0xFF61, 0xFF9F) &&
- iso2022jp_state !== state.Katakana) {
- code_point_pointer.offset(-1);
- iso2022jp_state = state.Katakana;
- return output_byte_stream.emit(0x1B, 0x28, 0x49);
- }
- if (inRange(code_point, 0xFF61, 0xFF9F)) {
- return output_byte_stream.emit(code_point - 0xFF61 - 0x21);
- }
- if (iso2022jp_state !== state.lead) {
- code_point_pointer.offset(-1);
- iso2022jp_state = state.lead;
- return output_byte_stream.emit(0x1B, 0x24, 0x42);
- }
- var pointer = indexPointerFor(code_point, indexes['jis0208']);
- if (pointer === null) {
- return encoderError(code_point);
- }
- var lead = div(pointer, 94) + 0x21;
- var trail = pointer % 94 + 0x21;
- return output_byte_stream.emit(lead, trail);
- };
- }
- /** @param {{fatal: boolean}} options */
- name_to_encoding['iso-2022-jp'].getEncoder = function(options) {
- return new ISO2022JPEncoder(options);
- };
- /** @param {{fatal: boolean}} options */
- name_to_encoding['iso-2022-jp'].getDecoder = function(options) {
- return new ISO2022JPDecoder(options);
- };
- // 12.3 shift_jis
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function ShiftJISDecoder(options) {
- var fatal = options.fatal;
- var /** @type {number} */ shiftjis_lead = 0x00;
- /**
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
- * @return {?number} The next code point decoded, or null if not enough
- * data exists in the input stream to decode a complete code point.
- */
- this.decode = function(byte_pointer) {
- var bite = byte_pointer.get();
- if (bite === EOF_byte && shiftjis_lead === 0x00) {
- return EOF_code_point;
- }
- if (bite === EOF_byte && shiftjis_lead !== 0x00) {
- shiftjis_lead = 0x00;
- return decoderError(fatal);
- }
- byte_pointer.offset(1);
- if (shiftjis_lead !== 0x00) {
- var lead = shiftjis_lead;
- shiftjis_lead = 0x00;
- if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFC)) {
- var offset = (bite < 0x7F) ? 0x40 : 0x41;
- var lead_offset = (lead < 0xA0) ? 0x81 : 0xC1;
- var code_point = indexCodePointFor((lead - lead_offset) * 188 +
- bite - offset, indexes['jis0208']);
- if (code_point === null) {
- return decoderError(fatal);
- }
- return code_point;
- }
- byte_pointer.offset(-1);
- return decoderError(fatal);
- }
- if (inRange(bite, 0x00, 0x80)) {
- return bite;
- }
- if (inRange(bite, 0xA1, 0xDF)) {
- return 0xFF61 + bite - 0xA1;
- }
- if (inRange(bite, 0x81, 0x9F) || inRange(bite, 0xE0, 0xFC)) {
- shiftjis_lead = bite;
- return null;
- }
- return decoderError(fatal);
- };
- }
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function ShiftJISEncoder(options) {
- var fatal = options.fatal;
- /**
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
- * @param {CodePointInputStream} code_point_pointer Input stream.
- * @return {number} The last byte emitted.
- */
- this.encode = function(output_byte_stream, code_point_pointer) {
- var code_point = code_point_pointer.get();
- if (code_point === EOF_code_point) {
- return EOF_byte;
- }
- code_point_pointer.offset(1);
- if (inRange(code_point, 0x0000, 0x0080)) {
- return output_byte_stream.emit(code_point);
- }
- if (code_point === 0x00A5) {
- return output_byte_stream.emit(0x5C);
- }
- if (code_point === 0x203E) {
- return output_byte_stream.emit(0x7E);
- }
- if (inRange(code_point, 0xFF61, 0xFF9F)) {
- return output_byte_stream.emit(code_point - 0xFF61 + 0xA1);
- }
- var pointer = indexPointerFor(code_point, indexes['jis0208']);
- if (pointer === null) {
- return encoderError(code_point);
- }
- var lead = div(pointer, 188);
- var lead_offset = lead < 0x1F ? 0x81 : 0xC1;
- var trail = pointer % 188;
- var offset = trail < 0x3F ? 0x40 : 0x41;
- return output_byte_stream.emit(lead + lead_offset, trail + offset);
- };
- }
- /** @param {{fatal: boolean}} options */
- name_to_encoding['shift_jis'].getEncoder = function(options) {
- return new ShiftJISEncoder(options);
- };
- /** @param {{fatal: boolean}} options */
- name_to_encoding['shift_jis'].getDecoder = function(options) {
- return new ShiftJISDecoder(options);
- };
- //
- // 13. Legacy multi-byte Korean encodings
- //
- // 13.1 euc-kr
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function EUCKRDecoder(options) {
- var fatal = options.fatal;
- var /** @type {number} */ euckr_lead = 0x00;
- /**
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
- * @return {?number} The next code point decoded, or null if not enough
- * data exists in the input stream to decode a complete code point.
- */
- this.decode = function(byte_pointer) {
- var bite = byte_pointer.get();
- if (bite === EOF_byte && euckr_lead === 0) {
- return EOF_code_point;
- }
- if (bite === EOF_byte && euckr_lead !== 0) {
- euckr_lead = 0x00;
- return decoderError(fatal);
- }
- byte_pointer.offset(1);
- if (euckr_lead !== 0x00) {
- var lead = euckr_lead;
- var pointer = null;
- euckr_lead = 0x00;
- if (inRange(lead, 0x81, 0xC6)) {
- var temp = (26 + 26 + 126) * (lead - 0x81);
- if (inRange(bite, 0x41, 0x5A)) {
- pointer = temp + bite - 0x41;
- } else if (inRange(bite, 0x61, 0x7A)) {
- pointer = temp + 26 + bite - 0x61;
- } else if (inRange(bite, 0x81, 0xFE)) {
- pointer = temp + 26 + 26 + bite - 0x81;
- }
- }
- if (inRange(lead, 0xC7, 0xFD) && inRange(bite, 0xA1, 0xFE)) {
- pointer = (26 + 26 + 126) * (0xC7 - 0x81) + (lead - 0xC7) * 94 +
- (bite - 0xA1);
- }
- var code_point = (pointer === null) ? null :
- indexCodePointFor(pointer, indexes['euc-kr']);
- if (pointer === null) {
- byte_pointer.offset(-1);
- }
- if (code_point === null) {
- return decoderError(fatal);
- }
- return code_point;
- }
- if (inRange(bite, 0x00, 0x7F)) {
- return bite;
- }
- if (inRange(bite, 0x81, 0xFD)) {
- euckr_lead = bite;
- return null;
- }
- return decoderError(fatal);
- };
- }
- /**
- * @constructor
- * @param {{fatal: boolean}} options
- */
- function EUCKREncoder(options) {
- var fatal = options.fatal;
- /**
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
- * @param {CodePointInputStream} code_point_pointer Input stream.
- * @return {number} The last byte emitted.
- */
- this.encode = function(output_byte_stream, code_point_pointer) {
- var code_point = code_point_pointer.get();
- if (code_point === EOF_code_point) {
- return EOF_byte;
- }
- code_point_pointer.offset(1);
- if (inRange(code_point, 0x0000, 0x007F)) {
- return output_byte_stream.emit(code_point);
- }
- var pointer = indexPointerFor(code_point, indexes['euc-kr']);
- if (pointer === null) {
- return encoderError(code_point);
- }
- var lead, trail;
- if (pointer < ((26 + 26 + 126) * (0xC7 - 0x81))) {
- lead = div(pointer, (26 + 26 + 126)) + 0x81;
- trail = pointer % (26 + 26 + 126);
- var offset = trail < 26 ? 0x41 : trail < 26 + 26 ? 0x47 : 0x4D;
- return output_byte_stream.emit(lead, trail + offset);
- }
- pointer = pointer - (26 + 26 + 126) * (0xC7 - 0x81);
- lead = div(pointer, 94) + 0xC7;
- trail = pointer % 94 + 0xA1;
- return output_byte_stream.emit(lead, trail);
- };
- }
- /** @param {{fatal: boolean}} options */
- name_to_encoding['euc-kr'].getEncoder = function(options) {
- return new EUCKREncoder(options);
- };
- /** @param {{fatal: boolean}} options */
- name_to_encoding['euc-kr'].getDecoder = function(options) {
- return new EUCKRDecoder(options);
- };
- //
- // 14. Legacy miscellaneous encodings
- //
- // 14.1 replacement
- // Not needed - API throws TypeError
- // 14.2 utf-16
- /**
- * @constructor
- * @param {boolean} utf16_be True if big-endian, false if little-endian.
- * @param {{fatal: boolean}} options
- */
- function UTF16Decoder(utf16_be, options) {
- var fatal = options.fatal;
- var /** @type {?number} */ utf16_lead_byte = null,
- /** @type {?number} */ utf16_lead_surrogate = null;
- /**
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
- * @return {?number} The next code point decoded, or null if not enough
- * data exists in the input stream to decode a complete code point.
- */
- this.decode = function(byte_pointer) {
- var bite = byte_pointer.get();
- if (bite === EOF_byte && utf16_lead_byte === null &&
- utf16_lead_surrogate === null) {
- return EOF_code_point;
- }
- if (bite === EOF_byte && (utf16_lead_byte !== null ||
- utf16_lead_surrogate !== null)) {
- return decoderError(fatal);
- }
- byte_pointer.offset(1);
- if (utf16_lead_byte === null) {
- utf16_lead_byte = bite;
- return null;
- }
- var code_point;
- if (utf16_be) {
- code_point = (utf16_lead_byte << 8) + bite;
- } else {
- code_point = (bite << 8) + utf16_lead_byte;
- }
- utf16_lead_byte = null;
- if (utf16_lead_surrogate !== null) {
- var lead_surrogate = utf16_lead_surrogate;
- utf16_lead_surrogate = null;
- if (inRange(code_point, 0xDC00, 0xDFFF)) {
- return 0x10000 + (lead_surrogate - 0xD800) * 0x400 +
- (code_point - 0xDC00);
- }
- byte_pointer.offset(-2);
- return decoderError(fatal);
- }
- if (inRange(code_point, 0xD800, 0xDBFF)) {
- utf16_lead_surrogate = code_point;
- return null;
- }
- if (inRange(code_point, 0xDC00, 0xDFFF)) {
- return decoderError(fatal);
- }
- return code_point;
- };
- }
- /**
- * @constructor
- * @param {boolean} utf16_be True if big-endian, false if little-endian.
- * @param {{fatal: boolean}} options
- */
- function UTF16Encoder(utf16_be, options) {
- var fatal = options.fatal;
- /**
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
- * @param {CodePointInputStream} code_point_pointer Input stream.
- * @return {number} The last byte emitted.
- */
- this.encode = function(output_byte_stream, code_point_pointer) {
- /**
- * @param {number} code_unit
- * @return {number} last byte emitted
- */
- function convert_to_bytes(code_unit) {
- var byte1 = code_unit >> 8;
- var byte2 = code_unit & 0x00FF;
- if (utf16_be) {
- return output_byte_stream.emit(byte1, byte2);
- }
- return output_byte_stream.emit(byte2, byte1);
- }
- var code_point = code_point_pointer.get();
- if (code_point === EOF_code_point) {
- return EOF_byte;
- }
- code_point_pointer.offset(1);
- if (inRange(code_point, 0xD800, 0xDFFF)) {
- encoderError(code_point);
- }
- if (code_point <= 0xFFFF) {
- return convert_to_bytes(code_point);
- }
- var lead = div((code_point - 0x10000), 0x400) + 0xD800;
- var trail = ((code_point - 0x10000) % 0x400) + 0xDC00;
- convert_to_bytes(lead);
- return convert_to_bytes(trail);
- };
- }
- // 14.3 utf-16be
- /** @param {{fatal: boolean}} options */
- name_to_encoding['utf-16be'].getEncoder = function(options) {
- return new UTF16Encoder(true, options);
- };
- /** @param {{fatal: boolean}} options */
- name_to_encoding['utf-16be'].getDecoder = function(options) {
- return new UTF16Decoder(true, options);
- };
- // 14.4 utf-16le
- /** @param {{fatal: boolean}} options */
- name_to_encoding['utf-16le'].getEncoder = function(options) {
- return new UTF16Encoder(false, options);
- };
- /** @param {{fatal: boolean}} options */
- name_to_encoding['utf-16le'].getDecoder = function(options) {
- return new UTF16Decoder(false, options);
- };
- // 14.5 x-user-defined
- // TODO: Implement this encoding.
- // NOTE: currently unused
- /**
- * @param {string} label The encoding label.
- * @param {ByteInputStream} input_stream The byte stream to test.
- */
- function detectEncoding(label, input_stream) {
- if (input_stream.match([0xFF, 0xFE])) {
- input_stream.offset(2);
- return 'utf-16le';
- }
- if (input_stream.match([0xFE, 0xFF])) {
- input_stream.offset(2);
- return 'utf-16be';
- }
- if (input_stream.match([0xEF, 0xBB, 0xBF])) {
- input_stream.offset(3);
- return 'utf-8';
- }
- return label;
- }
- exports.TextEncoder = TextEncoder;
- exports.TextDecoder = TextDecoder;
- exports.encodingExists = getEncoding;
|