Add readUInt64LE and writeUInt64LE from bitcoinjs-lib

This commit is contained in:
Jack Grigg 2017-05-31 15:42:48 +12:00
parent 8e9433a218
commit b6db786098
No known key found for this signature in database
GPG Key ID: 665DBCD284F7DAFF
3 changed files with 160 additions and 0 deletions

34
src/bufferutils.js Normal file
View File

@ -0,0 +1,34 @@
/**
* Copyright (c) 2011-2016 bitcoinjs-lib contributors
*/
// https://github.com/feross/buffer/blob/master/index.js#L1127
function verifuint (value, max) {
if (typeof value !== 'number') throw new Error('cannot write a non-number as a number')
if (value < 0) throw new Error('specified a negative value for writing an unsigned value')
if (value > max) throw new Error('RangeError: value out of range')
if (Math.floor(value) !== value) throw new Error('value has a fractional component')
}
function readUInt64LE (buffer, offset) {
var a = buffer.readUInt32LE(offset)
var b = buffer.readUInt32LE(offset + 4)
b *= 0x100000000
verifuint(b + a, 0x001fffffffffffff)
return b + a
}
function writeUInt64LE (buffer, value, offset) {
verifuint(value, 0x001fffffffffffff)
buffer.writeInt32LE(value & -1, offset)
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4)
return offset + 8
}
module.exports = {
readUInt64LE: readUInt64LE,
writeUInt64LE: writeUInt64LE
}

50
test/bufferutils.js Normal file
View File

@ -0,0 +1,50 @@
/* global describe, it */
var assert = require('assert')
var bufferutils = require('../src/bufferutils')
var fixtures = require('./fixtures/bufferutils.json')
describe('bufferutils', function () {
describe('readUInt64LE', function () {
fixtures.valid.forEach(function (f) {
it('decodes ' + f.hex64 + ' correctly', function () {
var buffer = Buffer.from(f.hex64, 'hex')
var number = bufferutils.readUInt64LE(buffer, 0)
assert.strictEqual(number, f.dec)
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
it('throws on ' + f.description, function () {
var buffer = Buffer.from(f.hex64, 'hex')
assert.throws(function () {
bufferutils.readUInt64LE(buffer, 0)
}, new RegExp(f.exception))
})
})
})
describe('writeUInt64LE', function () {
fixtures.valid.forEach(function (f) {
it('encodes ' + f.dec + ' correctly', function () {
var buffer = Buffer.alloc(8, 0)
bufferutils.writeUInt64LE(buffer, f.dec, 0)
assert.strictEqual(buffer.toString('hex'), f.hex64)
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
it('throws on ' + f.description, function () {
var buffer = Buffer.alloc(8, 0)
assert.throws(function () {
bufferutils.writeUInt64LE(buffer, f.dec, 0)
}, new RegExp(f.exception))
})
})
})
})

76
test/fixtures/bufferutils.json vendored Normal file
View File

@ -0,0 +1,76 @@
{
"valid": [
{
"dec": 0,
"hex64": "0000000000000000"
},
{
"dec": 1,
"hex64": "0100000000000000"
},
{
"dec": 252,
"hex64": "fc00000000000000"
},
{
"dec": 253,
"hex64": "fd00000000000000"
},
{
"dec": 254,
"hex64": "fe00000000000000"
},
{
"dec": 255,
"hex64": "ff00000000000000"
},
{
"dec": 65534,
"hex64": "feff000000000000"
},
{
"dec": 65535,
"hex64": "ffff000000000000"
},
{
"dec": 65536,
"hex64": "0000010000000000"
},
{
"dec": 65537,
"hex64": "0100010000000000"
},
{
"dec": 4294967295,
"hex64": "ffffffff00000000"
},
{
"dec": 4294967296,
"hex64": "0000000001000000"
},
{
"dec": 4294967297,
"hex64": "0100000001000000"
},
{
"dec": 9007199254740991,
"hex64": "ffffffffffff1f00"
}
],
"invalid": {
"readUInt64LE": [
{
"description": "n === 2^53",
"exception": "RangeError: value out of range",
"hex64": "0000000000002000",
"dec": 9007199254740992
},
{
"description": "n > 2^53",
"exception": "RangeError: value out of range",
"hex64": "0100000000002000",
"dec": 9007199254740993
}
]
}
}