cputils.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*! cputils.js (C) 2013-present SheetJS -- http://sheetjs.com */
  2. /* vim: set ft=javascript: */
  3. /*jshint newcap: false */
  4. (function(root, factory) {
  5. /*jshint ignore:start */
  6. /*eslint-disable */
  7. "use strict";
  8. if(typeof cptable === "undefined") {
  9. if(typeof require !== "undefined"){
  10. var cpt = require('./cptable');
  11. if (typeof module !== 'undefined' && module.exports && typeof DO_NOT_EXPORT_CODEPAGE === 'undefined') module.exports = factory(cpt);
  12. else root.cptable = factory(cpt);
  13. } else throw new Error("cptable not found");
  14. } else cptable = factory(cptable);
  15. /*eslint-enable */
  16. /*jshint ignore:end */
  17. }(this, function(cpt){
  18. "use strict";
  19. /*global module, Buffer */
  20. var magic = {
  21. "1200":"utf16le",
  22. "1201":"utf16be",
  23. "12000":"utf32le",
  24. "12001":"utf32be",
  25. "16969":"utf64le",
  26. "20127":"ascii",
  27. "65000":"utf7",
  28. "65001":"utf8"
  29. };
  30. var sbcs_cache = [874,1250,1251,1252,1253,1254,1255,1256,10000];
  31. var dbcs_cache = [932,936,949,950];
  32. var magic_cache = [65001];
  33. var magic_decode = {};
  34. var magic_encode = {};
  35. var cpdcache = {};
  36. var cpecache = {};
  37. var sfcc = function sfcc(x) { return String.fromCharCode(x); };
  38. var cca = function cca(x) { return x.charCodeAt(0); };
  39. var has_buf = (typeof Buffer !== 'undefined');
  40. var Buffer_from = function(){};
  41. if(has_buf) {
  42. var nbfs = !Buffer.from;
  43. if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
  44. Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
  45. // $FlowIgnore
  46. if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); };
  47. var mdl = 1024, mdb = Buffer.allocUnsafe(mdl);
  48. var make_EE = function make_EE(E){
  49. var EE = Buffer.allocUnsafe(65536);
  50. for(var i = 0; i < 65536;++i) EE[i] = 0;
  51. var keys = Object.keys(E), len = keys.length;
  52. for(var ee = 0, e = keys[ee]; ee < len; ++ee) {
  53. if(!(e = keys[ee])) continue;
  54. EE[e.charCodeAt(0)] = E[e];
  55. }
  56. return EE;
  57. };
  58. var sbcs_encode = function make_sbcs_encode(cp) {
  59. var EE = make_EE(cpt[cp].enc);
  60. return function sbcs_e(data, ofmt) {
  61. var len = data.length;
  62. var out, i=0, j=0, D=0, w=0;
  63. if(typeof data === 'string') {
  64. out = Buffer.allocUnsafe(len);
  65. for(i = 0; i < len; ++i) out[i] = EE[data.charCodeAt(i)];
  66. } else if(Buffer.isBuffer(data)) {
  67. out = Buffer.allocUnsafe(2*len);
  68. j = 0;
  69. for(i = 0; i < len; ++i) {
  70. D = data[i];
  71. if(D < 128) out[j++] = EE[D];
  72. else if(D < 224) { out[j++] = EE[((D&31)<<6)+(data[i+1]&63)]; ++i; }
  73. else if(D < 240) { out[j++] = EE[((D&15)<<12)+((data[i+1]&63)<<6)+(data[i+2]&63)]; i+=2; }
  74. else {
  75. w = ((D&7)<<18)+((data[i+1]&63)<<12)+((data[i+2]&63)<<6)+(data[i+3]&63); i+=3;
  76. if(w < 65536) out[j++] = EE[w];
  77. else { w -= 65536; out[j++] = EE[0xD800 + ((w>>10)&1023)]; out[j++] = EE[0xDC00 + (w&1023)]; }
  78. }
  79. }
  80. out = out.slice(0,j);
  81. } else {
  82. out = Buffer.allocUnsafe(len);
  83. for(i = 0; i < len; ++i) out[i] = EE[data[i].charCodeAt(0)];
  84. }
  85. if(!ofmt || ofmt === 'buf') return out;
  86. if(ofmt !== 'arr') return out.toString('binary');
  87. return [].slice.call(out);
  88. };
  89. };
  90. var sbcs_decode = function make_sbcs_decode(cp) {
  91. var D = cpt[cp].dec;
  92. var DD = Buffer.allocUnsafe(131072), d=0, c="";
  93. for(d=0;d<D.length;++d) {
  94. if(!(c=D[d])) continue;
  95. var w = c.charCodeAt(0);
  96. DD[2*d] = w&255; DD[2*d+1] = w>>8;
  97. }
  98. return function sbcs_d(data) {
  99. var len = data.length, i=0, j=0;
  100. if(2 * len > mdl) { mdl = 2 * len; mdb = Buffer.allocUnsafe(mdl); }
  101. if(Buffer.isBuffer(data)) {
  102. for(i = 0; i < len; i++) {
  103. j = 2*data[i];
  104. mdb[2*i] = DD[j]; mdb[2*i+1] = DD[j+1];
  105. }
  106. } else if(typeof data === "string") {
  107. for(i = 0; i < len; i++) {
  108. j = 2*data.charCodeAt(i);
  109. mdb[2*i] = DD[j]; mdb[2*i+1] = DD[j+1];
  110. }
  111. } else {
  112. for(i = 0; i < len; i++) {
  113. j = 2*data[i];
  114. mdb[2*i] = DD[j]; mdb[2*i+1] = DD[j+1];
  115. }
  116. }
  117. return mdb.slice(0, 2 * len).toString('ucs2');
  118. };
  119. };
  120. var dbcs_encode = function make_dbcs_encode(cp) {
  121. var E = cpt[cp].enc;
  122. var EE = Buffer.allocUnsafe(131072);
  123. for(var i = 0; i < 131072; ++i) EE[i] = 0;
  124. var keys = Object.keys(E);
  125. for(var ee = 0, e = keys[ee]; ee < keys.length; ++ee) {
  126. if(!(e = keys[ee])) continue;
  127. var f = e.charCodeAt(0);
  128. EE[2*f] = E[e] & 255; EE[2*f+1] = E[e]>>8;
  129. }
  130. return function dbcs_e(data, ofmt) {
  131. var len = data.length, out = Buffer.allocUnsafe(2*len), i=0, j=0, jj=0, k=0, D=0;
  132. if(typeof data === 'string') {
  133. for(i = k = 0; i < len; ++i) {
  134. j = data.charCodeAt(i)*2;
  135. out[k++] = EE[j+1] || EE[j]; if(EE[j+1] > 0) out[k++] = EE[j];
  136. }
  137. out = out.slice(0,k);
  138. } else if(Buffer.isBuffer(data)) {
  139. for(i = k = 0; i < len; ++i) {
  140. D = data[i];
  141. if(D < 128) j = D;
  142. else if(D < 224) { j = ((D&31)<<6)+(data[i+1]&63); ++i; }
  143. else if(D < 240) { j = ((D&15)<<12)+((data[i+1]&63)<<6)+(data[i+2]&63); i+=2; }
  144. else { j = ((D&7)<<18)+((data[i+1]&63)<<12)+((data[i+2]&63)<<6)+(data[i+3]&63); i+=3; }
  145. if(j<65536) { j*=2; out[k++] = EE[j+1] || EE[j]; if(EE[j+1] > 0) out[k++] = EE[j]; }
  146. else { jj = j-65536;
  147. j=2*(0xD800 + ((jj>>10)&1023)); out[k++] = EE[j+1] || EE[j]; if(EE[j+1] > 0) out[k++] = EE[j];
  148. j=2*(0xDC00 + (jj&1023)); out[k++] = EE[j+1] || EE[j]; if(EE[j+1] > 0) out[k++] = EE[j];
  149. }
  150. }
  151. out = out.slice(0,k);
  152. } else {
  153. for(i = k = 0; i < len; i++) {
  154. j = data[i].charCodeAt(0)*2;
  155. out[k++] = EE[j+1] || EE[j]; if(EE[j+1] > 0) out[k++] = EE[j];
  156. }
  157. }
  158. if(!ofmt || ofmt === 'buf') return out;
  159. if(ofmt !== 'arr') return out.toString('binary');
  160. return [].slice.call(out);
  161. };
  162. };
  163. var dbcs_decode = function make_dbcs_decode(cp) {
  164. var D = cpt[cp].dec;
  165. var DD = Buffer.allocUnsafe(131072), d=0, c, w=0, j=0, i=0;
  166. for(i = 0; i < 65536; ++i) { DD[2*i] = 0xFF; DD[2*i+1] = 0xFD;}
  167. for(d = 0; d < D.length; ++d) {
  168. if(!(c=D[d])) continue;
  169. w = c.charCodeAt(0);
  170. j = 2*d;
  171. DD[j] = w&255; DD[j+1] = w>>8;
  172. }
  173. return function dbcs_d(data) {
  174. var len = data.length, out = Buffer.allocUnsafe(2*len), i=0, j=0, k=0;
  175. if(Buffer.isBuffer(data)) {
  176. for(i = 0; i < len; i++) {
  177. j = 2*data[i];
  178. if(DD[j]===0xFF && DD[j+1]===0xFD) { j=2*((data[i]<<8)+data[i+1]); ++i; }
  179. out[k++] = DD[j]; out[k++] = DD[j+1];
  180. }
  181. } else if(typeof data === "string") {
  182. for(i = 0; i < len; i++) {
  183. j = 2*data.charCodeAt(i);
  184. if(DD[j]===0xFF && DD[j+1]===0xFD) { j=2*((data.charCodeAt(i)<<8)+data.charCodeAt(i+1)); ++i; }
  185. out[k++] = DD[j]; out[k++] = DD[j+1];
  186. }
  187. } else {
  188. for(i = 0; i < len; i++) {
  189. j = 2*data[i];
  190. if(DD[j]===0xFF && DD[j+1]===0xFD) { j=2*((data[i]<<8)+data[i+1]); ++i; }
  191. out[k++] = DD[j]; out[k++] = DD[j+1];
  192. }
  193. }
  194. return out.slice(0,k).toString('ucs2');
  195. };
  196. };
  197. magic_decode[65001] = function utf8_d(data) {
  198. if(typeof data === "string") return utf8_d(data.split("").map(cca));
  199. var len = data.length, w = 0, ww = 0;
  200. if(4 * len > mdl) { mdl = 4 * len; mdb = Buffer.allocUnsafe(mdl); }
  201. var i = 0;
  202. if(len >= 3 && data[0] == 0xEF) if(data[1] == 0xBB && data[2] == 0xBF) i = 3;
  203. for(var j = 1, k = 0, D = 0; i < len; i+=j) {
  204. j = 1; D = data[i];
  205. if(D < 128) w = D;
  206. else if(D < 224) { w=(D&31)*64+(data[i+1]&63); j=2; }
  207. else if(D < 240) { w=((D&15)<<12)+(data[i+1]&63)*64+(data[i+2]&63); j=3; }
  208. else { w=(D&7)*262144+((data[i+1]&63)<<12)+(data[i+2]&63)*64+(data[i+3]&63); j=4; }
  209. if(w < 65536) { mdb[k++] = w&255; mdb[k++] = w>>8; }
  210. else {
  211. w -= 65536; ww = 0xD800 + ((w>>10)&1023); w = 0xDC00 + (w&1023);
  212. mdb[k++] = ww&255; mdb[k++] = ww>>>8; mdb[k++] = w&255; mdb[k++] = (w>>>8)&255;
  213. }
  214. }
  215. return mdb.slice(0,k).toString('ucs2');
  216. };
  217. magic_encode[65001] = function utf8_e(data, ofmt) {
  218. if(has_buf && Buffer.isBuffer(data)) {
  219. if(!ofmt || ofmt === 'buf') return data;
  220. if(ofmt !== 'arr') return data.toString('binary');
  221. return [].slice.call(data);
  222. }
  223. var len = data.length, w = 0, ww = 0, j = 0;
  224. var direct = typeof data === "string";
  225. if(4 * len > mdl) { mdl = 4 * len; mdb = Buffer.allocUnsafe(mdl); }
  226. for(var i = 0; i < len; ++i) {
  227. w = direct ? data.charCodeAt(i) : data[i].charCodeAt(0);
  228. if(w <= 0x007F) mdb[j++] = w;
  229. else if(w <= 0x07FF) {
  230. mdb[j++] = 192 + (w >> 6);
  231. mdb[j++] = 128 + (w&63);
  232. } else if(w >= 0xD800 && w <= 0xDFFF) {
  233. w -= 0xD800; ++i;
  234. ww = (direct ? data.charCodeAt(i) : data[i].charCodeAt(0)) - 0xDC00 + (w << 10);
  235. mdb[j++] = 240 + ((ww>>>18) & 0x07);
  236. mdb[j++] = 144 + ((ww>>>12) & 0x3F);
  237. mdb[j++] = 128 + ((ww>>>6) & 0x3F);
  238. mdb[j++] = 128 + (ww & 0x3F);
  239. } else {
  240. mdb[j++] = 224 + (w >> 12);
  241. mdb[j++] = 128 + ((w >> 6)&63);
  242. mdb[j++] = 128 + (w&63);
  243. }
  244. }
  245. if(!ofmt || ofmt === 'buf') return mdb.slice(0,j);
  246. if(ofmt !== 'arr') return mdb.slice(0,j).toString('binary');
  247. return [].slice.call(mdb, 0, j);
  248. };
  249. }
  250. var encache = function encache() {
  251. if(has_buf) {
  252. if(cpdcache[sbcs_cache[0]]) return;
  253. var i=0, s=0;
  254. for(i = 0; i < sbcs_cache.length; ++i) {
  255. s = sbcs_cache[i];
  256. if(cpt[s]) {
  257. cpdcache[s] = sbcs_decode(s);
  258. cpecache[s] = sbcs_encode(s);
  259. }
  260. }
  261. for(i = 0; i < dbcs_cache.length; ++i) {
  262. s = dbcs_cache[i];
  263. if(cpt[s]) {
  264. cpdcache[s] = dbcs_decode(s);
  265. cpecache[s] = dbcs_encode(s);
  266. }
  267. }
  268. for(i = 0; i < magic_cache.length; ++i) {
  269. s = magic_cache[i];
  270. if(magic_decode[s]) cpdcache[s] = magic_decode[s];
  271. if(magic_encode[s]) cpecache[s] = magic_encode[s];
  272. }
  273. }
  274. };
  275. var null_enc = function(data, ofmt) { void ofmt; return ""; };
  276. var cp_decache = function cp_decache(cp) { delete cpdcache[cp]; delete cpecache[cp]; };
  277. var decache = function decache() {
  278. if(has_buf) {
  279. if(!cpdcache[sbcs_cache[0]]) return;
  280. sbcs_cache.forEach(cp_decache);
  281. dbcs_cache.forEach(cp_decache);
  282. magic_cache.forEach(cp_decache);
  283. }
  284. last_enc = null_enc; last_cp = 0;
  285. };
  286. var cache = {
  287. encache: encache,
  288. decache: decache,
  289. sbcs: sbcs_cache,
  290. dbcs: dbcs_cache
  291. };
  292. encache();
  293. var BM = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  294. var SetD = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),-./:?";
  295. var last_enc = null_enc, last_cp = 0;
  296. var encode = function encode(cp, data, ofmt) {
  297. if(cp === last_cp && last_enc) { return last_enc(data, ofmt); }
  298. if(cpecache[cp]) { last_enc = cpecache[last_cp=cp]; return last_enc(data, ofmt); }
  299. if(has_buf && Buffer.isBuffer(data)) data = data.toString('utf8');
  300. var len = data.length;
  301. var out = has_buf ? Buffer.allocUnsafe(4*len) : [], w=0, i=0, j = 0, ww=0;
  302. var C = cpt[cp], E, M = "";
  303. var isstr = typeof data === 'string';
  304. if(C && (E=C.enc)) for(i = 0; i < len; ++i, ++j) {
  305. w = E[isstr? data.charAt(i) : data[i]];
  306. if(w > 255) {
  307. out[j] = w>>8;
  308. out[++j] = w&255;
  309. } else out[j] = w&255;
  310. }
  311. else if((M=magic[cp])) switch(M) {
  312. case "utf8":
  313. if(has_buf && isstr) { out = Buffer_from(data, M); j = out.length; break; }
  314. for(i = 0; i < len; ++i, ++j) {
  315. w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
  316. if(w <= 0x007F) out[j] = w;
  317. else if(w <= 0x07FF) {
  318. out[j] = 192 + (w >> 6);
  319. out[++j] = 128 + (w&63);
  320. } else if(w >= 0xD800 && w <= 0xDFFF) {
  321. w -= 0xD800;
  322. ww = (isstr ? data.charCodeAt(++i) : data[++i].charCodeAt(0)) - 0xDC00 + (w << 10);
  323. out[j] = 240 + ((ww>>>18) & 0x07);
  324. out[++j] = 144 + ((ww>>>12) & 0x3F);
  325. out[++j] = 128 + ((ww>>>6) & 0x3F);
  326. out[++j] = 128 + (ww & 0x3F);
  327. } else {
  328. out[j] = 224 + (w >> 12);
  329. out[++j] = 128 + ((w >> 6)&63);
  330. out[++j] = 128 + (w&63);
  331. }
  332. }
  333. break;
  334. case "ascii":
  335. if(has_buf && typeof data === "string") { out = Buffer_from(data, M); j = out.length; break; }
  336. for(i = 0; i < len; ++i, ++j) {
  337. w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
  338. if(w <= 0x007F) out[j] = w;
  339. else throw new Error("bad ascii " + w);
  340. }
  341. break;
  342. case "utf16le":
  343. if(has_buf && typeof data === "string") { out = Buffer_from(data, M); j = out.length; break; }
  344. for(i = 0; i < len; ++i) {
  345. w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
  346. out[j++] = w&255;
  347. out[j++] = w>>8;
  348. }
  349. break;
  350. case "utf16be":
  351. for(i = 0; i < len; ++i) {
  352. w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
  353. out[j++] = w>>8;
  354. out[j++] = w&255;
  355. }
  356. break;
  357. case "utf32le":
  358. for(i = 0; i < len; ++i) {
  359. w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
  360. if(w >= 0xD800 && w <= 0xDFFF) w = 0x10000 + ((w - 0xD800) << 10) + (data[++i].charCodeAt(0) - 0xDC00);
  361. out[j++] = w&255; w >>= 8;
  362. out[j++] = w&255; w >>= 8;
  363. out[j++] = w&255; w >>= 8;
  364. out[j++] = w&255;
  365. }
  366. break;
  367. case "utf32be":
  368. for(i = 0; i < len; ++i) {
  369. w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
  370. if(w >= 0xD800 && w <= 0xDFFF) w = 0x10000 + ((w - 0xD800) << 10) + (data[++i].charCodeAt(0) - 0xDC00);
  371. out[j+3] = w&255; w >>= 8;
  372. out[j+2] = w&255; w >>= 8;
  373. out[j+1] = w&255; w >>= 8;
  374. out[j] = w&255;
  375. j+=4;
  376. }
  377. break;
  378. case "utf7":
  379. for(i = 0; i < len; i++) {
  380. var c = isstr ? data.charAt(i) : data[i].charAt(0);
  381. if(c === "+") { out[j++] = 0x2b; out[j++] = 0x2d; continue; }
  382. if(SetD.indexOf(c) > -1) { out[j++] = c.charCodeAt(0); continue; }
  383. var tt = encode(1201, c);
  384. out[j++] = 0x2b;
  385. out[j++] = BM.charCodeAt(tt[0]>>2);
  386. out[j++] = BM.charCodeAt(((tt[0]&0x03)<<4) + ((tt[1]||0)>>4));
  387. out[j++] = BM.charCodeAt(((tt[1]&0x0F)<<2) + ((tt[2]||0)>>6));
  388. out[j++] = 0x2d;
  389. }
  390. break;
  391. default: throw new Error("Unsupported magic: " + cp + " " + magic[cp]);
  392. }
  393. else throw new Error("Unrecognized CP: " + cp);
  394. out = out.slice(0,j);
  395. if(!has_buf) return (ofmt == 'str') ? (out).map(sfcc).join("") : out;
  396. if(!ofmt || ofmt === 'buf') return out;
  397. if(ofmt !== 'arr') return out.toString('binary');
  398. return [].slice.call(out);
  399. };
  400. var decode = function decode(cp, data) {
  401. var F; if((F=cpdcache[cp])) return F(data);
  402. if(typeof data === "string") return decode(cp, data.split("").map(cca));
  403. var len = data.length, out = new Array(len), s="", w=0, i=0, j=1, k=0, ww=0;
  404. var C = cpt[cp], D, M="";
  405. if(C && (D=C.dec)) {
  406. for(i = 0; i < len; i+=j) {
  407. j = 2;
  408. s = D[(data[i]<<8)+ data[i+1]];
  409. if(!s) {
  410. j = 1;
  411. s = D[data[i]];
  412. }
  413. if(!s) throw new Error('Unrecognized code: ' + data[i] + ' ' + data[i+j-1] + ' ' + i + ' ' + j + ' ' + D[data[i]]);
  414. out[k++] = s;
  415. }
  416. }
  417. else if((M=magic[cp])) switch(M) {
  418. case "utf8":
  419. if(len >= 3 && data[0] == 0xEF) if(data[1] == 0xBB && data[2] == 0xBF) i = 3;
  420. for(; i < len; i+=j) {
  421. j = 1;
  422. if(data[i] < 128) w = data[i];
  423. else if(data[i] < 224) { w=(data[i]&31)*64+(data[i+1]&63); j=2; }
  424. else if(data[i] < 240) { w=((data[i]&15)<<12)+(data[i+1]&63)*64+(data[i+2]&63); j=3; }
  425. else { w=(data[i]&7)*262144+((data[i+1]&63)<<12)+(data[i+2]&63)*64+(data[i+3]&63); j=4; }
  426. if(w < 65536) { out[k++] = String.fromCharCode(w); }
  427. else {
  428. w -= 65536; ww = 0xD800 + ((w>>10)&1023); w = 0xDC00 + (w&1023);
  429. out[k++] = String.fromCharCode(ww); out[k++] = String.fromCharCode(w);
  430. }
  431. }
  432. break;
  433. case "ascii":
  434. if(has_buf && Buffer.isBuffer(data)) return data.toString(M);
  435. for(i = 0; i < len; i++) out[i] = String.fromCharCode(data[i]);
  436. k = len; break;
  437. case "utf16le":
  438. if(len >= 2 && data[0] == 0xFF) if(data[1] == 0xFE) i = 2;
  439. if(has_buf && Buffer.isBuffer(data)) return data.toString(M);
  440. j = 2;
  441. for(; i+1 < len; i+=j) {
  442. out[k++] = String.fromCharCode((data[i+1]<<8) + data[i]);
  443. }
  444. break;
  445. case "utf16be":
  446. if(len >= 2 && data[0] == 0xFE) if(data[1] == 0xFF) i = 2;
  447. j = 2;
  448. for(; i+1 < len; i+=j) {
  449. out[k++] = String.fromCharCode((data[i]<<8) + data[i+1]);
  450. }
  451. break;
  452. case "utf32le":
  453. if(len >= 4 && data[0] == 0xFF) if(data[1] == 0xFE && data[2] === 0 && data[3] === 0) i = 4;
  454. j = 4;
  455. for(; i < len; i+=j) {
  456. w = (data[i+3]<<24) + (data[i+2]<<16) + (data[i+1]<<8) + (data[i]);
  457. if(w > 0xFFFF) {
  458. w -= 0x10000;
  459. out[k++] = String.fromCharCode(0xD800 + ((w >> 10) & 0x3FF));
  460. out[k++] = String.fromCharCode(0xDC00 + (w & 0x3FF));
  461. }
  462. else out[k++] = String.fromCharCode(w);
  463. }
  464. break;
  465. case "utf32be":
  466. if(len >= 4 && data[3] == 0xFF) if(data[2] == 0xFE && data[1] === 0 && data[0] === 0) i = 4;
  467. j = 4;
  468. for(; i < len; i+=j) {
  469. w = (data[i]<<24) + (data[i+1]<<16) + (data[i+2]<<8) + (data[i+3]);
  470. if(w > 0xFFFF) {
  471. w -= 0x10000;
  472. out[k++] = String.fromCharCode(0xD800 + ((w >> 10) & 0x3FF));
  473. out[k++] = String.fromCharCode(0xDC00 + (w & 0x3FF));
  474. }
  475. else out[k++] = String.fromCharCode(w);
  476. }
  477. break;
  478. case "utf7":
  479. if(len >= 4 && data[0] == 0x2B && data[1] == 0x2F && data[2] == 0x76) {
  480. if(len >= 5 && data[3] == 0x38 && data[4] == 0x2D) i = 5;
  481. else if(data[3] == 0x38 || data[3] == 0x39 || data[3] == 0x2B || data[3] == 0x2F) i = 4;
  482. }
  483. for(; i < len; i+=j) {
  484. if(data[i] !== 0x2b) { j=1; out[k++] = String.fromCharCode(data[i]); continue; }
  485. j=1;
  486. if(data[i+1] === 0x2d) { j = 2; out[k++] = "+"; continue; }
  487. // eslint-disable-next-line no-useless-escape
  488. while(String.fromCharCode(data[i+j]).match(/[A-Za-z0-9+\/]/)) j++;
  489. var dash = 0;
  490. if(data[i+j] === 0x2d) { ++j; dash=1; }
  491. var tt = [];
  492. var o64 = "";
  493. var c1=0, c2=0, c3=0;
  494. var e1=0, e2=0, e3=0, e4=0;
  495. for(var l = 1; l < j - dash;) {
  496. e1 = BM.indexOf(String.fromCharCode(data[i+l++]));
  497. e2 = BM.indexOf(String.fromCharCode(data[i+l++]));
  498. c1 = e1 << 2 | e2 >> 4;
  499. tt.push(c1);
  500. e3 = BM.indexOf(String.fromCharCode(data[i+l++]));
  501. if(e3 === -1) break;
  502. c2 = (e2 & 15) << 4 | e3 >> 2;
  503. tt.push(c2);
  504. e4 = BM.indexOf(String.fromCharCode(data[i+l++]));
  505. if(e4 === -1) break;
  506. c3 = (e3 & 3) << 6 | e4;
  507. if(e4 < 64) tt.push(c3);
  508. }
  509. o64 = decode(1201, tt);
  510. for(l = 0; l < o64.length; ++l) out[k++] = o64.charAt(l);
  511. }
  512. break;
  513. default: throw new Error("Unsupported magic: " + cp + " " + magic[cp]);
  514. }
  515. else throw new Error("Unrecognized CP: " + cp);
  516. return out.slice(0,k).join("");
  517. };
  518. var hascp = function hascp(cp) { return !!(cpt[cp] || magic[cp]); };
  519. cpt.utils = { decode: decode, encode: encode, hascp: hascp, magic: magic, cache:cache };
  520. return cpt;
  521. }));