Socket
Socket
Sign inDemoInstall

bufio

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bufio - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

147

lib/bufio.js

@@ -48,1 +48,148 @@ /*!

};
function _read(func, size) {
return function(data, off) {
if (off + size > data.length)
throw new EncodingError(off, 'Out of bounds read');
return func(data, off);
};
}
function _readn(func) {
return function(data, off, len) {
if (off + len > data.length)
throw new EncodingError(off, 'Out of bounds read');
return func(data, off, len);
};
}
function _write(func, size) {
return function(data, num, off) {
if (off + size > data.length)
throw new EncodingError(off, 'Out of bounds write');
return func(data, num, off);
};
}
function _writen(func) {
return function(dst, num, off, len) {
if (off + len > dst.length)
throw new EncodingError(off, 'Out of bounds write');
return func(dst, num, off, len);
};
}
function _writecb(func, size) {
return function(data, num, off) {
if (off + size(num) > data.length)
throw new EncodingError(off, 'Out of bounds write');
return func(data, num, off);
};
}
exports.readU = _readn(encoding.readU);
exports.readU64 = _read(encoding.readU64, 8);
exports.readU56 = _read(encoding.readU56, 7);
exports.readU48 = _read(encoding.readU48, 6);
exports.readU40 = _read(encoding.readU40, 5);
exports.readU32 = _read(encoding.readU32, 4);
exports.readU24 = _read(encoding.readU24, 3);
exports.readU16 = _read(encoding.readU16, 2);
exports.readU8 = _read(encoding.readU8, 1);
exports.readUBE = _readn(encoding.readUBE);
exports.readU64BE = _read(encoding.readU64BE, 8);
exports.readU56BE = _read(encoding.readU56BE, 7);
exports.readU48BE = _read(encoding.readU48BE, 6);
exports.readU40BE = _read(encoding.readU40BE, 5);
exports.readU32BE = _read(encoding.readU32BE, 4);
exports.readU24BE = _read(encoding.readU24BE, 3);
exports.readU16BE = _read(encoding.readU16BE, 2);
exports.readI = _readn(encoding.readI);
exports.readI64 = _read(encoding.readI64, 8);
exports.readI56 = _read(encoding.readI56, 7);
exports.readI48 = _read(encoding.readI48, 6);
exports.readI40 = _read(encoding.readI40, 5);
exports.readI32 = _read(encoding.readI32, 4);
exports.readI24 = _read(encoding.readI24, 3);
exports.readI16 = _read(encoding.readI16, 2);
exports.readI8 = _read(encoding.readI8, 1);
exports.readIBE = _readn(encoding.readIBE);
exports.readI64BE = _read(encoding.readI64BE, 8);
exports.readI56BE = _read(encoding.readI56BE, 7);
exports.readI48BE = _read(encoding.readI48BE, 6);
exports.readI40BE = _read(encoding.readI40BE, 5);
exports.readI32BE = _read(encoding.readI32BE, 4);
exports.readI24BE = _read(encoding.readI24BE, 3);
exports.readI16BE = _read(encoding.readI16BE, 2);
exports.readFloat = _read(encoding.readFloat, 4);
exports.readFloatBE = _read(encoding.readFloatBE, 4);
exports.readDouble = _read(encoding.readDouble, 8);
exports.readDoubleBE = _read(encoding.readDoubleBE, 8);
exports.writeU = _writen(encoding.writeU);
exports.writeU64 = _write(encoding.writeU64, 8);
exports.writeU56 = _write(encoding.writeU56, 7);
exports.writeU48 = _write(encoding.writeU48, 6);
exports.writeU40 = _write(encoding.writeU40, 5);
exports.writeU32 = _write(encoding.writeU32, 4);
exports.writeU24 = _write(encoding.writeU24, 3);
exports.writeU16 = _write(encoding.writeU16, 2);
exports.writeU8 = _write(encoding.writeU8, 1);
exports.writeUBE = _writen(encoding.writeUBE);
exports.writeU64BE = _write(encoding.writeU64BE, 8);
exports.writeU56BE = _write(encoding.writeU56BE, 7);
exports.writeU48BE = _write(encoding.writeU48BE, 6);
exports.writeU40BE = _write(encoding.writeU40BE, 5);
exports.writeU32BE = _write(encoding.writeU32BE, 4);
exports.writeU24BE = _write(encoding.writeU24BE, 3);
exports.writeU16BE = _write(encoding.writeU16BE, 2);
exports.writeI = _writen(encoding.writeI);
exports.writeI64 = _write(encoding.writeI64, 8);
exports.writeI56 = _write(encoding.writeI56, 7);
exports.writeI48 = _write(encoding.writeI48, 6);
exports.writeI40 = _write(encoding.writeI40, 5);
exports.writeI32 = _write(encoding.writeI32, 4);
exports.writeI24 = _write(encoding.writeI24, 3);
exports.writeI16 = _write(encoding.writeI16, 2);
exports.writeI8 = _write(encoding.writeI8, 1);
exports.writeIBE = _writen(encoding.writeIBE);
exports.writeI64BE = _write(encoding.writeI64BE, 8);
exports.writeI56BE = _write(encoding.writeI56BE, 7);
exports.writeI48BE = _write(encoding.writeI48BE, 6);
exports.writeI40BE = _write(encoding.writeI40BE, 5);
exports.writeI32BE = _write(encoding.writeI32BE, 4);
exports.writeI24BE = _write(encoding.writeI24BE, 3);
exports.writeI16BE = _write(encoding.writeI16BE, 2);
exports.writeFloat = _write(encoding.writeFloat, 4);
exports.writeFloatBE = _write(encoding.writeFloatBE, 4);
exports.writeDouble = _write(encoding.writeDouble, 8);
exports.writeDoubleBE = _write(encoding.writeDoubleBE, 8);
exports.readVarint = encoding.readVarint;
exports.writeVarint = _writecb(encoding.writeVarint, encoding.sizeVarint);
exports.sizeVarint = encoding.sizeVarint;
exports.readVarint2 = encoding.readVarint2;
exports.writeVarint2 = _writecb(encoding.writeVarint2, encoding.sizeVarint2);
exports.sizeVarint2 = encoding.sizeVarint2;
exports.sliceBytes = encoding.sliceBytes;
exports.readBytes = encoding.readBytes;
exports.writeBytes = encoding.writeBytes;
exports.readString = encoding.readString;
exports.writeString = encoding.writeString;
exports.realloc = encoding.realloc;
exports.copy = encoding.copy;
exports.concat = encoding.concat;
exports.sizeVarBytes = encoding.sizeVarBytes;
exports.sizeVarlen = encoding.sizeVarlen;
exports.sizeVarString = encoding.sizeVarString;

1359

lib/encoding.js

@@ -8,5 +8,6 @@ /*!

/* eslint no-implicit-coercion: "off" */
'use strict';
const {U64, I64} = require('n64');
const EncodingError = require('./error');

@@ -18,226 +19,813 @@

const BASE128_MAX = U64.UINT64_MAX.shrn(7);
const {MAX_SAFE_INTEGER} = Number;
const {floor} = Math;
const float32Array = new Float32Array(1);
const uInt8Float32Array = new Uint8Array(float32Array.buffer);
const float64Array = new Float64Array(1);
const uInt8Float64Array = new Uint8Array(float64Array.buffer);
float32Array[0] = -1;
const bigEndian = uInt8Float32Array[3] === 0;
/*
* Module
* Read Unsigned LE
*/
const encoding = exports;
function readU(data, off, len) {
switch (len) {
case 8:
return readU64(data, off);
case 7:
return readU56(data, off);
case 6:
return readU48(data, off);
case 5:
return readU40(data, off);
case 4:
return readU32(data, off);
case 3:
return readU24(data, off);
case 2:
return readU16(data, off);
case 1:
return readU8(data, off);
default:
throw new EncodingError(off, 'Invalid read length');
}
}
/**
* Read uint64le as a js number.
* @param {Buffer} data
* @param {Number} off
* @returns {Number}
* @throws on num > MAX_SAFE_INTEGER
*/
function readU64(data, off) {
const hi = readU32(data, off + 4);
const lo = readU32(data, off);
enforce((hi & 0xffe00000) === 0, off, 'Number exceeds 2^53-1');
return hi * 0x100000000 + lo;
}
encoding.readU64 = function readU64(data, off) {
const hi = data.readUInt32LE(off + 4, true);
const lo = data.readUInt32LE(off, true);
function readU56(data, off) {
const hi = readU24(data, off + 4);
const lo = readU32(data, off);
enforce((hi & 0xffe00000) === 0, off, 'Number exceeds 2^53-1');
return hi * 0x100000000 + lo;
};
}
/**
* Read uint64be as a js number.
* @param {Buffer} data
* @param {Number} off
* @returns {Number}
* @throws on num > MAX_SAFE_INTEGER
function readU48(data, off) {
const first = data[off];
const last = data[off + 5];
return first +
data[++off] * 2 ** 8 +
data[++off] * 2 ** 16 +
data[++off] * 2 ** 24 +
(data[++off] + last * 2 ** 8) * 2 ** 32;
}
function readU40(data, off) {
const first = data[off];
const last = data[off + 4];
return first +
data[++off] * 2 ** 8 +
data[++off] * 2 ** 16 +
data[++off] * 2 ** 24 +
last * 2 ** 32;
}
function readU32(data, off) {
const first = data[off];
const last = data[off + 3];
return first +
data[++off] * 2 ** 8 +
data[++off] * 2 ** 16 +
last * 2 ** 24;
}
function readU24(data, off) {
const first = data[off];
const last = data[off + 2];
return first + data[++off] * 2 ** 8 + last * 2 ** 16;
}
function readU16(data, off) {
const first = data[off];
const last = data[off + 1];
return first + last * 2 ** 8;
}
function readU8(data, off) {
return data[off];
}
/*
* Read Unsigned BE
*/
encoding.readU64BE = function readU64BE(data, off) {
const hi = data.readUInt32BE(off, true);
const lo = data.readUInt32BE(off + 4, true);
function readUBE(data, off, len) {
switch (len) {
case 8:
return readU64BE(data, off);
case 7:
return readU56BE(data, off);
case 6:
return readU48BE(data, off);
case 5:
return readU40BE(data, off);
case 4:
return readU32BE(data, off);
case 3:
return readU24BE(data, off);
case 2:
return readU16BE(data, off);
case 1:
return readU8(data, off);
default:
throw new EncodingError(off, 'Invalid read length');
}
}
function readU64BE(data, off) {
const hi = readU32BE(data, off);
const lo = readU32BE(data, off + 4);
enforce((hi & 0xffe00000) === 0, off, 'Number exceeds 2^53-1');
return hi * 0x100000000 + lo;
};
}
/**
* Read int64be as a js number.
* @param {Buffer} data
* @param {Number} off
* @returns {Number}
* @throws on num > MAX_SAFE_INTEGER
function readU56BE(data, off) {
const hi = readU24BE(data, off);
const lo = readU32BE(data, off + 3);
enforce((hi & 0xffe00000) === 0, off, 'Number exceeds 2^53-1');
return hi * 0x100000000 + lo;
}
function readU48BE(data, off) {
const first = data[off];
const last = data[off + 5];
return (first * 2 ** 8 + data[++off]) * 2 ** 32 +
data[++off] * 2 ** 24 +
data[++off] * 2 ** 16 +
data[++off] * 2 ** 8 +
last;
}
function readU40BE(data, off) {
const first = data[off];
const last = data[off + 4];
return first * 2 ** 32 +
data[++off] * 2 ** 24 +
data[++off] * 2 ** 16 +
data[++off] * 2 ** 8 +
last;
}
function readU32BE(data, off) {
const first = data[off];
const last = data[off + 3];
return first * 2 ** 24 +
data[++off] * 2 ** 16 +
data[++off] * 2 ** 8 +
last;
}
function readU24BE(data, off) {
const first = data[off];
const last = data[off + 2];
return first * 2 ** 16 + data[++off] * 2 ** 8 + last;
}
function readU16BE(data, off) {
const first = data[off];
const last = data[off + 1];
return first * 2 ** 8 + last;
}
/*
* Read Signed LE
*/
encoding.readI64 = function readI64(data, off) {
const hi = data.readInt32LE(off + 4, true);
const lo = data.readUInt32LE(off, true);
function readI(data, off, len) {
switch (len) {
case 8:
return readI64(data, off);
case 7:
return readI56(data, off);
case 6:
return readI48(data, off);
case 5:
return readI40(data, off);
case 4:
return readI32(data, off);
case 3:
return readI24(data, off);
case 2:
return readI16(data, off);
case 1:
return readI8(data, off);
default:
throw new EncodingError(off, 'Invalid read length');
}
}
function readI64(data, off) {
const hi = readI32(data, off + 4);
const lo = readU32(data, off);
enforce(isSafe(hi, lo), 'Number exceeds 2^53-1');
return hi * 0x100000000 + lo;
};
}
/**
* Read int64be as a js number.
* @param {Buffer} data
* @param {Number} off
* @returns {Number}
* @throws on num > MAX_SAFE_INTEGER
function readI56(data, off) {
const hi = readI24(data, off + 4);
const lo = readU32(data, off);
enforce(isSafe(hi, lo), 'Number exceeds 2^53-1');
return hi * 0x100000000 + lo;
}
function readI48(data, off) {
const first = data[off];
const last = data[off + 5];
const val = data[off + 4] + last * 2 ** 8;
return (val | (val & 2 ** 15) * 0x1fffe) * 2 ** 32 +
first +
data[++off] * 2 ** 8 +
data[++off] * 2 ** 16 +
data[++off] * 2 ** 24;
}
function readI40(data, off) {
const first = data[off];
const last = data[off + 4];
return (last | (last & 2 ** 7) * 0x1fffffe) * 2 ** 32 +
first +
data[++off] * 2 ** 8 +
data[++off] * 2 ** 16 +
data[++off] * 2 ** 24;
}
function readI32(data, off) {
const first = data[off];
const last = data[off + 3];
return first +
data[++off] * 2 ** 8 +
data[++off] * 2 ** 16 +
(last << 24); // Overflow
}
function readI24(data, off) {
const first = data[off];
const last = data[off + 2];
const val = first + data[++off] * 2 ** 8 + last * 2 ** 16;
return val | (val & 2 ** 23) * 0x1fe;
}
function readI16(data, off) {
const first = data[off];
const last = data[off + 1];
const val = first + last * 2 ** 8;
return val | (val & 2 ** 15) * 0x1fffe;
}
function readI8(data, off) {
const val = data[off];
return val | (val & 2 ** 7) * 0x1fffffe;
}
/*
* Read Signed BE
*/
encoding.readI64BE = function readI64BE(data, off) {
const hi = data.readInt32BE(off, true);
const lo = data.readUInt32BE(off + 4, true);
function readIBE(data, off, len) {
switch (len) {
case 8:
return readI64BE(data, off);
case 7:
return readI56BE(data, off);
case 6:
return readI48BE(data, off);
case 5:
return readI40BE(data, off);
case 4:
return readI32BE(data, off);
case 3:
return readI24BE(data, off);
case 2:
return readI16BE(data, off);
case 1:
return readI8(data, off);
default:
throw new EncodingError(off, 'Invalid read length');
}
}
function readI64BE(data, off) {
const hi = readI32BE(data, off);
const lo = readU32BE(data, off + 4);
enforce(isSafe(hi, lo), 'Number exceeds 2^53-1');
return hi * 0x100000000 + lo;
};
}
/**
* Write a javascript number as a uint64le.
* @param {Buffer} dst
* @param {Number} num
* @param {Number} off
* @returns {Number} Buffer offset.
* @throws on num > MAX_SAFE_INTEGER
*/
function readI56BE(data, off) {
const hi = readI24BE(data, off);
const lo = readU32BE(data, off + 3);
enforce(isSafe(hi, lo), 'Number exceeds 2^53-1');
return hi * 0x100000000 + lo;
}
encoding.writeU64 = function writeU64(dst, num, off) {
return write64(dst, num, off, false);
};
function readI48BE(data, off) {
const first = data[off];
const last = data[off + 5];
/**
* Write a javascript number as a uint64be.
* @param {Buffer} dst
* @param {Number} num
* @param {Number} off
* @returns {Number} Buffer offset.
* @throws on num > MAX_SAFE_INTEGER
const val = data[++off] + first * 2 ** 8;
return (val | (val & 2 ** 15) * 0x1fffe) * 2 ** 32 +
data[++off] * 2 ** 24 +
data[++off] * 2 ** 16 +
data[++off] * 2 ** 8 +
last;
}
function readI40BE(data, off) {
const first = data[off];
const last = data[off + 4];
return (first | (first & 2 ** 7) * 0x1fffffe) * 2 ** 32 +
data[++off] * 2 ** 24 +
data[++off] * 2 ** 16 +
data[++off] * 2 ** 8 +
last;
}
function readI32BE(data, off) {
const first = data[off];
const last = data[off + 3];
return (first << 24) + // Overflow
data[++off] * 2 ** 16 +
data[++off] * 2 ** 8 +
last;
}
function readI24BE(data, off) {
const first = data[off];
const last = data[off + 2];
const val = first * 2 ** 16 + data[++off] * 2 ** 8 + last;
return val | (val & 2 ** 23) * 0x1fe;
}
function readI16BE(data, off) {
const first = data[off];
const last = data[off + 1];
const val = first * 2 ** 8 + last;
return val | (val & 2 ** 15) * 0x1fffe;
}
/*
* Read Float
*/
encoding.writeU64BE = function writeU64BE(dst, num, off) {
return write64(dst, num, off, true);
};
function _readFloatBackwards(data, off) {
const first = data[off];
const last = data[off + 3];
/**
* Write a javascript number as an int64le.
* @param {Buffer} dst
* @param {Number} num
* @param {Number} off
* @returns {Number} Buffer offset.
* @throws on num > MAX_SAFE_INTEGER
uInt8Float32Array[3] = first;
uInt8Float32Array[2] = data[++off];
uInt8Float32Array[1] = data[++off];
uInt8Float32Array[0] = last;
return float32Array[0];
}
function _readFloatForwards(data, off) {
const first = data[off];
const last = data[off + 3];
uInt8Float32Array[0] = first;
uInt8Float32Array[1] = data[++off];
uInt8Float32Array[2] = data[++off];
uInt8Float32Array[3] = last;
return float32Array[0];
}
function _readDoubleBackwards(data, off) {
const first = data[off];
const last = data[off + 7];
uInt8Float64Array[7] = first;
uInt8Float64Array[6] = data[++off];
uInt8Float64Array[5] = data[++off];
uInt8Float64Array[4] = data[++off];
uInt8Float64Array[3] = data[++off];
uInt8Float64Array[2] = data[++off];
uInt8Float64Array[1] = data[++off];
uInt8Float64Array[0] = last;
return float64Array[0];
}
function _readDoubleForwards(data, off) {
const first = data[off];
const last = data[off + 7];
uInt8Float64Array[0] = first;
uInt8Float64Array[1] = data[++off];
uInt8Float64Array[2] = data[++off];
uInt8Float64Array[3] = data[++off];
uInt8Float64Array[4] = data[++off];
uInt8Float64Array[5] = data[++off];
uInt8Float64Array[6] = data[++off];
uInt8Float64Array[7] = last;
return float64Array[0];
}
const readFloat = bigEndian ? _readFloatBackwards : _readFloatForwards;
const readFloatBE = bigEndian ? _readFloatForwards : _readFloatBackwards;
const readDouble = bigEndian ? _readDoubleBackwards : _readDoubleForwards;
const readDoubleBE = bigEndian ? _readDoubleForwards : _readDoubleBackwards;
/*
* Write Unsigned LE
*/
encoding.writeI64 = function writeI64(dst, num, off) {
function writeU(dst, num, off, len) {
switch (len) {
case 8:
return writeU64(dst, num, off);
case 7:
return writeU56(dst, num, off);
case 6:
return writeU48(dst, num, off);
case 5:
return writeU40(dst, num, off);
case 4:
return writeU32(dst, num, off);
case 3:
return writeU24(dst, num, off);
case 2:
return writeU16(dst, num, off);
case 1:
return writeU8(dst, num, off);
default:
throw new EncodingError(off, 'Invalid write length');
}
}
function writeU64(dst, num, off) {
return write64(dst, num, off, false);
};
}
/**
* Write a javascript number as an int64be.
* @param {Buffer} dst
* @param {Number} num
* @param {Number} off
* @returns {Number} Buffer offset.
* @throws on num > MAX_SAFE_INTEGER
function writeU56(dst, num, off) {
return write56(dst, num, off, false);
}
function writeU48(dst, num, off) {
num = +num;
const newVal = floor(num * 2 ** -32);
dst[off++] = num;
num = num >>> 8;
dst[off++] = num;
num = num >>> 8;
dst[off++] = num;
num = num >>> 8;
dst[off++] = num;
dst[off++] = newVal;
dst[off++] = (newVal >>> 8);
return off;
}
function writeU40(dst, num, off) {
num = +num;
const newVal = num;
dst[off++] = num;
num = num >>> 8;
dst[off++] = num;
num = num >>> 8;
dst[off++] = num;
num = num >>> 8;
dst[off++] = num;
dst[off++] = floor(newVal * 2 ** -32);
return off;
}
function writeU32(dst, num, off) {
num = +num;
dst[off++] = num;
num = num >>> 8;
dst[off++] = num;
num = num >>> 8;
dst[off++] = num;
num = num >>> 8;
dst[off++] = num;
return off;
}
function writeU24(dst, num, off) {
num = +num;
dst[off++] = num;
num = num >>> 8;
dst[off++] = num;
num = num >>> 8;
dst[off++] = num;
return off;
}
function writeU16(dst, num, off) {
num = +num;
dst[off++] = num;
dst[off++] = (num >>> 8);
return off;
}
function writeU8(dst, num, off) {
num = +num;
dst[off] = num;
return off + 1;
}
/*
* Write Unsigned BE
*/
encoding.writeI64BE = function writeI64BE(dst, num, off) {
function writeUBE(dst, num, off, len) {
switch (len) {
case 8:
return writeU64BE(dst, num, off);
case 7:
return writeU56BE(dst, num, off);
case 6:
return writeU48BE(dst, num, off);
case 5:
return writeU40BE(dst, num, off);
case 4:
return writeU32BE(dst, num, off);
case 3:
return writeU24BE(dst, num, off);
case 2:
return writeU16BE(dst, num, off);
case 1:
return writeU8(dst, num, off);
default:
throw new EncodingError(off, 'Invalid write length');
}
}
function writeU64BE(dst, num, off) {
return write64(dst, num, off, true);
};
}
/**
* Read uint64le.
* @param {Buffer} data
* @param {Number} off
* @returns {U64}
*/
function writeU56BE(dst, num, off) {
return write56(dst, num, off, true);
}
encoding.readU64N = function readU64N(data, off) {
return U64.readLE(data, off);
};
function writeU48BE(dst, num, off) {
num = +num;
/**
* Read uint64be.
* @param {Buffer} data
* @param {Number} off
* @returns {U64}
*/
const newVal = floor(num * 2 ** -32);
dst[off++] = (newVal >>> 8);
dst[off++] = newVal;
dst[off + 3] = num;
num = num >>> 8;
dst[off + 2] = num;
num = num >>> 8;
dst[off + 1] = num;
num = num >>> 8;
dst[off] = num;
return off + 4;
}
encoding.readU64BEN = function readU64BEN(data, off) {
return U64.readBE(data, off);
};
function writeU40BE(dst, num, off) {
num = +num;
/**
* Read int64le.
* @param {Buffer} data
* @param {Number} off
* @returns {I64}
*/
dst[off++] = floor(num * 2 ** -32);
dst[off + 3] = num;
num = num >>> 8;
dst[off + 2] = num;
num = num >>> 8;
dst[off + 1] = num;
num = num >>> 8;
dst[off] = num;
return off + 4;
}
encoding.readI64N = function readI64N(data, off) {
return I64.readLE(data, off);
};
/**
* Read int64be.
* @param {Buffer} data
* @param {Number} off
* @returns {I64}
*/
function writeU32BE(dst, num, off) {
num = +num;
encoding.readI64BEN = function readI64BEN(data, off) {
return I64.readBE(data, off);
};
dst[off + 3] = num;
num = num >>> 8;
dst[off + 2] = num;
num = num >>> 8;
dst[off + 1] = num;
num = num >>> 8;
dst[off] = num;
return off + 4;
}
/**
* Write uint64le.
* @param {Buffer} dst
* @param {U64} num
* @param {Number} off
* @returns {Number} Buffer offset.
*/
function writeU24BE(dst, num, off) {
num = +num;
encoding.writeU64N = function writeU64N(dst, num, off) {
enforce(!num.sign, off, 'Signed');
return num.writeLE(dst, off);
};
dst[off + 2] = num;
num = num >>> 8;
dst[off + 1] = num;
num = num >>> 8;
dst[off] = num;
return off + 3;
}
/**
* Write uint64be.
* @param {Buffer} dst
* @param {U64} num
* @param {Number} off
* @returns {Number} Buffer offset.
*/
function writeU16BE(dst, num, off) {
num = +num;
encoding.writeU64BEN = function writeU64BEN(dst, num, off) {
enforce(!num.sign, off, 'Signed');
return num.writeBE(dst, off);
};
dst[off++] = (num >>> 8);
dst[off++] = num;
return off;
}
/**
* Write int64le.
* @param {Buffer} dst
* @param {U64} num
* @param {Number} off
* @returns {Number} Buffer offset.
/*
* Write Signed LE
*/
encoding.writeI64N = function writeI64N(dst, num, off) {
enforce(num.sign, off, 'Not signed');
return num.writeLE(dst, off);
};
function writeI(dst, num, off, len) {
switch (len) {
case 8:
return writeU64(dst, num, off);
case 7:
return writeU56(dst, num, off);
case 6:
return writeU48(dst, num, off);
case 5:
return writeU40(dst, num, off);
case 4:
return writeU24(dst, num, off);
case 3:
return writeU32(dst, num, off);
case 2:
return writeU16(dst, num, off);
case 1:
return writeU8(dst, num, off);
default:
throw new EncodingError(off, 'Invalid write length');
}
}
/**
* Write int64be.
* @param {Buffer} dst
* @param {I64} num
* @param {Number} off
* @returns {Number} Buffer offset.
function writeI64(dst, num, off) {
return writeU64(dst, num, off);
}
function writeI56(dst, num, off) {
return writeU56(dst, num, off);
}
function writeI48(dst, num, off) {
return writeU48(dst, num, off);
}
function writeI40(dst, num, off) {
return writeU40(dst, num, off);
}
function writeI32(dst, num, off) {
return writeU32(dst, num, off);
}
function writeI24(dst, num, off) {
return writeU24(dst, num, off);
}
function writeI16(dst, num, off) {
return writeU16(dst, num, off);
}
function writeI8(dst, num, off) {
return writeU8(dst, num, off);
}
/*
* Write Signed BE
*/
encoding.writeI64BEN = function writeI64BEN(dst, num, off) {
enforce(num.sign, off, 'Not signed');
return num.writeBE(dst, off);
};
function writeIBE(dst, num, off, len) {
switch (len) {
case 8:
return writeU64BE(dst, num, off);
case 7:
return writeU56BE(dst, num, off);
case 6:
return writeU48BE(dst, num, off);
case 5:
return writeU40BE(dst, num, off);
case 4:
return writeU32BE(dst, num, off);
case 3:
return writeU24BE(dst, num, off);
case 2:
return writeU16BE(dst, num, off);
case 1:
return writeU8(dst, num, off);
default:
throw new EncodingError(off, 'Invalid write length');
}
}
/**
* Read a varint.
* @param {Buffer} data
* @param {Number} off
* @returns {Object}
function writeI64BE(dst, num, off) {
return writeU64BE(dst, num, off);
}
function writeI56BE(dst, num, off) {
return writeU56BE(dst, num, off);
}
function writeI48BE(dst, num, off) {
return writeU48BE(dst, num, off);
}
function writeI40BE(dst, num, off) {
return writeU40BE(dst, num, off);
}
function writeI32BE(dst, num, off) {
return writeU32BE(dst, num, off);
}
function writeI24BE(dst, num, off) {
return writeU24BE(dst, num, off);
}
function writeI16BE(dst, num, off) {
return writeU16BE(dst, num, off);
}
function _writeDoubleForwards(dst, num, off) {
num = +num;
float64Array[0] = num;
dst[off++] = uInt8Float64Array[0];
dst[off++] = uInt8Float64Array[1];
dst[off++] = uInt8Float64Array[2];
dst[off++] = uInt8Float64Array[3];
dst[off++] = uInt8Float64Array[4];
dst[off++] = uInt8Float64Array[5];
dst[off++] = uInt8Float64Array[6];
dst[off++] = uInt8Float64Array[7];
return off;
}
function _writeDoubleBackwards(dst, num, off) {
num = +num;
float64Array[0] = num;
dst[off++] = uInt8Float64Array[7];
dst[off++] = uInt8Float64Array[6];
dst[off++] = uInt8Float64Array[5];
dst[off++] = uInt8Float64Array[4];
dst[off++] = uInt8Float64Array[3];
dst[off++] = uInt8Float64Array[2];
dst[off++] = uInt8Float64Array[1];
dst[off++] = uInt8Float64Array[0];
return off;
}
function _writeFloatForwards(dst, num, off) {
num = +num;
float32Array[0] = num;
dst[off++] = uInt8Float32Array[0];
dst[off++] = uInt8Float32Array[1];
dst[off++] = uInt8Float32Array[2];
dst[off++] = uInt8Float32Array[3];
return off;
}
function _writeFloatBackwards(dst, num, off) {
num = +num;
float32Array[0] = num;
dst[off++] = uInt8Float32Array[3];
dst[off++] = uInt8Float32Array[2];
dst[off++] = uInt8Float32Array[1];
dst[off++] = uInt8Float32Array[0];
return off;
}
const writeFloat = bigEndian ? _writeFloatBackwards : _writeFloatForwards;
const writeFloatBE = bigEndian ? _writeFloatForwards : _writeFloatBackwards;
const writeDouble = bigEndian ? _writeDoubleBackwards : _writeDoubleForwards;
const writeDoubleBE = bigEndian ? _writeDoubleForwards : _writeDoubleBackwards;
/*
* Varints
*/
encoding.readVarint = function readVarint(data, off) {
function readVarint(data, off) {
let value, size;

@@ -251,3 +839,3 @@

check(off + size <= data.length, off);
value = encoding.readU64(data, off + 1);
value = readU64(data, off + 1);
enforce(value > 0xffffffff, off, 'Non-canonical varint');

@@ -258,3 +846,3 @@ break;

check(off + size <= data.length, off);
value = data.readUInt32LE(off + 1, true);
value = readU32(data, off + 1);
enforce(value > 0xffff, off, 'Non-canonical varint');

@@ -275,13 +863,5 @@ break;

return new Varint(size, value);
};
}
/**
* Write a varint.
* @param {Buffer} dst
* @param {Number} num
* @param {Number} off
* @returns {Number} Buffer offset.
*/
encoding.writeVarint = function writeVarint(dst, num, off) {
function writeVarint(dst, num, off) {
if (num < 0xfd) {

@@ -309,13 +889,7 @@ dst[off++] = num & 0xff;

dst[off++] = 0xff;
off = encoding.writeU64(dst, num, off);
off = writeU64(dst, num, off);
return off;
};
}
/**
* Calculate size of varint.
* @param {Number} num
* @returns {Number} size
*/
encoding.sizeVarint = function sizeVarint(num) {
function sizeVarint(num) {
if (num < 0xfd)

@@ -331,69 +905,5 @@ return 1;

return 9;
};
}
/**
* Read a varint.
* @param {Buffer} data
* @param {Number} off
* @returns {Object}
*/
encoding.readVarintN = function readVarintN(data, off) {
check(off < data.length, off);
if (data[off] === 0xff) {
const size = 9;
check(off + size <= data.length, off);
const value = encoding.readU64N(data, off + 1);
enforce(value.hi !== 0, off, 'Non-canonical varint');
return new Varint(size, value);
}
const {size, value} = encoding.readVarint(data, off);
return new Varint(size, U64.fromInt(value));
};
/**
* Write a varint.
* @param {Buffer} dst
* @param {U64} num
* @param {Number} off
* @returns {Number} Buffer offset.
*/
encoding.writeVarintN = function writeVarintN(dst, num, off) {
enforce(!num.sign, off, 'Signed');
if (num.hi !== 0) {
dst[off++] = 0xff;
return encoding.writeU64N(dst, num, off);
}
return encoding.writeVarint(dst, num.toInt(), off);
};
/**
* Calculate size of varint.
* @param {U64} num
* @returns {Number} size
*/
encoding.sizeVarintN = function sizeVarintN(num) {
enforce(!num.sign, 0, 'Signed');
if (num.hi !== 0)
return 9;
return encoding.sizeVarint(num.toInt());
};
/**
* Read a varint (type 2).
* @param {Buffer} data
* @param {Number} off
* @returns {Object}
*/
encoding.readVarint2 = function readVarint2(data, off) {
function readVarint2(data, off) {
let num = 0;

@@ -422,13 +932,5 @@ let size = 0;

return new Varint(size, num);
};
}
/**
* Write a varint (type 2).
* @param {Buffer} dst
* @param {Number} num
* @param {Number} off
* @returns {Number} Buffer offset.
*/
encoding.writeVarint2 = function writeVarint2(dst, num, off) {
function writeVarint2(dst, num, off) {
const tmp = [];

@@ -454,11 +956,5 @@

return off;
};
}
/**
* Calculate size of varint (type 2).
* @param {Number} num
* @returns {Number} size
*/
encoding.sizeVarint2 = function sizeVarint2(num) {
function sizeVarint2(num) {
let size = 0;

@@ -475,133 +971,96 @@

return size;
};
}
/**
* Read a varint (type 2).
* @param {Buffer} data
* @param {Number} off
* @returns {Object}
/*
* Bytes
*/
encoding.readVarint2N = function readVarint2N(data, off) {
const num = new U64();
function sliceBytes(data, off, size) {
if (off + size > data.length)
throw new EncodingError(off, 'Out of bounds read');
let size = 0;
return data.slice(off, off + size);
}
for (;;) {
check(off < data.length, off);
function readBytes(data, off, size) {
if (off + size > data.length)
throw new EncodingError(off, 'Out of bounds read');
const ch = data[off++];
size += 1;
const buf = Buffer.allocUnsafe(size);
data.copy(buf, 0, off, off + size);
return buf;
}
enforce(num.lte(BASE128_MAX), off, 'Number exceeds 2^64-1');
function writeBytes(data, value, off) {
if (off + value.length > data.length)
throw new EncodingError(off, 'Out of bounds write');
num.ishln(7).iorn(ch & 0x7f);
return value.copy(data, off, 0, value.length);
}
if ((ch & 0x80) === 0)
break;
function readString(data, off, size, enc) {
if (!enc)
enc = 'ascii';
enforce(!num.eq(U64.UINT64_MAX), off, 'Number exceeds 2^64-1');
num.iaddn(1);
}
if (off + size > data.length)
throw new EncodingError(off, 'Out of bounds read');
return new Varint(size, num);
};
return data.toString(enc, off, off + size);
}
/**
* Write a varint (type 2).
* @param {Buffer} dst
* @param {U64} num
* @param {Number} off
* @returns {Number} Buffer offset.
*/
function writeString(data, str, off, enc) {
if (!enc)
enc = 'ascii';
encoding.writeVarint2N = function writeVarint2N(dst, num, off) {
enforce(!num.sign, off, 'Signed');
if (str.length === 0)
return 0;
if (num.hi === 0)
return encoding.writeVarint2(dst, num.toInt(), off);
const size = Buffer.byteLength(str, enc);
num = num.clone();
if (off + size > data.length)
throw new EncodingError(off, 'Out of bounds write');
const tmp = [];
data.write(str, off, enc);
let len = 0;
return size;
}
for (;;) {
tmp[len] = num.andln(0x7f) | (len ? 0x80 : 0x00);
if (num.lten(0x7f))
break;
num.ishrn(7).isubn(1);
len += 1;
}
function realloc(data, size) {
const buf = Buffer.allocUnsafe(size);
data.copy(buf, 0);
return buf;
}
enforce(off + len + 1 <= dst.length, off, 'Out of bounds write');
function copy(data) {
return realloc(data, data.length);
}
do {
dst[off++] = tmp[len];
} while (len--);
function concat(a, b) {
const size = a.length + b.length;
const buf = Buffer.allocUnsafe(size);
a.copy(buf, 0);
b.copy(buf, a.length);
return buf;
}
return off;
};
/**
* Calculate size of varint (type 2).
* @param {U64} num
* @returns {Number} size
/*
* Size Helpers
*/
encoding.sizeVarint2N = function sizeVarint2N(num) {
enforce(!num.sign, 0, 'Signed');
function sizeVarBytes(data) {
return sizeVarint(data.length) + data.length;
}
if (num.hi === 0)
return encoding.sizeVarint2(num.toInt());
function sizeVarlen(len) {
return sizeVarint(len) + len;
}
num = num.clone();
let size = 0;
for (;;) {
size += 1;
if (num.lten(0x7f))
break;
num.ishrn(7).isubn(1);
}
return size;
};
/**
* Get size of varint-prefixed bytes.
* @param {Buffer} data
* @returns {Number}
*/
encoding.sizeVarBytes = function sizeVarBytes(data) {
return encoding.sizeVarint(data.length) + data.length;
};
/**
* Get size of varint-prefixed length.
* @param {Number} len
* @returns {Number}
*/
encoding.sizeVarlen = function sizeVarlen(len) {
return encoding.sizeVarint(len) + len;
};
/**
* Get size of varint-prefixed string.
* @param {String} str
* @returns {Number}
*/
encoding.sizeVarString = function sizeVarString(str, enc) {
function sizeVarString(str, enc) {
if (typeof str !== 'string')
return encoding.sizeVarBytes(str);
return sizeVarBytes(str);
const len = Buffer.byteLength(str, enc);
return encoding.sizeVarint(len) + len;
};
return sizeVarint(len) + len;
}

@@ -643,7 +1102,7 @@ /*

if (be) {
off = dst.writeInt32BE(hi, off, true);
off = dst.writeInt32BE(lo, off, true);
off = writeI32BE(dst, hi, off);
off = writeI32BE(dst, lo, off);
} else {
off = dst.writeInt32LE(lo, off, true);
off = dst.writeInt32LE(hi, off, true);
off = writeI32(dst, lo, off);
off = writeI32(dst, hi, off);
}

@@ -654,2 +1113,33 @@

function write56(dst, num, off, be) {
let neg = false;
if (num < 0) {
num = -num;
neg = true;
}
let hi = (num * (1 / 0x100000000)) | 0;
let lo = num | 0;
if (neg) {
if (lo === 0) {
hi = (~hi + 1) | 0;
} else {
hi = ~hi;
lo = ~lo + 1;
}
}
if (be) {
off = writeI24BE(dst, hi, off);
off = writeI32BE(dst, lo, off);
} else {
off = writeI32(dst, lo, off);
off = writeI24(dst, hi, off);
}
return off;
}
class Varint {

@@ -671,1 +1161,112 @@ constructor(size, value) {

}
/*
* Expose
*/
exports.readU = readU;
exports.readU64 = readU64;
exports.readU56 = readU56;
exports.readU48 = readU48;
exports.readU40 = readU40;
exports.readU32 = readU32;
exports.readU24 = readU24;
exports.readU16 = readU16;
exports.readU8 = readU8;
exports.readUBE = readUBE;
exports.readU64BE = readU64BE;
exports.readU56BE = readU56BE;
exports.readU48BE = readU48BE;
exports.readU40BE = readU40BE;
exports.readU32BE = readU32BE;
exports.readU24BE = readU24BE;
exports.readU16BE = readU16BE;
exports.readI = readI;
exports.readI64 = readI64;
exports.readI56 = readI56;
exports.readI48 = readI48;
exports.readI40 = readI40;
exports.readI32 = readI32;
exports.readI24 = readI24;
exports.readI16 = readI16;
exports.readI8 = readI8;
exports.readIBE = readIBE;
exports.readI64BE = readI64BE;
exports.readI56BE = readI56BE;
exports.readI48BE = readI48BE;
exports.readI40BE = readI40BE;
exports.readI32BE = readI32BE;
exports.readI24BE = readI24BE;
exports.readI16BE = readI16BE;
exports.readFloat = readFloat;
exports.readFloatBE = readFloatBE;
exports.readDouble = readDouble;
exports.readDoubleBE = readDoubleBE;
exports.writeU = writeU;
exports.writeU64 = writeU64;
exports.writeU56 = writeU56;
exports.writeU48 = writeU48;
exports.writeU40 = writeU40;
exports.writeU32 = writeU32;
exports.writeU24 = writeU24;
exports.writeU16 = writeU16;
exports.writeU8 = writeU8;
exports.writeUBE = writeUBE;
exports.writeU64BE = writeU64BE;
exports.writeU56BE = writeU56BE;
exports.writeU48BE = writeU48BE;
exports.writeU40BE = writeU40BE;
exports.writeU32BE = writeU32BE;
exports.writeU24BE = writeU24BE;
exports.writeU16BE = writeU16BE;
exports.writeI = writeI;
exports.writeI64 = writeI64;
exports.writeI56 = writeI56;
exports.writeI48 = writeI48;
exports.writeI40 = writeI40;
exports.writeI32 = writeI32;
exports.writeI24 = writeI24;
exports.writeI16 = writeI16;
exports.writeI8 = writeI8;
exports.writeIBE = writeIBE;
exports.writeI64BE = writeI64BE;
exports.writeI56BE = writeI56BE;
exports.writeI48BE = writeI48BE;
exports.writeI40BE = writeI40BE;
exports.writeI32BE = writeI32BE;
exports.writeI24BE = writeI24BE;
exports.writeI16BE = writeI16BE;
exports.writeFloat = writeFloat;
exports.writeFloatBE = writeFloatBE;
exports.writeDouble = writeDouble;
exports.writeDoubleBE = writeDoubleBE;
exports.readVarint = readVarint;
exports.writeVarint = writeVarint;
exports.sizeVarint = sizeVarint;
exports.readVarint2 = readVarint2;
exports.writeVarint2 = writeVarint2;
exports.sizeVarint2 = sizeVarint2;
exports.sliceBytes = sliceBytes;
exports.readBytes = readBytes;
exports.writeBytes = writeBytes;
exports.readString = readString;
exports.writeString = writeString;
exports.realloc = realloc;
exports.copy = copy;
exports.concat = concat;
exports.sizeVarBytes = sizeVarBytes;
exports.sizeVarlen = sizeVarlen;
exports.sizeVarString = sizeVarString;

@@ -127,3 +127,3 @@ /*!

writeU8(value) {
POOL8.writeUInt8(value, 0, true);
encoding.writeU8(POOL8, value, 0);
this.ctx.update(POOL8);

@@ -139,3 +139,3 @@ return this;

writeU16(value) {
POOL16.writeUInt16LE(value, 0, true);
encoding.writeU16(POOL16, value, 0);
this.ctx.update(POOL16);

@@ -151,3 +151,3 @@ return this;

writeU16BE(value) {
POOL16.writeUInt16BE(value, 0, true);
encoding.writeU16BE(POOL16, value, 0);
this.ctx.update(POOL16);

@@ -163,3 +163,3 @@ return this;

writeU32(value) {
POOL32.writeUInt32LE(value, 0, true);
encoding.writeU32(POOL32, value, 0);
this.ctx.update(POOL32);

@@ -175,3 +175,3 @@ return this;

writeU32BE(value) {
POOL32.writeUInt32BE(value, 0, true);
encoding.writeU32BE(POOL32, value, 0);
this.ctx.update(POOL32);

@@ -204,24 +204,2 @@ return this;

/**
* Write uint64le.
* @param {U64} value
*/
writeU64N(value) {
encoding.writeU64N(POOL64, value, 0);
this.ctx.update(POOL64);
return this;
}
/**
* Write uint64be.
* @param {U64} value
*/
writeU64BEN(value) {
encoding.writeU64BEN(POOL64, value, 0);
this.ctx.update(POOL64);
return this;
}
/**
* Write int8.

@@ -232,3 +210,3 @@ * @param {Number} value

writeI8(value) {
POOL8.writeInt8(value, 0, true);
encoding.writeI8(POOL8, value, 0);
this.ctx.update(POOL8);

@@ -244,3 +222,3 @@ return this;

writeI16(value) {
POOL16.writeInt16LE(value, 0, true);
encoding.writeI16(POOL16, value, 0);
this.ctx.update(POOL16);

@@ -256,3 +234,3 @@ return this;

writeI16BE(value) {
POOL16.writeInt16BE(value, 0, true);
encoding.writeI16BE(POOL16, value, 0);
this.ctx.update(POOL16);

@@ -268,3 +246,3 @@ return this;

writeI32(value) {
POOL32.writeInt32LE(value, 0, true);
encoding.writeI32(POOL32, value, 0);
this.ctx.update(POOL32);

@@ -280,3 +258,3 @@ return this;

writeI32BE(value) {
POOL32.writeInt32BE(value, 0, true);
encoding.writeI32BE(POOL32, value, 0);
this.ctx.update(POOL32);

@@ -309,24 +287,2 @@ return this;

/**
* Write int64le.
* @param {I64} value
*/
writeI64N(value) {
encoding.writeI64N(POOL64, value, 0);
this.ctx.update(POOL64);
return this;
}
/**
* Write int64be.
* @param {I64} value
*/
writeI64BEN(value) {
encoding.writeI64BEN(POOL64, value, 0);
this.ctx.update(POOL64);
return this;
}
/**
* Write float le.

@@ -337,3 +293,3 @@ * @param {Number} value

writeFloat(value) {
POOL32.writeFloatLE(value, 0, true);
encoding.writeFloat(POOL32, value, 0);
this.ctx.update(POOL32);

@@ -349,3 +305,3 @@ return this;

writeFloatBE(value) {
POOL32.writeFloatBE(value, 0, true);
encoding.writeFloatBE(POOL32, value, 0);
this.ctx.update(POOL32);

@@ -361,3 +317,3 @@ return this;

writeDouble(value) {
POOL64.writeDoubleLE(value, 0, true);
encoding.writeDouble(POOL64, value, 0);
this.ctx.update(POOL64);

@@ -373,3 +329,3 @@ return this;

writeDoubleBE(value) {
POOL64.writeDoubleBE(value, 0, true);
encoding.writeDoubleBE(POOL64, value, 0);
this.ctx.update(POOL64);

@@ -393,15 +349,2 @@ return this;

/**
* Write a varint.
* @param {U64} value
*/
writeVarintN(value) {
const size = encoding.sizeVarintN(value);
const pool = poolBySize[size];
encoding.writeVarintN(pool, value, 0);
this.ctx.update(pool);
return this;
}
/**
* Write a varint (type 2).

@@ -420,15 +363,2 @@ * @param {Number} value

/**
* Write a varint (type 2).
* @param {U64} value
*/
writeVarint2N(value) {
const size = encoding.sizeVarint2N(value);
const pool = poolBySize[size];
encoding.writeVarint2N(pool, value, 0);
this.ctx.update(pool);
return this;
}
/**
* Write bytes.

@@ -435,0 +365,0 @@ * @param {Buffer} value

@@ -179,3 +179,3 @@ /*!

this.assert(this.offset + 2 <= this.data.length);
const ret = this.data.readUInt16LE(this.offset, true);
const ret = encoding.readU16(this.data, this.offset);
this.offset += 2;

@@ -192,3 +192,3 @@ return ret;

this.assert(this.offset + 2 <= this.data.length);
const ret = this.data.readUInt16BE(this.offset, true);
const ret = encoding.readU16BE(this.data, this.offset);
this.offset += 2;

@@ -205,3 +205,3 @@ return ret;

this.assert(this.offset + 4 <= this.data.length);
const ret = this.data.readUInt32LE(this.offset, true);
const ret = encoding.readU32(this.data, this.offset);
this.offset += 4;

@@ -218,3 +218,3 @@ return ret;

this.assert(this.offset + 4 <= this.data.length);
const ret = this.data.readUInt32BE(this.offset, true);
const ret = encoding.readU32BE(this.data, this.offset);
this.offset += 4;

@@ -257,3 +257,3 @@ return ret;

this.assert(this.offset + 1 <= this.data.length);
const ret = this.data.readInt8(this.offset, true);
const ret = encoding.readI8(this.data, this.offset);
this.offset += 1;

@@ -270,3 +270,3 @@ return ret;

this.assert(this.offset + 2 <= this.data.length);
const ret = this.data.readInt16LE(this.offset, true);
const ret = encoding.readI16(this.data, this.offset);
this.offset += 2;

@@ -283,3 +283,3 @@ return ret;

this.assert(this.offset + 2 <= this.data.length);
const ret = this.data.readInt16BE(this.offset, true);
const ret = encoding.readI16BE(this.data, this.offset);
this.offset += 2;

@@ -296,3 +296,3 @@ return ret;

this.assert(this.offset + 4 <= this.data.length);
const ret = this.data.readInt32LE(this.offset, true);
const ret = encoding.readI32(this.data, this.offset);
this.offset += 4;

@@ -309,3 +309,3 @@ return ret;

this.assert(this.offset + 4 <= this.data.length);
const ret = this.data.readInt32BE(this.offset, true);
const ret = encoding.readI32BE(this.data, this.offset);
this.offset += 4;

@@ -342,50 +342,2 @@ return ret;

/**
* Read uint64le.
* @returns {U64}
*/
readU64N() {
this.assert(this.offset + 8 <= this.data.length);
const ret = encoding.readU64N(this.data, this.offset);
this.offset += 8;
return ret;
}
/**
* Read uint64be.
* @returns {U64}
*/
readU64BEN() {
this.assert(this.offset + 8 <= this.data.length);
const ret = encoding.readU64BEN(this.data, this.offset);
this.offset += 8;
return ret;
}
/**
* Read int64le.
* @returns {I64}
*/
readI64N() {
this.assert(this.offset + 8 <= this.data.length);
const ret = encoding.readI64N(this.data, this.offset);
this.offset += 8;
return ret;
}
/**
* Read int64be.
* @returns {I64}
*/
readI64BEN() {
this.assert(this.offset + 8 <= this.data.length);
const ret = encoding.readI64BEN(this.data, this.offset);
this.offset += 8;
return ret;
}
/**
* Read float le.

@@ -397,3 +349,3 @@ * @returns {Number}

this.assert(this.offset + 4 <= this.data.length);
const ret = this.data.readFloatLE(this.offset, true);
const ret = encoding.readFloat(this.data, this.offset);
this.offset += 4;

@@ -410,3 +362,3 @@ return ret;

this.assert(this.offset + 4 <= this.data.length);
const ret = this.data.readFloatBE(this.offset, true);
const ret = encoding.readFloatBE(this.data, this.offset);
this.offset += 4;

@@ -423,3 +375,3 @@ return ret;

this.assert(this.offset + 8 <= this.data.length);
const ret = this.data.readDoubleLE(this.offset, true);
const ret = encoding.readDouble(this.data, this.offset);
this.offset += 8;

@@ -436,3 +388,3 @@ return ret;

this.assert(this.offset + 8 <= this.data.length);
const ret = this.data.readDoubleBE(this.offset, true);
const ret = encoding.readDoubleBE(this.data, this.offset);
this.offset += 8;

@@ -454,13 +406,2 @@ return ret;

/**
* Read a varint.
* @returns {U64}
*/
readVarintN() {
const {size, value} = encoding.readVarintN(this.data, this.offset);
this.offset += size;
return value;
}
/**
* Read a varint (type 2).

@@ -477,13 +418,2 @@ * @returns {Number}

/**
* Read a varint (type 2).
* @returns {U64}
*/
readVarint2N() {
const {size, value} = encoding.readVarint2N(this.data, this.offset);
this.offset += size;
return value;
}
/**
* Read N bytes (will do a fast slice if zero copy).

@@ -527,2 +457,22 @@ * @param {Number} size

/**
* Slice N bytes and create a child reader.
* @param {Number} size
* @returns {BufferReader}
*/
readChild(size) {
assert(size >= 0);
this.assert(this.offset + size <= this.data.length);
const data = this.data.slice(0, this.offset + size);
const br = new this.constructor(data);
br.offset = this.offset;
this.offset += size;
return br;
}
/**
* Read a string.

@@ -605,3 +555,3 @@ * @param {String} enc - Any buffer-supported encoding.

return hash(data).readUInt32LE(0, true);
return encoding.readU32(hash(data), 0);
}

@@ -608,0 +558,0 @@

@@ -11,2 +11,3 @@ /*!

const encoding = require('./encoding');
const EncodingError = require('./error');

@@ -30,11 +31,34 @@ /*

* @constructor
* @param {Number} size
* @param {Number|Buffer} options
*/
constructor(size = 0) {
this.data = size ? Buffer.allocUnsafe(size) : EMPTY;
constructor(options) {
this.data = EMPTY;
this.offset = 0;
if (options != null)
this.init(options);
}
/**
* Initialize options.
* @param {Object} options
*/
init(options) {
if (Buffer.isBuffer(options)) {
this.data = options;
this.offset = 0;
return this;
}
assert((options >>> 0) === options);
this.data = Buffer.allocUnsafe(options);
this.offset = 0;
return this;
}
/**
* Allocate writer from preallocated 100kb pool.

@@ -64,5 +88,9 @@ * @param {Number} size

render() {
const data = this.data;
assert(this.offset === data.length);
const {data, offset} = this;
if (offset !== data.length)
throw new EncodingError(offset, 'Out of bounds write');
this.destroy();
return data;

@@ -72,2 +100,18 @@ }

/**
* Slice the final buffer at written offset.
* @returns {Buffer} Rendered buffer.
*/
slice() {
const {data, offset} = this;
if (offset > data.length)
throw new EncodingError(offset, 'Out of bounds write');
this.destroy();
return data.slice(0, offset);
}
/**
* Get size of data written so far.

@@ -107,3 +151,3 @@ * @returns {Number}

writeU8(value) {
this.offset = this.data.writeUInt8(value, this.offset, true);
this.offset = encoding.writeU8(this.data, value, this.offset);
return this;

@@ -118,3 +162,3 @@ }

writeU16(value) {
this.offset = this.data.writeUInt16LE(value, this.offset, true);
this.offset = encoding.writeU16(this.data, value, this.offset);
return this;

@@ -129,3 +173,3 @@ }

writeU16BE(value) {
this.offset = this.data.writeUInt16BE(value, this.offset, true);
this.offset = encoding.writeU16BE(this.data, value, this.offset);
return this;

@@ -140,3 +184,3 @@ }

writeU32(value) {
this.offset = this.data.writeUInt32LE(value, this.offset, true);
this.offset = encoding.writeU32(this.data, value, this.offset);
return this;

@@ -151,3 +195,3 @@ }

writeU32BE(value) {
this.offset = this.data.writeUInt32BE(value, this.offset, true);
this.offset = encoding.writeU32BE(this.data, value, this.offset);
return this;

@@ -177,22 +221,2 @@ }

/**
* Write uint64le.
* @param {U64} value
*/
writeU64N(value) {
this.offset = encoding.writeU64N(this.data, value, this.offset);
return this;
}
/**
* Write uint64be.
* @param {U64} value
*/
writeU64BEN(value) {
this.offset = encoding.writeU64BEN(this.data, value, this.offset);
return this;
}
/**
* Write int8.

@@ -203,3 +227,3 @@ * @param {Number} value

writeI8(value) {
this.offset = this.data.writeInt8(value, this.offset, true);
this.offset = encoding.writeI8(this.data, value, this.offset);
return this;

@@ -214,3 +238,3 @@ }

writeI16(value) {
this.offset = this.data.writeInt16LE(value, this.offset, true);
this.offset = encoding.writeI16(this.data, value, this.offset);
return this;

@@ -225,3 +249,3 @@ }

writeI16BE(value) {
this.offset = this.data.writeInt16BE(value, this.offset, true);
this.offset = encoding.writeI16BE(this.data, value, this.offset);
return this;

@@ -236,3 +260,3 @@ }

writeI32(value) {
this.offset = this.data.writeInt32LE(value, this.offset, true);
this.offset = encoding.writeI32(this.data, value, this.offset);
return this;

@@ -247,3 +271,3 @@ }

writeI32BE(value) {
this.offset = this.data.writeInt32BE(value, this.offset, true);
this.offset = encoding.writeI32BE(this.data, value, this.offset);
return this;

@@ -273,22 +297,2 @@ }

/**
* Write int64le.
* @param {I64} value
*/
writeI64N(value) {
this.offset = encoding.writeI64N(this.data, value, this.offset);
return this;
}
/**
* Write int64be.
* @param {I64} value
*/
writeI64BEN(value) {
this.offset = encoding.writeI64BEN(this.data, value, this.offset);
return this;
}
/**
* Write float le.

@@ -299,3 +303,3 @@ * @param {Number} value

writeFloat(value) {
this.offset = this.data.writeFloatLE(value, this.offset, true);
this.offset = encoding.writeFloat(this.data, value, this.offset);
return this;

@@ -310,3 +314,3 @@ }

writeFloatBE(value) {
this.offset = this.data.writeFloatBE(value, this.offset, true);
this.offset = encoding.writeFloatBE(this.data, value, this.offset);
return this;

@@ -321,3 +325,3 @@ }

writeDouble(value) {
this.offset = this.data.writeDoubleLE(value, this.offset, true);
this.offset = encoding.writeDouble(this.data, value, this.offset);
return this;

@@ -332,3 +336,3 @@ }

writeDoubleBE(value) {
this.offset = this.data.writeDoubleBE(value, this.offset, true);
this.offset = encoding.writeDoubleBE(this.data, value, this.offset);
return this;

@@ -348,12 +352,2 @@ }

/**
* Write a varint.
* @param {U64} value
*/
writeVarintN(value) {
this.offset = encoding.writeVarintN(this.data, value, this.offset);
return this;
}
/**
* Write a varint (type 2).

@@ -369,12 +363,2 @@ * @param {Number} value

/**
* Write a varint (type 2).
* @param {U64} value
*/
writeVarint2N(value) {
this.offset = encoding.writeVarint2N(this.data, value, this.offset);
return this;
}
/**
* Write bytes.

@@ -381,0 +365,0 @@ * @param {Buffer} value

@@ -12,2 +12,3 @@ /*!

const encoding = require('./encoding');
const EncodingError = require('./error');

@@ -19,32 +20,26 @@ /*

const SEEK = 0;
const UI8 = 1;
const UI16 = 2;
const UI16BE = 3;
const UI32 = 4;
const UI32BE = 5;
const UI64 = 6;
const UI64BE = 7;
const UI64N = 8;
const UI64BEN = 9;
const I8 = 10;
const I16 = 11;
const I16BE = 12;
const I32 = 13;
const I32BE = 14;
const I64 = 15;
const I64BE = 16;
const I64N = 17;
const I64BEN = 18;
const FL = 19;
const FLBE = 20;
const DBL = 21;
const DBLBE = 22;
const VARINT = 23;
const VARINTN = 24;
const VARINT2 = 25;
const VARINT2N = 26;
const BYTES = 27;
const STR = 28;
const CHECKSUM = 29;
const FILL = 30;
const U8 = 1;
const U16 = 2;
const U16BE = 3;
const U32 = 4;
const U32BE = 5;
const U64 = 6;
const U64BE = 7;
const I8 = 8;
const I16 = 9;
const I16BE = 10;
const I32 = 11;
const I32BE = 12;
const I64 = 13;
const I64BE = 14;
const FL = 15;
const FLBE = 16;
const DBL = 17;
const DBLBE = 18;
const VARINT = 19;
const VARINT2 = 20;
const BYTES = 21;
const STR = 22;
const CHECKSUM = 23;
const FILL = 24;

@@ -81,43 +76,37 @@ /**

break;
case UI8:
off = data.writeUInt8(op.value, off, true);
case U8:
off = encoding.writeU8(data, op.value, off);
break;
case UI16:
off = data.writeUInt16LE(op.value, off, true);
case U16:
off = encoding.writeU16(data, op.value, off);
break;
case UI16BE:
off = data.writeUInt16BE(op.value, off, true);
case U16BE:
off = encoding.writeU16BE(data, op.value, off);
break;
case UI32:
off = data.writeUInt32LE(op.value, off, true);
case U32:
off = encoding.writeU32(data, op.value, off);
break;
case UI32BE:
off = data.writeUInt32BE(op.value, off, true);
case U32BE:
off = encoding.writeU32BE(data, op.value, off);
break;
case UI64:
case U64:
off = encoding.writeU64(data, op.value, off);
break;
case UI64BE:
case U64BE:
off = encoding.writeU64BE(data, op.value, off);
break;
case UI64N:
off = encoding.writeU64N(data, op.value, off);
break;
case UI64BEN:
off = encoding.writeU64BEN(data, op.value, off);
break;
case I8:
off = data.writeInt8(op.value, off, true);
off = encoding.writeI8(data, op.value, off);
break;
case I16:
off = data.writeInt16LE(op.value, off, true);
off = encoding.writeI16(data, op.value, off);
break;
case I16BE:
off = data.writeInt16BE(op.value, off, true);
off = encoding.writeI16BE(data, op.value, off);
break;
case I32:
off = data.writeInt32LE(op.value, off, true);
off = encoding.writeI32(data, op.value, off);
break;
case I32BE:
off = data.writeInt32BE(op.value, off, true);
off = encoding.writeI32BE(data, op.value, off);
break;

@@ -130,19 +119,13 @@ case I64:

break;
case I64N:
off = encoding.writeI64N(data, op.value, off);
break;
case I64BEN:
off = encoding.writeI64BEN(data, op.value, off);
break;
case FL:
off = data.writeFloatLE(op.value, off, true);
off = encoding.writeFloat(data, op.value, off);
break;
case FLBE:
off = data.writeFloatBE(op.value, off, true);
off = encoding.writeFloatBE(data, op.value, off);
break;
case DBL:
off = data.writeDoubleLE(op.value, off, true);
off = encoding.writeDouble(data, op.value, off);
break;
case DBLBE:
off = data.writeDoubleBE(op.value, off, true);
off = encoding.writeDoubleBE(data, op.value, off);
break;

@@ -152,11 +135,5 @@ case VARINT:

break;
case VARINTN:
off = encoding.writeVarintN(data, op.value, off);
break;
case VARINT2:
off = encoding.writeVarint2(data, op.value, off);
break;
case VARINT2N:
off = encoding.writeVarint2N(data, op.value, off);
break;
case BYTES:

@@ -181,3 +158,4 @@ off += op.data.copy(data, off);

assert(off === data.length);
if (off !== data.length)
throw new EncodingError(off, 'Out of bounds write');

@@ -226,3 +204,3 @@ this.destroy();

this.offset += 1;
this.ops.push(new NumberOp(UI8, value));
this.ops.push(new NumberOp(U8, value));
return this;

@@ -238,3 +216,3 @@ }

this.offset += 2;
this.ops.push(new NumberOp(UI16, value));
this.ops.push(new NumberOp(U16, value));
return this;

@@ -250,3 +228,3 @@ }

this.offset += 2;
this.ops.push(new NumberOp(UI16BE, value));
this.ops.push(new NumberOp(U16BE, value));
return this;

@@ -262,3 +240,3 @@ }

this.offset += 4;
this.ops.push(new NumberOp(UI32, value));
this.ops.push(new NumberOp(U32, value));
return this;

@@ -274,3 +252,3 @@ }

this.offset += 4;
this.ops.push(new NumberOp(UI32BE, value));
this.ops.push(new NumberOp(U32BE, value));
return this;

@@ -286,3 +264,3 @@ }

this.offset += 8;
this.ops.push(new NumberOp(UI64, value));
this.ops.push(new NumberOp(U64, value));
return this;

@@ -298,3 +276,3 @@ }

this.offset += 8;
this.ops.push(new NumberOp(UI64BE, value));
this.ops.push(new NumberOp(U64BE, value));
return this;

@@ -304,24 +282,2 @@ }

/**
* Write uint64le.
* @param {U64} value
*/
writeU64N(value) {
this.offset += 8;
this.ops.push(new NumberOp(UI64N, value));
return this;
}
/**
* Write uint64be.
* @param {U64} value
*/
writeU64BEN(value) {
this.offset += 8;
this.ops.push(new NumberOp(UI64BEN, value));
return this;
}
/**
* Write int8.

@@ -404,24 +360,2 @@ * @param {Number} value

/**
* Write int64le.
* @param {I64} value
*/
writeI64N(value) {
this.offset += 8;
this.ops.push(new NumberOp(I64N, value));
return this;
}
/**
* Write int64be.
* @param {I64} value
*/
writeI64BEN(value) {
this.offset += 8;
this.ops.push(new NumberOp(I64BEN, value));
return this;
}
/**
* Write float le.

@@ -482,13 +416,2 @@ * @param {Number} value

/**
* Write a varint.
* @param {U64} value
*/
writeVarintN(value) {
this.offset += encoding.sizeVarintN(value);
this.ops.push(new NumberOp(VARINTN, value));
return this;
}
/**
* Write a varint (type 2).

@@ -505,13 +428,2 @@ * @param {Number} value

/**
* Write a varint (type 2).
* @param {U64} value
*/
writeVarint2N(value) {
this.offset += encoding.sizeVarint2N(value);
this.ops.push(new NumberOp(VARINT2N, value));
return this;
}
/**
* Write bytes.

@@ -518,0 +430,0 @@ * @param {Buffer} value

{
"name": "bufio",
"version": "0.1.0",
"version": "0.2.0",
"description": "Buffer and serialization utilities for javascript",

@@ -22,21 +22,20 @@ "keywords": [

"test": "mocha --reporter spec test/*-test.js",
"webpack": "webpack --config webpack.config.js"
"webpack": "webpack --mode production --config webpack.config.js"
},
"dependencies": {
"n64": "~0.1.1"
},
"devDependencies": {
"babelify": "^8.0.0",
"babel-core": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.6.1",
"browserify": "^16.1.1",
"eslint": "^4.18.2",
"mocha": "^5.0.4",
"uglifyjs-webpack-plugin": "^1.2.3",
"babel-preset-env": "^1.7.0",
"browserify": "^16.2.2",
"eslint": "^4.19.1",
"mocha": "^5.2.0",
"n64": "~0.2.0",
"uglifyjs-webpack-plugin": "^1.2.5",
"uglify-es": "^3.3.9",
"webpack": "^4.1.1"
"webpack": "^4.11.1",
"webpack-cli": "^3.0.3"
},
"engines": {
"node": ">=7.6.0"
"node": ">=8.0.0"
},

@@ -43,0 +42,0 @@ "browserify": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc