Skip to content

Commit f400810

Browse files
authored
Correctly transform uint8array to string (#696)
* correctly transform uint8array to string * export function
1 parent 11a6959 commit f400810

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

Diff for: arduino-ide-extension/src/browser/create/create-api.ts

+43-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,47 @@ export namespace ResponseResultProvider {
1515
export const JSON: ResponseResultProvider = (response) => response.json();
1616
}
1717

18+
export function Utf8ArrayToStr(array: Uint8Array): string {
19+
let out, i, c;
20+
let char2, char3;
21+
22+
out = '';
23+
const len = array.length;
24+
i = 0;
25+
while (i < len) {
26+
c = array[i++];
27+
switch (c >> 4) {
28+
case 0:
29+
case 1:
30+
case 2:
31+
case 3:
32+
case 4:
33+
case 5:
34+
case 6:
35+
case 7:
36+
// 0xxxxxxx
37+
out += String.fromCharCode(c);
38+
break;
39+
case 12:
40+
case 13:
41+
// 110x xxxx 10xx xxxx
42+
char2 = array[i++];
43+
out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
44+
break;
45+
case 14:
46+
// 1110 xxxx 10xx xxxx 10xx xxxx
47+
char2 = array[i++];
48+
char3 = array[i++];
49+
out += String.fromCharCode(
50+
((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)
51+
);
52+
break;
53+
}
54+
}
55+
56+
return out;
57+
}
58+
1859
type ResourceType = 'f' | 'd';
1960

2061
@injectable()
@@ -275,9 +316,7 @@ export class CreateApi {
275316

276317
// parse the secret file
277318
const secrets = (
278-
typeof content === 'string'
279-
? content
280-
: new TextDecoder().decode(content)
319+
typeof content === 'string' ? content : Utf8ArrayToStr(content)
281320
)
282321
.split(/\r?\n/)
283322
.reduce((prev, curr) => {
@@ -341,7 +380,7 @@ export class CreateApi {
341380
const headers = await this.headers();
342381

343382
let data: string =
344-
typeof content === 'string' ? content : new TextDecoder().decode(content);
383+
typeof content === 'string' ? content : Utf8ArrayToStr(content);
345384
data = await this.toggleSecretsInclude(posixPath, data, 'remove');
346385

347386
const payload = { data: btoa(data) };

0 commit comments

Comments
 (0)