Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lua5.1 compatibility #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 51 additions & 33 deletions kaitaistruct.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
local class = require("class")
local stringstream = require("string_stream")
local lunpack = require("struct").unpack

local function try_require(name)
local success, mod = pcall(require, name)
if success then
return mod
else
return nil
end
end

-- bit is required in translated lua files, so provide it in this parent module
bit = try_require('bit') or try_require('bit32') or error[[no bitwise library found]]

KaitaiStruct = class.class()

Expand Down Expand Up @@ -74,79 +87,79 @@ end
-------------------------------------------------------------------------------

function KaitaiStream:read_s1()
return string.unpack('b', self._io:read(1))
return lunpack('b', self._io:read(1))
end

--.............................................................................
-- Big-endian
--.............................................................................

function KaitaiStream:read_s2be()
return string.unpack('>i2', self._io:read(2))
return lunpack('>h', self._io:read(2))
end

function KaitaiStream:read_s4be()
return string.unpack('>i4', self._io:read(4))
return lunpack('>i', self._io:read(4))
end

function KaitaiStream:read_s8be()
return string.unpack('>i8', self._io:read(8))
return lunpack('>l', self._io:read(8))
end

--.............................................................................
-- Little-endian
--.............................................................................

function KaitaiStream:read_s2le()
return string.unpack('<i2', self._io:read(2))
return lunpack('<h', self._io:read(2))
end

function KaitaiStream:read_s4le()
return string.unpack('<i4', self._io:read(4))
return lunpack('<i', self._io:read(4))
end

function KaitaiStream:read_s8le()
return string.unpack('<i8', self._io:read(8))
return lunpack('<l', self._io:read(8))
end

-------------------------------------------------------------------------------
-- Unsigned
-------------------------------------------------------------------------------

function KaitaiStream:read_u1()
return string.unpack('B', self._io:read(1))
return lunpack('B', self._io:read(1))
end

--.............................................................................
-- Big-endian
--.............................................................................

function KaitaiStream:read_u2be()
return string.unpack('>I2', self._io:read(2))
return lunpack('>H', self._io:read(2))
end

function KaitaiStream:read_u4be()
return string.unpack('>I4', self._io:read(4))
return lunpack('>I', self._io:read(4))
end

function KaitaiStream:read_u8be()
return string.unpack('>I8', self._io:read(8))
return lunpack('>L', self._io:read(8))
end

--.............................................................................
-- Little-endian
--.............................................................................

function KaitaiStream:read_u2le()
return string.unpack('<I2', self._io:read(2))
return lunpack('<H', self._io:read(2))
end

function KaitaiStream:read_u4le()
return string.unpack('<I4', self._io:read(4))
return lunpack('<I', self._io:read(4))
end

function KaitaiStream:read_u8le()
return string.unpack('<I8', self._io:read(8))
return lunpack('<L', self._io:read(8))
end

--=============================================================================
Expand All @@ -158,23 +171,23 @@ end
-------------------------------------------------------------------------------

function KaitaiStream:read_f4be()
return string.unpack('>f', self._io:read(4))
return lunpack('>f', self._io:read(4))
end

function KaitaiStream:read_f8be()
return string.unpack('>d', self._io:read(8))
return lunpack('>d', self._io:read(8))
end

-------------------------------------------------------------------------------
-- Little-endian
-------------------------------------------------------------------------------

function KaitaiStream:read_f4le()
return string.unpack('<f', self._io:read(4))
return lunpack('<f', self._io:read(4))
end

function KaitaiStream:read_f8le()
return string.unpack('<d', self._io:read(8))
return lunpack('<d', self._io:read(8))
end

--=============================================================================
Expand All @@ -196,21 +209,21 @@ function KaitaiStream:read_bits_int_be(n)
local buf = self._io:read(bytes_needed)
for i = 1, #buf do
local byte = buf:byte(i)
self.bits = self.bits << 8
self.bits = self.bits | byte
self.bits = bit.lshift(self.bits, 8)
self.bits = bit.bor(self.bits, byte)
self.bits_left = self.bits_left + 8
end
end

-- Raw mask with required number of 1s, starting from lowest bit
local mask = (1 << n) - 1
local mask = bit.lshift(1, n) - 1
-- Shift self.bits to align the highest bits with the mask & derive reading result
local shift_bits = self.bits_left - n
local res = (self.bits >> shift_bits) & mask
local res = bit.band(bit.rshift(self.bits, shift_bits), mask)
-- Clear top bits that we've just read => AND with 1s
self.bits_left = self.bits_left - n
mask = (1 << self.bits_left) - 1
self.bits = self.bits & mask
mask = bit.lshift(1, self.bits_left) - 1
self.bits = bit.band(self.bits, mask)

return res
end
Expand All @@ -234,18 +247,23 @@ function KaitaiStream:read_bits_int_le(n)
local buf = self._io:read(bytes_needed)
for i = 1, #buf do
local byte = buf:byte(i)
self.bits = self.bits | (byte << self.bits_left)
self.bits = bit.lshift(self.bits, 8)
self.bits = bit.bor(self.bits, byte)
self.bits_left = self.bits_left + 8
end
end

-- Raw mask with required number of 1s, starting from lowest bit
local mask = (1 << n) - 1
local mask = bit.lshift(1, n) - 1
-- Shift mask to align with highest bits available in self.bits
local shift_bits = self.bits_left - n
mask = bit.lshift(mask, shift_bits)
-- Derive reading result
local res = self.bits & mask
-- Remove bottom bits that we've just read by shifting
self.bits = self.bits >> n
local res = bit.rshift(bit.band(self.bits, mask), shift_bits)
-- Clear top bits that we've just read => AND with 1s
self.bits_left = self.bits_left - n
mask = bit.lshift(1, self.bits_left) - 1
self.bits = bit.band(self.bits, mask)

return res
end
Expand Down Expand Up @@ -348,7 +366,7 @@ function KaitaiStream.process_xor_one(data, key)
local r = ""

for i = 1, #data do
local c = data:byte(i) ~ key
local c = bit.bxor(data:byte(i), key)
r = r .. string.char(c)
end

Expand All @@ -361,7 +379,7 @@ function KaitaiStream.process_xor_many(data, key)
local ki = 1

for i = 1, #data do
local c = data:byte(i) ~ key:byte(ki)
local c = bit.bxor(data:byte(i), key:byte(ki))
r = r .. string.char(c)
ki = ki + 1
if ki > kl then
Expand All @@ -379,11 +397,11 @@ function KaitaiStream.process_rotate_left(data, amount, group_size)

local result = ""
local mask = group_size * 8 - 1
local anti_amount = -amount & mask
local anti_amount = bit.band(-amount, mask)

for i = 1, #data do
local c = data:byte(i)
c = ((c << amount) & 0xFF) | (c >> anti_amount)
c = bit.bor(bit.band(bit.lshift(c, amount), 0xFF), bit.rshift(c, anti_amount))
result = result .. string.char(c)
end

Expand Down
35 changes: 7 additions & 28 deletions string_decode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,21 @@
-- String decoder functions
--

local stringdecode = {}

-- From http://lua-users.org/wiki/LuaUnicode
local function utf8_to_32(utf8str)
assert(type(utf8str) == "string")
local res, seq, val = {}, 0, nil

for i = 1, #utf8str do
local c = string.byte(utf8str, i)
if seq == 0 then
table.insert(res, val)
seq = c < 0x80 and 1 or c < 0xE0 and 2 or c < 0xF0 and 3 or
c < 0xF8 and 4 or --c < 0xFC and 5 or c < 0xFE and 6 or
error("Invalid UTF-8 character sequence")
val = bit32.band(c, 2^(8-seq) - 1)
else
val = bit32.bor(bit32.lshift(val, 6), bit32.band(c, 0x3F))
end

seq = seq - 1
end
local lunicode = require(".unicode")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulations!
Yet the dot here must be a typo?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar uses utf8.lua for relative require, and the unicode module is relative to string_decode.lua in the same directory currently, so it seemed appropriate
https://github.com/Stepets/utf8.lua/blame/master/README.md#L54

currently works even with tests, so I'm not sure it's wrong, however that might change with implementing #11


table.insert(res, val)

return res
end
local stringdecode = {}

function stringdecode.decode(str, encoding)
local enc = encoding and encoding:lower() or "ascii"

if enc == "ascii" then
return str
elseif enc == "utf-8" then
local code_points = utf8_to_32(str)

return utf8.char(table.unpack(code_points))
return lunicode.transcode(str, lunicode.utf8_dec, lunicode.utf8_enc, false, false)
elseif enc == "sjis" then
return table.concat(lunicode.decode(str, lunicode.sjis_dec, true))
elseif enc == "cp437" then
return lunicode.transcode(str, lunicode.cp437_dec, lunicode.utf8_enc, false, false)
smarek marked this conversation as resolved.
Show resolved Hide resolved
else
error("Encoding " .. encoding .. " not supported")
end
Expand Down
Loading