Skip to content

Commit 9a96a6c

Browse files
committed
refactor small int + big int decoders
1 parent 725c2b2 commit 9a96a6c

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
lines changed

lib/decoder.js

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ function cursor(value, offset) {
3030

3131
module.exports = Decoder;
3232

33-
function Decoder(fileStream, baseOffset) {
34-
assert(this.fileStream = fileStream, 'File stream is required');
33+
function Decoder(db, baseOffset) {
34+
assert(this.db = db, 'File stream is required');
3535
assert(this.baseOffset = baseOffset, 'Base offset is required');
3636
}
3737

3838

3939
Decoder.prototype.decode = function(offset) {
4040
var tmp,
41-
ctrlByte = this.fileStream[offset++],
41+
ctrlByte = this.db[offset++],
4242
type = types[ctrlByte >> 5];
4343

4444

@@ -48,7 +48,7 @@ Decoder.prototype.decode = function(offset) {
4848
}
4949

5050
if (type === 'extended') {
51-
tmp = this.fileStream[offset] + 7;
51+
tmp = this.db[offset] + 7;
5252
if (tmp < 8) {
5353
throw new Error('Maxmind: Invalid Extended Type at offset ' + offset + ' val ' + tmp);
5454
}
@@ -81,7 +81,7 @@ Decoder.prototype.decodeByType = function(type, offset, size) {
8181
case 'map':
8282
return this.decodeMap(size, offset);
8383
case 'uint32':
84-
return cursor(this.decodeUint32(offset, size), newOffset);
84+
return cursor(this.decodeUint(offset, size), newOffset);
8585
case 'double':
8686
return cursor(this.decodeDouble(offset, size), newOffset);
8787
case 'array':
@@ -93,17 +93,16 @@ Decoder.prototype.decodeByType = function(type, offset, size) {
9393
case 'bytes':
9494
return cursor(this.decodeBytes(offset, size), newOffset);
9595
case 'uint16':
96-
return cursor(this.decodeUint32(offset, size), newOffset);
96+
return cursor(this.decodeUint(offset, size), newOffset);
9797
case 'int32':
9898
return cursor(this.decodeInt32(offset, size), newOffset);
9999
case 'uint64':
100-
return cursor(this.decodeUint64(offset, size), newOffset);
100+
return cursor(this.decodeUint(offset, size), newOffset);
101101
case 'uint128':
102-
return cursor(this.decodeUint128(offset, size), newOffset);
103-
104-
default:
105-
throw new Error('Maxmind: Unknown or unexpected type ' + type + ' at offset ' + offset);
102+
return cursor(this.decodeUint(offset, size), newOffset);
106103
}
104+
105+
throw new Error('Maxmind: Unknown or unexpected type ' + type + ' at offset ' + offset);
107106
};
108107

109108

@@ -133,25 +132,25 @@ Decoder.prototype.sizeFromCtrlByte = function(ctrlByte, offset) {
133132
// If the value is 29, then the size is 29 + *the next byte after the type
134133
// specifying bytes as an unsigned integer*.
135134
if (size === 29)
136-
return cursor(29 + this.fileStream[offset], offset + 1);
135+
return cursor(29 + this.db[offset], offset + 1);
137136

138137
// If the value is 30, then the size is 285 + *the next two bytes after the type
139138
// specifying bytes as a single unsigned integer*.
140139
if (size === 30)
141-
return cursor(285 + this.fileStream.readUInt16BE(offset, false), offset + 2);
140+
return cursor(285 + this.db.readUInt16BE(offset, false), offset + 2);
142141

143142
// If the value is 31, then the size is 65,821 + *the next three bytes after the
144143
// type specifying bytes as a single unsigned integer*.
145144
if (size === 31)
146145
return cursor(
147-
65821 + utils.concat3(this.fileStream[offset], this.fileStream[offset + 1], this.fileStream[offset + 2]),
146+
65821 + utils.concat3(this.db[offset], this.db[offset + 1], this.db[offset + 2]),
148147
offset + 3
149148
);
150149
};
151150

152151

153152
Decoder.prototype.decodeBytes = function(offset, size) {
154-
return this.fileStream.slice(offset, offset + size)
153+
return this.db.slice(offset, offset + size)
155154
};
156155

157156

@@ -171,23 +170,23 @@ Decoder.prototype.decodePointer = function(ctrlByte, offset) {
171170
// If the size is 0, the pointer is built by appending the next byte to the last
172171
// three bits to produce an 11-bit value.
173172
if (pointerSize === 0) {
174-
packed = utils.concat2(ctrlByte & 7, this.fileStream[offset]);
173+
packed = utils.concat2(ctrlByte & 7, this.db[offset]);
175174

176175
// If the size is 1, the pointer is built by appending the next two bytes to the
177176
// last three bits to produce a 19-bit value + 2048.
178177
} else if (pointerSize === 1) {
179-
packed = utils.concat3(ctrlByte & 7, this.fileStream[offset], this.fileStream[offset + 1]);
178+
packed = utils.concat3(ctrlByte & 7, this.db[offset], this.db[offset + 1]);
180179

181180
// If the size is 2, the pointer is built by appending the next three bytes to the
182181
// last three bits to produce a 27-bit value + 526336.
183182
} else if (pointerSize === 2) {
184-
packed = utils.concat4(ctrlByte & 7, this.fileStream[offset], this.fileStream[offset + 1], this.fileStream[offset + 2]);
183+
packed = utils.concat4(ctrlByte & 7, this.db[offset], this.db[offset + 1], this.db[offset + 2]);
185184

186185
// Finally, if the size is 3, the pointer's value is contained in the next four
187186
// bytes as a 32-bit value. In this case, the last three bits of the control byte
188187
// are ignored.
189188
} else if (pointerSize === 3) {
190-
packed = this.fileStream.readUInt32BE(offset, true);
189+
packed = this.db.readUInt32BE(offset, true);
191190
}
192191

193192
offset += pointerSize + 1;
@@ -215,12 +214,12 @@ Decoder.prototype.decodeBoolean = function(size) {
215214

216215

217216
Decoder.prototype.decodeDouble = function(offset, size) {
218-
return this.fileStream.readDoubleBE(offset, true);
217+
return this.db.readDoubleBE(offset, true);
219218
};
220219

221220

222221
Decoder.prototype.decodeFloat = function(offset, size) {
223-
return this.fileStream.readFloatBE(offset, true);
222+
return this.db.readFloatBE(offset, true);
224223
};
225224

226225

@@ -243,36 +242,30 @@ Decoder.prototype.decodeMap = function(size, offset) {
243242

244243
Decoder.prototype.decodeInt32 = function(offset, size) {
245244
if (size == 0) return 0;
246-
return this.fileStream.readInt32BE(offset, true);
247-
};
248-
249-
250-
Decoder.prototype.decodeUint32 = function(offset, size) {
251-
if (size == 0) return 0;
252-
if (size == 1) return this.fileStream[offset];
253-
if (size == 2) return utils.concat2(this.fileStream[offset + 0], this.fileStream[offset + 1]);
254-
if (size == 3) return utils.concat3(this.fileStream[offset + 0], this.fileStream[offset + 1], this.fileStream[offset + 2]);
255-
if (size == 4) return utils.concat4(this.fileStream[offset + 0], this.fileStream[offset + 1], this.fileStream[offset + 2], this.fileStream[offset + 3]);
245+
return this.db.readInt32BE(offset, true);
256246
};
257247

258248

259-
Decoder.prototype.decodeUint64 = function(offset, size) {
260-
return this.decodeBigUint(offset, size, 8);
261-
};
262-
263-
264-
Decoder.prototype.decodeUint128 = function(offset, size) {
265-
return this.decodeBigUint(offset, size, 16);
249+
Decoder.prototype.decodeUint = function(offset, size) {
250+
switch (size) {
251+
case 0: return 0;
252+
case 1: return this.db[offset];
253+
case 2: return utils.concat2(this.db[offset + 0], this.db[offset + 1]);
254+
case 3: return utils.concat3(this.db[offset + 0], this.db[offset + 1], this.db[offset + 2]);
255+
case 4: return utils.concat4(this.db[offset + 0], this.db[offset + 1], this.db[offset + 2], this.db[offset + 3]);
256+
case 8: return this.decodeBigUint(offset, size);
257+
case 16: return this.decodeBigUint(offset, size);
258+
}
259+
return 0;
266260
};
267261

268262

269-
Decoder.prototype.decodeBigUint = function(offset, size, intSize) {
270-
var buffer = new Buffer(intSize);
271-
buffer.fill(0);
272-
this.fileStream.copy(buffer, intSize - size, offset, offset + intSize);
263+
Decoder.prototype.decodeBigUint = function(offset, size) {
264+
var buffer = new Buffer(size);
265+
this.db.copy(buffer, 0, offset, offset + size);
273266

274267
var integer = 0;
275-
var numberOfLongs = intSize / 4;
268+
var numberOfLongs = size / 4;
276269
for (var i = 0; i < numberOfLongs; i++) {
277270
integer = bigInt(integer).multiply(4294967296).add(buffer.readUInt32BE(i << 2, true));
278271
}
@@ -282,5 +275,5 @@ Decoder.prototype.decodeBigUint = function(offset, size, intSize) {
282275

283276

284277
Decoder.prototype.decodeString = function(offset, size) {
285-
return this.fileStream.utf8Slice(offset, offset + size);
278+
return this.db.utf8Slice(offset, offset + size);
286279
};

0 commit comments

Comments
 (0)