bitcore-lib-zcash/lib/bufferreader.js

91 lines
2.1 KiB
JavaScript
Raw Normal View History

2014-08-18 18:04:47 -07:00
var BufferReader = function BufferReader(buf, pos) {
if (!(this instanceof BufferReader))
return new BufferReader(buf);
this.buf = buf;
this.pos = pos || 0;
};
BufferReader.prototype.eof = function eof() {
return this.pos >= this.buf.length;
};
BufferReader.prototype.read = function() {
var buf = this.buf.slice(this.pos);
this.pos = this.buf.length;
return buf;
};
BufferReader.prototype.readUInt8 = function() {
var val = this.buf.readUInt8(this.pos);
this.pos = this.pos + 1;
return val;
};
BufferReader.prototype.readUInt16BE = function() {
var val = this.buf.readUInt16BE(this.pos);
this.pos = this.pos + 2;
return val;
};
BufferReader.prototype.readUInt16LE = function() {
var val = this.buf.readUInt16LE(this.pos);
this.pos = this.pos + 2;
return val;
};
BufferReader.prototype.readUInt32BE = function() {
var val = this.buf.readUInt32BE(this.pos);
this.pos = this.pos + 4;
return val;
};
BufferReader.prototype.readUInt32LE = function() {
var val = this.buf.readUInt32LE(this.pos);
this.pos = this.pos + 4;
return val;
};
//TODO: What if n is so large that it loses precision?
BufferReader.prototype.readUInt64BE = function() {
var val = 0;
for (var i = 0; i < 8; i++) {
val += Math.pow(256, i) * this.buf[this.pos + 8 - 1 - i];
}
this.pos = this.pos + 8;
return val;
};
//TODO: What if n is so large that it loses precision?
BufferReader.prototype.readUInt64LE = function() {
var val = 0;
for (var i = 0; i < 8; i++) {
val += Math.pow(256, i) * this.buf[this.pos + i];
}
this.pos = this.pos + 8;
return val;
};
BufferReader.prototype.readVarInt = function() {
var first = this.readUInt8();
switch (first) {
case 0xFD:
return this.readUInt16LE();
case 0xFE:
return this.readUInt32LE();
case 0xFF:
return this.readUInt64LE();
default:
return first;
}
};
BufferReader.prototype.reverse = function() {
var buf = new Buffer(this.buf.length);
for (var i = 0; i < buf.length; i++)
buf[i] = this.buf[this.buf.length - 1 - i]
this.buf = buf;
return this;
};
module.exports = BufferReader;