-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.js
executable file
·128 lines (108 loc) · 3.12 KB
/
writer.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var assert = require('assert');
var Writer = function(buf) {
if (typeof buf == 'undefined') {
buf = new Buffer(1024);
}
this.buf = buf;
this.offset = 0;
}
Writer.prototype.getBuffer = function() {
return this.buf.slice(0, this.offset);
}
Writer.prototype.ensureBuf = function(needBytes) {
if (this.buf.length < this.offset + needBytes) {
var newBuf = new Buffer(this.buf.length + 1024);
this.buf = Buffer.concat([this.buf, newBuf]);
}
}
Writer.prototype.writeInt8 = function(v) {
this.ensureBuf(1);
this.buf.writeInt8(v, this.offset);
this.offset += 1;
}
Writer.prototype.writeUint8 = function(v) {
this.ensureBuf(1);
this.buf.writeUInt8(v, this.offset);
this.offset += 1;
}
Writer.prototype.writeByte = Writer.prototype.writeUint8;
Writer.prototype.writeInt16 = function(v) {
this.ensureBuf(2);
this.buf.writeInt16BE(v, this.offset);
this.offset += 2;
}
Writer.prototype.writeUint16 = function(v) {
this.ensureBuf(2);
this.buf.writeUInt16BE(v, this.offset);
this.offset += 2;
}
Writer.prototype.writeInt32 = function(v) {
this.ensureBuf(4);
this.buf.writeInt32BE(v, this.offset);
this.offset += 4;
}
Writer.prototype.writeUint32 = function(v) {
this.ensureBuf(4);
this.buf.writeUInt32BE(v, this.offset);
this.offset += 4;
}
Writer.prototype.writeInt64 = function(v) {
throw 'not yet implemented';
}
Writer.prototype.writeUint64 = function(v) {
this.ensureBuf(8);
verifuint(v, 0x001FFFFFFFFFFFFf);
this.buf.writeUInt32BE(Math.floor(v / 0x100000000), this.offset);
this.buf.writeInt32BE(v & -1, this.offset + 4); // Nice trick.
this.offset += 8;
}
function uvarintSize(i) {
verifuint(i, 0x001FFFFFFFFFFFFf);
return i < 0x100 ? 1
: i < 0x0000000000010000 ? 2
: i < 0x0000000001000000 ? 3
: i < 0x0000000100000000 ? 4
: i < 0x0000010000000000 ? 5
: i < 0x0001000000000000 ? 6
: i < 0x0100000000000000 ? 7
: 8;
}
Writer.prototype.writeVarint = function(v) {
throw 'not yet implemented';
}
Writer.prototype.writeUvarint = function(v) {
var vLen = uvarintSize(v);
this.writeUint8(vLen);
var bytes = [];
for (var i = 0; i < vLen; i++) {
var remainder = v % 256;
bytes.push(remainder);
v = (v - remainder) / 256;
}
for (var i = 0; i < vLen; i++) {
this.writeUint8(bytes[vLen - i - 1]);
}
}
Writer.prototype.writeString = function(v) {
this.writeUvarint(v.length);
var utf8Buf = new Buffer(v, 'utf8');
if (this.buf.length - this.offset >= utf8Buf.length) {
for (var i = 0; i < utf8Buf.length; i++) {
this.buf[this.offset+i] = utf8Buf[i];
}
this.offset += utf8Buf.length;
} else {
this.buf = Buffer.concat([this.buf, utf8Buf]);
this.offset += utf8Buf.length;
}
}
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 = {
Writer: Writer,
uvarintSize: uvarintSize,
};