function testBinaryBlob() { var blob = bodec.create(256); for (var i = 0; i < 256; i++) { blob[i] = i; } var bin = codec.frame({type:"blob",body:blob}); var obj = codec.deframe(bin, true); if (bodec.toRaw(blob) !== bodec.toRaw(obj.body)) { throw new Error("Problem decoding binary blob"); } }
decrypt: function (encrypted) { var cipher = forge.aes.createDecryptionCipher(keys.key, 'CBC'); var iv = bodec.toRaw(encrypted, 0, 16); encrypted = bodec.toRaw(encrypted, 16); cipher.start(iv); cipher.update(forge.util.createBuffer(encrypted)); cipher.finish(); return bodec.fromRaw(cipher.output.bytes()); },
function testRoundTrip() { var deflated = deflate(bin); if (!bodec.isBinary(deflated)) { throw new Error("deflate output should be native binary"); } var inflated = inflate(deflated); if (!bodec.isBinary(inflated)) { throw new Error("inflate output should be native binary"); } if (bodec.toRaw(bin) !== bodec.toRaw(inflated)) { console.log([bin, inflated]); throw new Error("Problem with roundtrip"); } },
function testStream() { var done = false; var chunks = []; var deflated = deflate(bin); var inf = inflateStream(); for (var i = 0, l = deflated.length; i < l; ++i) { inf.write(deflated[i]); } var inflated = inf.flush(); if (bodec.toRaw(bin) !== bodec.toRaw(inflated)) { console.log([bin.length, inflated.length]); throw new Error("Problem with roundtrip"); } }
function decodeCommit(body) { var i = 0; var start; var key; var parents = []; var commit = { tree: "", parents: parents, author: "", committer: "", message: "" }; while (body[i] !== 0x0a) { start = i; i = indexOf(body, 0x20, start); if (i < 0) throw new SyntaxError("Missing space"); key = bodec.toRaw(body, start, i++); start = i; i = indexOf(body, 0x0a, start); if (i < 0) throw new SyntaxError("Missing linefeed"); var value = bodec.toUnicode(body, start, i++); if (key === "parent") { parents.push(value); } else { if (key === "author" || key === "committer") { value = decodePerson(value); } commit[key] = value; } } i++; commit.message = bodec.toUnicode(body, i, body.length); return commit; }
fs.readFile(path, function (err, binary) { if (err) return callback(err); if (binary === undefined) { return readPackedRef(ref, callback); } var hash; try { hash = bodec.toRaw(binary).trim(); } catch (err) { return callback(err); } callback(null, hash); });
function deframe(buffer, decode) { var space = indexOf(buffer, 0x20); if (space < 0) throw new Error("Invalid git object buffer"); var nil = indexOf(buffer, 0x00, space); if (nil < 0) throw new Error("Invalid git object buffer"); var body = bodec.slice(buffer, nil + 1); var size = parseDec(buffer, space + 1, nil); if (size !== body.length) throw new Error("Invalid body length."); var type = bodec.toRaw(buffer, 0, space); return { type: type, body: decode ? decoders[type](body) : body }; }
fs.readFile(path, function (err, binary) { if (binary === undefined) return callback(err); var hash; try { var text = bodec.toRaw(binary); var index = text.indexOf(ref); if (index >= 0) { hash = text.substring(index - 41, index - 1); } } catch (err) { return callback(err); } callback(null, hash); });
function decodeTag(body) { var i = 0; var start; var key; var tag = {}; while (body[i] !== 0x0a) { start = i; i = indexOf(body, 0x20, start); if (i < 0) throw new SyntaxError("Missing space"); key = bodec.toRaw(body, start, i++); start = i; i = indexOf(body, 0x0a, start); if (i < 0) throw new SyntaxError("Missing linefeed"); var value = bodec.toUnicode(body, start, i++); if (key === "tagger") value = decodePerson(value); tag[key] = value; } i++; tag.message = bodec.toUnicode(body, i, body.length); return tag; }