-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.ts
59 lines (59 loc) · 1.28 KB
/
reader.ts
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
/**
* Byte stream reader helper.
*
* This class exposes several helper functions for reading SSH structured data.
*/
export class Reader {
private pos = 0;
constructor(private view: DataView) {
this.view = view;
}
readUint8() {
return this.view.getUint8(this.pos++);
}
readUint32() {
const v = this.peekUint32();
this.pos += 4;
return v;
}
peekUint32() {
return this.view.getUint32(this.pos);
}
peekBytes(pos: number, num: number) {
const dv = new DataView(
this.view.buffer,
pos + this.view.byteOffset,
num,
);
return new Reader(dv);
}
readBytes(num: number) {
const reader = this.peekBytes(this.pos, num);
this.pos += num;
return reader;
}
readString() {
const len = this.readUint32();
return this.readBytes(len);
}
peekString() {
const len = this.peekUint32();
return this.peekBytes(this.pos + 4, len);
}
toString() {
let s = "";
for (let i = 0; i < this.view.byteLength; i++) {
s += String.fromCharCode(this.view.getUint8(i));
}
return s;
}
bytes() {
return this.view.buffer.slice(
this.view.byteOffset + this.pos,
this.view.byteOffset + this.view.byteLength,
);
}
get isAtEnd() {
return this.pos == this.view.byteLength;
}
}