Skip to content

Commit

Permalink
more cleanup to used pack/unpack, compatibility sjis
Browse files Browse the repository at this point in the history
  • Loading branch information
smarek committed Aug 13, 2020
1 parent cfc06f2 commit c5c371f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
38 changes: 19 additions & 19 deletions kaitaistruct.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local class = require("class")
local stringstream = require("string_stream")
local struct = string.unpack and string or require("struct")
local lunpack = require("struct").unpack

local function try_require(name)
local success, mod = pcall(require, name)
Expand Down Expand Up @@ -83,79 +83,79 @@ end
-------------------------------------------------------------------------------

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

--=============================================================================
Expand All @@ -167,23 +167,23 @@ end
-------------------------------------------------------------------------------

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

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

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

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

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

--=============================================================================
Expand Down
1 change: 0 additions & 1 deletion string_decode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
-- String decoder functions
--

local unpack = rawget(_G, 'unpack') or table.unpack
local lunicode = require(".unicode")

local stringdecode = {}
Expand Down
20 changes: 6 additions & 14 deletions unicode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ local bit = try_require('bit') or try_require('bit32') or error[[no bitwise libr
-- over every char of a string
local byte = string.byte
local char = string.char
local utf8char = string.char
if rawget(_G, 'utf8') ~= nil then
utf8char = utf8.char
end
local lunpack = string.unpack or require("struct").unpack
local lpack = string.pack or require("struct").pack
local lunpack = require("struct").unpack
local lpack = require("struct").pack
local concat = table.concat

local unicode = {}
Expand Down Expand Up @@ -3820,20 +3816,16 @@ function unicode.sjis_dec(buf, pos, bigendian)
end

local cp = lunpack(fmt, buf, pos)
local utf16cp = unicode.sjis_decode[cp]
local utf8cp = unicode.sjis_decode[cp]

local ret = nil

if utf16cp == nil then
if utf8cp == nil then
error(string.format("sjis missing cp %x", cp))
else
if pcall(function() ret = utf8char(utf16cp) end) ~= true then
_, b1, b2, b3 = lunpack("BBBB", lpack(">I", utf16cp))
ret = utf8char(b1) .. utf8char(b2) .. utf8char(b3)
end
_, b1, b2, b3 = lunpack("BBBB", lpack(">I", utf8cp))
return pos + 2, char(b1) .. char(b2) .. char(b3)
end

return pos + 2, ret
end


Expand Down

0 comments on commit c5c371f

Please sign in to comment.