node-gamedig/lib/reader.js

172 lines
5 KiB
JavaScript
Raw Normal View History

const Iconv = require('iconv-lite'),
Long = require('long'),
Core = require('../protocols/core'),
2019-01-10 13:03:07 +01:00
Buffer = require('buffer'),
Varint = require('varint');
2014-10-29 08:02:03 +01:00
function readUInt64BE(buffer,offset) {
const high = buffer.readUInt32BE(offset);
const low = buffer.readUInt32BE(offset+4);
2017-08-09 12:32:09 +02:00
return new Long(low,high,true);
2014-10-29 08:02:03 +01:00
}
function readUInt64LE(buffer,offset) {
const low = buffer.readUInt32LE(offset);
const high = buffer.readUInt32LE(offset+4);
2017-08-09 12:32:09 +02:00
return new Long(low,high,true);
2014-10-29 08:02:03 +01:00
}
class Reader {
/**
* @param {Core} query
* @param {Buffer} buffer
**/
2017-08-09 12:32:09 +02:00
constructor(query,buffer) {
2019-02-05 02:58:28 +01:00
this.defaultEncoding = query.options.encoding || query.encoding;
this.defaultDelimiter = query.delimiter;
this.defaultByteOrder = query.byteorder;
2017-08-09 12:32:09 +02:00
this.buffer = buffer;
this.i = 0;
}
2017-08-09 12:32:09 +02:00
offset() {
return this.i;
}
2017-08-09 12:32:09 +02:00
skip(i) {
this.i += i;
}
2014-10-29 08:02:03 +01:00
2019-02-05 02:58:28 +01:00
pascalString(bytesForSize, adjustment=0) {
const length = this.uint(bytesForSize) + adjustment;
return this.string(length);
}
string(arg) {
let encoding = this.defaultEncoding;
let length = null;
let delimiter = this.defaultDelimiter;
if(typeof arg === 'string') delimiter = arg;
else if(typeof arg === 'number') length = arg;
else if(typeof arg === 'object') {
if ('encoding' in arg) encoding = arg.encoding;
if ('length' in arg) length = arg.length;
if ('delimiter' in arg) delimiter = arg.delimiter;
2017-08-09 12:32:09 +02:00
}
2019-02-05 02:58:28 +01:00
if(encoding === 'latin1') encoding = 'win1252';
2017-08-09 12:32:09 +02:00
2019-02-05 02:58:28 +01:00
const start = this.i;
2017-08-09 12:32:09 +02:00
let end = start;
2019-02-05 02:58:28 +01:00
if(length === null) {
2017-08-09 12:32:09 +02:00
// terminated by the delimiter
2019-02-05 02:58:28 +01:00
let delim = delimiter;
if (typeof delim === 'string') delim = delim.charCodeAt(0);
while (true) {
if (end >= this.buffer.length) {
2017-08-09 12:32:09 +02:00
end = this.buffer.length;
break;
}
2019-02-05 02:58:28 +01:00
if (this.buffer.readUInt8(end) === delim) break;
2017-08-09 12:32:09 +02:00
end++;
}
2019-02-05 02:58:28 +01:00
this.i = end + 1;
} else if (length <= 0) {
return '';
2017-08-09 12:32:09 +02:00
} else {
2019-02-05 02:58:28 +01:00
end = start+length;
2017-08-09 12:32:09 +02:00
if(end >= this.buffer.length) {
end = this.buffer.length;
}
this.i = end;
}
2019-02-05 02:58:28 +01:00
const slice = this.buffer.slice(start, end);
const enc = encoding;
2017-08-09 12:32:09 +02:00
if(enc === 'utf8' || enc === 'ucs2' || enc === 'binary') {
2019-02-05 02:58:28 +01:00
return slice.toString(enc);
2017-08-09 12:32:09 +02:00
} else {
2019-02-05 02:58:28 +01:00
return Iconv.decode(slice,enc);
2017-08-09 12:32:09 +02:00
}
}
int(bytes) {
let r = 0;
if(this.remaining() >= bytes) {
2019-02-05 02:58:28 +01:00
if(this.defaultByteOrder === 'be') {
2017-08-09 12:32:09 +02:00
if(bytes === 1) r = this.buffer.readInt8(this.i);
else if(bytes === 2) r = this.buffer.readInt16BE(this.i);
else if(bytes === 4) r = this.buffer.readInt32BE(this.i);
} else {
if(bytes === 1) r = this.buffer.readInt8(this.i);
else if(bytes === 2) r = this.buffer.readInt16LE(this.i);
else if(bytes === 4) r = this.buffer.readInt32LE(this.i);
}
}
this.i += bytes;
return r;
}
/** @returns {number} */
uint(bytes) {
let r = 0;
if(this.remaining() >= bytes) {
2019-02-05 02:58:28 +01:00
if(this.defaultByteOrder === 'be') {
2017-08-09 12:32:09 +02:00
if(bytes === 1) r = this.buffer.readUInt8(this.i);
else if(bytes === 2) r = this.buffer.readUInt16BE(this.i);
else if(bytes === 4) r = this.buffer.readUInt32BE(this.i);
else if(bytes === 8) r = readUInt64BE(this.buffer,this.i).toString();
} else {
if(bytes === 1) r = this.buffer.readUInt8(this.i);
else if(bytes === 2) r = this.buffer.readUInt16LE(this.i);
else if(bytes === 4) r = this.buffer.readUInt32LE(this.i);
else if(bytes === 8) r = readUInt64LE(this.buffer,this.i).toString();
}
}
this.i += bytes;
return r;
}
float() {
let r = 0;
if(this.remaining() >= 4) {
2019-02-05 02:58:28 +01:00
if(this.defaultByteOrder === 'be') r = this.buffer.readFloatBE(this.i);
2017-08-09 12:32:09 +02:00
else r = this.buffer.readFloatLE(this.i);
}
this.i += 4;
return r;
}
2019-01-10 13:03:07 +01:00
varint() {
const out = Varint.decode(this.buffer, this.i);
this.i += Varint.decode.bytes;
return out;
}
/** @returns Buffer */
2017-08-09 12:32:09 +02:00
part(bytes) {
let r;
if(this.remaining() >= bytes) {
r = this.buffer.slice(this.i,this.i+bytes);
} else {
r = Buffer.from([]);
}
this.i += bytes;
return r;
}
remaining() {
return this.buffer.length-this.i;
}
rest() {
return this.buffer.slice(this.i);
}
done() {
return this.i >= this.buffer.length;
}
}
2014-10-29 08:02:03 +01:00
module.exports = Reader;