local intutil = {} function intutil.fromle16(tab, idx) local n = tab[idx] n = n | tab[idx+1] << 8 return n end function intutil.tole16(tab, idx, n) tab[idx] = n & 0xFF tab[idx+1] = (n >> 8) & 0xFF end function intutil.fromle32(tab, idx) local n = tab[idx] n = n | tab[idx+1] << 8 n = n | tab[idx+2] << 16 n = n | tab[idx+3] << 24 return n end function intutil.tole32(tab, idx, n) tab[idx] = n & 0xFF tab[idx+1] = (n >> 8) & 0xFF tab[idx+2] = (n >> 16) & 0xFF tab[idx+3] = (n >> 24) & 0xFF end function intutil.fromle64(tab, idx) local n = tab[idx] n = n | tab[idx+1] << 8 n = n | tab[idx+2] << 16 n = n | tab[idx+3] << 24 n = n | tab[idx+4] << 32 n = n | tab[idx+5] << 40 n = n | tab[idx+6] << 48 n = n | tab[idx+7] << 56 return n end function intutil.tole64(tab, idx, n) tab[idx] = n & 0xFF tab[idx+1] = (n >> 8) & 0xFF tab[idx+2] = (n >> 16) & 0xFF tab[idx+3] = (n >> 24) & 0xFF tab[idx+4] = (n >> 32) & 0xFF tab[idx+5] = (n >> 40) & 0xFF tab[idx+6] = (n >> 48) & 0xFF tab[idx+7] = (n >> 56) & 0xFF end function intutil.signexti32(n) if (n & 0x80000000) ~= 0 then n = n | 0xFFFFFFFF00000000 end return n end function intutil.signexti16(n) if (n & 0x8000) ~= 0 then n = n | 0xFFFFFFFFFFFF0000 end return n end function intutil.signexti8(n) if (n & 0x80) ~= 0 then n = n | 0xFFFFFFFFFFFFFF00 end return n end function intutil.fromuleb128(tab, idx) local result = 0 local shift = 0 local len = 0 repeat local byte = tab[idx+len] result = result | ((byte & 0x7F) << shift) shift = shift + 7 len = len + 1 until (byte & 0x80) == 0 return len, result end function intutil.fromsleb128(tab, idx, bits) bits = bits or 64 local result = 0 local shift = 0 local len = 0 repeat local byte = tab[idx+len] result = result | ((byte & 0x7F) << shift) shift = shift + 7 len = len + 1 until (byte & 0x80) == 0 if (shift < bits) and ((tab[idx+len-1] & 0x40) ~= 0) then result = result | (((1 << bits) - 1) << shift) end return len, result & ((1 << bits) - 1) end return intutil