Comparing version 0.0.5 to 0.0.6
224
index.js
@@ -1,3 +0,1 @@ | ||
var internals = {}; | ||
/** | ||
@@ -10,31 +8,31 @@ * RLP Encoding based on: https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP | ||
**/ | ||
internals.encode = exports.encode = function(input) { | ||
if (input instanceof Array) { | ||
var output = []; | ||
for (var i in input) { | ||
output.push(internals.encode(input[i])); | ||
} | ||
var buf = Buffer.concat(output); | ||
return Buffer.concat([internals.encodeLength(buf.length, 192), buf]); | ||
} else { | ||
input = internals.toBuffer(input); | ||
if (input.length == 1 && input[0] < 128) { | ||
return input; | ||
} else { | ||
return Buffer.concat([internals.encodeLength(input.length, 128), input]); | ||
} | ||
var encode = exports.encode = function (input) { | ||
if (input instanceof Array) { | ||
var output = []; | ||
for (var i in input) { | ||
output.push(encode(input[i])); | ||
} | ||
}; | ||
internals.encodeLength = function(len, offset) { | ||
if (len < 56) { | ||
return new Buffer([len + offset]); | ||
var buf = Buffer.concat(output); | ||
return Buffer.concat([encodeLength(buf.length, 192), buf]); | ||
} else { | ||
input = toBuffer(input); | ||
if (input.length === 1 && input[0] < 128) { | ||
return input; | ||
} else { | ||
var hexLength = internals.intToHex(len); | ||
var lLength = hexLength.length / 2; | ||
var firstByte = internals.intToHex(offset + 55 + lLength); | ||
return new Buffer(firstByte + hexLength, 'hex'); | ||
return Buffer.concat([encodeLength(input.length, 128), input]); | ||
} | ||
} | ||
}; | ||
function encodeLength(len, offset) { | ||
if (len < 56) { | ||
return new Buffer([len + offset]); | ||
} else { | ||
var hexLength = intToHex(len); | ||
var lLength = hexLength.length / 2; | ||
var firstByte = intToHex(offset + 55 + lLength); | ||
return new Buffer(firstByte + hexLength, 'hex'); | ||
} | ||
} | ||
/** | ||
@@ -46,97 +44,97 @@ * RLP Decoding based on: {@link https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP|RLP} | ||
**/ | ||
exports.decode = function(input) { | ||
var input = internals.toBuffer(input); | ||
return internals._decode(input).data; | ||
var decode = exports.decode = function (input) { | ||
input = toBuffer(input); | ||
return _decode(input).data; | ||
}; | ||
internals._decode = function(input) { | ||
var firstByte = input[0]; | ||
if (firstByte <= 0x7f) { | ||
//a single byte whose value is in the [0x00, 0x7f] range, that byte is its own RLP encoding. | ||
return { | ||
data: input.slice(0, 1), | ||
remainder: input.slice(1) | ||
}; | ||
} else if (firstByte <= 0xb7) { | ||
//string is 0-55 bytes long. A single byte with value 0x80 plus the length of the string followed by the string | ||
//The range of the first byte is [0x80, 0xb7] | ||
var length = firstByte - 0x7f, | ||
data; | ||
//set 0x80 null to 0 | ||
if(firstByte === 0x80){ | ||
data = new Buffer([0]); | ||
}else{ | ||
data = input.slice(1, length); | ||
} | ||
return { | ||
data: data, | ||
remainder: input.slice(length) | ||
}; | ||
} else if (firstByte <= 0xbf) { | ||
var llength = firstByte - 0xb6; | ||
var length = parseInt(input.slice(1, llength).toString('hex'), 16); | ||
return { | ||
data: input.slice(llength, length + llength), | ||
remainder: input.slice(length + llength) | ||
}; | ||
} else if (firstByte <= 0xf7) { | ||
//a list between 0-55 bytes long | ||
var length = firstByte - 0xbf; | ||
var remainder = input.slice(1); | ||
var innerRemainder = input.slice(1, length); | ||
var decoded = []; | ||
while (innerRemainder.length) { | ||
var d = internals._decode(innerRemainder); | ||
decoded.push(d.data); | ||
innerRemainder = d.remainder; | ||
} | ||
function _decode (input) { | ||
var firstByte = input[0]; | ||
if (firstByte <= 0x7f) { | ||
//a single byte whose value is in the [0x00, 0x7f] range, that byte is its own RLP encoding. | ||
return { | ||
data: input.slice(0, 1), | ||
remainder: input.slice(1) | ||
}; | ||
} else if (firstByte <= 0xb7) { | ||
//string is 0-55 bytes long. A single byte with value 0x80 plus the length of the string followed by the string | ||
//The range of the first byte is [0x80, 0xb7] | ||
var length = firstByte - 0x7f, | ||
data; | ||
return { | ||
data: decoded, | ||
remainder: input.slice(length) | ||
}; | ||
//set 0x80 null to 0 | ||
if (firstByte === 0x80) { | ||
data = new Buffer([0]); | ||
} else { | ||
//a list over 55 bytes long | ||
var llength = firstByte - 0xf6; | ||
var length = parseInt(input.slice(1, llength).toString('hex'), 16); | ||
var remainder = input.slice(llength); | ||
var innerRemainder = input.slice(llength, llength + length); | ||
var decoded = []; | ||
while (innerRemainder.length) { | ||
var d = internals._decode(innerRemainder); | ||
decoded.push(d.data); | ||
innerRemainder = d.remainder; | ||
} | ||
return { | ||
data: decoded, | ||
remainder: input.slice(llength + length) | ||
}; | ||
data = input.slice(1, length); | ||
} | ||
}; | ||
return { | ||
data: data, | ||
remainder: input.slice(length) | ||
}; | ||
} else if (firstByte <= 0xbf) { | ||
var llength = firstByte - 0xb6; | ||
var length = parseInt(input.slice(1, llength).toString('hex'), 16); | ||
return { | ||
data: input.slice(llength, length + llength), | ||
remainder: input.slice(length + llength) | ||
}; | ||
} else if (firstByte <= 0xf7) { | ||
//a list between 0-55 bytes long | ||
var length = firstByte - 0xbf; | ||
var remainder = input.slice(1); | ||
var innerRemainder = input.slice(1, length); | ||
var decoded = []; | ||
while (innerRemainder.length) { | ||
var d = _decode(innerRemainder); | ||
decoded.push(d.data); | ||
innerRemainder = d.remainder; | ||
} | ||
internals.intToHex = function(i) { | ||
var hex = i.toString(16); | ||
if (hex.length % 2) { | ||
hex = '0' + hex; | ||
return { | ||
data: decoded, | ||
remainder: input.slice(length) | ||
}; | ||
} else { | ||
//a list over 55 bytes long | ||
var llength = firstByte - 0xf6; | ||
var length = parseInt(input.slice(1, llength).toString('hex'), 16); | ||
var remainder = input.slice(llength); | ||
var innerRemainder = input.slice(llength, llength + length); | ||
var decoded = []; | ||
while (innerRemainder.length) { | ||
var d = _decode(innerRemainder); | ||
decoded.push(d.data); | ||
innerRemainder = d.remainder; | ||
} | ||
return hex; | ||
}; | ||
return { | ||
data: decoded, | ||
remainder: input.slice(llength + length) | ||
}; | ||
} | ||
} | ||
internals.toBuffer = function(input) { | ||
if (Buffer.isBuffer(input)) { | ||
if(input.length === 1 && input[0] === 0){ | ||
return this.toBuffer(null); | ||
}else{ | ||
return input; | ||
} | ||
} else if (input === null || input === 0 || input === undefined) { | ||
return new Buffer(0); | ||
} else if (!isNaN(input)) { | ||
var hex = internals.intToHex(input); | ||
return new Buffer(hex, 'hex'); | ||
} else if (!Buffer.isBuffer(input)) { | ||
return new Buffer(input.toString()); | ||
function intToHex (i) { | ||
var hex = i.toString(16); | ||
if (hex.length % 2) { | ||
hex = '0' + hex; | ||
} | ||
return hex; | ||
} | ||
function toBuffer (input) { | ||
if (Buffer.isBuffer(input)) { | ||
if (input.length === 1 && input[0] === 0) { | ||
return this.toBuffer(null); | ||
} else { | ||
return input; | ||
} | ||
}; | ||
} else if (input === null || input === 0 || input === undefined) { | ||
return new Buffer(0); | ||
} else if (!isNaN(input)) { | ||
var hex = intToHex(input); | ||
return new Buffer(hex, 'hex'); | ||
} else if (!Buffer.isBuffer(input)) { | ||
return new Buffer(input.toString()); | ||
} | ||
} |
{ | ||
"name": "rlp", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Recursive Length Prefix Encoding Module", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
97604
17
2700
0
3