-
Notifications
You must be signed in to change notification settings - Fork 0
/
ByteTools_browser_export.js
169 lines (117 loc) · 2.44 KB
/
ByteTools_browser_export.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* ByteTools.js: Browser
* Copyright (c) 2020 PROPHESSOR
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
export default class ByteTools {
/**
*
* @param {DataView} view - DataView (new DataView(ArrayBuffer))
*/
constructor(view) {
this.buffer = view;
this.offset = 0;
}
// basics
get length() {
return this.buffer.byteLength;
}
tell() {
return this.offset;
}
/**
*
* @param {number} offset
* @param {'START'|'CUR'} mode
*/
seek(offset, mode = 'START') {
switch (mode) {
case 'CUR':
return this.offset += offset;
case 'START':
default:
return this.offset = offset;
}
}
// read
readString(length = Infinity, trimZeroEnding = true) {
let string = '';
for (let i = 0; i < length; i++) {
const char = this.readUInt8();
if (length === Infinity && char === 0) return string;
string += String.fromCharCode(char);
}
if (trimZeroEnding) return string.replace(/\x00/g, '');
return string;
}
// int8
readInt8() {
const tmp = this.buffer.getInt8(this.offset);
this.offset += 1;
return tmp;
}
readUInt8() {
const tmp = this.buffer.getUint8(this.offset);
this.offset += 1;
return tmp;
}
// int16
readInt16() {
const tmp = this.buffer.getInt16(this.offset, true);
this.offset += 2;
return tmp;
}
readUInt16() {
const tmp = this.buffer.getUint16(this.offset, true);
this.offset += 2;
return tmp;
}
// int32
readInt32() {
const tmp = this.buffer.getInt32(this.offset, true);
this.offset += 4;
return tmp;
}
readUInt32() {
const tmp = this.buffer.getUint32(this.offset, true);
this.offset += 4;
return tmp;
}
// seek
// int8
seekInt8() {
return this.buffer.getInt8(this.offset);
}
seekUInt8() {
return this.buffer.getUint8(this.offset);
}
// int16
seekInt16() {
return this.buffer.getInt16(this.offset, true);
}
seekUInt16() {
return this.buffer.getUint16(this.offset, true);
}
// int32
seekInt32() {
return this.buffer.getInt32(this.offset, true);
}
seekUInt32() {
return this.buffer.getUint32(this.offset, true);
}
// skip
// int8
skipInt8() {
return this.offset += 1;
}
// int16
skipInt16() {
return this.offset += 2;
}
// int32
skipInt32() {
return this.offset += 4;
}
}