-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.js
More file actions
72 lines (59 loc) · 1.84 KB
/
reader.js
File metadata and controls
72 lines (59 loc) · 1.84 KB
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
class BufferReader {
currentPos = 0
buffer;
constructor(buffer) {
this.buffer = buffer
}
readUInt16BE() {
const buff = this.buffer.readUInt16BE(this.currentPos)
this.currentPos +=2;
return buff
}
readUInt32BE() {
const buff = this.buffer.readUInt32BE(this.currentPos)
this.currentPos +=4;
return buff
}
readUInt8() {
const buff = this.buffer.readUInt8(this.currentPos)
this.currentPos +=1;
return buff
}
readRange(len) {
const res = this.buffer.subarray(this.currentPos,this.currentPos+len)
this.currentPos += len
return res
}
getJump(buffer) {
const firstTwoBytes = [buffer.readUInt8(),buffer.readUInt8(1)];
const isPointer = (Buffer.from(firstTwoBytes).readUInt16BE() & 0xC000) === 0xC000;
if (isPointer) {
return Buffer.from(firstTwoBytes).readUInt16BE() ^ Buffer.from([firstTwoBytes[0], 0x00]).readUInt16BE()
}
return 0
}
peekName(start) {
const tmpBuff = this.buffer.subarray(start)
let res = []
let len = tmpBuff.readUInt8()
let hasJumped = false
let currIndex = 0
while (len > 0 && !hasJumped) {
const jump = this.getJump(tmpBuff.subarray(currIndex))
if (jump) {
hasJumped = true
res.push(...this.peekName(jump).split('.'))
} else {
const labelPart = tmpBuff.subarray(currIndex+1,len +currIndex+1);
res.push(labelPart.toString())
currIndex += 1+len
len = tmpBuff.readUInt8(currIndex)
}
}
return res.join('.')
}
peekLeftBytes() {
console.log(this.buffer.subarray(this.currentPos))
}
}
module.exports= {BufferReader}