Skip to content

Commit f831369

Browse files
authored
[wasm2js] Fix base64 encoding (#1670)
The static std::string base64Encode(std::vector<char> &data) { uses signed char in input data. The ((int)data[0]) converts it the signed int, making '\xFF' char into -1. The patch fixes casting.
1 parent a0cbc9d commit f831369

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

src/wasm2js.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,10 @@ static std::string base64Encode(std::vector<char> &data) {
447447
"0123456789+/";
448448

449449
while (i + 3 <= data.size()) {
450-
int bits =
451-
(((int) data[i + 0]) << 16) |
452-
(((int) data[i + 1]) << 8) |
453-
(((int) data[i + 2]) << 0);
450+
uint32_t bits =
451+
(((uint32_t)(uint8_t) data[i + 0]) << 16) |
452+
(((uint32_t)(uint8_t) data[i + 1]) << 8) |
453+
(((uint32_t)(uint8_t) data[i + 2]) << 0);
454454
ret += alphabet[(bits >> 18) & 0x3f];
455455
ret += alphabet[(bits >> 12) & 0x3f];
456456
ret += alphabet[(bits >> 6) & 0x3f];
@@ -459,15 +459,15 @@ static std::string base64Encode(std::vector<char> &data) {
459459
}
460460

461461
if (i + 2 == data.size()) {
462-
int bits =
463-
(((int) data[i + 0]) << 8) |
464-
(((int) data[i + 1]) << 0);
462+
uint32_t bits =
463+
(((uint32_t)(uint8_t) data[i + 0]) << 8) |
464+
(((uint32_t)(uint8_t) data[i + 1]) << 0);
465465
ret += alphabet[(bits >> 10) & 0x3f];
466466
ret += alphabet[(bits >> 4) & 0x3f];
467467
ret += alphabet[(bits << 2) & 0x3f];
468468
ret += '=';
469469
} else if (i + 1 == data.size()) {
470-
int bits =(int) data[i + 0];
470+
uint32_t bits = (uint32_t)(uint8_t) data[i + 0];
471471
ret += alphabet[(bits >> 2) & 0x3f];
472472
ret += alphabet[(bits << 4) & 0x3f];
473473
ret += '=';

test/wasm2js/base64.2asm.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function asmFunc(global, env, buffer) {
2+
"use asm";
3+
var HEAP8 = new global.Int8Array(buffer);
4+
var HEAP16 = new global.Int16Array(buffer);
5+
var HEAP32 = new global.Int32Array(buffer);
6+
var HEAPU8 = new global.Uint8Array(buffer);
7+
var HEAPU16 = new global.Uint16Array(buffer);
8+
var HEAPU32 = new global.Uint32Array(buffer);
9+
var HEAPF32 = new global.Float32Array(buffer);
10+
var HEAPF64 = new global.Float64Array(buffer);
11+
var Math_imul = global.Math.imul;
12+
var Math_fround = global.Math.fround;
13+
var Math_abs = global.Math.abs;
14+
var Math_clz32 = global.Math.clz32;
15+
var Math_min = global.Math.min;
16+
var Math_max = global.Math.max;
17+
var Math_floor = global.Math.floor;
18+
var Math_ceil = global.Math.ceil;
19+
var Math_sqrt = global.Math.sqrt;
20+
var abort = env.abort;
21+
var nan = global.NaN;
22+
var infinity = global.Infinity;
23+
var i64toi32_i32$HIGH_BITS = 0;
24+
return {
25+
26+
};
27+
}
28+
29+
const memasmFunc = new ArrayBuffer(65536);
30+
const assignasmFunc = (
31+
function(mem) {
32+
const _mem = new Uint8Array(mem);
33+
return function(offset, s) {
34+
if (typeof Buffer === 'undefined') {
35+
const bytes = atob(s);
36+
for (let i = 0; i < bytes.length; i++)
37+
_mem[offset + i] = bytes.charCodeAt(i);
38+
} else {
39+
const bytes = Buffer.from(s, 'base64');
40+
for (let i = 0; i < bytes.length; i++)
41+
_mem[offset + i] = bytes[i];
42+
}
43+
}
44+
}
45+
)(memasmFunc);
46+
assignasmFunc(2, "AP7/");
47+
const retasmFunc = asmFunc({Math,Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,NaN,Infinity}, {abort:function() { throw new Error('abort'); }},memasmFunc);

test/wasm2js/base64.wast

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(module
2+
(memory $memory 1 1)
3+
(data (i32.const 2) "\00\fe\ff")
4+
)

0 commit comments

Comments
 (0)