2014-01-28 16:38:02 +00:00
|
|
|
/* [MS-XLSB] 2.1.4 Record */
|
2017-02-10 19:23:01 +00:00
|
|
|
function recordhopper(data, cb/*:RecordHopperCB*/, opts/*:?any*/) {
|
|
|
|
if(!data) return;
|
2014-01-29 06:00:09 +00:00
|
|
|
var tmpbyte, cntbyte, length;
|
2014-01-28 16:38:02 +00:00
|
|
|
prep_blob(data, data.l || 0);
|
2017-04-09 04:03:19 +00:00
|
|
|
var L = data.length, RT = 0, tgt = 0;
|
|
|
|
while(data.l < L) {
|
|
|
|
RT = data.read_shift(1);
|
2014-01-28 16:38:02 +00:00
|
|
|
if(RT & 0x80) RT = (RT & 0x7F) + ((data.read_shift(1) & 0x7F)<<7);
|
2015-04-02 20:32:22 +00:00
|
|
|
var R = XLSBRecordEnum[RT] || XLSBRecordEnum[0xFFFF];
|
2014-02-12 06:09:42 +00:00
|
|
|
tmpbyte = data.read_shift(1);
|
|
|
|
length = tmpbyte & 0x7F;
|
2014-01-28 16:38:02 +00:00
|
|
|
for(cntbyte = 1; cntbyte <4 && (tmpbyte & 0x80); ++cntbyte) length += ((tmpbyte = data.read_shift(1)) & 0x7F)<<(7*cntbyte);
|
2017-04-09 04:03:19 +00:00
|
|
|
tgt = data.l + length;
|
2021-11-08 04:52:18 +00:00
|
|
|
var d = R.f && R.f(data, length, opts);
|
2017-04-02 06:47:25 +00:00
|
|
|
data.l = tgt;
|
2022-03-12 14:05:57 +00:00
|
|
|
if(cb(d, R, RT)) return;
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|
2014-06-29 18:29:45 +00:00
|
|
|
}
|
2014-05-16 00:33:34 +00:00
|
|
|
|
|
|
|
/* control buffer usage for fixed-length buffers */
|
2017-02-10 19:23:01 +00:00
|
|
|
function buf_array()/*:BufArray*/ {
|
2017-12-30 05:40:35 +00:00
|
|
|
var bufs/*:Array<Block>*/ = [], blksz = has_buf ? 256 : 2048;
|
|
|
|
var newblk = function ba_newblk(sz/*:number*/)/*:Block*/ {
|
2017-03-12 18:02:43 +00:00
|
|
|
var o/*:Block*/ = (new_buf(sz)/*:any*/);
|
2014-06-29 18:29:45 +00:00
|
|
|
prep_blob(o, 0);
|
2014-05-29 22:30:03 +00:00
|
|
|
return o;
|
|
|
|
};
|
|
|
|
|
2017-12-30 05:40:35 +00:00
|
|
|
var curbuf/*:Block*/ = newblk(blksz);
|
2014-05-29 22:30:03 +00:00
|
|
|
|
2014-06-29 18:29:45 +00:00
|
|
|
var endbuf = function ba_endbuf() {
|
2017-02-10 19:23:01 +00:00
|
|
|
if(!curbuf) return;
|
2022-07-12 04:28:03 +00:00
|
|
|
// workaround for new Buffer(3).slice(0,0) bug in bun 0.1.3
|
|
|
|
if(curbuf.l) {
|
|
|
|
if(curbuf.length > curbuf.l) { curbuf = curbuf.slice(0, curbuf.l); curbuf.l = curbuf.length; }
|
|
|
|
if(curbuf.length > 0) bufs.push(curbuf);
|
|
|
|
}
|
2014-05-29 22:30:03 +00:00
|
|
|
curbuf = null;
|
|
|
|
};
|
|
|
|
|
2017-12-30 05:40:35 +00:00
|
|
|
var next = function ba_next(sz/*:number*/)/*:Block*/ {
|
2018-02-08 18:21:39 +00:00
|
|
|
if(curbuf && (sz < (curbuf.length - curbuf.l))) return curbuf;
|
2014-05-29 22:30:03 +00:00
|
|
|
endbuf();
|
|
|
|
return (curbuf = newblk(Math.max(sz+1, blksz)));
|
|
|
|
};
|
|
|
|
|
2014-06-29 18:29:45 +00:00
|
|
|
var end = function ba_end() {
|
2014-05-29 22:30:03 +00:00
|
|
|
endbuf();
|
2022-02-10 12:40:50 +00:00
|
|
|
return bconcat(bufs);
|
2014-05-29 22:30:03 +00:00
|
|
|
};
|
|
|
|
|
2018-02-08 18:21:39 +00:00
|
|
|
var push = function ba_push(buf) { endbuf(); curbuf = buf; if(curbuf.l == null) curbuf.l = curbuf.length; next(blksz); };
|
2014-05-29 22:30:03 +00:00
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
return ({ next:next, push:push, end:end, _bufs:bufs }/*:any*/);
|
2014-06-29 18:29:45 +00:00
|
|
|
}
|
2014-05-29 22:30:03 +00:00
|
|
|
|
2022-03-12 14:05:57 +00:00
|
|
|
function write_record(ba/*:BufArray*/, type/*:number*/, payload, length/*:?number*/) {
|
|
|
|
var t/*:number*/ = +type, l;
|
2017-02-10 19:23:01 +00:00
|
|
|
if(isNaN(t)) return; // TODO: throw something here?
|
2015-04-02 20:32:22 +00:00
|
|
|
if(!length) length = XLSBRecordEnum[t].p || (payload||[]).length || 0;
|
2017-12-30 05:40:35 +00:00
|
|
|
l = 1 + (t >= 0x80 ? 1 : 0) + 1/* + length*/;
|
2014-05-29 22:30:03 +00:00
|
|
|
if(length >= 0x80) ++l; if(length >= 0x4000) ++l; if(length >= 0x200000) ++l;
|
|
|
|
var o = ba.next(l);
|
|
|
|
if(t <= 0x7F) o.write_shift(1, t);
|
|
|
|
else {
|
|
|
|
o.write_shift(1, (t & 0x7F) + 0x80);
|
|
|
|
o.write_shift(1, (t >> 7));
|
|
|
|
}
|
|
|
|
for(var i = 0; i != 4; ++i) {
|
|
|
|
if(length >= 0x80) { o.write_shift(1, (length & 0x7F)+0x80); length >>= 7; }
|
|
|
|
else { o.write_shift(1, length); break; }
|
|
|
|
}
|
2017-02-03 20:50:45 +00:00
|
|
|
if(/*:: length != null &&*/length > 0 && is_buf(payload)) ba.push(payload);
|
2014-06-29 18:29:45 +00:00
|
|
|
}
|