-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.js
executable file
·96 lines (79 loc) · 2.12 KB
/
reader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var assert = require('assert');
var Reader = function(buf) {
if (Buffer.isBuffer(buf)) {
this.buf = buf;
} else {
this.buf = new Buffer(buf);
}
this.offset = 0;
}
Reader.prototype.readInt8 = function() {
var v = this.buf.readInt8(this.offset);
this.offset += 1;
return v;
}
Reader.prototype.readUint8 = function() {
var v = this.buf.readUInt8(this.offset);
this.offset += 1;
return v;
}
Reader.prototype.readByte = Reader.prototype.readUint8;
Reader.prototype.readInt16 = function() {
var v = this.buf.readInt16BE(this.offset);
this.offset += 2;
return v;
}
Reader.prototype.readUint16 = function() {
var v = this.buf.readUInt16BE(this.offset);
this.offset += 2;
return v;
}
Reader.prototype.readInt32 = function() {
var v = this.buf.readInt32BE(this.offset);
this.offset += 4;
return v;
}
Reader.prototype.readUint32 = function() {
var v = this.buf.readUInt32BE(this.offset);
this.offset += 4;
return v;
}
Reader.prototype.readInt64 = function() {
throw 'not yet implemented'
}
Reader.prototype.readUint64 = function() {
var a = this.buf.readUInt32BE(this.offset);
var b = this.buf.readUInt32BE(this.offset + 4);
a *= 0x100000000;
this.offset += 8;
verifuint(a + b, 0x001FFFFFFFFFFFFF);
return a + b;
}
Reader.prototype.readVarint = function() {
throw 'not yet implemented'
}
Reader.prototype.readUvarint = function() {
var vLen = this.readUint8();
var v = 0;
for (var i = 0; i < vLen; i++) {
var next = this.readUint8();
v = v * 256 + next;
}
verifuint(v, 0x001FFFFFFFFFFFFF);
return v;
}
Reader.prototype.readString = function() {
var length = this.readUvarint();
var v = this.buf.toString('utf8', this.offset, this.offset + length);
this.offset += length;
return v;
}
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');
}
module.exports = {
Reader: Reader,
};