switched to using string.(un)pack in libmtar and liblz16, meaning it works on both big and little endian machines, and should be smaller and faster
This commit is contained in:
parent
c5f304380e
commit
8e84eb0c67
@ -3,31 +3,13 @@ local buffer = require "buffer"
|
||||
|
||||
lz16 = {}
|
||||
|
||||
local function cint(n,l)
|
||||
local t={}
|
||||
for i = 0, 7 do
|
||||
t[i+1] = (n >> (i * 8)) & 0xFF
|
||||
end
|
||||
return string.reverse(string.char(table.unpack(t)):sub(1,l))
|
||||
end
|
||||
local function toint(s)
|
||||
local n = 0
|
||||
local i = 1
|
||||
for p in s:gmatch(".") do
|
||||
n = n << 8
|
||||
n = n | string.byte(p)
|
||||
i=i+1
|
||||
end
|
||||
return n
|
||||
end
|
||||
|
||||
local function readBuffer(fi)
|
||||
local stream = {}
|
||||
if fi:read(4) ~= "lz16" then
|
||||
return false, "not an lz16 archive"
|
||||
end
|
||||
function stream.read()
|
||||
local len = toint(fi:read(2) or "\0\0")
|
||||
local len = string.unpack(">I2", fi:read(2) or "\0\0")
|
||||
if len < 1 then
|
||||
return nil
|
||||
end
|
||||
@ -44,7 +26,7 @@ local function writeBuffer(fo)
|
||||
local stream = {}
|
||||
function stream:write(data)
|
||||
local cblock = lz.compress(data)
|
||||
fo:write(cint(cblock:len(),2)..cblock)
|
||||
fo:write(string.pack(">I2", cblock:len()) .. cblock)
|
||||
return cblock:len()+2
|
||||
end
|
||||
function stream.close()
|
||||
|
@ -1,24 +1,5 @@
|
||||
local mtar = {}
|
||||
|
||||
local function toint(s)
|
||||
local n = 0
|
||||
local i = 1
|
||||
for p in s:gmatch(".") do
|
||||
n = n << 8
|
||||
n = n | string.byte(p)
|
||||
i=i+1
|
||||
end
|
||||
return n
|
||||
end
|
||||
|
||||
local function cint(n,l)
|
||||
local t={}
|
||||
for i = 0, 7 do
|
||||
t[i+1] = (n >> (i * 8)) & 0xFF
|
||||
end
|
||||
return string.reverse(string.char(table.unpack(t)):sub(1,l))
|
||||
end
|
||||
|
||||
local function cleanPath(path)
|
||||
local pt = {}
|
||||
for segment in path:gmatch("[^/]+") do
|
||||
@ -32,7 +13,7 @@ local function cleanPath(path)
|
||||
end
|
||||
|
||||
function mtar.genHeader(fname,len) -- string number -- string -- generate a header for file *fname* when provided with file length *len*
|
||||
return string.format("%s%s%s",cint(fname:len(),2),fname,cint(len,2))
|
||||
return string.format("%s%s%s", string.pack(">I2",fname:len()), fname, string.pack(">I2",len))
|
||||
end
|
||||
|
||||
function mtar.iter(stream) -- table -- function -- Given buffer *stream*, returns an iterator suitable for use with *for* that returns, for each iteration, the file name, a function to read from the file, and the length of the file.
|
||||
@ -46,12 +27,12 @@ function mtar.iter(stream) -- table -- function -- Given buffer *stream*, return
|
||||
while remain > 0 do
|
||||
remain=remain-#stream:read(math.min(remain,2048))
|
||||
end
|
||||
local nlen = toint(stream:read(2) or "\0\0")
|
||||
local nlen = string.unpack(">I2", stream:read(2) or "\0\0")
|
||||
if nlen == 0 then
|
||||
return
|
||||
end
|
||||
local name = cleanPath(stream:read(nlen))
|
||||
local fsize = toint(stream:read(2))
|
||||
local fsize = string.unpack(">I2", stream:read(2))
|
||||
remain = fsize
|
||||
return name, read, fsize
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user