cfb.js 60 KB

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