forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathencodings.js
More file actions
266 lines (229 loc) · 5.23 KB
/
encodings.js
File metadata and controls
266 lines (229 loc) · 5.23 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
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
import alert from "dialogs/alert";
import settings from "lib/settings";
let encodings = {};
/**
* @typedef {Object} Encoding
* @property {string} label
* @property {string[]} aliases
* @property {string} name
*/
/**
* Get the encoding label from the charset
* @param {string} charset
* @returns {Encoding|undefined}
*/
export function getEncoding(charset) {
charset = charset.toLowerCase();
const found = Object.keys(encodings).find((key) => {
if (key.toLowerCase() === charset) {
return true;
}
const alias = encodings[key].aliases.find(
(alias) => alias.toLowerCase() === charset,
);
if (alias) {
return true;
}
return false;
});
if (found) {
return encodings[found];
}
return encodings["UTF-8"];
}
function detectBOM(bytes) {
if (
bytes.length >= 3 &&
bytes[0] === 0xef &&
bytes[1] === 0xbb &&
bytes[2] === 0xbf
)
return "UTF-8";
if (bytes.length >= 2 && bytes[0] === 0xff && bytes[1] === 0xfe)
return "UTF-16LE";
if (bytes.length >= 2 && bytes[0] === 0xfe && bytes[1] === 0xff)
return "UTF-16BE";
return null;
}
function isValidUTF8(bytes) {
let i = 0;
while (i < bytes.length) {
const byte = bytes[i];
if (byte < 0x80) {
i++;
} else if (byte >> 5 === 0x06) {
if (i + 1 >= bytes.length || bytes[i + 1] >> 6 !== 0x02) return false;
i += 2;
} else if (byte >> 4 === 0x0e) {
if (
i + 2 >= bytes.length ||
bytes[i + 1] >> 6 !== 0x02 ||
bytes[i + 2] >> 6 !== 0x02
)
return false;
i += 3;
} else if (byte >> 3 === 0x1e) {
if (
i + 3 >= bytes.length ||
bytes[i + 1] >> 6 !== 0x02 ||
bytes[i + 2] >> 6 !== 0x02 ||
bytes[i + 3] >> 6 !== 0x02
)
return false;
i += 4;
} else {
return false;
}
}
return true;
}
export async function detectEncoding(buffer) {
if (!buffer || buffer.byteLength === 0) {
const def = settings.value.defaultFileEncoding;
return def === "auto" ? "UTF-8" : def || "UTF-8";
}
const bytes = new Uint8Array(buffer);
const bomEncoding = detectBOM(bytes);
if (bomEncoding) return bomEncoding;
const sample = bytes.subarray(0, Math.min(2048, bytes.length));
let nulls = 0,
ascii = 0;
for (const byte of sample) {
if (byte === 0) nulls++;
else if (byte < 0x80) ascii++;
}
if (nulls > sample.length * 0.3) return "UTF-16LE";
if (isValidUTF8(sample)) return "UTF-8";
const encodings = [
...new Set([
"UTF-8",
settings.value.defaultFileEncoding === "auto"
? "UTF-8"
: settings.value.defaultFileEncoding || "UTF-8",
"windows-1252",
"ISO-8859-1",
]),
];
const testSample = sample.subarray(0, 512);
const testBuffer = testSample.buffer.slice(
testSample.byteOffset,
testSample.byteOffset + testSample.byteLength,
);
for (const encoding of encodings) {
try {
const encodingObj = getEncoding(encoding);
if (!encodingObj) continue;
const text = await execDecode(testBuffer, encodingObj.name);
if (
!text.includes("\uFFFD") &&
!/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/.test(text)
) {
return encoding;
}
} catch (error) {
continue;
}
}
const def = settings.value.defaultFileEncoding;
return def === "auto" ? "UTF-8" : def || "UTF-8";
}
/**
* Decodes arrayBuffer to String according given encoding type
* @param {ArrayBuffer} buffer
* @param {string} [charset]
* @returns {Promise<string>}
*/
export async function decode(buffer, charset) {
let isJson = false;
if (charset === "json") {
charset = null;
isJson = true;
}
if (!charset) {
charset = settings.value.defaultFileEncoding;
}
if (charset === "auto") charset = "UTF-8";
charset = getEncoding(charset).name;
const text = await execDecode(buffer, charset);
if (isJson) {
return JSON.parse(text);
}
return text;
}
/**
* Encodes text to ArrayBuffer according given encoding type
* @param {string} text
* @param {string} charset
* @returns {Promise<ArrayBuffer>}
*/
export function encode(text, charset) {
if (!charset) {
charset = settings.value.defaultFileEncoding;
}
if (charset === "auto") charset = "UTF-8";
charset = getEncoding(charset).name;
return execEncode(text, charset);
}
export async function initEncodings() {
return new Promise((resolve, reject) => {
cordova.exec(
(map) => {
Object.keys(map).forEach((key) => {
const encoding = map[key];
encodings[key] = encoding;
});
resolve();
},
(error) => {
alert(strings.error, error.message || error);
reject(error);
},
"System",
"get-available-encodings",
[],
);
});
}
/**
* Decodes arrayBuffer to String according given encoding type
* @param {ArrayBuffer} buffer
* @param {string} charset
* @returns {Promise<string>}
*/
function execDecode(buffer, charset) {
return new Promise((resolve, reject) => {
cordova.exec(
(text) => {
resolve(text);
},
(error) => {
reject(error);
},
"System",
"decode",
[buffer, charset],
);
});
}
/**
* Encodes text to ArrayBuffer according given encoding type
* @param {string} text
* @param {string} charset
* @returns {Promise<ArrayBuffer>}
*/
function execEncode(text, charset) {
return new Promise((resolve, reject) => {
cordova.exec(
(buffer) => {
resolve(buffer);
},
(error) => {
reject(error);
},
"System",
"encode",
[text, charset],
);
});
}
export default encodings;