@esutils/dns-packet
Version:
A minimal dns-packet library that implemented in `typescript`
62 lines • 1.77 kB
JavaScript
/* eslint-disable no-bitwise */
Object.defineProperty(exports, "__esModule", { value: true });
exports.BufferReader = void 0;
function p(a) {
let n = 0;
const f = a.length - 1;
for (let i = f; i >= 0; i -= 1) {
if (a[f - i])
n += 2 ** i;
}
return n;
}
/**
* [Reader description]
* @param {[type]} buffer [description]
* @param {[type]} offset [description]
*/
class BufferReader {
buffer;
offset;
constructor(buffer, offset) {
this.buffer = buffer;
this.offset = offset || 0;
}
/**
* [read description]
* @param {[type]} buffer [description]
* @param {[type]} offset [description]
* @param {[type]} length [description]
* @return {[type]} [description]
*/
static read(buffer, offset, length) {
let a = [];
let c = Math.ceil(length / 8);
let l = Math.floor(offset / 8);
const m = offset % 8;
function t(n) {
const r = [0, 0, 0, 0, 0, 0, 0, 0];
for (let i = 7; i >= 0; i -= 1) {
r[7 - i] = n & 2 ** i ? 1 : 0;
}
a = a.concat(r);
}
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
while (c--)
t(view.getUint8(l++));
return p(a.slice(m, m + length));
}
/**
* [read description]
* @param {[type]} size [description]
* @return {[type]} [description]
*/
read(size) {
const val = BufferReader.read(this.buffer, this.offset, size);
this.offset += size;
return val;
}
}
exports.BufferReader = BufferReader;
//# sourceMappingURL=reader.js.map
;