cfb.js 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  1. /* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
  2. /* vim: set ts=2: */
  3. /*jshint eqnull:true */
  4. /*exported CFB */
  5. /*global module, require:false, process:false, Buffer:false, Uint8Array:false, Uint16Array:false */
  6. var Base64_map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  7. function Base64_encode(input) {
  8. var o = "";
  9. var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0;
  10. for (var i = 0; i < input.length; ) {
  11. c1 = input.charCodeAt(i++);
  12. e1 = (c1 >> 2);
  13. c2 = input.charCodeAt(i++);
  14. e2 = ((c1 & 3) << 4) | (c2 >> 4);
  15. c3 = input.charCodeAt(i++);
  16. e3 = ((c2 & 15) << 2) | (c3 >> 6);
  17. e4 = (c3 & 63);
  18. if (isNaN(c2)) e3 = e4 = 64;
  19. else if (isNaN(c3)) e4 = 64;
  20. o += Base64_map.charAt(e1) + Base64_map.charAt(e2) + Base64_map.charAt(e3) + Base64_map.charAt(e4);
  21. }
  22. return o;
  23. }
  24. function Base64_decode(input) {
  25. var o = "";
  26. var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0;
  27. input = input.replace(/[^\w\+\/\=]/g, "");
  28. for (var i = 0; i < input.length;) {
  29. e1 = Base64_map.indexOf(input.charAt(i++));
  30. e2 = Base64_map.indexOf(input.charAt(i++));
  31. c1 = (e1 << 2) | (e2 >> 4);
  32. o += String.fromCharCode(c1);
  33. e3 = Base64_map.indexOf(input.charAt(i++));
  34. c2 = ((e2 & 15) << 4) | (e3 >> 2);
  35. if (e3 !== 64) o += String.fromCharCode(c2);
  36. e4 = Base64_map.indexOf(input.charAt(i++));
  37. c3 = ((e3 & 3) << 6) | e4;
  38. if (e4 !== 64) o += String.fromCharCode(c3);
  39. }
  40. return o;
  41. }
  42. var has_buf = (function() { return typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && !!process.versions.node; })();
  43. var Buffer_from = (function() {
  44. if(typeof Buffer !== 'undefined') {
  45. var nbfs = !Buffer.from;
  46. if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
  47. return nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
  48. }
  49. return function() {};
  50. })();
  51. function new_raw_buf(len) {
  52. /* jshint -W056 */
  53. if(has_buf) {
  54. if(Buffer.alloc) return Buffer.alloc(len);
  55. var b = new Buffer(len); b.fill(0); return b;
  56. }
  57. return typeof Uint8Array != "undefined" ? new Uint8Array(len) : new Array(len);
  58. /* jshint +W056 */
  59. }
  60. function new_unsafe_buf(len) {
  61. /* jshint -W056 */
  62. if(has_buf) return Buffer.allocUnsafe ? Buffer.allocUnsafe(len) : new Buffer(len);
  63. return typeof Uint8Array != "undefined" ? new Uint8Array(len) : new Array(len);
  64. /* jshint +W056 */
  65. }
  66. var s2a = function s2a(s) {
  67. if(has_buf) return Buffer_from(s, "binary");
  68. return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; });
  69. };
  70. var chr0 = /\u0000/g, chr1 = /[\u0001-\u0006]/g;
  71. var __toBuffer = function(bufs) { var x = []; for(var i = 0; i < bufs[0].length; ++i) { x.push.apply(x, bufs[0][i]); } return x; };
  72. var ___toBuffer = __toBuffer;
  73. var __utf16le = function(b,s,e) { var ss=[]; for(var i=s; i<e; i+=2) ss.push(String.fromCharCode(__readUInt16LE(b,i))); return ss.join("").replace(chr0,''); };
  74. var ___utf16le = __utf16le;
  75. var __hexlify = function(b,s,l) { var ss=[]; for(var i=s; i<s+l; ++i) ss.push(("0" + b[i].toString(16)).slice(-2)); return ss.join(""); };
  76. var ___hexlify = __hexlify;
  77. var __bconcat = function(bufs) {
  78. if(Array.isArray(bufs[0])) return [].concat.apply([], bufs);
  79. var maxlen = 0, i = 0;
  80. for(i = 0; i < bufs.length; ++i) maxlen += bufs[i].length;
  81. var o = new Uint8Array(maxlen);
  82. for(i = 0, maxlen = 0; i < bufs.length; maxlen += bufs[i].length, ++i) o.set(bufs[i], maxlen);
  83. return o;
  84. };
  85. var bconcat = __bconcat;
  86. if(has_buf) {
  87. __utf16le = function(b,s,e) {
  88. if(!Buffer.isBuffer(b)) return ___utf16le(b,s,e);
  89. return b.toString('utf16le',s,e).replace(chr0,'')/*.replace(chr1,'!')*/;
  90. };
  91. __hexlify = function(b,s,l) { return Buffer.isBuffer(b) ? b.toString('hex',s,s+l) : ___hexlify(b,s,l); };
  92. __toBuffer = function(bufs) { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat((bufs[0])) : ___toBuffer(bufs);};
  93. s2a = function(s) { return Buffer_from(s, "binary"); };
  94. bconcat = function(bufs) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : __bconcat(bufs); };
  95. }
  96. var __readUInt8 = function(b, idx) { return b[idx]; };
  97. var __readUInt16LE = function(b, idx) { return b[idx+1]*(1<<8)+b[idx]; };
  98. var __readInt16LE = function(b, idx) { var u = b[idx+1]*(1<<8)+b[idx]; return (u < 0x8000) ? u : (0xffff - u + 1) * -1; };
  99. var __readUInt32LE = function(b, idx) { return b[idx+3]*(1<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
  100. var __readInt32LE = function(b, idx) { return (b[idx+3]<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
  101. function ReadShift(size, t) {
  102. var oI, oS, type = 0;
  103. switch(size) {
  104. case 1: oI = __readUInt8(this, this.l); break;
  105. case 2: oI = (t !== 'i' ? __readUInt16LE : __readInt16LE)(this, this.l); break;
  106. case 4: oI = __readInt32LE(this, this.l); break;
  107. case 16: type = 2; oS = __hexlify(this, this.l, size);
  108. }
  109. this.l += size; if(type === 0) return oI; return oS;
  110. }
  111. var __writeUInt32LE = function(b, val, idx) { b[idx] = (val & 0xFF); b[idx+1] = ((val >>> 8) & 0xFF); b[idx+2] = ((val >>> 16) & 0xFF); b[idx+3] = ((val >>> 24) & 0xFF); };
  112. var __writeInt32LE = function(b, val, idx) { b[idx] = (val & 0xFF); b[idx+1] = ((val >> 8) & 0xFF); b[idx+2] = ((val >> 16) & 0xFF); b[idx+3] = ((val >> 24) & 0xFF); };
  113. function WriteShift(t, val, f) {
  114. var size = 0, i = 0;
  115. switch(f) {
  116. case "hex": for(; i < t; ++i) {
  117. this[this.l++] = parseInt(val.slice(2*i, 2*i+2), 16)||0;
  118. } return this;
  119. case "utf16le":
  120. var end = this.l + t;
  121. for(i = 0; i < Math.min(val.length, t); ++i) {
  122. var cc = val.charCodeAt(i);
  123. this[this.l++] = cc & 0xff;
  124. this[this.l++] = cc >> 8;
  125. }
  126. while(this.l < end) this[this.l++] = 0;
  127. return this;
  128. }
  129. switch(t) {
  130. case 1: size = 1; this[this.l] = val&0xFF; break;
  131. case 2: size = 2; this[this.l] = val&0xFF; val >>>= 8; this[this.l+1] = val&0xFF; break;
  132. case 4: size = 4; __writeUInt32LE(this, val, this.l); break;
  133. case -4: size = 4; __writeInt32LE(this, val, this.l); break;
  134. }
  135. this.l += size; return this;
  136. }
  137. function CheckField(hexstr, fld) {
  138. var m = __hexlify(this,this.l,hexstr.length>>1);
  139. if(m !== hexstr) throw new Error(fld + 'Expected ' + hexstr + ' saw ' + m);
  140. this.l += hexstr.length>>1;
  141. }
  142. function prep_blob(blob, pos) {
  143. blob.l = pos;
  144. blob.read_shift = ReadShift;
  145. blob.chk = CheckField;
  146. blob.write_shift = WriteShift;
  147. }
  148. function new_buf(sz) {
  149. var o = (new_raw_buf(sz));
  150. prep_blob(o, 0);
  151. return o;
  152. }
  153. /*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
  154. /* vim: set ts=2: */
  155. /*exported CRC32 */
  156. var CRC32 = (function() {
  157. var CRC32 = {};
  158. CRC32.version = '1.2.1';
  159. /*global Int32Array */
  160. function signed_crc_table() {
  161. var c = 0, table = new Array(256);
  162. for(var n =0; n != 256; ++n){
  163. c = n;
  164. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  165. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  166. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  167. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  168. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  169. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  170. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  171. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  172. table[n] = c;
  173. }
  174. return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
  175. }
  176. var T0 = signed_crc_table();
  177. function slice_by_16_tables(T) {
  178. var c = 0, v = 0, n = 0, table = typeof Int32Array !== 'undefined' ? new Int32Array(4096) : new Array(4096) ;
  179. for(n = 0; n != 256; ++n) table[n] = T[n];
  180. for(n = 0; n != 256; ++n) {
  181. v = T[n];
  182. for(c = 256 + n; c < 4096; c += 256) v = table[c] = (v >>> 8) ^ T[v & 0xFF];
  183. }
  184. var out = [];
  185. for(n = 1; n != 16; ++n) out[n - 1] = typeof Int32Array !== 'undefined' ? table.subarray(n * 256, n * 256 + 256) : table.slice(n * 256, n * 256 + 256);
  186. return out;
  187. }
  188. var TT = slice_by_16_tables(T0);
  189. var T1 = TT[0], T2 = TT[1], T3 = TT[2], T4 = TT[3], T5 = TT[4];
  190. var T6 = TT[5], T7 = TT[6], T8 = TT[7], T9 = TT[8], Ta = TT[9];
  191. var Tb = TT[10], Tc = TT[11], Td = TT[12], Te = TT[13], Tf = TT[14];
  192. function crc32_bstr(bstr, seed) {
  193. var C = seed ^ -1;
  194. for(var i = 0, L = bstr.length; i < L;) C = (C>>>8) ^ T0[(C^bstr.charCodeAt(i++))&0xFF];
  195. return ~C;
  196. }
  197. function crc32_buf(B, seed) {
  198. var C = seed ^ -1, L = B.length - 15, i = 0;
  199. for(; i < L;) C =
  200. Tf[B[i++] ^ (C & 255)] ^
  201. Te[B[i++] ^ ((C >> 8) & 255)] ^
  202. Td[B[i++] ^ ((C >> 16) & 255)] ^
  203. Tc[B[i++] ^ (C >>> 24)] ^
  204. Tb[B[i++]] ^ Ta[B[i++]] ^ T9[B[i++]] ^ T8[B[i++]] ^
  205. T7[B[i++]] ^ T6[B[i++]] ^ T5[B[i++]] ^ T4[B[i++]] ^
  206. T3[B[i++]] ^ T2[B[i++]] ^ T1[B[i++]] ^ T0[B[i++]];
  207. L += 15;
  208. while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF];
  209. return ~C;
  210. }
  211. function crc32_str(str, seed) {
  212. var C = seed ^ -1;
  213. for(var i = 0, L = str.length, c = 0, d = 0; i < L;) {
  214. c = str.charCodeAt(i++);
  215. if(c < 0x80) {
  216. C = (C>>>8) ^ T0[(C^c)&0xFF];
  217. } else if(c < 0x800) {
  218. C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF];
  219. C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF];
  220. } else if(c >= 0xD800 && c < 0xE000) {
  221. c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
  222. C = (C>>>8) ^ T0[(C ^ (240|((c>>8)&7)))&0xFF];
  223. C = (C>>>8) ^ T0[(C ^ (128|((c>>2)&63)))&0xFF];
  224. C = (C>>>8) ^ T0[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
  225. C = (C>>>8) ^ T0[(C ^ (128|(d&63)))&0xFF];
  226. } else {
  227. C = (C>>>8) ^ T0[(C ^ (224|((c>>12)&15)))&0xFF];
  228. C = (C>>>8) ^ T0[(C ^ (128|((c>>6)&63)))&0xFF];
  229. C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF];
  230. }
  231. }
  232. return ~C;
  233. }
  234. CRC32.table = T0;
  235. CRC32.bstr = crc32_bstr;
  236. CRC32.buf = crc32_buf;
  237. CRC32.str = crc32_str;
  238. return CRC32;
  239. })();
  240. /* [MS-CFB] v20171201 */
  241. var CFB = (function _CFB(){
  242. var exports = {};
  243. exports.version = '1.2.2';
  244. /* [MS-CFB] 2.6.4 */
  245. function namecmp(l, r) {
  246. var L = l.split("/"), R = r.split("/");
  247. for(var i = 0, c = 0, Z = Math.min(L.length, R.length); i < Z; ++i) {
  248. if((c = L[i].length - R[i].length)) return c;
  249. if(L[i] != R[i]) return L[i] < R[i] ? -1 : 1;
  250. }
  251. return L.length - R.length;
  252. }
  253. function dirname(p) {
  254. if(p.charAt(p.length - 1) == "/") return (p.slice(0,-1).indexOf("/") === -1) ? p : dirname(p.slice(0, -1));
  255. var c = p.lastIndexOf("/");
  256. return (c === -1) ? p : p.slice(0, c+1);
  257. }
  258. function filename(p) {
  259. if(p.charAt(p.length - 1) == "/") return filename(p.slice(0, -1));
  260. var c = p.lastIndexOf("/");
  261. return (c === -1) ? p : p.slice(c+1);
  262. }
  263. /* -------------------------------------------------------------------------- */
  264. /* DOS Date format:
  265. high|YYYYYYYm.mmmddddd.HHHHHMMM.MMMSSSSS|low
  266. add 1980 to stored year
  267. stored second should be doubled
  268. */
  269. /* write JS date to buf as a DOS date */
  270. function write_dos_date(buf, date) {
  271. if(typeof date === "string") date = new Date(date);
  272. var hms = date.getHours();
  273. hms = hms << 6 | date.getMinutes();
  274. hms = hms << 5 | (date.getSeconds()>>>1);
  275. buf.write_shift(2, hms);
  276. var ymd = (date.getFullYear() - 1980);
  277. ymd = ymd << 4 | (date.getMonth()+1);
  278. ymd = ymd << 5 | date.getDate();
  279. buf.write_shift(2, ymd);
  280. }
  281. /* read four bytes from buf and interpret as a DOS date */
  282. function parse_dos_date(buf) {
  283. var hms = buf.read_shift(2) & 0xFFFF;
  284. var ymd = buf.read_shift(2) & 0xFFFF;
  285. var val = new Date();
  286. var d = ymd & 0x1F; ymd >>>= 5;
  287. var m = ymd & 0x0F; ymd >>>= 4;
  288. val.setMilliseconds(0);
  289. val.setFullYear(ymd + 1980);
  290. val.setMonth(m-1);
  291. val.setDate(d);
  292. var S = hms & 0x1F; hms >>>= 5;
  293. var M = hms & 0x3F; hms >>>= 6;
  294. val.setHours(hms);
  295. val.setMinutes(M);
  296. val.setSeconds(S<<1);
  297. return val;
  298. }
  299. function parse_extra_field(blob) {
  300. prep_blob(blob, 0);
  301. var o = {};
  302. var flags = 0;
  303. while(blob.l <= blob.length - 4) {
  304. var type = blob.read_shift(2);
  305. var sz = blob.read_shift(2), tgt = blob.l + sz;
  306. var p = {};
  307. switch(type) {
  308. /* UNIX-style Timestamps */
  309. case 0x5455: {
  310. flags = blob.read_shift(1);
  311. if(flags & 1) p.mtime = blob.read_shift(4);
  312. /* for some reason, CD flag corresponds to LFH */
  313. if(sz > 5) {
  314. if(flags & 2) p.atime = blob.read_shift(4);
  315. if(flags & 4) p.ctime = blob.read_shift(4);
  316. }
  317. if(p.mtime) p.mt = new Date(p.mtime*1000);
  318. }
  319. break;
  320. }
  321. blob.l = tgt;
  322. o[type] = p;
  323. }
  324. return o;
  325. }
  326. var fs;
  327. function get_fs() { return fs || (fs = require('fs')); }
  328. function parse(file, options) {
  329. if(file[0] == 0x50 && file[1] == 0x4b) return parse_zip(file, options);
  330. if((file[0] | 0x20) == 0x6d && (file[1]|0x20) == 0x69) return parse_mad(file, options);
  331. if(file.length < 512) throw new Error("CFB file size " + file.length + " < 512");
  332. var mver = 3;
  333. var ssz = 512;
  334. var nmfs = 0; // number of mini FAT sectors
  335. var difat_sec_cnt = 0;
  336. var dir_start = 0;
  337. var minifat_start = 0;
  338. var difat_start = 0;
  339. var fat_addrs = []; // locations of FAT sectors
  340. /* [MS-CFB] 2.2 Compound File Header */
  341. var blob = file.slice(0,512);
  342. prep_blob(blob, 0);
  343. /* major version */
  344. var mv = check_get_mver(blob);
  345. mver = mv[0];
  346. switch(mver) {
  347. case 3: ssz = 512; break; case 4: ssz = 4096; break;
  348. case 0: if(mv[1] == 0) return parse_zip(file, options);
  349. /* falls through */
  350. default: throw new Error("Major Version: Expected 3 or 4 saw " + mver);
  351. }
  352. /* reprocess header */
  353. if(ssz !== 512) { blob = file.slice(0,ssz); prep_blob(blob, 28 /* blob.l */); }
  354. /* Save header for final object */
  355. var header = file.slice(0,ssz);
  356. check_shifts(blob, mver);
  357. // Number of Directory Sectors
  358. var dir_cnt = blob.read_shift(4, 'i');
  359. if(mver === 3 && dir_cnt !== 0) throw new Error('# Directory Sectors: Expected 0 saw ' + dir_cnt);
  360. // Number of FAT Sectors
  361. blob.l += 4;
  362. // First Directory Sector Location
  363. dir_start = blob.read_shift(4, 'i');
  364. // Transaction Signature
  365. blob.l += 4;
  366. // Mini Stream Cutoff Size
  367. blob.chk('00100000', 'Mini Stream Cutoff Size: ');
  368. // First Mini FAT Sector Location
  369. minifat_start = blob.read_shift(4, 'i');
  370. // Number of Mini FAT Sectors
  371. nmfs = blob.read_shift(4, 'i');
  372. // First DIFAT sector location
  373. difat_start = blob.read_shift(4, 'i');
  374. // Number of DIFAT Sectors
  375. difat_sec_cnt = blob.read_shift(4, 'i');
  376. // Grab FAT Sector Locations
  377. for(var q = -1, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
  378. q = blob.read_shift(4, 'i');
  379. if(q<0) break;
  380. fat_addrs[j] = q;
  381. }
  382. /** Break the file up into sectors */
  383. var sectors = sectorify(file, ssz);
  384. sleuth_fat(difat_start, difat_sec_cnt, sectors, ssz, fat_addrs);
  385. /** Chains */
  386. var sector_list = make_sector_list(sectors, dir_start, fat_addrs, ssz);
  387. sector_list[dir_start].name = "!Directory";
  388. if(nmfs > 0 && minifat_start !== ENDOFCHAIN) sector_list[minifat_start].name = "!MiniFAT";
  389. sector_list[fat_addrs[0]].name = "!FAT";
  390. sector_list.fat_addrs = fat_addrs;
  391. sector_list.ssz = ssz;
  392. /* [MS-CFB] 2.6.1 Compound File Directory Entry */
  393. var files = {}, Paths = [], FileIndex = [], FullPaths = [];
  394. read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex, minifat_start);
  395. build_full_paths(FileIndex, FullPaths, Paths);
  396. Paths.shift();
  397. var o = {
  398. FileIndex: FileIndex,
  399. FullPaths: FullPaths
  400. };
  401. // $FlowIgnore
  402. if(options && options.raw) o.raw = {header: header, sectors: sectors};
  403. return o;
  404. } // parse
  405. /* [MS-CFB] 2.2 Compound File Header -- read up to major version */
  406. function check_get_mver(blob) {
  407. if(blob[blob.l] == 0x50 && blob[blob.l + 1] == 0x4b) return [0, 0];
  408. // header signature 8
  409. blob.chk(HEADER_SIGNATURE, 'Header Signature: ');
  410. // clsid 16
  411. //blob.chk(HEADER_CLSID, 'CLSID: ');
  412. blob.l += 16;
  413. // minor version 2
  414. var mver = blob.read_shift(2, 'u');
  415. return [blob.read_shift(2,'u'), mver];
  416. }
  417. function check_shifts(blob, mver) {
  418. var shift = 0x09;
  419. // Byte Order
  420. //blob.chk('feff', 'Byte Order: '); // note: some writers put 0xffff
  421. blob.l += 2;
  422. // Sector Shift
  423. switch((shift = blob.read_shift(2))) {
  424. case 0x09: if(mver != 3) throw new Error('Sector Shift: Expected 9 saw ' + shift); break;
  425. case 0x0c: if(mver != 4) throw new Error('Sector Shift: Expected 12 saw ' + shift); break;
  426. default: throw new Error('Sector Shift: Expected 9 or 12 saw ' + shift);
  427. }
  428. // Mini Sector Shift
  429. blob.chk('0600', 'Mini Sector Shift: ');
  430. // Reserved
  431. blob.chk('000000000000', 'Reserved: ');
  432. }
  433. /** Break the file up into sectors */
  434. function sectorify(file, ssz) {
  435. var nsectors = Math.ceil(file.length/ssz)-1;
  436. var sectors = [];
  437. for(var i=1; i < nsectors; ++i) sectors[i-1] = file.slice(i*ssz,(i+1)*ssz);
  438. sectors[nsectors-1] = file.slice(nsectors*ssz);
  439. return sectors;
  440. }
  441. /* [MS-CFB] 2.6.4 Red-Black Tree */
  442. function build_full_paths(FI, FP, Paths) {
  443. var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
  444. var dad = [], q = [];
  445. for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
  446. for(; j < q.length; ++j) {
  447. i = q[j];
  448. L = FI[i].L; R = FI[i].R; C = FI[i].C;
  449. if(dad[i] === i) {
  450. if(L !== -1 /*NOSTREAM*/ && dad[L] !== L) dad[i] = dad[L];
  451. if(R !== -1 && dad[R] !== R) dad[i] = dad[R];
  452. }
  453. if(C !== -1 /*NOSTREAM*/) dad[C] = i;
  454. if(L !== -1 && i != dad[i]) { dad[L] = dad[i]; if(q.lastIndexOf(L) < j) q.push(L); }
  455. if(R !== -1 && i != dad[i]) { dad[R] = dad[i]; if(q.lastIndexOf(R) < j) q.push(R); }
  456. }
  457. for(i=1; i < pl; ++i) if(dad[i] === i) {
  458. if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R];
  459. else if(L !== -1 && dad[L] !== L) dad[i] = dad[L];
  460. }
  461. for(i=1; i < pl; ++i) {
  462. if(FI[i].type === 0 /* unknown */) continue;
  463. j = i;
  464. if(j != dad[j]) do {
  465. j = dad[j];
  466. FP[i] = FP[j] + "/" + FP[i];
  467. } while (j !== 0 && -1 !== dad[j] && j != dad[j]);
  468. dad[i] = -1;
  469. }
  470. FP[0] += "/";
  471. for(i=1; i < pl; ++i) {
  472. if(FI[i].type !== 2 /* stream */) FP[i] += "/";
  473. }
  474. }
  475. function get_mfat_entry(entry, payload, mini) {
  476. var start = entry.start, size = entry.size;
  477. //return (payload.slice(start*MSSZ, start*MSSZ + size));
  478. var o = [];
  479. var idx = start;
  480. while(mini && size > 0 && idx >= 0) {
  481. o.push(payload.slice(idx * MSSZ, idx * MSSZ + MSSZ));
  482. size -= MSSZ;
  483. idx = __readInt32LE(mini, idx * 4);
  484. }
  485. if(o.length === 0) return (new_buf(0));
  486. return (bconcat(o).slice(0, entry.size));
  487. }
  488. /** Chase down the rest of the DIFAT chain to build a comprehensive list
  489. DIFAT chains by storing the next sector number as the last 32 bits */
  490. function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
  491. var q = ENDOFCHAIN;
  492. if(idx === ENDOFCHAIN) {
  493. if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
  494. } else if(idx !== -1 /*FREESECT*/) {
  495. var sector = sectors[idx], m = (ssz>>>2)-1;
  496. if(!sector) return;
  497. for(var i = 0; i < m; ++i) {
  498. if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break;
  499. fat_addrs.push(q);
  500. }
  501. if(cnt >= 1) sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, sectors, ssz, fat_addrs);
  502. }
  503. }
  504. /** Follow the linked list of sectors for a given starting point */
  505. function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
  506. var buf = [], buf_chain = [];
  507. if(!chkd) chkd = [];
  508. var modulus = ssz - 1, j = 0, jj = 0;
  509. for(j=start; j>=0;) {
  510. chkd[j] = true;
  511. buf[buf.length] = j;
  512. buf_chain.push(sectors[j]);
  513. var addr = fat_addrs[Math.floor(j*4/ssz)];
  514. jj = ((j*4) & modulus);
  515. if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
  516. if(!sectors[addr]) break;
  517. j = __readInt32LE(sectors[addr], jj);
  518. }
  519. return {nodes: buf, data:__toBuffer([buf_chain])};
  520. }
  521. /** Chase down the sector linked lists */
  522. function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
  523. var sl = sectors.length, sector_list = ([]);
  524. var chkd = [], buf = [], buf_chain = [];
  525. var modulus = ssz - 1, i=0, j=0, k=0, jj=0;
  526. for(i=0; i < sl; ++i) {
  527. buf = ([]);
  528. k = (i + dir_start); if(k >= sl) k-=sl;
  529. if(chkd[k]) continue;
  530. buf_chain = [];
  531. var seen = [];
  532. for(j=k; j>=0;) {
  533. seen[j] = true;
  534. chkd[j] = true;
  535. buf[buf.length] = j;
  536. buf_chain.push(sectors[j]);
  537. var addr = fat_addrs[Math.floor(j*4/ssz)];
  538. jj = ((j*4) & modulus);
  539. if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
  540. if(!sectors[addr]) break;
  541. j = __readInt32LE(sectors[addr], jj);
  542. if(seen[j]) break;
  543. }
  544. sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])});
  545. }
  546. return sector_list;
  547. }
  548. /* [MS-CFB] 2.6.1 Compound File Directory Entry */
  549. function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex, mini) {
  550. var minifat_store = 0, pl = (Paths.length?2:0);
  551. var sector = sector_list[dir_start].data;
  552. var i = 0, namelen = 0, name;
  553. for(; i < sector.length; i+= 128) {
  554. var blob = sector.slice(i, i+128);
  555. prep_blob(blob, 64);
  556. namelen = blob.read_shift(2);
  557. name = __utf16le(blob,0,namelen-pl);
  558. Paths.push(name);
  559. var o = ({
  560. name: name,
  561. type: blob.read_shift(1),
  562. color: blob.read_shift(1),
  563. L: blob.read_shift(4, 'i'),
  564. R: blob.read_shift(4, 'i'),
  565. C: blob.read_shift(4, 'i'),
  566. clsid: blob.read_shift(16),
  567. state: blob.read_shift(4, 'i'),
  568. start: 0,
  569. size: 0
  570. });
  571. var ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
  572. if(ctime !== 0) o.ct = read_date(blob, blob.l-8);
  573. var mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
  574. if(mtime !== 0) o.mt = read_date(blob, blob.l-8);
  575. o.start = blob.read_shift(4, 'i');
  576. o.size = blob.read_shift(4, 'i');
  577. if(o.size < 0 && o.start < 0) { o.size = o.type = 0; o.start = ENDOFCHAIN; o.name = ""; }
  578. if(o.type === 5) { /* root */
  579. minifat_store = o.start;
  580. if(nmfs > 0 && minifat_store !== ENDOFCHAIN) sector_list[minifat_store].name = "!StreamData";
  581. /*minifat_size = o.size;*/
  582. } else if(o.size >= 4096 /* MSCSZ */) {
  583. o.storage = 'fat';
  584. if(sector_list[o.start] === undefined) sector_list[o.start] = get_sector_list(sectors, o.start, sector_list.fat_addrs, sector_list.ssz);
  585. sector_list[o.start].name = o.name;
  586. o.content = (sector_list[o.start].data.slice(0,o.size));
  587. } else {
  588. o.storage = 'minifat';
  589. if(o.size < 0) o.size = 0;
  590. else if(minifat_store !== ENDOFCHAIN && o.start !== ENDOFCHAIN && sector_list[minifat_store]) {
  591. o.content = get_mfat_entry(o, sector_list[minifat_store].data, (sector_list[mini]||{}).data);
  592. }
  593. }
  594. if(o.content) prep_blob(o.content, 0);
  595. files[name] = o;
  596. FileIndex.push(o);
  597. }
  598. }
  599. function read_date(blob, offset) {
  600. return new Date(( ( (__readUInt32LE(blob,offset+4)/1e7)*Math.pow(2,32)+__readUInt32LE(blob,offset)/1e7 ) - 11644473600)*1000);
  601. }
  602. function read_file(filename, options) {
  603. get_fs();
  604. return parse(fs.readFileSync(filename), options);
  605. }
  606. function read(blob, options) {
  607. var type = options && options.type;
  608. if(!type) {
  609. if(has_buf && Buffer.isBuffer(blob)) type = "buffer";
  610. }
  611. switch(type || "base64") {
  612. case "file": return read_file(blob, options);
  613. case "base64": return parse(s2a(Base64_decode(blob)), options);
  614. case "binary": return parse(s2a(blob), options);
  615. }
  616. return parse(blob, options);
  617. }
  618. function init_cfb(cfb, opts) {
  619. var o = opts || {}, root = o.root || "Root Entry";
  620. if(!cfb.FullPaths) cfb.FullPaths = [];
  621. if(!cfb.FileIndex) cfb.FileIndex = [];
  622. if(cfb.FullPaths.length !== cfb.FileIndex.length) throw new Error("inconsistent CFB structure");
  623. if(cfb.FullPaths.length === 0) {
  624. cfb.FullPaths[0] = root + "/";
  625. cfb.FileIndex[0] = ({ name: root, type: 5 });
  626. }
  627. if(o.CLSID) cfb.FileIndex[0].clsid = o.CLSID;
  628. seed_cfb(cfb);
  629. }
  630. function seed_cfb(cfb) {
  631. var nm = "\u0001Sh33tJ5";
  632. if(CFB.find(cfb, "/" + nm)) return;
  633. var p = new_buf(4); p[0] = 55; p[1] = p[3] = 50; p[2] = 54;
  634. cfb.FileIndex.push(({ name: nm, type: 2, content:p, size:4, L:69, R:69, C:69 }));
  635. cfb.FullPaths.push(cfb.FullPaths[0] + nm);
  636. rebuild_cfb(cfb);
  637. }
  638. function rebuild_cfb(cfb, f) {
  639. init_cfb(cfb);
  640. var gc = false, s = false;
  641. for(var i = cfb.FullPaths.length - 1; i >= 0; --i) {
  642. var _file = cfb.FileIndex[i];
  643. switch(_file.type) {
  644. case 0:
  645. if(s) gc = true;
  646. else { cfb.FileIndex.pop(); cfb.FullPaths.pop(); }
  647. break;
  648. case 1: case 2: case 5:
  649. s = true;
  650. if(isNaN(_file.R * _file.L * _file.C)) gc = true;
  651. if(_file.R > -1 && _file.L > -1 && _file.R == _file.L) gc = true;
  652. break;
  653. default: gc = true; break;
  654. }
  655. }
  656. if(!gc && !f) return;
  657. var now = new Date(1987, 1, 19), j = 0;
  658. // Track which names exist
  659. var fullPaths = Object.create ? Object.create(null) : {};
  660. var data = [];
  661. for(i = 0; i < cfb.FullPaths.length; ++i) {
  662. fullPaths[cfb.FullPaths[i]] = true;
  663. if(cfb.FileIndex[i].type === 0) continue;
  664. data.push([cfb.FullPaths[i], cfb.FileIndex[i]]);
  665. }
  666. for(i = 0; i < data.length; ++i) {
  667. var dad = dirname(data[i][0]);
  668. s = fullPaths[dad];
  669. while(!s) {
  670. while(dirname(dad) && !fullPaths[dirname(dad)]) dad = dirname(dad);
  671. data.push([dad, ({
  672. name: filename(dad).replace("/",""),
  673. type: 1,
  674. clsid: HEADER_CLSID,
  675. ct: now, mt: now,
  676. content: null
  677. })]);
  678. // Add name to set
  679. fullPaths[dad] = true;
  680. dad = dirname(data[i][0]);
  681. s = fullPaths[dad];
  682. }
  683. }
  684. data.sort(function(x,y) { return namecmp(x[0], y[0]); });
  685. cfb.FullPaths = []; cfb.FileIndex = [];
  686. for(i = 0; i < data.length; ++i) { cfb.FullPaths[i] = data[i][0]; cfb.FileIndex[i] = data[i][1]; }
  687. for(i = 0; i < data.length; ++i) {
  688. var elt = cfb.FileIndex[i];
  689. var nm = cfb.FullPaths[i];
  690. elt.name = filename(nm).replace("/","");
  691. elt.L = elt.R = elt.C = -(elt.color = 1);
  692. elt.size = elt.content ? elt.content.length : 0;
  693. elt.start = 0;
  694. elt.clsid = (elt.clsid || HEADER_CLSID);
  695. if(i === 0) {
  696. elt.C = data.length > 1 ? 1 : -1;
  697. elt.size = 0;
  698. elt.type = 5;
  699. } else if(nm.slice(-1) == "/") {
  700. for(j=i+1;j < data.length; ++j) if(dirname(cfb.FullPaths[j])==nm) break;
  701. elt.C = j >= data.length ? -1 : j;
  702. for(j=i+1;j < data.length; ++j) if(dirname(cfb.FullPaths[j])==dirname(nm)) break;
  703. elt.R = j >= data.length ? -1 : j;
  704. elt.type = 1;
  705. } else {
  706. if(dirname(cfb.FullPaths[i+1]||"") == dirname(nm)) elt.R = i + 1;
  707. elt.type = 2;
  708. }
  709. }
  710. }
  711. function _write(cfb, options) {
  712. var _opts = options || {};
  713. /* MAD is order-sensitive, skip rebuild and sort */
  714. if(_opts.fileType == 'mad') return write_mad(cfb, _opts);
  715. rebuild_cfb(cfb);
  716. switch(_opts.fileType) {
  717. case 'zip': return write_zip(cfb, _opts);
  718. //case 'mad': return write_mad(cfb, _opts);
  719. }
  720. var L = (function(cfb){
  721. var mini_size = 0, fat_size = 0;
  722. for(var i = 0; i < cfb.FileIndex.length; ++i) {
  723. var file = cfb.FileIndex[i];
  724. if(!file.content) continue;
  725. var flen = file.content.length;
  726. if(flen > 0){
  727. if(flen < 0x1000) mini_size += (flen + 0x3F) >> 6;
  728. else fat_size += (flen + 0x01FF) >> 9;
  729. }
  730. }
  731. var dir_cnt = (cfb.FullPaths.length +3) >> 2;
  732. var mini_cnt = (mini_size + 7) >> 3;
  733. var mfat_cnt = (mini_size + 0x7F) >> 7;
  734. var fat_base = mini_cnt + fat_size + dir_cnt + mfat_cnt;
  735. var fat_cnt = (fat_base + 0x7F) >> 7;
  736. var difat_cnt = fat_cnt <= 109 ? 0 : Math.ceil((fat_cnt-109)/0x7F);
  737. while(((fat_base + fat_cnt + difat_cnt + 0x7F) >> 7) > fat_cnt) difat_cnt = ++fat_cnt <= 109 ? 0 : Math.ceil((fat_cnt-109)/0x7F);
  738. var L = [1, difat_cnt, fat_cnt, mfat_cnt, dir_cnt, fat_size, mini_size, 0];
  739. cfb.FileIndex[0].size = mini_size << 6;
  740. L[7] = (cfb.FileIndex[0].start=L[0]+L[1]+L[2]+L[3]+L[4]+L[5])+((L[6]+7) >> 3);
  741. return L;
  742. })(cfb);
  743. var o = new_buf(L[7] << 9);
  744. var i = 0, T = 0;
  745. {
  746. for(i = 0; i < 8; ++i) o.write_shift(1, HEADER_SIG[i]);
  747. for(i = 0; i < 8; ++i) o.write_shift(2, 0);
  748. o.write_shift(2, 0x003E);
  749. o.write_shift(2, 0x0003);
  750. o.write_shift(2, 0xFFFE);
  751. o.write_shift(2, 0x0009);
  752. o.write_shift(2, 0x0006);
  753. for(i = 0; i < 3; ++i) o.write_shift(2, 0);
  754. o.write_shift(4, 0);
  755. o.write_shift(4, L[2]);
  756. o.write_shift(4, L[0] + L[1] + L[2] + L[3] - 1);
  757. o.write_shift(4, 0);
  758. o.write_shift(4, 1<<12);
  759. o.write_shift(4, L[3] ? L[0] + L[1] + L[2] - 1: ENDOFCHAIN);
  760. o.write_shift(4, L[3]);
  761. o.write_shift(-4, L[1] ? L[0] - 1: ENDOFCHAIN);
  762. o.write_shift(4, L[1]);
  763. for(i = 0; i < 109; ++i) o.write_shift(-4, i < L[2] ? L[1] + i : -1);
  764. }
  765. if(L[1]) {
  766. for(T = 0; T < L[1]; ++T) {
  767. for(; i < 236 + T * 127; ++i) o.write_shift(-4, i < L[2] ? L[1] + i : -1);
  768. o.write_shift(-4, T === L[1] - 1 ? ENDOFCHAIN : T + 1);
  769. }
  770. }
  771. var chainit = function(w) {
  772. for(T += w; i<T-1; ++i) o.write_shift(-4, i+1);
  773. if(w) { ++i; o.write_shift(-4, ENDOFCHAIN); }
  774. };
  775. T = i = 0;
  776. for(T+=L[1]; i<T; ++i) o.write_shift(-4, consts.DIFSECT);
  777. for(T+=L[2]; i<T; ++i) o.write_shift(-4, consts.FATSECT);
  778. chainit(L[3]);
  779. chainit(L[4]);
  780. var j = 0, flen = 0;
  781. var file = cfb.FileIndex[0];
  782. for(; j < cfb.FileIndex.length; ++j) {
  783. file = cfb.FileIndex[j];
  784. if(!file.content) continue;
  785. flen = file.content.length;
  786. if(flen < 0x1000) continue;
  787. file.start = T;
  788. chainit((flen + 0x01FF) >> 9);
  789. }
  790. chainit((L[6] + 7) >> 3);
  791. while(o.l & 0x1FF) o.write_shift(-4, consts.ENDOFCHAIN);
  792. T = i = 0;
  793. for(j = 0; j < cfb.FileIndex.length; ++j) {
  794. file = cfb.FileIndex[j];
  795. if(!file.content) continue;
  796. flen = file.content.length;
  797. if(!flen || flen >= 0x1000) continue;
  798. file.start = T;
  799. chainit((flen + 0x3F) >> 6);
  800. }
  801. while(o.l & 0x1FF) o.write_shift(-4, consts.ENDOFCHAIN);
  802. for(i = 0; i < L[4]<<2; ++i) {
  803. var nm = cfb.FullPaths[i];
  804. if(!nm || nm.length === 0) {
  805. for(j = 0; j < 17; ++j) o.write_shift(4, 0);
  806. for(j = 0; j < 3; ++j) o.write_shift(4, -1);
  807. for(j = 0; j < 12; ++j) o.write_shift(4, 0);
  808. continue;
  809. }
  810. file = cfb.FileIndex[i];
  811. if(i === 0) file.start = file.size ? file.start - 1 : ENDOFCHAIN;
  812. var _nm = (i === 0 && _opts.root) || file.name;
  813. if(_nm.length > 32) {
  814. console.error("Name " + _nm + " will be truncated to " + _nm.slice(0,32));
  815. _nm = _nm.slice(0, 32);
  816. }
  817. flen = 2*(_nm.length+1);
  818. o.write_shift(64, _nm, "utf16le");
  819. o.write_shift(2, flen);
  820. o.write_shift(1, file.type);
  821. o.write_shift(1, file.color);
  822. o.write_shift(-4, file.L);
  823. o.write_shift(-4, file.R);
  824. o.write_shift(-4, file.C);
  825. if(!file.clsid) for(j = 0; j < 4; ++j) o.write_shift(4, 0);
  826. else o.write_shift(16, file.clsid, "hex");
  827. o.write_shift(4, file.state || 0);
  828. o.write_shift(4, 0); o.write_shift(4, 0);
  829. o.write_shift(4, 0); o.write_shift(4, 0);
  830. o.write_shift(4, file.start);
  831. o.write_shift(4, file.size); o.write_shift(4, 0);
  832. }
  833. for(i = 1; i < cfb.FileIndex.length; ++i) {
  834. file = cfb.FileIndex[i];
  835. if(file.size >= 0x1000) {
  836. o.l = (file.start+1) << 9;
  837. if (has_buf && Buffer.isBuffer(file.content)) {
  838. file.content.copy(o, o.l, 0, file.size);
  839. // o is a 0-filled Buffer so just set next offset
  840. o.l += (file.size + 511) & -512;
  841. } else {
  842. for(j = 0; j < file.size; ++j) o.write_shift(1, file.content[j]);
  843. for(; j & 0x1FF; ++j) o.write_shift(1, 0);
  844. }
  845. }
  846. }
  847. for(i = 1; i < cfb.FileIndex.length; ++i) {
  848. file = cfb.FileIndex[i];
  849. if(file.size > 0 && file.size < 0x1000) {
  850. if (has_buf && Buffer.isBuffer(file.content)) {
  851. file.content.copy(o, o.l, 0, file.size);
  852. // o is a 0-filled Buffer so just set next offset
  853. o.l += (file.size + 63) & -64;
  854. } else {
  855. for(j = 0; j < file.size; ++j) o.write_shift(1, file.content[j]);
  856. for(; j & 0x3F; ++j) o.write_shift(1, 0);
  857. }
  858. }
  859. }
  860. if (has_buf) {
  861. o.l = o.length;
  862. } else {
  863. // When using Buffer, already 0-filled
  864. while(o.l < o.length) o.write_shift(1, 0);
  865. }
  866. return o;
  867. }
  868. /* [MS-CFB] 2.6.4 (Unicode 3.0.1 case conversion) */
  869. function find(cfb, path) {
  870. var UCFullPaths = cfb.FullPaths.map(function(x) { return x.toUpperCase(); });
  871. var UCPaths = UCFullPaths.map(function(x) { var y = x.split("/"); return y[y.length - (x.slice(-1) == "/" ? 2 : 1)]; });
  872. var k = false;
  873. if(path.charCodeAt(0) === 47 /* "/" */) { k = true; path = UCFullPaths[0].slice(0, -1) + path; }
  874. else k = path.indexOf("/") !== -1;
  875. var UCPath = path.toUpperCase();
  876. var w = k === true ? UCFullPaths.indexOf(UCPath) : UCPaths.indexOf(UCPath);
  877. if(w !== -1) return cfb.FileIndex[w];
  878. var m = !UCPath.match(chr1);
  879. UCPath = UCPath.replace(chr0,'');
  880. if(m) UCPath = UCPath.replace(chr1,'!');
  881. for(w = 0; w < UCFullPaths.length; ++w) {
  882. if((m ? UCFullPaths[w].replace(chr1,'!') : UCFullPaths[w]).replace(chr0,'') == UCPath) return cfb.FileIndex[w];
  883. if((m ? UCPaths[w].replace(chr1,'!') : UCPaths[w]).replace(chr0,'') == UCPath) return cfb.FileIndex[w];
  884. }
  885. return null;
  886. }
  887. /** CFB Constants */
  888. var MSSZ = 64; /* Mini Sector Size = 1<<6 */
  889. //var MSCSZ = 4096; /* Mini Stream Cutoff Size */
  890. /* 2.1 Compound File Sector Numbers and Types */
  891. var ENDOFCHAIN = -2;
  892. /* 2.2 Compound File Header */
  893. var HEADER_SIGNATURE = 'd0cf11e0a1b11ae1';
  894. var HEADER_SIG = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
  895. var HEADER_CLSID = '00000000000000000000000000000000';
  896. var consts = {
  897. /* 2.1 Compund File Sector Numbers and Types */
  898. MAXREGSECT: -6,
  899. DIFSECT: -4,
  900. FATSECT: -3,
  901. ENDOFCHAIN: ENDOFCHAIN,
  902. FREESECT: -1,
  903. /* 2.2 Compound File Header */
  904. HEADER_SIGNATURE: HEADER_SIGNATURE,
  905. HEADER_MINOR_VERSION: '3e00',
  906. MAXREGSID: -6,
  907. NOSTREAM: -1,
  908. HEADER_CLSID: HEADER_CLSID,
  909. /* 2.6.1 Compound File Directory Entry */
  910. EntryTypes: ['unknown','storage','stream','lockbytes','property','root']
  911. };
  912. function write_file(cfb, filename, options) {
  913. get_fs();
  914. var o = _write(cfb, options);
  915. fs.writeFileSync(filename, o);
  916. }
  917. function a2s(o) {
  918. var out = new Array(o.length);
  919. for(var i = 0; i < o.length; ++i) out[i] = String.fromCharCode(o[i]);
  920. return out.join("");
  921. }
  922. function write(cfb, options) {
  923. var o = _write(cfb, options);
  924. switch(options && options.type || "buffer") {
  925. case "file": get_fs(); fs.writeFileSync(options.filename, (o)); return o;
  926. case "binary": return typeof o == "string" ? o : a2s(o);
  927. case "base64": return Base64_encode(typeof o == "string" ? o : a2s(o));
  928. case "buffer": if(has_buf) return Buffer.isBuffer(o) ? o : Buffer_from(o);
  929. /* falls through */
  930. case "array": return typeof o == "string" ? s2a(o) : o;
  931. }
  932. return o;
  933. }
  934. /* node < 8.1 zlib does not expose bytesRead, so default to pure JS */
  935. var _zlib;
  936. function use_zlib(zlib) { try {
  937. var InflateRaw = zlib.InflateRaw;
  938. var InflRaw = new InflateRaw();
  939. InflRaw._processChunk(new Uint8Array([3, 0]), InflRaw._finishFlushFlag);
  940. if(InflRaw.bytesRead) _zlib = zlib;
  941. else throw new Error("zlib does not expose bytesRead");
  942. } catch(e) {console.error("cannot use native zlib: " + (e.message || e)); } }
  943. function _inflateRawSync(payload, usz) {
  944. if(!_zlib) return _inflate(payload, usz);
  945. var InflateRaw = _zlib.InflateRaw;
  946. var InflRaw = new InflateRaw();
  947. var out = InflRaw._processChunk(payload.slice(payload.l), InflRaw._finishFlushFlag);
  948. payload.l += InflRaw.bytesRead;
  949. return out;
  950. }
  951. function _deflateRawSync(payload) {
  952. return _zlib ? _zlib.deflateRawSync(payload) : _deflate(payload);
  953. }
  954. var CLEN_ORDER = [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
  955. /* LEN_ID = [ 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285 ]; */
  956. var LEN_LN = [ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13 , 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 ];
  957. /* DST_ID = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 ]; */
  958. var DST_LN = [ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 ];
  959. function bit_swap_8(n) { var t = (((((n<<1)|(n<<11)) & 0x22110) | (((n<<5)|(n<<15)) & 0x88440))); return ((t>>16) | (t>>8) |t)&0xFF; }
  960. var use_typed_arrays = typeof Uint8Array !== 'undefined';
  961. var bitswap8 = use_typed_arrays ? new Uint8Array(1<<8) : [];
  962. for(var q = 0; q < (1<<8); ++q) bitswap8[q] = bit_swap_8(q);
  963. function bit_swap_n(n, b) {
  964. var rev = bitswap8[n & 0xFF];
  965. if(b <= 8) return rev >>> (8-b);
  966. rev = (rev << 8) | bitswap8[(n>>8)&0xFF];
  967. if(b <= 16) return rev >>> (16-b);
  968. rev = (rev << 8) | bitswap8[(n>>16)&0xFF];
  969. return rev >>> (24-b);
  970. }
  971. /* helpers for unaligned bit reads */
  972. function read_bits_2(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 6 ? 0 : buf[h+1]<<8))>>>w)& 0x03; }
  973. function read_bits_3(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 5 ? 0 : buf[h+1]<<8))>>>w)& 0x07; }
  974. function read_bits_4(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 4 ? 0 : buf[h+1]<<8))>>>w)& 0x0F; }
  975. function read_bits_5(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 3 ? 0 : buf[h+1]<<8))>>>w)& 0x1F; }
  976. function read_bits_7(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 1 ? 0 : buf[h+1]<<8))>>>w)& 0x7F; }
  977. /* works up to n = 3 * 8 + 1 = 25 */
  978. function read_bits_n(buf, bl, n) {
  979. var w = (bl&7), h = (bl>>>3), f = ((1<<n)-1);
  980. var v = buf[h] >>> w;
  981. if(n < 8 - w) return v & f;
  982. v |= buf[h+1]<<(8-w);
  983. if(n < 16 - w) return v & f;
  984. v |= buf[h+2]<<(16-w);
  985. if(n < 24 - w) return v & f;
  986. v |= buf[h+3]<<(24-w);
  987. return v & f;
  988. }
  989. /* helpers for unaligned bit writes */
  990. function write_bits_3(buf, bl, v) { var w = bl & 7, h = bl >>> 3;
  991. if(w <= 5) buf[h] |= (v & 7) << w;
  992. else {
  993. buf[h] |= (v << w) & 0xFF;
  994. buf[h+1] = (v&7) >> (8-w);
  995. }
  996. return bl + 3;
  997. }
  998. function write_bits_1(buf, bl, v) {
  999. var w = bl & 7, h = bl >>> 3;
  1000. v = (v&1) << w;
  1001. buf[h] |= v;
  1002. return bl + 1;
  1003. }
  1004. function write_bits_8(buf, bl, v) {
  1005. var w = bl & 7, h = bl >>> 3;
  1006. v <<= w;
  1007. buf[h] |= v & 0xFF; v >>>= 8;
  1008. buf[h+1] = v;
  1009. return bl + 8;
  1010. }
  1011. function write_bits_16(buf, bl, v) {
  1012. var w = bl & 7, h = bl >>> 3;
  1013. v <<= w;
  1014. buf[h] |= v & 0xFF; v >>>= 8;
  1015. buf[h+1] = v & 0xFF;
  1016. buf[h+2] = v >>> 8;
  1017. return bl + 16;
  1018. }
  1019. /* until ArrayBuffer#realloc is a thing, fake a realloc */
  1020. function realloc(b, sz) {
  1021. var L = b.length, M = 2*L > sz ? 2*L : sz + 5, i = 0;
  1022. if(L >= sz) return b;
  1023. if(has_buf) {
  1024. var o = new_unsafe_buf(M);
  1025. // $FlowIgnore
  1026. if(b.copy) b.copy(o);
  1027. else for(; i < b.length; ++i) o[i] = b[i];
  1028. return o;
  1029. } else if(use_typed_arrays) {
  1030. var a = new Uint8Array(M);
  1031. if(a.set) a.set(b);
  1032. else for(; i < L; ++i) a[i] = b[i];
  1033. return a;
  1034. }
  1035. b.length = M;
  1036. return b;
  1037. }
  1038. /* zero-filled arrays for older browsers */
  1039. function zero_fill_array(n) {
  1040. var o = new Array(n);
  1041. for(var i = 0; i < n; ++i) o[i] = 0;
  1042. return o;
  1043. }
  1044. /* build tree (used for literals and lengths) */
  1045. function build_tree(clens, cmap, MAX) {
  1046. var maxlen = 1, w = 0, i = 0, j = 0, ccode = 0, L = clens.length;
  1047. var bl_count = use_typed_arrays ? new Uint16Array(32) : zero_fill_array(32);
  1048. for(i = 0; i < 32; ++i) bl_count[i] = 0;
  1049. for(i = L; i < MAX; ++i) clens[i] = 0;
  1050. L = clens.length;
  1051. var ctree = use_typed_arrays ? new Uint16Array(L) : zero_fill_array(L); // []
  1052. /* build code tree */
  1053. for(i = 0; i < L; ++i) {
  1054. bl_count[(w = clens[i])]++;
  1055. if(maxlen < w) maxlen = w;
  1056. ctree[i] = 0;
  1057. }
  1058. bl_count[0] = 0;
  1059. for(i = 1; i <= maxlen; ++i) bl_count[i+16] = (ccode = (ccode + bl_count[i-1])<<1);
  1060. for(i = 0; i < L; ++i) {
  1061. ccode = clens[i];
  1062. if(ccode != 0) ctree[i] = bl_count[ccode+16]++;
  1063. }
  1064. /* cmap[maxlen + 4 bits] = (off&15) + (lit<<4) reverse mapping */
  1065. var cleni = 0;
  1066. for(i = 0; i < L; ++i) {
  1067. cleni = clens[i];
  1068. if(cleni != 0) {
  1069. ccode = bit_swap_n(ctree[i], maxlen)>>(maxlen-cleni);
  1070. for(j = (1<<(maxlen + 4 - cleni)) - 1; j>=0; --j)
  1071. cmap[ccode|(j<<cleni)] = (cleni&15) | (i<<4);
  1072. }
  1073. }
  1074. return maxlen;
  1075. }
  1076. /* Fixed Huffman */
  1077. var fix_lmap = use_typed_arrays ? new Uint16Array(512) : zero_fill_array(512);
  1078. var fix_dmap = use_typed_arrays ? new Uint16Array(32) : zero_fill_array(32);
  1079. if(!use_typed_arrays) {
  1080. for(var i = 0; i < 512; ++i) fix_lmap[i] = 0;
  1081. for(i = 0; i < 32; ++i) fix_dmap[i] = 0;
  1082. }
  1083. (function() {
  1084. var dlens = [];
  1085. var i = 0;
  1086. for(;i<32; i++) dlens.push(5);
  1087. build_tree(dlens, fix_dmap, 32);
  1088. var clens = [];
  1089. i = 0;
  1090. for(; i<=143; i++) clens.push(8);
  1091. for(; i<=255; i++) clens.push(9);
  1092. for(; i<=279; i++) clens.push(7);
  1093. for(; i<=287; i++) clens.push(8);
  1094. build_tree(clens, fix_lmap, 288);
  1095. })();var _deflateRaw = (function _deflateRawIIFE() {
  1096. var DST_LN_RE = use_typed_arrays ? new Uint8Array(0x8000) : [];
  1097. var j = 0, k = 0;
  1098. for(; j < DST_LN.length - 1; ++j) {
  1099. for(; k < DST_LN[j+1]; ++k) DST_LN_RE[k] = j;
  1100. }
  1101. for(;k < 32768; ++k) DST_LN_RE[k] = 29;
  1102. var LEN_LN_RE = use_typed_arrays ? new Uint8Array(0x103) : [];
  1103. for(j = 0, k = 0; j < LEN_LN.length - 1; ++j) {
  1104. for(; k < LEN_LN[j+1]; ++k) LEN_LN_RE[k] = j;
  1105. }
  1106. function write_stored(data, out) {
  1107. var boff = 0;
  1108. while(boff < data.length) {
  1109. var L = Math.min(0xFFFF, data.length - boff);
  1110. var h = boff + L == data.length;
  1111. out.write_shift(1, +h);
  1112. out.write_shift(2, L);
  1113. out.write_shift(2, (~L) & 0xFFFF);
  1114. while(L-- > 0) out[out.l++] = data[boff++];
  1115. }
  1116. return out.l;
  1117. }
  1118. /* Fixed Huffman */
  1119. function write_huff_fixed(data, out) {
  1120. var bl = 0;
  1121. var boff = 0;
  1122. var addrs = use_typed_arrays ? new Uint16Array(0x8000) : [];
  1123. while(boff < data.length) {
  1124. var L = /* data.length - boff; */ Math.min(0xFFFF, data.length - boff);
  1125. /* write a stored block for short data */
  1126. if(L < 10) {
  1127. bl = write_bits_3(out, bl, +!!(boff + L == data.length)); // jshint ignore:line
  1128. if(bl & 7) bl += 8 - (bl & 7);
  1129. out.l = (bl / 8) | 0;
  1130. out.write_shift(2, L);
  1131. out.write_shift(2, (~L) & 0xFFFF);
  1132. while(L-- > 0) out[out.l++] = data[boff++];
  1133. bl = out.l * 8;
  1134. continue;
  1135. }
  1136. bl = write_bits_3(out, bl, +!!(boff + L == data.length) + 2); // jshint ignore:line
  1137. var hash = 0;
  1138. while(L-- > 0) {
  1139. var d = data[boff];
  1140. hash = ((hash << 5) ^ d) & 0x7FFF;
  1141. var match = -1, mlen = 0;
  1142. if((match = addrs[hash])) {
  1143. match |= boff & ~0x7FFF;
  1144. if(match > boff) match -= 0x8000;
  1145. if(match < boff) while(data[match + mlen] == data[boff + mlen] && mlen < 250) ++mlen;
  1146. }
  1147. if(mlen > 2) {
  1148. /* Copy Token */
  1149. d = LEN_LN_RE[mlen];
  1150. if(d <= 22) bl = write_bits_8(out, bl, bitswap8[d+1]>>1) - 1;
  1151. else {
  1152. write_bits_8(out, bl, 3);
  1153. bl += 5;
  1154. write_bits_8(out, bl, bitswap8[d-23]>>5);
  1155. bl += 3;
  1156. }
  1157. var len_eb = (d < 8) ? 0 : ((d - 4)>>2);
  1158. if(len_eb > 0) {
  1159. write_bits_16(out, bl, mlen - LEN_LN[d]);
  1160. bl += len_eb;
  1161. }
  1162. d = DST_LN_RE[boff - match];
  1163. bl = write_bits_8(out, bl, bitswap8[d]>>3);
  1164. bl -= 3;
  1165. var dst_eb = d < 4 ? 0 : (d-2)>>1;
  1166. if(dst_eb > 0) {
  1167. write_bits_16(out, bl, boff - match - DST_LN[d]);
  1168. bl += dst_eb;
  1169. }
  1170. for(var q = 0; q < mlen; ++q) {
  1171. addrs[hash] = boff & 0x7FFF;
  1172. hash = ((hash << 5) ^ data[boff]) & 0x7FFF;
  1173. ++boff;
  1174. }
  1175. L-= mlen - 1;
  1176. } else {
  1177. /* Literal Token */
  1178. if(d <= 143) d = d + 48;
  1179. else bl = write_bits_1(out, bl, 1);
  1180. bl = write_bits_8(out, bl, bitswap8[d]);
  1181. addrs[hash] = boff & 0x7FFF;
  1182. ++boff;
  1183. }
  1184. }
  1185. bl = write_bits_8(out, bl, 0) - 1;
  1186. }
  1187. out.l = ((bl + 7)/8)|0;
  1188. return out.l;
  1189. }
  1190. return function _deflateRaw(data, out) {
  1191. if(data.length < 8) return write_stored(data, out);
  1192. return write_huff_fixed(data, out);
  1193. };
  1194. })();
  1195. function _deflate(data) {
  1196. var buf = new_buf(50+Math.floor(data.length*1.1));
  1197. var off = _deflateRaw(data, buf);
  1198. return buf.slice(0, off);
  1199. }
  1200. /* modified inflate function also moves original read head */
  1201. var dyn_lmap = use_typed_arrays ? new Uint16Array(32768) : zero_fill_array(32768);
  1202. var dyn_dmap = use_typed_arrays ? new Uint16Array(32768) : zero_fill_array(32768);
  1203. var dyn_cmap = use_typed_arrays ? new Uint16Array(128) : zero_fill_array(128);
  1204. var dyn_len_1 = 1, dyn_len_2 = 1;
  1205. /* 5.5.3 Expanding Huffman Codes */
  1206. function dyn(data, boff) {
  1207. /* nomenclature from RFC1951 refers to bit values; these are offset by the implicit constant */
  1208. var _HLIT = read_bits_5(data, boff) + 257; boff += 5;
  1209. var _HDIST = read_bits_5(data, boff) + 1; boff += 5;
  1210. var _HCLEN = read_bits_4(data, boff) + 4; boff += 4;
  1211. var w = 0;
  1212. /* grab and store code lengths */
  1213. var clens = use_typed_arrays ? new Uint8Array(19) : zero_fill_array(19);
  1214. var ctree = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  1215. var maxlen = 1;
  1216. var bl_count = use_typed_arrays ? new Uint8Array(8) : zero_fill_array(8);
  1217. var next_code = use_typed_arrays ? new Uint8Array(8) : zero_fill_array(8);
  1218. var L = clens.length; /* 19 */
  1219. for(var i = 0; i < _HCLEN; ++i) {
  1220. clens[CLEN_ORDER[i]] = w = read_bits_3(data, boff);
  1221. if(maxlen < w) maxlen = w;
  1222. bl_count[w]++;
  1223. boff += 3;
  1224. }
  1225. /* build code tree */
  1226. var ccode = 0;
  1227. bl_count[0] = 0;
  1228. for(i = 1; i <= maxlen; ++i) next_code[i] = ccode = (ccode + bl_count[i-1])<<1;
  1229. for(i = 0; i < L; ++i) if((ccode = clens[i]) != 0) ctree[i] = next_code[ccode]++;
  1230. /* cmap[7 bits from stream] = (off&7) + (lit<<3) */
  1231. var cleni = 0;
  1232. for(i = 0; i < L; ++i) {
  1233. cleni = clens[i];
  1234. if(cleni != 0) {
  1235. ccode = bitswap8[ctree[i]]>>(8-cleni);
  1236. for(var j = (1<<(7-cleni))-1; j>=0; --j) dyn_cmap[ccode|(j<<cleni)] = (cleni&7) | (i<<3);
  1237. }
  1238. }
  1239. /* read literal and dist codes at once */
  1240. var hcodes = [];
  1241. maxlen = 1;
  1242. for(; hcodes.length < _HLIT + _HDIST;) {
  1243. ccode = dyn_cmap[read_bits_7(data, boff)];
  1244. boff += ccode & 7;
  1245. switch((ccode >>>= 3)) {
  1246. case 16:
  1247. w = 3 + read_bits_2(data, boff); boff += 2;
  1248. ccode = hcodes[hcodes.length - 1];
  1249. while(w-- > 0) hcodes.push(ccode);
  1250. break;
  1251. case 17:
  1252. w = 3 + read_bits_3(data, boff); boff += 3;
  1253. while(w-- > 0) hcodes.push(0);
  1254. break;
  1255. case 18:
  1256. w = 11 + read_bits_7(data, boff); boff += 7;
  1257. while(w -- > 0) hcodes.push(0);
  1258. break;
  1259. default:
  1260. hcodes.push(ccode);
  1261. if(maxlen < ccode) maxlen = ccode;
  1262. break;
  1263. }
  1264. }
  1265. /* build literal / length trees */
  1266. var h1 = hcodes.slice(0, _HLIT), h2 = hcodes.slice(_HLIT);
  1267. for(i = _HLIT; i < 286; ++i) h1[i] = 0;
  1268. for(i = _HDIST; i < 30; ++i) h2[i] = 0;
  1269. dyn_len_1 = build_tree(h1, dyn_lmap, 286);
  1270. dyn_len_2 = build_tree(h2, dyn_dmap, 30);
  1271. return boff;
  1272. }
  1273. /* return [ data, bytesRead ] */
  1274. function inflate(data, usz) {
  1275. /* shortcircuit for empty buffer [0x03, 0x00] */
  1276. if(data[0] == 3 && !(data[1] & 0x3)) { return [new_raw_buf(usz), 2]; }
  1277. /* bit offset */
  1278. var boff = 0;
  1279. /* header includes final bit and type bits */
  1280. var header = 0;
  1281. var outbuf = new_unsafe_buf(usz ? usz : (1<<18));
  1282. var woff = 0;
  1283. var OL = outbuf.length>>>0;
  1284. var max_len_1 = 0, max_len_2 = 0;
  1285. while((header&1) == 0) {
  1286. header = read_bits_3(data, boff); boff += 3;
  1287. if((header >>> 1) == 0) {
  1288. /* Stored block */
  1289. if(boff & 7) boff += 8 - (boff&7);
  1290. /* 2 bytes sz, 2 bytes bit inverse */
  1291. var sz = data[boff>>>3] | data[(boff>>>3)+1]<<8;
  1292. boff += 32;
  1293. /* push sz bytes */
  1294. if(sz > 0) {
  1295. if(!usz && OL < woff + sz) { outbuf = realloc(outbuf, woff + sz); OL = outbuf.length; }
  1296. while(sz-- > 0) { outbuf[woff++] = data[boff>>>3]; boff += 8; }
  1297. }
  1298. continue;
  1299. } else if((header >> 1) == 1) {
  1300. /* Fixed Huffman */
  1301. max_len_1 = 9; max_len_2 = 5;
  1302. } else {
  1303. /* Dynamic Huffman */
  1304. boff = dyn(data, boff);
  1305. max_len_1 = dyn_len_1; max_len_2 = dyn_len_2;
  1306. }
  1307. for(;;) { // while(true) is apparently out of vogue in modern JS circles
  1308. if(!usz && (OL < woff + 32767)) { outbuf = realloc(outbuf, woff + 32767); OL = outbuf.length; }
  1309. /* ingest code and move read head */
  1310. var bits = read_bits_n(data, boff, max_len_1);
  1311. var code = (header>>>1) == 1 ? fix_lmap[bits] : dyn_lmap[bits];
  1312. boff += code & 15; code >>>= 4;
  1313. /* 0-255 are literals, 256 is end of block token, 257+ are copy tokens */
  1314. if(((code>>>8)&0xFF) === 0) outbuf[woff++] = code;
  1315. else if(code == 256) break;
  1316. else {
  1317. code -= 257;
  1318. var len_eb = (code < 8) ? 0 : ((code-4)>>2); if(len_eb > 5) len_eb = 0;
  1319. var tgt = woff + LEN_LN[code];
  1320. /* length extra bits */
  1321. if(len_eb > 0) {
  1322. tgt += read_bits_n(data, boff, len_eb);
  1323. boff += len_eb;
  1324. }
  1325. /* dist code */
  1326. bits = read_bits_n(data, boff, max_len_2);
  1327. code = (header>>>1) == 1 ? fix_dmap[bits] : dyn_dmap[bits];
  1328. boff += code & 15; code >>>= 4;
  1329. var dst_eb = (code < 4 ? 0 : (code-2)>>1);
  1330. var dst = DST_LN[code];
  1331. /* dist extra bits */
  1332. if(dst_eb > 0) {
  1333. dst += read_bits_n(data, boff, dst_eb);
  1334. boff += dst_eb;
  1335. }
  1336. /* in the common case, manual byte copy is faster than TA set / Buffer copy */
  1337. if(!usz && OL < tgt) { outbuf = realloc(outbuf, tgt + 100); OL = outbuf.length; }
  1338. while(woff < tgt) { outbuf[woff] = outbuf[woff - dst]; ++woff; }
  1339. }
  1340. }
  1341. }
  1342. if(usz) return [outbuf, (boff+7)>>>3];
  1343. return [outbuf.slice(0, woff), (boff+7)>>>3];
  1344. }
  1345. function _inflate(payload, usz) {
  1346. var data = payload.slice(payload.l||0);
  1347. var out = inflate(data, usz);
  1348. payload.l += out[1];
  1349. return out[0];
  1350. }
  1351. function warn_or_throw(wrn, msg) {
  1352. if(wrn) { if(typeof console !== 'undefined') console.error(msg); }
  1353. else throw new Error(msg);
  1354. }
  1355. function parse_zip(file, options) {
  1356. var blob = file;
  1357. prep_blob(blob, 0);
  1358. var FileIndex = [], FullPaths = [];
  1359. var o = {
  1360. FileIndex: FileIndex,
  1361. FullPaths: FullPaths
  1362. };
  1363. init_cfb(o, { root: options.root });
  1364. /* find end of central directory, start just after signature */
  1365. var i = blob.length - 4;
  1366. while((blob[i] != 0x50 || blob[i+1] != 0x4b || blob[i+2] != 0x05 || blob[i+3] != 0x06) && i >= 0) --i;
  1367. blob.l = i + 4;
  1368. /* parse end of central directory */
  1369. blob.l += 4;
  1370. var fcnt = blob.read_shift(2);
  1371. blob.l += 6;
  1372. var start_cd = blob.read_shift(4);
  1373. /* parse central directory */
  1374. blob.l = start_cd;
  1375. for(i = 0; i < fcnt; ++i) {
  1376. /* trust local file header instead of CD entry */
  1377. blob.l += 20;
  1378. var csz = blob.read_shift(4);
  1379. var usz = blob.read_shift(4);
  1380. var namelen = blob.read_shift(2);
  1381. var efsz = blob.read_shift(2);
  1382. var fcsz = blob.read_shift(2);
  1383. blob.l += 8;
  1384. var offset = blob.read_shift(4);
  1385. var EF = parse_extra_field(blob.slice(blob.l+namelen, blob.l+namelen+efsz));
  1386. blob.l += namelen + efsz + fcsz;
  1387. var L = blob.l;
  1388. blob.l = offset + 4;
  1389. parse_local_file(blob, csz, usz, o, EF);
  1390. blob.l = L;
  1391. }
  1392. return o;
  1393. }
  1394. /* head starts just after local file header signature */
  1395. function parse_local_file(blob, csz, usz, o, EF) {
  1396. /* [local file header] */
  1397. blob.l += 2;
  1398. var flags = blob.read_shift(2);
  1399. var meth = blob.read_shift(2);
  1400. var date = parse_dos_date(blob);
  1401. if(flags & 0x2041) throw new Error("Unsupported ZIP encryption");
  1402. var crc32 = blob.read_shift(4);
  1403. var _csz = blob.read_shift(4);
  1404. var _usz = blob.read_shift(4);
  1405. var namelen = blob.read_shift(2);
  1406. var efsz = blob.read_shift(2);
  1407. // TODO: flags & (1<<11) // UTF8
  1408. var name = ""; for(var i = 0; i < namelen; ++i) name += String.fromCharCode(blob[blob.l++]);
  1409. if(efsz) {
  1410. var ef = parse_extra_field(blob.slice(blob.l, blob.l + efsz));
  1411. if((ef[0x5455]||{}).mt) date = ef[0x5455].mt;
  1412. if(((EF||{})[0x5455]||{}).mt) date = EF[0x5455].mt;
  1413. }
  1414. blob.l += efsz;
  1415. /* [encryption header] */
  1416. /* [file data] */
  1417. var data = blob.slice(blob.l, blob.l + _csz);
  1418. switch(meth) {
  1419. case 8: data = _inflateRawSync(blob, _usz); break;
  1420. case 0: break; // TODO: scan for magic number
  1421. default: throw new Error("Unsupported ZIP Compression method " + meth);
  1422. }
  1423. /* [data descriptor] */
  1424. var wrn = false;
  1425. if(flags & 8) {
  1426. crc32 = blob.read_shift(4);
  1427. if(crc32 == 0x08074b50) { crc32 = blob.read_shift(4); wrn = true; }
  1428. _csz = blob.read_shift(4);
  1429. _usz = blob.read_shift(4);
  1430. }
  1431. if(_csz != csz) warn_or_throw(wrn, "Bad compressed size: " + csz + " != " + _csz);
  1432. if(_usz != usz) warn_or_throw(wrn, "Bad uncompressed size: " + usz + " != " + _usz);
  1433. var _crc32 = CRC32.buf(data, 0);
  1434. if((crc32>>0) != (_crc32>>0)) warn_or_throw(wrn, "Bad CRC32 checksum: " + crc32 + " != " + _crc32);
  1435. cfb_add(o, name, data, {unsafe: true, mt: date});
  1436. }
  1437. function write_zip(cfb, options) {
  1438. var _opts = options || {};
  1439. var out = [], cdirs = [];
  1440. var o = new_buf(1);
  1441. var method = (_opts.compression ? 8 : 0), flags = 0;
  1442. var desc = false;
  1443. if(desc) flags |= 8;
  1444. var i = 0, j = 0;
  1445. var start_cd = 0, fcnt = 0;
  1446. var root = cfb.FullPaths[0], fp = root, fi = cfb.FileIndex[0];
  1447. var crcs = [];
  1448. var sz_cd = 0;
  1449. for(i = 1; i < cfb.FullPaths.length; ++i) {
  1450. fp = cfb.FullPaths[i].slice(root.length); fi = cfb.FileIndex[i];
  1451. if(!fi.size || !fi.content || fp == "\u0001Sh33tJ5") continue;
  1452. var start = start_cd;
  1453. /* TODO: CP437 filename */
  1454. var namebuf = new_buf(fp.length);
  1455. for(j = 0; j < fp.length; ++j) namebuf.write_shift(1, fp.charCodeAt(j) & 0x7F);
  1456. namebuf = namebuf.slice(0, namebuf.l);
  1457. crcs[fcnt] = CRC32.buf(fi.content, 0);
  1458. var outbuf = fi.content;
  1459. if(method == 8) outbuf = _deflateRawSync(outbuf);
  1460. /* local file header */
  1461. o = new_buf(30);
  1462. o.write_shift(4, 0x04034b50);
  1463. o.write_shift(2, 20);
  1464. o.write_shift(2, flags);
  1465. o.write_shift(2, method);
  1466. /* TODO: last mod file time/date */
  1467. if(fi.mt) write_dos_date(o, fi.mt);
  1468. else o.write_shift(4, 0);
  1469. o.write_shift(-4, (flags & 8) ? 0 : crcs[fcnt]);
  1470. o.write_shift(4, (flags & 8) ? 0 : outbuf.length);
  1471. o.write_shift(4, (flags & 8) ? 0 : fi.content.length);
  1472. o.write_shift(2, namebuf.length);
  1473. o.write_shift(2, 0);
  1474. start_cd += o.length;
  1475. out.push(o);
  1476. start_cd += namebuf.length;
  1477. out.push(namebuf);
  1478. /* TODO: extra fields? */
  1479. /* TODO: encryption header ? */
  1480. start_cd += outbuf.length;
  1481. out.push(outbuf);
  1482. /* data descriptor */
  1483. if(flags & 8) {
  1484. o = new_buf(12);
  1485. o.write_shift(-4, crcs[fcnt]);
  1486. o.write_shift(4, outbuf.length);
  1487. o.write_shift(4, fi.content.length);
  1488. start_cd += o.l;
  1489. out.push(o);
  1490. }
  1491. /* central directory */
  1492. o = new_buf(46);
  1493. o.write_shift(4, 0x02014b50);
  1494. o.write_shift(2, 0);
  1495. o.write_shift(2, 20);
  1496. o.write_shift(2, flags);
  1497. o.write_shift(2, method);
  1498. o.write_shift(4, 0); /* TODO: last mod file time/date */
  1499. o.write_shift(-4, crcs[fcnt]);
  1500. o.write_shift(4, outbuf.length);
  1501. o.write_shift(4, fi.content.length);
  1502. o.write_shift(2, namebuf.length);
  1503. o.write_shift(2, 0);
  1504. o.write_shift(2, 0);
  1505. o.write_shift(2, 0);
  1506. o.write_shift(2, 0);
  1507. o.write_shift(4, 0);
  1508. o.write_shift(4, start);
  1509. sz_cd += o.l;
  1510. cdirs.push(o);
  1511. sz_cd += namebuf.length;
  1512. cdirs.push(namebuf);
  1513. ++fcnt;
  1514. }
  1515. /* end of central directory */
  1516. o = new_buf(22);
  1517. o.write_shift(4, 0x06054b50);
  1518. o.write_shift(2, 0);
  1519. o.write_shift(2, 0);
  1520. o.write_shift(2, fcnt);
  1521. o.write_shift(2, fcnt);
  1522. o.write_shift(4, sz_cd);
  1523. o.write_shift(4, start_cd);
  1524. o.write_shift(2, 0);
  1525. return bconcat(([bconcat((out)), bconcat(cdirs), o]));
  1526. }
  1527. var ContentTypeMap = ({
  1528. "htm": "text/html",
  1529. "xml": "text/xml",
  1530. "gif": "image/gif",
  1531. "jpg": "image/jpeg",
  1532. "png": "image/png",
  1533. "mso": "application/x-mso",
  1534. "thmx": "application/vnd.ms-officetheme",
  1535. "sh33tj5": "application/octet-stream"
  1536. });
  1537. function get_content_type(fi, fp) {
  1538. if(fi.ctype) return fi.ctype;
  1539. var ext = fi.name || "", m = ext.match(/\.([^\.]+)$/);
  1540. if(m && ContentTypeMap[m[1]]) return ContentTypeMap[m[1]];
  1541. if(fp) {
  1542. m = (ext = fp).match(/[\.\\]([^\.\\])+$/);
  1543. if(m && ContentTypeMap[m[1]]) return ContentTypeMap[m[1]];
  1544. }
  1545. return "application/octet-stream";
  1546. }
  1547. /* 76 character chunks TODO: intertwine encoding */
  1548. function write_base64_76(bstr) {
  1549. var data = Base64_encode(bstr);
  1550. var o = [];
  1551. for(var i = 0; i < data.length; i+= 76) o.push(data.slice(i, i+76));
  1552. return o.join("\r\n") + "\r\n";
  1553. }
  1554. /*
  1555. Rules for QP:
  1556. - escape =## applies for all non-display characters and literal "="
  1557. - space or tab at end of line must be encoded
  1558. - \r\n newlines can be preserved, but bare \r and \n must be escaped
  1559. - lines must not exceed 76 characters, use soft breaks =\r\n
  1560. TODO: Some files from word appear to write line extensions with bare equals:
  1561. ```
  1562. <table class=3DMsoTableGrid border=3D1 cellspacing=3D0 cellpadding=3D0 width=
  1563. ="70%"
  1564. ```
  1565. */
  1566. function write_quoted_printable(text) {
  1567. var encoded = text.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7E-\xFF=]/g, function(c) {
  1568. var w = c.charCodeAt(0).toString(16).toUpperCase();
  1569. return "=" + (w.length == 1 ? "0" + w : w);
  1570. });
  1571. encoded = encoded.replace(/ $/mg, "=20").replace(/\t$/mg, "=09");
  1572. if(encoded.charAt(0) == "\n") encoded = "=0D" + encoded.slice(1);
  1573. encoded = encoded.replace(/\r(?!\n)/mg, "=0D").replace(/\n\n/mg, "\n=0A").replace(/([^\r\n])\n/mg, "$1=0A");
  1574. var o = [], split = encoded.split("\r\n");
  1575. for(var si = 0; si < split.length; ++si) {
  1576. var str = split[si];
  1577. if(str.length == 0) { o.push(""); continue; }
  1578. for(var i = 0; i < str.length;) {
  1579. var end = 76;
  1580. var tmp = str.slice(i, i + end);
  1581. if(tmp.charAt(end - 1) == "=") end --;
  1582. else if(tmp.charAt(end - 2) == "=") end -= 2;
  1583. else if(tmp.charAt(end - 3) == "=") end -= 3;
  1584. tmp = str.slice(i, i + end);
  1585. i += end;
  1586. if(i < str.length) tmp += "=";
  1587. o.push(tmp);
  1588. }
  1589. }
  1590. return o.join("\r\n");
  1591. }
  1592. function parse_quoted_printable(data) {
  1593. var o = [];
  1594. /* unify long lines */
  1595. for(var di = 0; di < data.length; ++di) {
  1596. var line = data[di];
  1597. while(di <= data.length && line.charAt(line.length - 1) == "=") line = line.slice(0, line.length - 1) + data[++di];
  1598. o.push(line);
  1599. }
  1600. /* decode */
  1601. for(var oi = 0; oi < o.length; ++oi) o[oi] = o[oi].replace(/[=][0-9A-Fa-f]{2}/g, function($$) { return String.fromCharCode(parseInt($$.slice(1), 16)); });
  1602. return s2a(o.join("\r\n"));
  1603. }
  1604. function parse_mime(cfb, data, root) {
  1605. var fname = "", cte = "", ctype = "", fdata;
  1606. var di = 0;
  1607. for(;di < 10; ++di) {
  1608. var line = data[di];
  1609. if(!line || line.match(/^\s*$/)) break;
  1610. var m = line.match(/^(.*?):\s*([^\s].*)$/);
  1611. if(m) switch(m[1].toLowerCase()) {
  1612. case "content-location": fname = m[2].trim(); break;
  1613. case "content-type": ctype = m[2].trim(); break;
  1614. case "content-transfer-encoding": cte = m[2].trim(); break;
  1615. }
  1616. }
  1617. ++di;
  1618. switch(cte.toLowerCase()) {
  1619. case 'base64': fdata = s2a(Base64_decode(data.slice(di).join(""))); break;
  1620. case 'quoted-printable': fdata = parse_quoted_printable(data.slice(di)); break;
  1621. default: throw new Error("Unsupported Content-Transfer-Encoding " + cte);
  1622. }
  1623. var file = cfb_add(cfb, fname.slice(root.length), fdata, {unsafe: true});
  1624. if(ctype) file.ctype = ctype;
  1625. }
  1626. function parse_mad(file, options) {
  1627. if(a2s(file.slice(0,13)).toLowerCase() != "mime-version:") throw new Error("Unsupported MAD header");
  1628. var root = (options && options.root || "");
  1629. // $FlowIgnore
  1630. var data = (has_buf && Buffer.isBuffer(file) ? file.toString("binary") : a2s(file)).split("\r\n");
  1631. var di = 0, row = "";
  1632. /* if root is not specified, scan for the common prefix */
  1633. for(di = 0; di < data.length; ++di) {
  1634. row = data[di];
  1635. if(!/^Content-Location:/i.test(row)) continue;
  1636. row = row.slice(row.indexOf("file"));
  1637. if(!root) root = row.slice(0, row.lastIndexOf("/") + 1);
  1638. if(row.slice(0, root.length) == root) continue;
  1639. while(root.length > 0) {
  1640. root = root.slice(0, root.length - 1);
  1641. root = root.slice(0, root.lastIndexOf("/") + 1);
  1642. if(row.slice(0,root.length) == root) break;
  1643. }
  1644. }
  1645. var mboundary = (data[1] || "").match(/boundary="(.*?)"/);
  1646. if(!mboundary) throw new Error("MAD cannot find boundary");
  1647. var boundary = "--" + (mboundary[1] || "");
  1648. var FileIndex = [], FullPaths = [];
  1649. var o = {
  1650. FileIndex: FileIndex,
  1651. FullPaths: FullPaths
  1652. };
  1653. init_cfb(o);
  1654. var start_di, fcnt = 0;
  1655. for(di = 0; di < data.length; ++di) {
  1656. var line = data[di];
  1657. if(line !== boundary && line !== boundary + "--") continue;
  1658. if(fcnt++) parse_mime(o, data.slice(start_di, di), root);
  1659. start_di = di;
  1660. }
  1661. return o;
  1662. }
  1663. function write_mad(cfb, options) {
  1664. var opts = options || {};
  1665. var boundary = opts.boundary || "SheetJS";
  1666. boundary = '------=' + boundary;
  1667. var out = [
  1668. 'MIME-Version: 1.0',
  1669. 'Content-Type: multipart/related; boundary="' + boundary.slice(2) + '"',
  1670. '',
  1671. '',
  1672. ''
  1673. ];
  1674. var root = cfb.FullPaths[0], fp = root, fi = cfb.FileIndex[0];
  1675. for(var i = 1; i < cfb.FullPaths.length; ++i) {
  1676. fp = cfb.FullPaths[i].slice(root.length);
  1677. fi = cfb.FileIndex[i];
  1678. if(!fi.size || !fi.content || fp == "\u0001Sh33tJ5") continue;
  1679. /* Normalize filename */
  1680. fp = fp.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7E-\xFF]/g, function(c) {
  1681. return "_x" + c.charCodeAt(0).toString(16) + "_";
  1682. }).replace(/[\u0080-\uFFFF]/g, function(u) {
  1683. return "_u" + u.charCodeAt(0).toString(16) + "_";
  1684. });
  1685. /* Extract content as binary string */
  1686. var ca = fi.content;
  1687. // $FlowIgnore
  1688. var cstr = has_buf && Buffer.isBuffer(ca) ? ca.toString("binary") : a2s(ca);
  1689. /* 4/5 of first 1024 chars ascii -> quoted printable, else base64 */
  1690. var dispcnt = 0, L = Math.min(1024, cstr.length), cc = 0;
  1691. for(var csl = 0; csl <= L; ++csl) if((cc=cstr.charCodeAt(csl)) >= 0x20 && cc < 0x80) ++dispcnt;
  1692. var qp = dispcnt >= L * 4 / 5;
  1693. out.push(boundary);
  1694. out.push('Content-Location: ' + (opts.root || 'file:///C:/SheetJS/') + fp);
  1695. out.push('Content-Transfer-Encoding: ' + (qp ? 'quoted-printable' : 'base64'));
  1696. out.push('Content-Type: ' + get_content_type(fi, fp));
  1697. out.push('');
  1698. out.push(qp ? write_quoted_printable(cstr) : write_base64_76(cstr));
  1699. }
  1700. out.push(boundary + '--\r\n');
  1701. return out.join("\r\n");
  1702. }
  1703. function cfb_new(opts) {
  1704. var o = ({});
  1705. init_cfb(o, opts);
  1706. return o;
  1707. }
  1708. function cfb_add(cfb, name, content, opts) {
  1709. var unsafe = opts && opts.unsafe;
  1710. if(!unsafe) init_cfb(cfb);
  1711. var file = !unsafe && CFB.find(cfb, name);
  1712. if(!file) {
  1713. var fpath = cfb.FullPaths[0];
  1714. if(name.slice(0, fpath.length) == fpath) fpath = name;
  1715. else {
  1716. if(fpath.slice(-1) != "/") fpath += "/";
  1717. fpath = (fpath + name).replace("//","/");
  1718. }
  1719. file = ({name: filename(name), type: 2});
  1720. cfb.FileIndex.push(file);
  1721. cfb.FullPaths.push(fpath);
  1722. if(!unsafe) CFB.utils.cfb_gc(cfb);
  1723. }
  1724. file.content = (content);
  1725. file.size = content ? content.length : 0;
  1726. if(opts) {
  1727. if(opts.CLSID) file.clsid = opts.CLSID;
  1728. if(opts.mt) file.mt = opts.mt;
  1729. if(opts.ct) file.ct = opts.ct;
  1730. }
  1731. return file;
  1732. }
  1733. function cfb_del(cfb, name) {
  1734. init_cfb(cfb);
  1735. var file = CFB.find(cfb, name);
  1736. if(file) for(var j = 0; j < cfb.FileIndex.length; ++j) if(cfb.FileIndex[j] == file) {
  1737. cfb.FileIndex.splice(j, 1);
  1738. cfb.FullPaths.splice(j, 1);
  1739. return true;
  1740. }
  1741. return false;
  1742. }
  1743. function cfb_mov(cfb, old_name, new_name) {
  1744. init_cfb(cfb);
  1745. var file = CFB.find(cfb, old_name);
  1746. if(file) for(var j = 0; j < cfb.FileIndex.length; ++j) if(cfb.FileIndex[j] == file) {
  1747. cfb.FileIndex[j].name = filename(new_name);
  1748. cfb.FullPaths[j] = new_name;
  1749. return true;
  1750. }
  1751. return false;
  1752. }
  1753. function cfb_gc(cfb) { rebuild_cfb(cfb, true); }
  1754. exports.find = find;
  1755. exports.read = read;
  1756. exports.parse = parse;
  1757. exports.write = write;
  1758. exports.writeFile = write_file;
  1759. exports.utils = {
  1760. cfb_new: cfb_new,
  1761. cfb_add: cfb_add,
  1762. cfb_del: cfb_del,
  1763. cfb_mov: cfb_mov,
  1764. cfb_gc: cfb_gc,
  1765. ReadShift: ReadShift,
  1766. CheckField: CheckField,
  1767. prep_blob: prep_blob,
  1768. bconcat: bconcat,
  1769. use_zlib: use_zlib,
  1770. _deflateRaw: _deflate,
  1771. _inflateRaw: _inflate,
  1772. consts: consts
  1773. };
  1774. return exports;
  1775. })();
  1776. if(typeof require !== 'undefined' && typeof module !== 'undefined' && typeof DO_NOT_EXPORT_CFB === 'undefined') { module.exports = CFB; }