-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemoryWrapper.js
275 lines (248 loc) · 8.77 KB
/
memoryWrapper.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
class memoryWrapper {
///*
//
// The goal of this class is to provide a way to easily handle the memory used by a wasm code
//
///*
constructor(memoryBuffer) {
///*
// The constructor of the class. It needs a reference to the memory buffer
///*
this.memoryView = new DataView(memoryBuffer);
}
read32bitsUnsigned(address) {
///*
// Returns the 32 bits unsigned value in the memory buffer at the *address* memory address
///*
return this.memoryView.getUint32(address);
}
read32bitsSigned(address) {
///*
// Returns the 32 bits signed value in the memory buffer at the *address* memory address
///*
return this.memoryView.getInt32(address);
}
read16bitsUnsigned(address) {
///*
// Returns the 16 bits unsigned value in the memory buffer at the *address* memory address
///*
return this.memoryView.getUint16(address);
}
read16bitsSigned(address) {
///*
// Returns the 16 bits signed value in the memory buffer at the *address* memory address
///*
return this.memoryView.getInt16(address);
}
read8bitsUnsigned(address) {
///*
// Returns the 8 bits unsigned value in the memory buffer at the *address* memory address
///*
return this.memoryView.getUint8(address);
}
read8bitsSigned(address) {
///*
// Returns the 8 bits signed value in the memory buffer at the *address* memory address
///*
return this.memoryView.getInt8(address);
}
write32bitsUnsigned(address, value) {
///*
// Write the 32 bits unsigned *value* value in the memory buffer at the *address* memory address
///*
this.memoryView.setUint32(address, value);
}
write32bitsSigned(address, value) {
///*
// Write the 32 bits signed *value* value in the memory buffer at the *address* memory address
///*
this.memoryView.setInt32(address, value);
}
write16bitsUnsigned(address, value) {
///*
// Write the 16 bits unsigned *value* value in the memory buffer at the *address* memory address
///*
this.memoryView.setUint16(address, value);
}
write16bitsSigned(address, value) {
///*
// Write the 16 bits signed *value* value in the memory buffer at the *address* memory address
///*
this.memoryView.setInt16(address, value);
}
write8bitsUnsigned(address, value) {
///*
// Write the 8 bits signed *value* value in the memory buffer at the *address* memory address
///*
this.memoryView.setUint8(address, value);
}
write8bitsSigned(address, value) {
///*
// Write the 8 bits signed *value* value in the memory buffer at the *address* memory address
///*
this.memoryView.setInt8(address, value);
}
readString(address) {
///*
// Returns the string in the memory buffer starting at the *address* memory address.
// The function stop at the first '0x00' character encountered.
///*
var result = "";
var i = 0;
var currentChar = this.read8bitsUnsigned(address);
while (currentChar !== 0) {
result = result + String.fromCharCode(currentChar);
i += 1;
currentChar = this.read8bitsUnsigned(address + i);
}
return result;
}
writeString(address, value) {
///*
// Write the *value* string in the memory buffer starting at the *address* memory address.
// The function writes a '0x00' character at the end of the string.
///*
var i = 0;
for (; i < value.length; i++){
this.write8bitsUnsigned(address + i, value.charCodeAt(i));
}
this.write8bitsUnsigned(address + i, 0);
}
readBytes(address, length) {
///*
// Returns an array of values representing the bytes at the *address* memory address.
// The function reads exactly *length* bytes
///*
var result = [];
for (var i = 0; i < length; i++) {
result.push(this.read8bitsUnsigned(address + i));
}
return result;
}
writeBytes(address, values) {
///*
// Write the *values* bytes in the memory buffer starting at the *address* memory address.
// The *values* argument must be an array of bytes (int < 255).
///*
for (var i = 0; i < values.length; i++) {
this.write8bitsUnsigned(address + i, values[i])
}
}
search32bitsUnsigned(value) {
///*
// Returns a list of memory addresses where the 32 bits unsigned value *value* is present in the memory buffer.
///*
var addresses = [];
for (var i = 0; i < this.memoryView.byteLength; i += 4) {
if (this.read32bitsUnsigned(i) === value) {
addresses.push(i);
}
}
return addresses;
}
search32bitsSigned(value) {
///*
// Returns a list of memory addresses where the 32 bits signed value *value* is present in the memory buffer.
///*
var addresses = [];
for (var i = 0; i < this.memoryView.byteLength; i += 4) {
if (this.read32bitsSigned(i) === value) {
addresses.push(i);
}
}
return addresses;
}
search16bitsUnsigned(value) {
///*
// Returns a list of memory addresses where the 16 bits unsigned value *value* is present in the memory buffer.
///*
var addresses = [];
for (var i = 0; i < this.memoryView.byteLength; i += 2) {
if (this.read16bitsUnsigned(i) === value) {
addresses.push(i);
}
}
return addresses;
}
search16bitsSigned(value) {
///*
// Returns a list of memory addresses where the 16 bits signed value *value* is present in the memory buffer.
///*
var addresses = [];
for (var i = 0; i < this.memoryView.byteLength; i += 2) {
if (this.read16bitsSigned(i) === value) {
addresses.push(i);
}
}
return addresses;
}
search8bitsUnsigned(value) {
///*
// Returns a list of memory addresses where the 8 bits unsigned value *value* is present in the memory buffer.
///*
var addresses = [];
for (var i = 0; i < this.memoryView.byteLength; i += 1) {
if (this.read8bitsUnsigned(i) === value) {
addresses.push(i);
}
}
return addresses;
}
search8bitsSigned(value) {
///*
// Returns a list of memory addresses where the 8 bits signed value *value* is present in the memory buffer.
///*
var addresses = [];
for (var i = 0; i < this.memoryView.byteLength; i += 1) {
if (this.read8bitsSigned(i) === value) {
addresses.push(i);
}
}
return addresses;
}
searchString(value) {
///*
// Returns a list of memory addresses where the string *value* is present in the memory buffer.
// The returned values point to the start of the strings.
// The function considers a string is finished at the first '0x00' character encountered.
///*
var potAddresses = this.search8bitsUnsigned(value.charCodeAt(0));
var addresses = [];
for (var address of potAddresses) {
if (this.readString(address) === value) {
addresses.push(address);
}
}
return addresses;
}
searchStringRegex(value) {
///*
// Returns a list of memory addresses where the regex *value* matches in the memory buffer.
// The returned values point to the start of the strings.
// The function considers a string is finished at the first '0x00' character encountered.
//
// Not as slow as I thought it would be...
///*
try {
value = new RegExp(value, 'g');
} catch(e) {
console.log("Invalid regular expression")
return []
}
var addresses = [];
var currentAddress = 0;
while (currentAddress < this.memoryView.byteLength) {
if (this.read8bitsUnsigned(currentAddress) === 0) {
currentAddress += 1;
} else {
var tmpString = this.readString(currentAddress);
let found;
while ((found = value.exec(tmpString)) !== null) {
addresses.push(currentAddress + found.index)
}
currentAddress += tmpString.length;
}
}
return addresses;
}
}