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 1 commit
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: 47 additions & 37 deletions kaitaistruct.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
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
smarek marked this conversation as resolved.
Show resolved Hide resolved
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 +83,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 +167,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 +205,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 @@ -221,10 +230,6 @@ end
-- Deprecated, use read_bits_int_be() instead.
--
function KaitaiStream:read_bits_int(n)
return self:read_bits_int_be(n)
end

function KaitaiStream:read_bits_int_le(n)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how this mess happened (you probably took the read_bits_int definition from the Johnnynator/kaitai_struct_luajit_runtime repo), but the result is wrong. You completely dropped the read_bits_int_le method (introduced in fc9556d from Apr 28, 2020 0:27), and this new read_bits_int method is only a duplicate of the existing read_bits_int_be method when it didn't have changes from cb6138b commited on Apr 28, 2020 0:13 yet.

Context: in 0.8 and older versions of KS, there had been only the big-endian version of bit-sized integers, so there had been only one method – read_bits_int. However, in 0.9 along with the advent of little-endian bit integers (kaitai-io/kaitai_struct#155), it was necessary to add another function read_bits_int_le for reading bit integers in LE manner. To maintain naming consistency, the old read_bits_int for reading BE bit-integers was renamed to read_bits_int_be, and in order not to break backward compatibility (to make parsing codes generated by KSC ≤0.8 work with 0.9 runtime library), the outdated method read_bits_int has been kept as a deprecated alias of read_bits_int_be. See kaitai-io/kaitai_struct#155 (comment) for more info.

In short, the canonical names are read_bits_int_be and read_bits_int_le. The name read_bits_int became deprecated (it's ambiguous now), and it'll be removed in a future release.

Anyway, this is a side-by-side comparison of your read_bits_int_be and read_bits_int methods (these are the same changes as introduced in cb6138b):

mergely-lua-removed-read-bits-int-le

Please see how read_bits_int and read_bits_int_le methods are currently implemented on master branch and rewrite them using the bit module from scratch.

That reminds me that I have to run tests locally before I can mark this PR as OK.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I took a lot of code over from luajit_runtime repo, but there must have been reason for me to do it this way, it's hard to remember after few months since i've done it.
Strangely, this did not mess up any tests in place

Also no, i'm not going to rewrite the functions using bit module from scratch now, but we can surely add that task to backlog of other issues, this runtime has.

Copy link
Member

@generalmimon generalmimon Sep 30, 2020

Choose a reason for hiding this comment

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

there must have been reason for me to do it this way

I really doubt that. It's obviously wrong, please read my above comment.

Also no, i'm not going to rewrite the functions using bit module from scratch now, but we can surely add that task to backlog of other issues, this runtime has.

Sorry, maybe I wrote it ambiguously, I meant copying the read_bits_int and read_bits_int_le method implementations and only replacing the ops & and >> with the bitmodule as you've done. Rewriting anything from scratch is undesirable, sorry for this wrong phrase.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, ok, that's something i can do for sure, no hard feelings 👍

Copy link
Member

Choose a reason for hiding this comment

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

Strangely, this did not mess up any tests in place

It certainly would now. How would possibly pass the test bits_simple_le when you completely removed the read_bit_ints_le method that it depends on?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, that's what happens, if my contribution from 13th is compared with updated tests/expectations from 23rd August
Merge this PR and the connected ones (specifically kaitai-io/ci_targets#7 and kaitai-io/kaitai_ci_ui#8 ), then we can work on fixing remaining issues and releasing a new version

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that's what happens, if my contribution from 13th is compared with updated tests/expectations from 23rd August

That's not true. Expectations on this matter have been last updated with these commits:

If this PR would be based on a earlier commit than fc9556d (imaginary situation), Git would not allow to merge this PR automatically, and merge conflicts would occur instead.

Copy link
Author

Choose a reason for hiding this comment

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

Sure, you're right, hopefully you feel satisfied

Copy link
Author

Choose a reason for hiding this comment

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

Fixed with 6f1e309

local bits_needed = n - self.bits_left
if bits_needed > 0 then
-- 1 bit => 1 byte
Expand All @@ -234,18 +239,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 +358,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 +371,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 +389,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