Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bignum/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var BigNumber = require('bignumber.js')
var bencode = module.exports

bencode.encode = require('../lib/encode')
bencode.decode = require('../lib/decode')

bencode.encode.number = function (buffers, data) {
buffers.push(new Buffer('i' + data.toString() + 'e'))
}

bencode.decode.integer = function () {
var end = bencode.decode.find(0x65)
var number = bencode.decode.data.toString('ascii', bencode.decode.position + 1, end)

bencode.decode.position += end + 1 - bencode.decode.position

return new BigNumber(number, 10)
}
117 changes: 0 additions & 117 deletions lib/decode.js

This file was deleted.

128 changes: 128 additions & 0 deletions lib/decoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* Decoder
* @return {Decoder}
*/
function Decoder () {
if (!(this instanceof Decoder)) {
return new Decoder()
}

this.bytes = 0
this.position = 0
this.data = null
this.encoding = null
}

/**
* Decoder prototype
* @type {Object}
*/
Decoder.prototype = {

constructor: Decoder,

use: function (type, fn) {
this[type] = fn
},

decode: function (data, start, end, encoding) {
if (typeof start !== 'number' && encoding == null) {
encoding = start
start = undefined
}

if (typeof end !== 'number' && encoding == null) {
encoding = end
end = undefined
}

this.position = 0
this.encoding = encoding || null

this.data = !(Buffer.isBuffer(data))
? new Buffer(data)
: data.slice(start, end)

this.bytes = this.data.length

return this.next()
},

next: function () {
switch (this.data[this.position]) {
case 0x64: return this.object()
case 0x6C: return this.array()
case 0x69: return this.number()
default: return this.buffer()
}
},

find: function (chr) {
var i = this.position
var c = this.data.length
var d = this.data

while (i < c) {
if (d[i] === chr) return i
i++
}

throw new Error(
'Invalid data: Missing delimiter "' + String.fromCharCode(chr) +
'" [0x' + chr.toString(16) + ']'
)
},

object: function () {
this.position++

var dict = {}

while (this.data[this.position] !== 0x65) {
dict[this.buffer()] = this.next()
}

this.position++

return dict
},

array: function () {
this.position++

var lst = []

while (this.data[this.position] !== 0x65) {
lst.push(this.next())
}

this.position++

return lst
},

number: function () {
var end = this.find(0x65)
var number = this.data.toString('ascii', this.position + 1, end)

this.position += end + 1 - this.position

return parseInt(number, 10)
},

buffer: function () {
var sep = this.find(0x3A)
var length = parseInt(this.data.toString('ascii', this.position, sep), 10)
var end = ++sep + length

this.position = end

return this.encoding
? this.data.toString(this.encoding, sep, end)
: this.data.slice(sep, end)
}

}

// Exports
module.exports = Decoder
107 changes: 0 additions & 107 deletions lib/encode.js

This file was deleted.

Loading