-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
63 lines (51 loc) · 1.19 KB
/
util.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
function stringToBytes(string) {
b = []
for (var c = 0; c < string.length; c++) {
b.push(string.charCodeAt(c));
}
b.push(0);
return new Uint8Array(b);
}
function stringToUnicodeBytes(string) {
b = []
for (var c = 0; c < string.length; c++) {
b.push(string.charCodeAt(c));
b.push(0);
}
b.push(0);
return new Uint8Array(b);
}
function unxor(bytes) {
b = []
for (var i = 0; i < bytes.length; i++) {
b.push(~bytes[i]);
}
return new Uint8Array(b);
}
// Like C memcpy, but has an optional offset, so equivelant to:
// memcpy(a + o, b, n);
function memcpy(a, b, n, o) {
for (var i = 0; i < n; i++) {
a[i + o] = b[i];
}
}
// ,With offset for second data
// Will oveflow, but JS likes to return "undefined"
function compareBytes(a, b, n, o) {
for (var c = 0; c < n; c++) {
if (a[c] != b[c + o]) {
return 0;
}
}
return 1;
}
function parseUint32(bytes, offset) {
bytes = new Uint8Array(bytes).slice(offset, offset + 4);
return new Uint32Array(bytes.buffer)[0];
}
function bytesUint32(num) {
var arr = new ArrayBuffer(4);
var view = new DataView(arr);
view.setUint32(0, num, true);
return new Uint8Array(arr);
}