From 1b950bb872d368d943fafa4bbca91288b1f6c06c Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Thu, 1 Jan 2015 23:00:33 +0300 Subject: [PATCH] Implement var_str --- lib/struct.js | 36 ++++++++++++++++++++++++++++++++++-- test.js | 21 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lib/struct.js b/lib/struct.js index 5cbce53..fc8c1dc 100644 --- a/lib/struct.js +++ b/lib/struct.js @@ -13,11 +13,11 @@ var bitmessage = require("./"); * var_int. * @see {@link https://bitmessage.org/wiki/Protocol_specification#Variable_length_integer} */ -exports.var_int = { +var var_int = exports.var_int = { /** * Decode var_int. * @param {Buffer} buf - Buffer that starts with encoded var_int - * @return {{value: (number|Int64), length: number, rest: number}} + * @return {{value: (number|Int64), length: number, rest: Buffer}} * Decoded var_int structure. */ decode: function(buf) { @@ -105,3 +105,35 @@ exports.var_int = { return buf; }, }; + +/** + * var_str. + * @see {@link https://bitmessage.org/wiki/Protocol_specification#Variable_length_string} + */ +exports.var_str = { + /** + * Decode var_str. + * @param {Buffer} buf - Buffer that starts with encoded var_str + * @return {{str: string, length: number, rest: Buffer}} + * Decoded var_str structure. + */ + decode: function(buf) { + var decoded = var_int.decode(buf); + var strLength = decoded.value; + // XXX(Kagami): Spec doesn't mention encoding, using UTF-8. + var str = decoded.rest.slice(0, strLength).toString(); + var rest = decoded.rest.slice(strLength); + return {str: str, length: decoded.length + strLength, rest: rest}; + }, + + /** + * Encode string into var_str. + * @param {string} str - A string + * @return {Buffer} Encoded var_str. + */ + encode: function(str) { + // XXX(Kagami): Spec doesn't mention encoding, using UTF-8. + var strBuf = new Buffer(str); + return Buffer.concat([var_int.encode(strBuf.length), strBuf]); + }, +}; diff --git a/test.js b/test.js index 5784f45..a722f3c 100644 --- a/test.js +++ b/test.js @@ -8,6 +8,7 @@ var Int64 = bitmessage.Int64; var Address = bitmessage.Address; var wif = bitmessage.wif; var var_int = bitmessage.struct.var_int; +var var_str = bitmessage.struct.var_str; var bmcrypto = require("./lib/crypto"); describe("Core structures", function() { @@ -56,6 +57,26 @@ describe("Core structures", function() { expect(var_int.encode.bind(null, "test")).to.throw(Error); }); }); + + describe("var_str", function() { + it("should decode", function() { + var res; + res = var_str.decode(Buffer("0474657374", "hex")); + expect(res.str).to.equal("test"); + expect(res.length).to.equal(5); + expect(res.rest.toString("hex")).to.equal(""); + + res = var_str.decode(Buffer("00", "hex")); + expect(res.str).to.equal(""); + expect(res.length).to.equal(1); + expect(res.rest.toString("hex")).to.equal(""); + }); + + it("should encode", function() { + expect(var_str.encode("test").toString("hex")).to.equal("0474657374"); + expect(var_str.encode("").toString("hex")).to.equal("00"); + }); + }); }); describe("WIF", function() {