bitcoinjs-lib/src/bufferutils.js
2015-04-22 10:17:16 +10:00

188 lines
3.9 KiB
JavaScript

var assert = require('assert')
var opcodes = require('./opcodes')
// https://github.com/feross/buffer/blob/master/index.js#L1127
function verifuint (value, max) {
assert(typeof value === 'number', 'cannot write a non-number as a number')
assert(value >= 0, 'specified a negative value for writing an unsigned value')
assert(value <= max, 'value is larger than maximum value for type')
assert(Math.floor(value) === value, 'value has a fractional component')
}
function pushDataSize (i) {
return i < opcodes.OP_PUSHDATA1 ? 1
: i < 0xff ? 2
: i < 0xffff ? 3
: 5
}
function readPushDataInt (buffer, offset) {
var opcode = buffer.readUInt8(offset)
var number, size
// ~6 bit
if (opcode < opcodes.OP_PUSHDATA1) {
number = opcode
size = 1
// 8 bit
} else if (opcode === opcodes.OP_PUSHDATA1) {
if (offset + 2 > buffer.length) return null
number = buffer.readUInt8(offset + 1)
size = 2
// 16 bit
} else if (opcode === opcodes.OP_PUSHDATA2) {
if (offset + 3 > buffer.length) return null
number = buffer.readUInt16LE(offset + 1)
size = 3
// 32 bit
} else {
if (offset + 5 > buffer.length) return null
assert.equal(opcode, opcodes.OP_PUSHDATA4, 'Unexpected opcode')
number = buffer.readUInt32LE(offset + 1)
size = 5
}
return {
opcode: opcode,
number: number,
size: size
}
}
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 readVarInt (buffer, offset) {
var t = buffer.readUInt8(offset)
var number, size
// 8 bit
if (t < 253) {
number = t
size = 1
// 16 bit
} else if (t < 254) {
number = buffer.readUInt16LE(offset + 1)
size = 3
// 32 bit
} else if (t < 255) {
number = buffer.readUInt32LE(offset + 1)
size = 5
// 64 bit
} else {
number = readUInt64LE(buffer, offset + 1)
size = 9
}
return {
number: number,
size: size
}
}
function writePushDataInt (buffer, number, offset) {
var size = pushDataSize(number)
// ~6 bit
if (size === 1) {
buffer.writeUInt8(number, offset)
// 8 bit
} else if (size === 2) {
buffer.writeUInt8(opcodes.OP_PUSHDATA1, offset)
buffer.writeUInt8(number, offset + 1)
// 16 bit
} else if (size === 3) {
buffer.writeUInt8(opcodes.OP_PUSHDATA2, offset)
buffer.writeUInt16LE(number, offset + 1)
// 32 bit
} else {
buffer.writeUInt8(opcodes.OP_PUSHDATA4, offset)
buffer.writeUInt32LE(number, offset + 1)
}
return size
}
function writeUInt64LE (buffer, value, offset) {
verifuint(value, 0x001fffffffffffff)
buffer.writeInt32LE(value & -1, offset)
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4)
}
function varIntSize (i) {
return i < 253 ? 1
: i < 0x10000 ? 3
: i < 0x100000000 ? 5
: 9
}
function writeVarInt (buffer, number, offset) {
var size = varIntSize(number)
// 8 bit
if (size === 1) {
buffer.writeUInt8(number, offset)
// 16 bit
} else if (size === 3) {
buffer.writeUInt8(253, offset)
buffer.writeUInt16LE(number, offset + 1)
// 32 bit
} else if (size === 5) {
buffer.writeUInt8(254, offset)
buffer.writeUInt32LE(number, offset + 1)
// 64 bit
} else {
buffer.writeUInt8(255, offset)
writeUInt64LE(buffer, number, offset + 1)
}
return size
}
function varIntBuffer (i) {
var size = varIntSize(i)
var buffer = new Buffer(size)
writeVarInt(buffer, i, 0)
return buffer
}
function reverse (buffer) {
var buffer2 = new Buffer(buffer)
Array.prototype.reverse.call(buffer2)
return buffer2
}
module.exports = {
pushDataSize: pushDataSize,
readPushDataInt: readPushDataInt,
readUInt64LE: readUInt64LE,
readVarInt: readVarInt,
reverse: reverse,
varIntBuffer: varIntBuffer,
varIntSize: varIntSize,
writePushDataInt: writePushDataInt,
writeUInt64LE: writeUInt64LE,
writeVarInt: writeVarInt
}