Socket
Socket
Sign inDemoInstall

bncode

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bncode - npm Package Compare versions

Comparing version 0.5.2 to 0.5.3

226

bncode.js
/*jshint es5:false, asi:true, quotmark:false, eqeqeq:false, forin: false */
/*
* (c) 2011 Tim Becker, see file LICENSE for details
* (c) 2011-14 Tim Becker, see file LICENSE for details
*/

@@ -8,8 +9,7 @@

* Provides functionality for bencoding and decoding as use in
* bittorrent and described in:
* http://www.bittorrent.org/beps/bep_0003.html
* bittorrent and described in: http://www.bittorrent.org/beps/bep_0003.html
*
* Encoding is as follows:
*
* var benc = require("bncode"),
* var benc = require('bncode'),
* exmp = {}

@@ -67,11 +67,16 @@ *

var Transform = require('stream').Transform;
exports.encode = Bencode
exports.decoder = Bdecode
exports.decode = decode
exports.Stream = Stream
var inherits = require('util').inherits
var Transform = require('stream').Transform
var I = "i".charCodeAt(0)
var L = "l".charCodeAt(0)
var E = "e".charCodeAt(0)
var D = "d".charCodeAt(0)
var COLON = ":".charCodeAt(0)
var DASH = "-".charCodeAt(0)
var I = 'i'.charCodeAt(0)
var L = 'l'.charCodeAt(0)
var E = 'e'.charCodeAt(0)
var D = 'd'.charCodeAt(0)
var COLON = ':'.charCodeAt(0)
var DASH = '-'.charCodeAt(0)

@@ -85,5 +90,2 @@ var STATE_INITIAL = 0

function log (m) {console.log(m); process.stdout.flush()}
/*

@@ -110,17 +112,12 @@ * This is the internal state machine for taking apart bencoded strings,

var BdecodeSMachine = function (cb, cb_list, cb_dict, cb_end) {
var depth = 0
var state = STATE_INITIAL
function BdecodeSMachine (cb, cb_list, cb_dict, cb_end) {
var depth = 0
var state = STATE_INITIAL
var cb = cb
var cb_list = cb_list
var cb_dict = cb_dict
var cb_end = cb_end
this.consistent = function() {
return (state === STATE_INITIAL && depth === 0)
this.consistent = function () {
return state === STATE_INITIAL && depth === 0
}
var strLen = 0
var str = ""
var str = ''
var _int = 0

@@ -130,11 +127,10 @@ var neg = false

this.parse = function (buffer, encoding) {
encoding = encoding ? encoding : "utf8"
if ("string" === typeof(buffer)) {
buffer = new Buffer(buffer, encoding)
if (typeof buffer === 'string') {
buffer = new Buffer(buffer, encoding || 'utf8')
}
for (var pos = 0; pos != buffer.length; ++pos) {
switch(state) {
for (var pos = 0; pos !== buffer.length; ++pos) {
switch (state) {
case STATE_INITIAL:
switch(buffer[pos]){
switch (buffer[pos]) {
case 0x30:

@@ -173,3 +169,3 @@ case 0x31:

if (depth < 0) {
throw new Error("end with no beginning: "+pos)
throw new Error('end with no beginning: ' + pos)
} else {

@@ -180,3 +176,3 @@ cb_end()

}
break;
break
case STATE_STATE_STRING_LEN:

@@ -193,4 +189,4 @@ if (integer(buffer[pos])) {

case STATE_COLON:
if (buffer[pos] != COLON) {
throw new Error("not a colon at:"+pos.toString(16))
if (buffer[pos] !== COLON) {
throw new Error('not a colon at: ' + pos.toString(16))
}

@@ -221,3 +217,3 @@ state = STATE_STRING

state = STATE_INTEGER
if (buffer[pos] == DASH) {
if (buffer[pos] === DASH) {
neg = true // handle neg and zero within value.

@@ -230,3 +226,3 @@ break

_int += buffer[pos] - 0x30
} else if (buffer[pos] == E) {
} else if (buffer[pos] === E) {
var ret = neg ? 0 - _int : _int

@@ -236,5 +232,5 @@ cb(ret)

} else {
throw new Error("not part of int at:"+pos.toString(16))
throw new Error('not part of int at:'+pos.toString(16))
}
break;
break
} // switch state

@@ -244,7 +240,6 @@ } // for buffer

var integer = function (value) {
function integer (value) {
// check that value is a number and that
// its value is ascii integer.
if (! (typeof(value)==='number') ) {
if (typeof value !== 'number') {
return false

@@ -254,7 +249,6 @@ }

}
var between = function(val, min, max) {
function between (val, min, max) {
return (min <= val && val <= max)
}
} // end BdecodeSMachine

@@ -265,3 +259,3 @@

*/
var Bdecode = function () {
function Bdecode () {
// markers

@@ -271,17 +265,17 @@ var DICTIONARY_START = {}

var Context = function() {
var Context = function () {
var self = this
var stack = []
this.cb = function(o){
this.cb = function (o) {
stack.push(o)
}
this.cb_list = function(){
this.cb_list = function () {
self.cb(LIST_START)
}
this.cb_dict = function(){
this.cb_dict = function () {
self.cb(DICTIONARY_START)
}
this.cb_end = function(){
this.cb_end = function () {

@@ -292,10 +286,10 @@ // unwind the stack until either a DICTIONARY_START or LIST_START is

var obj = null,
tmp_stack = []
var obj = null
var tmp_stack = []
while ( undefined !== (obj = stack.pop()) ) {
while ((obj = stack.pop()) !== undefined) {
if (LIST_START === obj) {
var obj2 = null
var list = []
while( undefined !== (obj2 = tmp_stack.pop()) ) {
while((obj2 = tmp_stack.pop()) !== undefined) {
list.push(obj2)

@@ -306,11 +300,11 @@ }

} else if (DICTIONARY_START === obj) {
var key = null,
val = null,
dic = {}
while ( (undefined !== (key = tmp_stack.pop())) && (undefined !== (val = tmp_stack.pop())) ) {
var key = null
var val = null
var dic = {}
while ((key = tmp_stack.pop()) !== undefined && (val = tmp_stack.pop()) !== undefined) {
dic[key.toString()] = val
}
if (undefined !== key && undefined === dic[key]) {
throw new Error("uneven number of keys and values A")
if (key !== undefined && dic[key] === undefined) {
throw new Error('uneven number of keys and values A')
}

@@ -325,3 +319,3 @@ self.cb(dic)

// could this case even occur?
throw new Error("uneven number of keys and values B")
throw new Error('uneven number of keys and values B')
}

@@ -334,9 +328,9 @@ }

var self = this,
ctx = new Context(),
smachine = new BdecodeSMachine(ctx.cb, ctx.cb_list, ctx.cb_dict, ctx.cb_end)
var self = this
var ctx = new Context()
var smachine = new BdecodeSMachine(ctx.cb, ctx.cb_list, ctx.cb_dict, ctx.cb_end)
this.result = function () {
this.result = function () {
if (!smachine.consistent()) {
throw new Error("not in consistent state. More bytes coming?")
throw new Error('not in consistent state. More bytes coming?')
}

@@ -346,3 +340,3 @@ return ctx.result()

this.decode = function(buf, encoding) {
this.decode = function (buf, encoding) {
smachine.parse(buf, encoding)

@@ -352,3 +346,3 @@ }

var Bencode = function(obj) {
function Bencode (obj) {
var self = this

@@ -358,9 +352,8 @@ var to_encode = obj

switch (typeof (obj) ) {
case "string":
switch (typeof obj) {
case 'string':
return encodeString(obj)
case "number":
case 'number':
return encodeNumber(obj)
break
case "object":
case 'object':
if (obj instanceof Array) {

@@ -370,5 +363,3 @@ return encodeList(obj)

return encodeBuffer(obj)
}
{
} else {
// assume it's a hash

@@ -379,10 +370,10 @@ return encodeDict(obj)

function encodeString(obj) {
var blen = Buffer.byteLength(obj),
len = blen.toString(10),
buf = new Buffer(len.length + 1 + blen)
function encodeString (obj) {
var blen = Buffer.byteLength(obj)
var len = blen.toString(10)
var buf = new Buffer(len.length + 1 + blen)
buf.write(len, 0, "ascii")
buf.write(":", len.length, "ascii")
buf.write(obj, len.length+1, "utf8")
buf.write(len, 0, 'ascii')
buf.write(':', len.length, 'ascii')
buf.write(obj, len.length + 1, 'utf8')

@@ -392,9 +383,9 @@ return buf

function encodeNumber(num) {
var n = num.toString(10),
buf = new Buffer(n.length+2)
function encodeNumber (num) {
var n = num.toString(10)
var buf = new Buffer(n.length + 2)
buf.write("i", 0)
buf.write('i', 0)
buf.write(n, 1)
buf.write("e", n.length+1)
buf.write('e', n.length + 1)

@@ -404,8 +395,9 @@ return buf

function encodeDict(obj) {
function encodeDict (obj) {
var func = function (obj, pos) {
var keys = Object.keys(obj).sort()
for (var i in keys) {
var key = Bencode(keys[i]),
val = Bencode(obj[keys[i]])
keys.forEach(function (key) {
var val = new Bencode(obj[key])
key = new Bencode(key)
ensure(key.length + val.length, pos)

@@ -416,12 +408,12 @@ key.copy(buffer, pos, 0)

pos += val.length
}
})
return pos
}
return assemble(obj, "d", func)
return assemble(obj, 'd', func)
}
function encodeList(obj) {
function encodeList (obj) {
var func = function(obj, pos) {
obj.forEach (function(o){
var elem = Bencode(o)
obj.forEach(function (o) {
var elem = new Bencode(o)

@@ -434,12 +426,12 @@ ensure(elem.length, pos)

}
return assemble(obj, "l", func)
return assemble(obj, 'l', func)
}
function encodeBuffer(obj) {
var len = obj.length.toString(10),
buf = new Buffer(len.length+1+obj.length);
function encodeBuffer (obj) {
var len = obj.length.toString(10)
var buf = new Buffer(len.length + 1 + obj.length)
buf.write(len, 0, "ascii")
buf.write(":", len.length, "ascii")
obj.copy(buf, len.length+1, 0)
buf.write(len, 0, 'ascii')
buf.write(':', len.length, 'ascii')
obj.copy(buf, len.length + 1, 0)

@@ -458,3 +450,3 @@ return buf

buffer.write("e", pos++)
buffer.write('e', pos++)
return buffer.slice(0, pos)

@@ -467,3 +459,3 @@ }

} else {
if (buffer.length > num+pos+1) {
if (buffer.length > num + pos + 1) {
return

@@ -487,9 +479,9 @@ } else {

function Stream (options) {
options = options || {};
options.objectMode = true;
Transform.call(this, options);
options = options || {}
options.objectMode = true
Transform.call(this, options)
this._decoder = new Bdecode()
}
inherits(Stream, Transform);
inherits(Stream, Transform)

@@ -499,5 +491,5 @@ Stream.prototype._transform = function (chunk, encoding, callback) {

this._decoder.decode(chunk, encoding)
callback(null);
callback(null)
} catch(err) {
callback(err);
callback(err)
}

@@ -507,10 +499,4 @@ }

Stream.prototype._flush = function (callback) {
this.push(this._decoder.result()[0]);
callback(null);
this.push(this._decoder.result()[0])
callback(null)
}
exports.encode = Bencode
exports.decoder = Bdecode
exports.decode = decode
exports.Stream = Stream
{
"name": "bncode",
"version": "0.5.2",
"version": "0.5.3",
"description": "bittorrent bencoding and decoding.",

@@ -5,0 +5,0 @@ "author": "Tim Becker <tim.becker@kuriositaet.de>",

@@ -113,7 +113,7 @@ [![build status](https://secure.travis-ci.org/a2800276/bncode.png)](http://travis-ci.org/a2800276/bncode)

Roly Fentanes (fent) for bug reports.
Clark Fischer (clarkf)
The fine folks at Travis.
Patrick Williams
Feross Aboukhadijeh
* Roly Fentanes (fent) for bug reports.
* Clark Fischer (clarkf)
* The fine folks at Travis.
* Patrick Williams
* Feross Aboukhadijeh

@@ -120,0 +120,0 @@

@@ -8,3 +8,4 @@

function report () {
log ("#test:"+tests+" failures: "+failures+"("+(failures/tests)*100+"%)")
var perc_failed = (failures/tests)*100
log ("#tests: "+tests+" failures: "+failures+" ("+perc_failed.toFixed(2)+"%)")
}

@@ -60,2 +61,14 @@ function log(msg) {

function assert_no_throw(msg, f) {
++tests
try {
f()
} catch (e) {
log (msg + " failed, caught: "+e)
++failures
return false
}
return true
}
/**********************************************************************

@@ -328,2 +341,16 @@ * Encoding tests.

// https://github.com/a2800276/bncode/issues/16
function issue_16() {
Array.prototype.monkey_see_monkeypath = "bananas"
var arr = [1,2,3,4]
var obj = {
one : 1,
two : 2,
tri : 3
}
assert_no_throw("Issue 16", function() {
var encoded = benc.encode(obj)
})
}
docs()

@@ -346,2 +373,3 @@ str_e()

file_readStream("test/videos.torrent");
issue_16()

@@ -348,0 +376,0 @@ report()

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc