updated liblz16 and libmtar to use string.(un)pack and support bigger files

This commit is contained in:
Izaya 2021-05-26 17:49:42 +10:00
parent efe7e000c4
commit 1f5c0b013e
4 changed files with 44 additions and 63 deletions

View File

@ -3,35 +3,17 @@ 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
coroutine.yield()
if os.sleep then os.sleep(0) else coroutine.yield() end
return lz.decompress(fi:read(len))
end
function stream.close()
@ -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()
@ -54,14 +36,14 @@ local function writeBuffer(fo)
return buffer.new("wb",stream)
end
function lz16.buffer(stream)
function lz16.buffer(stream) -- table -- table -- Wrap a stream to read or write LZ16.
if stream.mode.w then
return writeBuffer(stream)
end
return readBuffer(stream)
end
function lz16.open(fname, mode)
function lz16.open(fname, mode) -- string string -- table -- Open file *fname* to read or write LZ16-compressed data depending on *mode*
local f = io.open(fname, mode)
if not f then return false end
f.mode.b = true

View File

@ -7,18 +7,6 @@ local args, opts = shell.parse(...)
local w, lz16 = pcall(require, "liblz16")
if not w then lz16 = nil end
local function toint(s)
s = s or "\0"
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 vw(s)
if opts.v and s then
io.write(tostring(s))
@ -82,6 +70,9 @@ function open(fn,mode)
local f
if opts.z then
f = lz16.open(fn, mode)
if mode == "w" and tonumber(opts.csize) then
f.bufferSize = tonumber(opts.csize)
end
else
f = io.open(fn, mode)
end
@ -151,7 +142,7 @@ elseif opts.c then
f:close()
else
print([[Usage:
mtar -[crtxvz] <archivefile> [otherfile] [otherfile]
mtar -[crtxvz] <archivefile> [otherfile] [otherfile] [--csize=blocksize]
Operations:
x: extract archive
c: create archive
@ -159,5 +150,7 @@ Operations:
Flags:
v: be verbose
z: use LZSS compression (requires liblz16)
r: append rather than overwrite]])
r: append rather than overwrite
Extended options:
csize: compression input block size]])
end

View File

@ -1,7 +1,7 @@
# mtar - Minitel Archiver
## Usage
mtar -[crtxvz] <archivefile> [otherfile] [otherfile]
mtar -[crtxvz] <archivefile> [otherfile] [otherfile] [--csize=block size]
## Operations
@ -12,7 +12,10 @@
## Flags
v: be verbose
z: use LZSS compression (requires liblz16)]])
z: use LZSS compression (requires liblz16)
## Extended options:
csize: compression input block size
## Examples

View File

@ -1,26 +1,24 @@
local mtar = {}
local versions = {}
versions[0] = {nlf = ">I2", flf = ">I2"} -- original version format
versions[1] = {nlf = ">I2", flf = ">I8"} -- extended file size format
mtar.versions = versions
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
local function cleanPath(path)
local pt = {}
for segment in path:gmatch("[^/]+") do
if segment == ".." then
pt[#pt] = nil
elseif segment ~= "." then
pt[#pt+1] = segment
end
end
return n
return table.concat(pt,"/")
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
function mtar.genHeader(fname,len) -- string number -- string -- Generates an mtar file header for a file called *name* that is *len* bytes long.
return string.format("%s%s%s",cint(fname:len(),2),fname,cint(len,2))
function mtar.genHeader(fname,len,version) -- string number -- string -- generate a header for file *fname* when provided with file length *len*
version=version or 1
return string.format("\255\255%s%s%s%s", string.char(version), string.pack(versions[version].nlf,fname:len()), fname, string.pack(versions[version].flf,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.
@ -31,15 +29,20 @@ function mtar.iter(stream) -- table -- function -- Given buffer *stream*, return
return rb
end
return function()
stream:read(remain)
local nlen = toint(stream:read(2) or "\0\0")
while remain > 0 do
remain=remain-#stream:read(math.min(remain,2048))
end
local version = 0
local nlen = string.unpack(">I2", stream:read(2) or "\0\0")
if nlen == 0 then
return
elseif nlen == 65535 then -- versioned header
version = string.byte(stream:read(1))
nlen = string.unpack(versions[version].nlf, stream:read(string.packsize(versions[version].nlf)))
end
local name = stream:read(nlen)
local fsize = toint(stream:read(2))
remain = fsize
return name, read, fsize
local name = cleanPath(stream:read(nlen))
remain = string.unpack(versions[version].flf, stream:read(string.packsize(versions[version].flf)))
return name, read, remain
end
end