Skip to content

Instantly share code, notes, and snippets.

@dpeek
Created February 28, 2018 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpeek/4b9ff39ba705cf47515f85cfce0cff40 to your computer and use it in GitHub Desktop.
Save dpeek/4b9ff39ba705cf47515f85cfce0cff40 to your computer and use it in GitHub Desktop.
BIF Parser
function Buffer(a) {
this.buffer = a;
this.position = 0;
}
Buffer.prototype = {
seek: function(a) {
this.position = a;
},
skip: function(a) {
this.position += a;
},
remaining: function() {
return this.buffer.length - this.position;
},
next: function() {
return this.buffer[this.position++];
},
readLength: function(a) {
var b = this.position;
this.position += a;
a = this.buffer;
return a.subarray
? a.subarray(b, this.position)
: a.slice(b, this.position);
},
hc: function(a) {
for (var b = 0; a--; ) b = 256 * b + this.buffer[this.position++];
return b;
},
bo: function(a) {
for (var b = ""; a--; )
b += String.fromCharCode(this.buffer[this.position++]);
return b;
},
EDa: function() {
for (var a = "", b; (b = this.next()); ) a += String.fromCharCode(b);
return a;
},
Vb: function() {
return this.hc(2);
},
sa: function() {
return this.hc(4);
},
Ld: function() {
return this.hc(8);
},
Y6: function() {
return this.hc(2) / 256;
},
HR: function() {
return this.hc(4) / 65536;
},
kz: function(a) {
for (var b, c = ""; a--; )
(b = this.next()),
(c += "0123456789ABCDEF"[b >>> 4] + "0123456789ABCDEF"[b & 15]);
return c;
},
au: function() {
return (
this.kz(4) +
"-" +
this.kz(2) +
"-" +
this.kz(2) +
"-" +
this.kz(2) +
"-" +
this.kz(6)
);
},
readBytes: function(a) {
for (var b = 0, c = 0; c < a; c++) b += this.next() << (c << 3);
return b;
},
readFour: function() {
return this.readBytes(4);
},
WT: function(a) {
this.buffer[this.position++] = a;
},
Y9: function(a, b) {
this.position += b;
for (var c = 1; c <= b; c++)
(this.buffer[this.position - c] = a & 255), (a = Math.floor(a / 256));
},
pJa: function(a) {
for (var b = a.length, c = 0; c < b; c++)
this.buffer[this.position++] = a[c];
},
cA: function(a, b) {
this.pJa(a.readLength(b));
}
};
var BIF_VERSION = 0;
var BIF_MAGIC = [137, 66, 73, 70, 13, 10, 26, 10];
var BIF_META_LENGTH = 44;
function parse(a) {
if (!a) throw new Error("invalid array buffer");
var b = new Uint8Array(a);
a = new Buffer(b);
var info = (function(a) {
if (a.remaining() < BIF_MAGIC.length + 4 + 4 + 4 + BIF_META_LENGTH)
throw new Error("array buffer too short");
BIF_MAGIC.forEach(function(b) {
if (b != a.next()) throw new Error("BIF has invalid magic.");
});
var version = a.readFour();
if (version > BIF_VERSION) throw new Error("BIF version in unsupported");
var frames = a.readFour();
if (frames == 0) throw new Error("BIF has no frames.");
var f = a.readFour(),
meta = a.readLength(BIF_META_LENGTH);
return { version: version, frames: frames, k9: f, meta: meta };
})(a);
a = (function(a) {
for (var f = [], g = 0; g <= info.frames; g++) {
var k,
h = { timestamp: a.readFour(), offset: a.readFour() };
void 0 != k && f.push(b.subarray(k.offset, h.offset));
k = h;
}
return f;
})(a);
return { info: info, images: a };
}
const fs = require("fs");
// wget https://occ-0-1683-300.1.nflxso.net/tp/tpa3/888/c3813c1cd2386ddb5efbc41a67029348.bif -O test.bif
const bif = fs.readFileSync("./test.bif");
const thumbs = parse(bif);
thumbs.images.forEach((image, i) => {
const thumbPath = `thumb/${i}.jpg`;
fs.writeFileSync(thumbPath, image);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment