-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathhexWorker.js
143 lines (129 loc) · 5.17 KB
/
hexWorker.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
importScripts("encoderAndDecoderForced.src.js");
onmessage= (function() {
var timingOut = false, binaryDataString="", hexadecimalString="", currentTextBuffer=null, currentHexBuffer=null;
const Uint8Array = self.Uint8Array;
const Object_prototype_toString = ({}).toString;
const ArrayBufferString = Object_prototype_toString.call(ArrayBuffer.prototype);
const setTimeout = self.setTimeout;
const TextEncoder_encode = (new TextEncoder).encode;
const TextDecoder_decode = (new TextDecoder).decode;
function convertToOrFromHex(){
timingOut = false;
if (currentHexBuffer !== null) {
hexadecimalString = TextDecoder_decode(currentHexBuffer);
currentHexBuffer = null;
postMessage({
"type": "hex",
"value": hexadecimalString
});
}
if (currentTextBuffer !== null) {
postMessage({
"type": "text",
"value": TextDecoder_decode(currentTextBuffer)
});
const inputAsU8Array = new Uint8Array(currentTextBuffer);
const len = inputAsU8Array.length|0;
currentTextBuffer = null; // reset it
let resultingString = "";
for (let i=0; i<len; i=i+1|0) resultingString += (
(i === 0 ? (inputAsU8Array[i]|0) < 16 ? "0" : "" : (inputAsU8Array[i]|0) < 16 ? " 0" : " ") +
(inputAsU8Array[i]|0).toString(16)
);
postMessage({
"type": "hex",
"value": resultingString
});
} else if (binaryDataString !== "") {
const inputAsU8Array = TextEncoder_encode(binaryDataString);
const len = inputAsU8Array.length|0;
binaryDataString = ""; // reset it
let resultingString = "";
for (let i=0; i<len; i=i+1|0) resultingString += (
(i === 0 ? (inputAsU8Array[i]|0) < 16 ? "0" : "" : (inputAsU8Array[i]|0) < 16 ? " 0" : " ") +
(inputAsU8Array[i]|0).toString(16)
);
postMessage({
"type": "hex",
"value": resultingString
});
} else if (hexadecimalString !== "") {
const strLen = hexadecimalString.length|0;
let hexadecimals = 0;
for (let i=0, code=0; i<strLen; i=i+1|0) {
code = hexadecimalString.charCodeAt(i)|0;
hexadecimals = hexadecimals + (
(48 <= code && code <= 57) | (
65 <= (code & 0xffdf/*0b11011111*/) &&
(code & 0xffdf/*0b11011111*/) <= 90
)
)|0;
}
const arrayLength = (hexadecimals+1) >> 1;
const resultingArray = new Uint8Array(arrayLength);
let resultingIndex = 0;
for (let i=0, code=0, lo=0, hi=0; i<strLen; ) {
code = hexadecimalString.charCodeAt(i)|0;
i = i + 1|0;
if (48 <= code && code <= 57) {
hi = code - 48 |0;
} else if (
65 <= (code & 0xffdf/*0b11011111*/) &&
(code & 0xffdf/*0b11011111*/) <= 90
) {
hi = (code & 0xffdf/*0b11011111*/) - 65 + 10 |0;
} else {
continue;
}
while (i < strLen) {
code = hexadecimalString.charCodeAt(i)|0;
i = i + 1|0;
if (48 <= code && code <= 57) {
hi <<= 4;
lo = code - 48|0;
break;
} else if (
65 <= (code & 0xffdf/*0b11011111*/) &&
(code & 0xffdf/*0b11011111*/) <= 90
) {
hi <<= 4;
lo = (code & 0xffdf/*0b11011111*/) - 65 + 10|0;
break;
}
}
resultingArray[resultingIndex] = hi | lo;
resultingIndex = resultingIndex + 1|0;
}
postMessage({
"type": "text",
"value": TextDecoder_decode(resultingArray)
});
}
binaryDataString = hexadecimalString = "";
currentTextBuffer = currentHexBuffer = null;
}
return function(evt){
var data = evt.data;
if (data && data["type"] === "text-buffer" && ArrayBufferString === Object_prototype_toString.call(data["value"])) {
binaryDataString = hexadecimalString = "";
currentHexBuffer = null;
currentTextBuffer = data["value"];
} else if (data && data["type"] === "hex-buffer" && ArrayBufferString === Object_prototype_toString.call(data["value"])) {
binaryDataString = hexadecimalString = "";
currentHexBuffer = null;
currentTextBuffer = data["value"];
} else if (data && data["type"] === "text" && typeof data["value"] === "string") {
currentTextBuffer = currentHexBuffer = null;
hexadecimalString = "";
binaryDataString = data["value"];
} else if (data && data["type"] === "hex" && typeof data["value"] === "string") {
currentTextBuffer = currentHexBuffer = null;
binaryDataString = "";
hexadecimalString = data["value"];
}
if (!timingOut) {
setTimeout(convertToOrFromHex, 1);
timingOut = true;
}
};
})();