2016-01-16 03:27:02 +00:00
|
|
|
/* cputils.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
2016-09-22 17:42:47 +00:00
|
|
|
/* vim: set ft=javascript: */
|
2014-04-14 20:45:36 +00:00
|
|
|
/*jshint newcap: false */
|
2016-09-22 17:42:47 +00:00
|
|
|
(function(root, factory) {
|
2018-01-18 22:47:47 +00:00
|
|
|
/*jshint ignore:start */
|
|
|
|
/*eslint-disable */
|
2014-04-14 20:45:36 +00:00
|
|
|
"use strict";
|
|
|
|
if(typeof cptable === "undefined") {
|
|
|
|
if(typeof require !== "undefined"){
|
2017-03-10 21:26:53 +00:00
|
|
|
var cpt = require('./cptable');
|
2017-08-05 01:32:37 +00:00
|
|
|
if (typeof module !== 'undefined' && module.exports && typeof DO_NOT_EXPORT_CODEPAGE === 'undefined') module.exports = factory(cpt);
|
2014-04-14 20:45:36 +00:00
|
|
|
else root.cptable = factory(cpt);
|
|
|
|
} else throw new Error("cptable not found");
|
|
|
|
} else cptable = factory(cptable);
|
2018-01-18 22:47:47 +00:00
|
|
|
/*eslint-enable */
|
|
|
|
/*jshint ignore:end */
|
2014-04-14 20:45:36 +00:00
|
|
|
}(this, function(cpt){
|
|
|
|
"use strict";
|
2018-01-18 22:47:47 +00:00
|
|
|
/*global module, Buffer */
|
2014-04-14 20:45:36 +00:00
|
|
|
var magic = {
|
|
|
|
"1200":"utf16le",
|
|
|
|
"1201":"utf16be",
|
|
|
|
"12000":"utf32le",
|
|
|
|
"12001":"utf32be",
|
|
|
|
"16969":"utf64le",
|
|
|
|
"20127":"ascii",
|
|
|
|
"65000":"utf7",
|
|
|
|
"65001":"utf8"
|
|
|
|
};
|
|
|
|
|
|
|
|
var sbcs_cache = [874,1250,1251,1252,1253,1254,1255,1256,10000];
|
|
|
|
var dbcs_cache = [932,936,949,950];
|
|
|
|
var magic_cache = [65001];
|
|
|
|
var magic_decode = {};
|
|
|
|
var magic_encode = {};
|
2014-06-26 05:54:13 +00:00
|
|
|
var cpdcache = {};
|
2016-09-22 17:42:47 +00:00
|
|
|
var cpecache = {};
|
2014-04-14 20:45:36 +00:00
|
|
|
|
2014-12-25 20:50:25 +00:00
|
|
|
var sfcc = function sfcc(x) { return String.fromCharCode(x); };
|
2016-09-22 17:42:47 +00:00
|
|
|
var cca = function cca(x) { return x.charCodeAt(0); };
|
2014-12-25 20:50:25 +00:00
|
|
|
|
|
|
|
var has_buf = (typeof Buffer !== 'undefined');
|
|
|
|
if(has_buf) {
|
|
|
|
var mdl = 1024, mdb = new Buffer(mdl);
|
|
|
|
var make_EE = function make_EE(E){
|
2014-06-26 05:54:13 +00:00
|
|
|
var EE = new Buffer(65536);
|
2014-12-25 20:50:25 +00:00
|
|
|
for(var i = 0; i < 65536;++i) EE[i] = 0;
|
2014-06-26 05:54:13 +00:00
|
|
|
var keys = Object.keys(E), len = keys.length;
|
2014-12-25 20:50:25 +00:00
|
|
|
for(var ee = 0, e = keys[ee]; ee < len; ++ee) {
|
2014-06-26 05:54:13 +00:00
|
|
|
if(!(e = keys[ee])) continue;
|
2014-04-14 20:45:36 +00:00
|
|
|
EE[e.charCodeAt(0)] = E[e];
|
2014-12-25 20:50:25 +00:00
|
|
|
}
|
|
|
|
return EE;
|
|
|
|
};
|
|
|
|
var sbcs_encode = function make_sbcs_encode(cp) {
|
|
|
|
var EE = make_EE(cpt[cp].enc);
|
|
|
|
return function sbcs_e(data, ofmt) {
|
|
|
|
var len = data.length;
|
2016-09-22 17:42:47 +00:00
|
|
|
var out, i=0, j=0, D=0, w=0;
|
2014-04-14 20:45:36 +00:00
|
|
|
if(typeof data === 'string') {
|
2016-09-22 17:42:47 +00:00
|
|
|
out = new Buffer(len);
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; ++i) out[i] = EE[data.charCodeAt(i)];
|
|
|
|
} else if(Buffer.isBuffer(data)) {
|
2016-09-22 17:42:47 +00:00
|
|
|
out = new Buffer(2*len);
|
2014-12-25 20:50:25 +00:00
|
|
|
j = 0;
|
|
|
|
for(i = 0; i < len; ++i) {
|
|
|
|
D = data[i];
|
|
|
|
if(D < 128) out[j++] = EE[D];
|
|
|
|
else if(D < 224) { out[j++] = EE[((D&31)<<6)+(data[i+1]&63)]; ++i; }
|
|
|
|
else if(D < 240) { out[j++] = EE[((D&15)<<12)+((data[i+1]&63)<<6)+(data[i+2]&63)]; i+=2; }
|
|
|
|
else {
|
|
|
|
w = ((D&7)<<18)+((data[i+1]&63)<<12)+((data[i+2]&63)<<6)+(data[i+3]&63); i+=3;
|
|
|
|
if(w < 65536) out[j++] = EE[w];
|
|
|
|
else { w -= 65536; out[j++] = EE[0xD800 + ((w>>10)&1023)]; out[j++] = EE[0xDC00 + (w&1023)]; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = out.slice(0,j);
|
2014-04-14 20:45:36 +00:00
|
|
|
} else {
|
2016-09-22 17:42:47 +00:00
|
|
|
out = new Buffer(len);
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; ++i) out[i] = EE[data[i].charCodeAt(0)];
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
2016-09-22 17:42:47 +00:00
|
|
|
if(!ofmt || ofmt === 'buf') return out;
|
2014-12-25 20:50:25 +00:00
|
|
|
if(ofmt !== 'arr') return out.toString('binary');
|
|
|
|
return [].slice.call(out);
|
2014-04-14 20:45:36 +00:00
|
|
|
};
|
|
|
|
};
|
2014-12-25 20:50:25 +00:00
|
|
|
var sbcs_decode = function make_sbcs_decode(cp) {
|
2014-04-14 20:45:36 +00:00
|
|
|
var D = cpt[cp].dec;
|
2016-09-22 17:42:47 +00:00
|
|
|
var DD = new Buffer(131072), d=0, c="";
|
2014-12-25 20:50:25 +00:00
|
|
|
for(d=0;d<D.length;++d) {
|
2014-06-26 05:54:13 +00:00
|
|
|
if(!(c=D[d])) continue;
|
|
|
|
var w = c.charCodeAt(0);
|
2014-12-25 20:50:25 +00:00
|
|
|
DD[2*d] = w&255; DD[2*d+1] = w>>8;
|
2014-06-26 05:54:13 +00:00
|
|
|
}
|
2014-12-25 20:50:25 +00:00
|
|
|
return function sbcs_d(data) {
|
2016-09-22 17:42:47 +00:00
|
|
|
var len = data.length, i=0, j=0;
|
2014-12-25 20:50:25 +00:00
|
|
|
if(2 * len > mdl) { mdl = 2 * len; mdb = new Buffer(mdl); }
|
|
|
|
if(Buffer.isBuffer(data)) {
|
|
|
|
for(i = 0; i < len; i++) {
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 2*data[i];
|
2014-12-25 20:50:25 +00:00
|
|
|
mdb[2*i] = DD[j]; mdb[2*i+1] = DD[j+1];
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
} else if(typeof data === "string") {
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; i++) {
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 2*data.charCodeAt(i);
|
2014-12-25 20:50:25 +00:00
|
|
|
mdb[2*i] = DD[j]; mdb[2*i+1] = DD[j+1];
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; i++) {
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 2*data[i];
|
2014-12-25 20:50:25 +00:00
|
|
|
mdb[2*i] = DD[j]; mdb[2*i+1] = DD[j+1];
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-16 03:27:02 +00:00
|
|
|
return mdb.slice(0, 2 * len).toString('ucs2');
|
2014-04-14 20:45:36 +00:00
|
|
|
};
|
|
|
|
};
|
2014-12-25 20:50:25 +00:00
|
|
|
var dbcs_encode = function make_dbcs_encode(cp) {
|
2014-04-14 20:45:36 +00:00
|
|
|
var E = cpt[cp].enc;
|
2014-06-26 05:54:13 +00:00
|
|
|
var EE = new Buffer(131072);
|
2014-12-25 20:50:25 +00:00
|
|
|
for(var i = 0; i < 131072; ++i) EE[i] = 0;
|
2014-06-26 05:54:13 +00:00
|
|
|
var keys = Object.keys(E);
|
2014-12-25 20:50:25 +00:00
|
|
|
for(var ee = 0, e = keys[ee]; ee < keys.length; ++ee) {
|
2014-06-26 05:54:13 +00:00
|
|
|
if(!(e = keys[ee])) continue;
|
|
|
|
var f = e.charCodeAt(0);
|
|
|
|
EE[2*f] = E[e] & 255; EE[2*f+1] = E[e]>>8;
|
2014-12-25 20:50:25 +00:00
|
|
|
}
|
|
|
|
return function dbcs_e(data, ofmt) {
|
2016-09-22 17:42:47 +00:00
|
|
|
var len = data.length, out = new Buffer(2*len), i=0, j=0, jj=0, k=0, D=0;
|
2014-04-14 20:45:36 +00:00
|
|
|
if(typeof data === 'string') {
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = k = 0; i < len; ++i) {
|
2014-04-14 20:45:36 +00:00
|
|
|
j = data.charCodeAt(i)*2;
|
|
|
|
out[k++] = EE[j+1] || EE[j]; if(EE[j+1] > 0) out[k++] = EE[j];
|
|
|
|
}
|
2014-12-25 20:50:25 +00:00
|
|
|
out = out.slice(0,k);
|
|
|
|
} else if(Buffer.isBuffer(data)) {
|
|
|
|
for(i = k = 0; i < len; ++i) {
|
|
|
|
D = data[i];
|
|
|
|
if(D < 128) j = D;
|
|
|
|
else if(D < 224) { j = ((D&31)<<6)+(data[i+1]&63); ++i; }
|
|
|
|
else if(D < 240) { j = ((D&15)<<12)+((data[i+1]&63)<<6)+(data[i+2]&63); i+=2; }
|
|
|
|
else { j = ((D&7)<<18)+((data[i+1]&63)<<12)+((data[i+2]&63)<<6)+(data[i+3]&63); i+=3; }
|
|
|
|
if(j<65536) { j*=2; out[k++] = EE[j+1] || EE[j]; if(EE[j+1] > 0) out[k++] = EE[j]; }
|
|
|
|
else { jj = j-65536;
|
|
|
|
j=2*(0xD800 + ((jj>>10)&1023)); out[k++] = EE[j+1] || EE[j]; if(EE[j+1] > 0) out[k++] = EE[j];
|
|
|
|
j=2*(0xDC00 + (jj&1023)); out[k++] = EE[j+1] || EE[j]; if(EE[j+1] > 0) out[k++] = EE[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = out.slice(0,k);
|
2014-04-14 20:45:36 +00:00
|
|
|
} else {
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = k = 0; i < len; i++) {
|
2014-04-14 20:45:36 +00:00
|
|
|
j = data[i].charCodeAt(0)*2;
|
|
|
|
out[k++] = EE[j+1] || EE[j]; if(EE[j+1] > 0) out[k++] = EE[j];
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 17:42:47 +00:00
|
|
|
if(!ofmt || ofmt === 'buf') return out;
|
2014-12-25 20:50:25 +00:00
|
|
|
if(ofmt !== 'arr') return out.toString('binary');
|
|
|
|
return [].slice.call(out);
|
2014-04-14 20:45:36 +00:00
|
|
|
};
|
|
|
|
};
|
2014-12-25 20:50:25 +00:00
|
|
|
var dbcs_decode = function make_dbcs_decode(cp) {
|
2014-04-14 20:45:36 +00:00
|
|
|
var D = cpt[cp].dec;
|
2014-12-25 20:50:25 +00:00
|
|
|
var DD = new Buffer(131072), d=0, c, w=0, j=0, i=0;
|
|
|
|
for(i = 0; i < 65536; ++i) { DD[2*i] = 0xFF; DD[2*i+1] = 0xFD;}
|
|
|
|
for(d = 0; d < D.length; ++d) {
|
2014-06-26 05:54:13 +00:00
|
|
|
if(!(c=D[d])) continue;
|
|
|
|
w = c.charCodeAt(0);
|
|
|
|
j = 2*d;
|
2014-12-25 20:50:25 +00:00
|
|
|
DD[j] = w&255; DD[j+1] = w>>8;
|
|
|
|
}
|
|
|
|
return function dbcs_d(data) {
|
2016-09-22 17:42:47 +00:00
|
|
|
var len = data.length, out = new Buffer(2*len), i=0, j=0, k=0;
|
2014-12-25 20:50:25 +00:00
|
|
|
if(Buffer.isBuffer(data)) {
|
|
|
|
for(i = 0; i < len; i++) {
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 2*data[i];
|
2014-12-25 20:50:25 +00:00
|
|
|
if(DD[j]===0xFF && DD[j+1]===0xFD) { j=2*((data[i]<<8)+data[i+1]); ++i; }
|
2014-04-14 20:45:36 +00:00
|
|
|
out[k++] = DD[j]; out[k++] = DD[j+1];
|
|
|
|
}
|
|
|
|
} else if(typeof data === "string") {
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; i++) {
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 2*data.charCodeAt(i);
|
2014-12-25 20:50:25 +00:00
|
|
|
if(DD[j]===0xFF && DD[j+1]===0xFD) { j=2*((data.charCodeAt(i)<<8)+data.charCodeAt(i+1)); ++i; }
|
2014-04-14 20:45:36 +00:00
|
|
|
out[k++] = DD[j]; out[k++] = DD[j+1];
|
|
|
|
}
|
|
|
|
} else {
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; i++) {
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 2*data[i];
|
2014-12-25 20:50:25 +00:00
|
|
|
if(DD[j]===0xFF && DD[j+1]===0xFD) { j=2*((data[i]<<8)+data[i+1]); ++i; }
|
2014-04-14 20:45:36 +00:00
|
|
|
out[k++] = DD[j]; out[k++] = DD[j+1];
|
|
|
|
}
|
|
|
|
}
|
2016-01-16 03:27:02 +00:00
|
|
|
return out.slice(0,k).toString('ucs2');
|
2014-04-14 20:45:36 +00:00
|
|
|
};
|
|
|
|
};
|
2014-12-25 20:50:25 +00:00
|
|
|
magic_decode[65001] = function utf8_d(data) {
|
2016-09-22 17:42:47 +00:00
|
|
|
if(typeof data === "string") return utf8_d(data.split("").map(cca));
|
2014-12-25 20:50:25 +00:00
|
|
|
var len = data.length, w = 0, ww = 0;
|
|
|
|
if(4 * len > mdl) { mdl = 4 * len; mdb = new Buffer(mdl); }
|
|
|
|
var i = 0;
|
|
|
|
if(len >= 3 && data[0] == 0xEF) if(data[1] == 0xBB && data[2] == 0xBF) i = 3;
|
|
|
|
for(var j = 1, k = 0, D = 0; i < len; i+=j) {
|
|
|
|
j = 1; D = data[i];
|
|
|
|
if(D < 128) w = D;
|
|
|
|
else if(D < 224) { w=(D&31)*64+(data[i+1]&63); j=2; }
|
|
|
|
else if(D < 240) { w=((D&15)<<12)+(data[i+1]&63)*64+(data[i+2]&63); j=3; }
|
|
|
|
else { w=(D&7)*262144+((data[i+1]&63)<<12)+(data[i+2]&63)*64+(data[i+3]&63); j=4; }
|
|
|
|
if(w < 65536) { mdb[k++] = w&255; mdb[k++] = w>>8; }
|
2014-06-26 05:54:13 +00:00
|
|
|
else {
|
|
|
|
w -= 65536; ww = 0xD800 + ((w>>10)&1023); w = 0xDC00 + (w&1023);
|
2014-12-25 20:50:25 +00:00
|
|
|
mdb[k++] = ww&255; mdb[k++] = ww>>>8; mdb[k++] = w&255; mdb[k++] = (w>>>8)&255;
|
2014-06-26 05:54:13 +00:00
|
|
|
}
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
2016-01-16 03:27:02 +00:00
|
|
|
return mdb.slice(0,k).toString('ucs2');
|
2014-12-25 20:50:25 +00:00
|
|
|
};
|
|
|
|
magic_encode[65001] = function utf8_e(data, ofmt) {
|
2016-09-22 17:42:47 +00:00
|
|
|
if(has_buf && Buffer.isBuffer(data)) {
|
|
|
|
if(!ofmt || ofmt === 'buf') return data;
|
|
|
|
if(ofmt !== 'arr') return data.toString('binary');
|
|
|
|
return [].slice.call(data);
|
|
|
|
}
|
2014-12-25 20:50:25 +00:00
|
|
|
var len = data.length, w = 0, ww = 0, j = 0;
|
|
|
|
var direct = typeof data === "string";
|
|
|
|
if(4 * len > mdl) { mdl = 4 * len; mdb = new Buffer(mdl); }
|
|
|
|
for(var i = 0; i < len; ++i) {
|
|
|
|
w = direct ? data.charCodeAt(i) : data[i].charCodeAt(0);
|
|
|
|
if(w <= 0x007F) mdb[j++] = w;
|
|
|
|
else if(w <= 0x07FF) {
|
|
|
|
mdb[j++] = 192 + (w >> 6);
|
|
|
|
mdb[j++] = 128 + (w&63);
|
|
|
|
} else if(w >= 0xD800 && w <= 0xDFFF) {
|
|
|
|
w -= 0xD800; ++i;
|
|
|
|
ww = (direct ? data.charCodeAt(i) : data[i].charCodeAt(0)) - 0xDC00 + (w << 10);
|
|
|
|
mdb[j++] = 240 + ((ww>>>18) & 0x07);
|
|
|
|
mdb[j++] = 144 + ((ww>>>12) & 0x3F);
|
|
|
|
mdb[j++] = 128 + ((ww>>>6) & 0x3F);
|
|
|
|
mdb[j++] = 128 + (ww & 0x3F);
|
|
|
|
} else {
|
|
|
|
mdb[j++] = 224 + (w >> 12);
|
|
|
|
mdb[j++] = 128 + ((w >> 6)&63);
|
|
|
|
mdb[j++] = 128 + (w&63);
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 17:42:47 +00:00
|
|
|
if(!ofmt || ofmt === 'buf') return mdb.slice(0,j);
|
2016-01-16 03:27:02 +00:00
|
|
|
if(ofmt !== 'arr') return mdb.slice(0,j).toString('binary');
|
|
|
|
return [].slice.call(mdb, 0, j);
|
2014-04-14 20:45:36 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-12-25 20:50:25 +00:00
|
|
|
var encache = function encache() {
|
|
|
|
if(has_buf) {
|
2014-06-26 05:54:13 +00:00
|
|
|
if(cpdcache[sbcs_cache[0]]) return;
|
2016-09-22 17:42:47 +00:00
|
|
|
var i=0, s=0;
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < sbcs_cache.length; ++i) {
|
|
|
|
s = sbcs_cache[i];
|
|
|
|
if(cpt[s]) {
|
|
|
|
cpdcache[s] = sbcs_decode(s);
|
|
|
|
cpecache[s] = sbcs_encode(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i = 0; i < dbcs_cache.length; ++i) {
|
|
|
|
s = dbcs_cache[i];
|
|
|
|
if(cpt[s]) {
|
|
|
|
cpdcache[s] = dbcs_decode(s);
|
|
|
|
cpecache[s] = dbcs_encode(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i = 0; i < magic_cache.length; ++i) {
|
|
|
|
s = magic_cache[i];
|
2014-04-14 20:45:36 +00:00
|
|
|
if(magic_decode[s]) cpdcache[s] = magic_decode[s];
|
|
|
|
if(magic_encode[s]) cpecache[s] = magic_encode[s];
|
2014-12-25 20:50:25 +00:00
|
|
|
}
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
};
|
2018-01-18 22:47:47 +00:00
|
|
|
var null_enc = function(data, ofmt) { void ofmt; return ""; };
|
2016-09-22 17:42:47 +00:00
|
|
|
var cp_decache = function cp_decache(cp) { delete cpdcache[cp]; delete cpecache[cp]; };
|
2014-12-25 20:50:25 +00:00
|
|
|
var decache = function decache() {
|
|
|
|
if(has_buf) {
|
2014-06-26 05:54:13 +00:00
|
|
|
if(!cpdcache[sbcs_cache[0]]) return;
|
2014-04-14 20:45:36 +00:00
|
|
|
sbcs_cache.forEach(cp_decache);
|
|
|
|
dbcs_cache.forEach(cp_decache);
|
|
|
|
magic_cache.forEach(cp_decache);
|
|
|
|
}
|
2016-09-22 17:42:47 +00:00
|
|
|
last_enc = null_enc; last_cp = 0;
|
2014-04-14 20:45:36 +00:00
|
|
|
};
|
|
|
|
var cache = {
|
|
|
|
encache: encache,
|
|
|
|
decache: decache,
|
|
|
|
sbcs: sbcs_cache,
|
|
|
|
dbcs: dbcs_cache
|
|
|
|
};
|
|
|
|
|
|
|
|
encache();
|
|
|
|
|
|
|
|
var BM = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
var SetD = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),-./:?";
|
2016-09-22 17:42:47 +00:00
|
|
|
var last_enc = null_enc, last_cp = 0;
|
2014-12-25 20:50:25 +00:00
|
|
|
var encode = function encode(cp, data, ofmt) {
|
2016-09-22 17:42:47 +00:00
|
|
|
if(cp === last_cp && last_enc) { return last_enc(data, ofmt); }
|
|
|
|
if(cpecache[cp]) { last_enc = cpecache[last_cp=cp]; return last_enc(data, ofmt); }
|
2014-12-25 20:50:25 +00:00
|
|
|
if(has_buf && Buffer.isBuffer(data)) data = data.toString('utf8');
|
|
|
|
var len = data.length;
|
2016-09-22 17:42:47 +00:00
|
|
|
var out = has_buf ? new Buffer(4*len) : [], w=0, i=0, j = 0, ww=0;
|
|
|
|
var C = cpt[cp], E, M = "";
|
2016-10-15 06:09:05 +00:00
|
|
|
var isstr = typeof data === 'string';
|
2014-12-25 20:50:25 +00:00
|
|
|
if(C && (E=C.enc)) for(i = 0; i < len; ++i, ++j) {
|
2016-10-15 06:09:05 +00:00
|
|
|
w = E[isstr? data.charAt(i) : data[i]];
|
2014-04-14 20:45:36 +00:00
|
|
|
if(w > 255) {
|
2014-12-25 20:50:25 +00:00
|
|
|
out[j] = w>>8;
|
|
|
|
out[++j] = w&255;
|
2016-09-22 17:42:47 +00:00
|
|
|
} else out[j] = w&255;
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
else if((M=magic[cp])) switch(M) {
|
|
|
|
case "utf8":
|
2016-10-15 06:09:05 +00:00
|
|
|
if(has_buf && isstr) { out = new Buffer(data, M); j = out.length; break; }
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; ++i, ++j) {
|
2016-10-15 06:09:05 +00:00
|
|
|
w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
|
2014-04-14 20:45:36 +00:00
|
|
|
if(w <= 0x007F) out[j] = w;
|
|
|
|
else if(w <= 0x07FF) {
|
|
|
|
out[j] = 192 + (w >> 6);
|
2014-12-25 20:50:25 +00:00
|
|
|
out[++j] = 128 + (w&63);
|
|
|
|
} else if(w >= 0xD800 && w <= 0xDFFF) {
|
|
|
|
w -= 0xD800;
|
2016-10-15 06:09:05 +00:00
|
|
|
ww = (isstr ? data.charCodeAt(++i) : data[++i].charCodeAt(0)) - 0xDC00 + (w << 10);
|
2014-12-25 20:50:25 +00:00
|
|
|
out[j] = 240 + ((ww>>>18) & 0x07);
|
|
|
|
out[++j] = 144 + ((ww>>>12) & 0x3F);
|
|
|
|
out[++j] = 128 + ((ww>>>6) & 0x3F);
|
|
|
|
out[++j] = 128 + (ww & 0x3F);
|
2014-04-14 20:45:36 +00:00
|
|
|
} else {
|
|
|
|
out[j] = 224 + (w >> 12);
|
2014-12-25 20:50:25 +00:00
|
|
|
out[++j] = 128 + ((w >> 6)&63);
|
|
|
|
out[++j] = 128 + (w&63);
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "ascii":
|
2014-12-25 20:50:25 +00:00
|
|
|
if(has_buf && typeof data === "string") { out = new Buffer(data, M); j = out.length; break; }
|
|
|
|
for(i = 0; i < len; ++i, ++j) {
|
2016-10-15 06:09:05 +00:00
|
|
|
w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
|
2014-04-14 20:45:36 +00:00
|
|
|
if(w <= 0x007F) out[j] = w;
|
|
|
|
else throw new Error("bad ascii " + w);
|
|
|
|
}
|
|
|
|
break;
|
2014-12-25 20:50:25 +00:00
|
|
|
case "utf16le":
|
|
|
|
if(has_buf && typeof data === "string") { out = new Buffer(data, M); j = out.length; break; }
|
|
|
|
for(i = 0; i < len; ++i) {
|
2016-10-15 06:09:05 +00:00
|
|
|
w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
|
2014-12-25 20:50:25 +00:00
|
|
|
out[j++] = w&255;
|
2014-04-14 20:45:36 +00:00
|
|
|
out[j++] = w>>8;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "utf16be":
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; ++i) {
|
2016-10-15 06:09:05 +00:00
|
|
|
w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
|
2014-04-14 20:45:36 +00:00
|
|
|
out[j++] = w>>8;
|
2014-12-25 20:50:25 +00:00
|
|
|
out[j++] = w&255;
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "utf32le":
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; ++i) {
|
2016-10-15 06:09:05 +00:00
|
|
|
w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
|
2014-12-25 20:50:25 +00:00
|
|
|
if(w >= 0xD800 && w <= 0xDFFF) w = 0x10000 + ((w - 0xD800) << 10) + (data[++i].charCodeAt(0) - 0xDC00);
|
|
|
|
out[j++] = w&255; w >>= 8;
|
|
|
|
out[j++] = w&255; w >>= 8;
|
|
|
|
out[j++] = w&255; w >>= 8;
|
|
|
|
out[j++] = w&255;
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "utf32be":
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; ++i) {
|
2016-10-15 06:09:05 +00:00
|
|
|
w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
|
2014-12-25 20:50:25 +00:00
|
|
|
if(w >= 0xD800 && w <= 0xDFFF) w = 0x10000 + ((w - 0xD800) << 10) + (data[++i].charCodeAt(0) - 0xDC00);
|
|
|
|
out[j+3] = w&255; w >>= 8;
|
|
|
|
out[j+2] = w&255; w >>= 8;
|
|
|
|
out[j+1] = w&255; w >>= 8;
|
2016-09-22 17:42:47 +00:00
|
|
|
out[j] = w&255;
|
2014-04-14 20:45:36 +00:00
|
|
|
j+=4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "utf7":
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; i++) {
|
2016-10-15 06:09:05 +00:00
|
|
|
var c = isstr ? data.charAt(i) : data[i].charAt(0);
|
2014-04-14 20:45:36 +00:00
|
|
|
if(c === "+") { out[j++] = 0x2b; out[j++] = 0x2d; continue; }
|
|
|
|
if(SetD.indexOf(c) > -1) { out[j++] = c.charCodeAt(0); continue; }
|
2016-09-22 17:42:47 +00:00
|
|
|
var tt = encode(1201, c);
|
2014-04-14 20:45:36 +00:00
|
|
|
out[j++] = 0x2b;
|
|
|
|
out[j++] = BM.charCodeAt(tt[0]>>2);
|
|
|
|
out[j++] = BM.charCodeAt(((tt[0]&0x03)<<4) + ((tt[1]||0)>>4));
|
|
|
|
out[j++] = BM.charCodeAt(((tt[1]&0x0F)<<2) + ((tt[2]||0)>>6));
|
|
|
|
out[j++] = 0x2d;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: throw new Error("Unsupported magic: " + cp + " " + magic[cp]);
|
|
|
|
}
|
|
|
|
else throw new Error("Unrecognized CP: " + cp);
|
2014-12-25 20:50:25 +00:00
|
|
|
out = out.slice(0,j);
|
2016-09-22 17:42:47 +00:00
|
|
|
if(!has_buf) return (ofmt == 'str') ? (out).map(sfcc).join("") : out;
|
|
|
|
if(!ofmt || ofmt === 'buf') return out;
|
2014-12-25 20:50:25 +00:00
|
|
|
if(ofmt !== 'arr') return out.toString('binary');
|
|
|
|
return [].slice.call(out);
|
2014-04-14 20:45:36 +00:00
|
|
|
};
|
2014-12-25 20:50:25 +00:00
|
|
|
var decode = function decode(cp, data) {
|
2014-04-14 20:45:36 +00:00
|
|
|
var F; if((F=cpdcache[cp])) return F(data);
|
2016-09-22 17:42:47 +00:00
|
|
|
if(typeof data === "string") return decode(cp, data.split("").map(cca));
|
|
|
|
var len = data.length, out = new Array(len), s="", w=0, i=0, j=1, k=0, ww=0;
|
|
|
|
var C = cpt[cp], D, M="";
|
2014-04-14 20:45:36 +00:00
|
|
|
if(C && (D=C.dec)) {
|
2014-12-25 20:50:25 +00:00
|
|
|
for(i = 0; i < len; i+=j) {
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 2;
|
2016-09-22 17:42:47 +00:00
|
|
|
s = D[(data[i]<<8)+ data[i+1]];
|
|
|
|
if(!s) {
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 1;
|
2016-09-22 17:42:47 +00:00
|
|
|
s = D[data[i]];
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
2016-09-22 17:42:47 +00:00
|
|
|
if(!s) throw new Error('Unrecognized code: ' + data[i] + ' ' + data[i+j-1] + ' ' + i + ' ' + j + ' ' + D[data[i]]);
|
|
|
|
out[k++] = s;
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if((M=magic[cp])) switch(M) {
|
|
|
|
case "utf8":
|
2014-12-25 20:50:25 +00:00
|
|
|
if(len >= 3 && data[0] == 0xEF) if(data[1] == 0xBB && data[2] == 0xBF) i = 3;
|
|
|
|
for(; i < len; i+=j) {
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 1;
|
|
|
|
if(data[i] < 128) w = data[i];
|
2014-12-25 20:50:25 +00:00
|
|
|
else if(data[i] < 224) { w=(data[i]&31)*64+(data[i+1]&63); j=2; }
|
|
|
|
else if(data[i] < 240) { w=((data[i]&15)<<12)+(data[i+1]&63)*64+(data[i+2]&63); j=3; }
|
|
|
|
else { w=(data[i]&7)*262144+((data[i+1]&63)<<12)+(data[i+2]&63)*64+(data[i+3]&63); j=4; }
|
|
|
|
if(w < 65536) { out[k++] = String.fromCharCode(w); }
|
|
|
|
else {
|
|
|
|
w -= 65536; ww = 0xD800 + ((w>>10)&1023); w = 0xDC00 + (w&1023);
|
|
|
|
out[k++] = String.fromCharCode(ww); out[k++] = String.fromCharCode(w);
|
|
|
|
}
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "ascii":
|
2014-12-25 20:50:25 +00:00
|
|
|
if(has_buf && Buffer.isBuffer(data)) return data.toString(M);
|
|
|
|
for(i = 0; i < len; i++) out[i] = String.fromCharCode(data[i]);
|
|
|
|
k = len; break;
|
2014-04-14 20:45:36 +00:00
|
|
|
case "utf16le":
|
2014-12-25 20:50:25 +00:00
|
|
|
if(len >= 2 && data[0] == 0xFF) if(data[1] == 0xFE) i = 2;
|
|
|
|
if(has_buf && Buffer.isBuffer(data)) return data.toString(M);
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 2;
|
2016-09-22 17:42:47 +00:00
|
|
|
for(; i+1 < len; i+=j) {
|
2014-12-25 20:50:25 +00:00
|
|
|
out[k++] = String.fromCharCode((data[i+1]<<8) + data[i]);
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "utf16be":
|
2014-12-25 20:50:25 +00:00
|
|
|
if(len >= 2 && data[0] == 0xFE) if(data[1] == 0xFF) i = 2;
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 2;
|
2016-09-22 17:42:47 +00:00
|
|
|
for(; i+1 < len; i+=j) {
|
2014-12-25 20:50:25 +00:00
|
|
|
out[k++] = String.fromCharCode((data[i]<<8) + data[i+1]);
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "utf32le":
|
2016-01-16 03:27:02 +00:00
|
|
|
if(len >= 4 && data[0] == 0xFF) if(data[1] == 0xFE && data[2] === 0 && data[3] === 0) i = 4;
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 4;
|
2014-12-25 20:50:25 +00:00
|
|
|
for(; i < len; i+=j) {
|
|
|
|
w = (data[i+3]<<24) + (data[i+2]<<16) + (data[i+1]<<8) + (data[i]);
|
|
|
|
if(w > 0xFFFF) {
|
|
|
|
w -= 0x10000;
|
|
|
|
out[k++] = String.fromCharCode(0xD800 + ((w >> 10) & 0x3FF));
|
|
|
|
out[k++] = String.fromCharCode(0xDC00 + (w & 0x3FF));
|
|
|
|
}
|
|
|
|
else out[k++] = String.fromCharCode(w);
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "utf32be":
|
2016-01-16 03:27:02 +00:00
|
|
|
if(len >= 4 && data[3] == 0xFF) if(data[2] == 0xFE && data[1] === 0 && data[0] === 0) i = 4;
|
2014-04-14 20:45:36 +00:00
|
|
|
j = 4;
|
2014-12-25 20:50:25 +00:00
|
|
|
for(; i < len; i+=j) {
|
|
|
|
w = (data[i]<<24) + (data[i+1]<<16) + (data[i+2]<<8) + (data[i+3]);
|
|
|
|
if(w > 0xFFFF) {
|
|
|
|
w -= 0x10000;
|
|
|
|
out[k++] = String.fromCharCode(0xD800 + ((w >> 10) & 0x3FF));
|
|
|
|
out[k++] = String.fromCharCode(0xDC00 + (w & 0x3FF));
|
|
|
|
}
|
|
|
|
else out[k++] = String.fromCharCode(w);
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "utf7":
|
2014-12-25 20:50:25 +00:00
|
|
|
if(len >= 4 && data[0] == 0x2B && data[1] == 0x2F && data[2] == 0x76) {
|
|
|
|
if(len >= 5 && data[3] == 0x38 && data[4] == 0x2D) i = 5;
|
|
|
|
else if(data[3] == 0x38 || data[3] == 0x39 || data[3] == 0x2B || data[3] == 0x2F) i = 4;
|
|
|
|
}
|
|
|
|
for(; i < len; i+=j) {
|
2014-04-14 20:45:36 +00:00
|
|
|
if(data[i] !== 0x2b) { j=1; out[k++] = String.fromCharCode(data[i]); continue; }
|
|
|
|
j=1;
|
|
|
|
if(data[i+1] === 0x2d) { j = 2; out[k++] = "+"; continue; }
|
2018-01-18 22:47:47 +00:00
|
|
|
// eslint-disable-next-line no-useless-escape
|
2014-04-14 20:45:36 +00:00
|
|
|
while(String.fromCharCode(data[i+j]).match(/[A-Za-z0-9+\/]/)) j++;
|
|
|
|
var dash = 0;
|
|
|
|
if(data[i+j] === 0x2d) { ++j; dash=1; }
|
|
|
|
var tt = [];
|
2016-09-22 17:42:47 +00:00
|
|
|
var o64 = "";
|
|
|
|
var c1=0, c2=0, c3=0;
|
|
|
|
var e1=0, e2=0, e3=0, e4=0;
|
2014-04-14 20:45:36 +00:00
|
|
|
for(var l = 1; l < j - dash;) {
|
|
|
|
e1 = BM.indexOf(String.fromCharCode(data[i+l++]));
|
|
|
|
e2 = BM.indexOf(String.fromCharCode(data[i+l++]));
|
|
|
|
c1 = e1 << 2 | e2 >> 4;
|
|
|
|
tt.push(c1);
|
|
|
|
e3 = BM.indexOf(String.fromCharCode(data[i+l++]));
|
|
|
|
if(e3 === -1) break;
|
|
|
|
c2 = (e2 & 15) << 4 | e3 >> 2;
|
|
|
|
tt.push(c2);
|
|
|
|
e4 = BM.indexOf(String.fromCharCode(data[i+l++]));
|
|
|
|
if(e4 === -1) break;
|
|
|
|
c3 = (e3 & 3) << 6 | e4;
|
2014-12-25 20:50:25 +00:00
|
|
|
if(e4 < 64) tt.push(c3);
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
o64 = decode(1201, tt);
|
2016-09-22 17:42:47 +00:00
|
|
|
for(l = 0; l < o64.length; ++l) out[k++] = o64.charAt(l);
|
2014-04-14 20:45:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: throw new Error("Unsupported magic: " + cp + " " + magic[cp]);
|
|
|
|
}
|
|
|
|
else throw new Error("Unrecognized CP: " + cp);
|
2016-01-16 03:27:02 +00:00
|
|
|
return out.slice(0,k).join("");
|
2014-04-14 20:45:36 +00:00
|
|
|
};
|
2016-09-22 17:42:47 +00:00
|
|
|
var hascp = function hascp(cp) { return !!(cpt[cp] || magic[cp]); };
|
2014-04-14 20:45:36 +00:00
|
|
|
cpt.utils = { decode: decode, encode: encode, hascp: hascp, magic: magic, cache:cache };
|
|
|
|
return cpt;
|
|
|
|
}));
|