bitcoinjs-lib/src/bufferutils.js

188 lines
3.8 KiB
JavaScript
Raw Normal View History

2014-05-01 11:38:18 +02:00
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) {
number = buffer.readUInt8(offset + 1)
size = 2
// 16 bit
} else if (opcode === opcodes.OP_PUSHDATA2) {
number = buffer.readUInt16LE(offset + 1)
size = 3
// 32 bit
} else {
assert.equal(opcode, opcodes.OP_PUSHDATA4, 'Unexpected opcode')
number = buffer.readUInt32LE(offset + 1)
size = 5
}
return {
2014-05-30 08:28:13 +02:00
opcode: opcode,
number: number,
size: size
}
}
2014-05-01 11:38:18 +02:00
function readUInt64LE(buffer, offset) {
var a = buffer.readUInt32LE(offset)
var b = buffer.readUInt32LE(offset + 4)
b *= 0x100000000
verifuint(b + a, 0x001fffffffffffff)
2014-05-01 11:38:18 +02:00
return b + a
}
function readVarInt(buffer, offset) {
var t = buffer.readUInt8(offset)
var number, size
// 8 bit
2014-05-01 11:38:18 +02:00
if (t < 253) {
number = t
size = 1
// 16 bit
2014-05-01 11:38:18 +02:00
} else if (t < 254) {
number = buffer.readUInt16LE(offset + 1)
size = 3
// 32 bit
2014-05-01 11:38:18 +02:00
} 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
}
2014-05-01 11:38:18 +02:00
function writeUInt64LE(buffer, value, offset) {
verifuint(value, 0x001fffffffffffff)
2014-05-01 11:38:18 +02:00
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
}
2014-10-17 04:07:14 +02:00
function varIntBuffer(i) {
var size = varIntSize(i)
var buffer = new Buffer(size)
writeVarInt(buffer, i, 0)
return buffer
}
2014-08-17 09:24:48 +02:00
function reverse(buffer) {
var buffer2 = new Buffer(buffer)
Array.prototype.reverse.call(buffer2)
return buffer2
}
2014-05-01 11:38:18 +02:00
module.exports = {
pushDataSize: pushDataSize,
readPushDataInt: readPushDataInt,
2014-05-01 11:38:18 +02:00
readUInt64LE: readUInt64LE,
readVarInt: readVarInt,
2014-08-17 09:24:48 +02:00
reverse: reverse,
2014-10-17 04:07:14 +02:00
varIntBuffer: varIntBuffer,
2014-05-01 11:38:18 +02:00
varIntSize: varIntSize,
writePushDataInt: writePushDataInt,
2014-05-01 11:38:18 +02:00
writeUInt64LE: writeUInt64LE,
writeVarInt: writeVarInt
}