forked from steven4547466/Rovird-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteReader.js
More file actions
213 lines (176 loc) · 5.69 KB
/
Copy pathByteReader.js
File metadata and controls
213 lines (176 loc) · 5.69 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Credit: AntiBoomz's BTRoblox Extension
// https://chrome.google.com/webstore/detail/btroblox-making-roblox-be/hbkpclpemjeibhioopcebchdmohaieln?hl=en-US
"use strict"
const { bufferToString } = require("./utils")
class ByteReader extends Uint8Array {
static ParseFloat(long) {
const exp = (long >>> 23) & 255
if (exp === 0) { return 0 }
const float = 2 ** (exp - 127) * (1 + (long & 0x7FFFFF) / 0x7FFFFF)
return long > 0x7FFFFFFF ? -float : float
}
static ParseRBXFloat(long) {
const exp = long >>> 24
if (exp === 0) { return 0 }
const float = 2 ** (exp - 127) * (1 + ((long >>> 1) & 0x7FFFFF) / 0x7FFFFF)
return long & 1 ? -float : float
}
static ParseDouble(long0, long1) {
const exp = (long0 >>> 20) & 0x7FF
const frac = (((long0 & 1048575) * 4294967296) + long1) / 4503599627370496
const neg = long0 & 2147483648
if (exp === 0) {
if (frac === 0) { return -0 }
const double = 2 ** (exp - 1023) * frac
return neg ? -double : double
} else if (exp === 2047) {
return frac === 0 ? Infinity : NaN
}
const double = 2 ** (exp - 1023) * (1 + frac)
return neg ? -double : double
}
constructor(buffer) {
if (buffer instanceof Uint8Array) {
super(buffer.buffer)
} else {
if (!(buffer instanceof ArrayBuffer)) {
throw new Error("buffer is not an ArrayBuffer")
}
super(buffer)
}
this.index = 0
}
SetIndex(n) { this.index = n }
GetIndex() { return this.index }
GetRemaining() { return this.length - this.index }
GetLength() { return this.length }
Jump(n) { this.index += n }
Clone() {
const clone = new ByteReader(this)
clone.SetIndex(this.index)
return clone
}
Array(n) {
const result = new Uint8Array(this.buffer, this.index, n)
this.index += n
return result
}
Match(arr) {
const begin = this.index
this.index += arr.length
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== this[begin + i]) { return false }
}
return true
}
Byte() { return this[this.index++] }
UInt8() { return this[this.index++] }
UInt16LE() { return this[this.index++] + (this[this.index++] * 256) }
UInt16BE() { return (this[this.index++] * 256) + this[this.index++] }
UInt32LE() { return this[this.index++] + (this[this.index++] * 256) + (this[this.index++] * 65536) + (this[this.index++] * 16777216) }
UInt32BE() { return (this[this.index++] * 16777216) + (this[this.index++] * 65536) + (this[this.index++] * 256) + this[this.index++] }
Int8() { return (this[this.index++]) << 24 >> 24 }
Int16LE() { return (this[this.index++] + (this[this.index++] * 256)) << 16 >> 16 }
Int16BE() { return ((this[this.index++] * 256) + this[this.index++]) << 16 >> 16 }
Int32LE() { return (this[this.index++] + (this[this.index++] * 256) + (this[this.index++] * 65536) + (this[this.index++] * 16777216)) >> 0 }
Int32BE() { return ((this[this.index++] * 16777216) + (this[this.index++] * 65536) + (this[this.index++] * 256) + this[this.index++]) >> 0 }
FloatLE() { return ByteReader.ParseFloat(this.UInt32LE()) }
FloatBE() { return ByteReader.ParseFloat(this.UInt32BE()) }
DoubleLE() {
const byte = this.UInt32LE()
return ByteReader.ParseDouble(this.UInt32LE(), byte)
}
DoubleBE() { return ByteReader.ParseDouble(this.UInt32BE(), this.UInt32BE()) }
String(n) {
const i = this.index
this.index += n
return bufferToString(new Uint8Array(this.buffer, i, n))
}
// Custom stuff
LZ4() {
const comLength = this.UInt32LE()
const decomLength = this.UInt32LE()
this.Jump(4)
if (comLength === 0) { // TOOD: This path is actually not supported by Roblox, may have to take a look at some point?
return this.Array(decomLength)
}
const start = this.index
const end = start + comLength
const data = new Uint8Array(decomLength)
let index = 0
while (this.index < end) {
const token = this.Byte()
let litLen = token >>> 4
if (litLen === 0xF) {
while (true) {
const lenByte = this.Byte()
litLen += lenByte
if (lenByte !== 0xFF) { break }
}
}
for (let i = 0; i < litLen; i++) {
data[index++] = this.Byte()
}
if (this.index < end) {
const offset = this.UInt16LE()
let len = token & 0xF
if (len === 0xF) {
while (true) {
const lenByte = this.Byte()
len += lenByte
if (lenByte !== 0xFF) { break }
}
}
len += 4
const begin = index - offset
for (let i = 0; i < len; i++) {
data[index++] = data[begin + i]
}
}
}
if (this.index !== end) throw new Error("[ByteReader.LZ4] LZ4 size mismatch")
return data
}
// RBX
RBXFloatLE() { return ByteReader.ParseRBXFloat(this.UInt32LE()) }
RBXFloatBE() { return ByteReader.ParseRBXFloat(this.UInt32BE()) }
RBXInterleavedUint32(count, fn) {
const result = new Array(count)
const byteCount = count * 4
for (let i = 0; i < count; i++) {
const value = (this[this.index + i] << 24)
+ (this[this.index + (i + count) % byteCount] << 16)
+ (this[this.index + (i + count * 2) % byteCount] << 8)
+ this[this.index + (i + count * 3) % byteCount]
result[i] = fn ? fn(value) : value
}
this.Jump(byteCount)
return result
}
RBXInterleavedInt32(count) {
return this.RBXInterleavedUint32(count, value =>
(value % 2 === 1 ? -(value + 1) / 2 : value / 2)
)
}
RBXInterleavedFloat(count) {
return this.RBXInterleavedUint32(count, value =>
ByteReader.ParseRBXFloat(value)
)
}
}
{
const peekMethods = [
"Byte", "UInt8", "UInt16LE", "UInt16BE", "UInt32LE", "UInt32BE",
"FloatLE", "FloatBE", "DoubleLE", "DoubleBE", "String"
]
peekMethods.forEach(key => {
const fn = ByteReader.prototype[key]
ByteReader.prototype["Peek" + key] = function (...args) {
const index = this.GetIndex()
const result = fn.apply(this, args)
this.SetIndex(index)
return result
}
})
}
module.exports = ByteReader