updated liblz16 and libmtar to use string.(un)pack and support bigger files
This commit is contained in:
parent
efe7e000c4
commit
1f5c0b013e
@ -3,35 +3,17 @@ local buffer = require "buffer"
|
|||||||
|
|
||||||
lz16 = {}
|
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 function readBuffer(fi)
|
||||||
local stream = {}
|
local stream = {}
|
||||||
if fi:read(4) ~= "lz16" then
|
if fi:read(4) ~= "lz16" then
|
||||||
return false, "not an lz16 archive"
|
return false, "not an lz16 archive"
|
||||||
end
|
end
|
||||||
function stream.read()
|
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
|
if len < 1 then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
coroutine.yield()
|
if os.sleep then os.sleep(0) else coroutine.yield() end
|
||||||
return lz.decompress(fi:read(len))
|
return lz.decompress(fi:read(len))
|
||||||
end
|
end
|
||||||
function stream.close()
|
function stream.close()
|
||||||
@ -44,7 +26,7 @@ local function writeBuffer(fo)
|
|||||||
local stream = {}
|
local stream = {}
|
||||||
function stream:write(data)
|
function stream:write(data)
|
||||||
local cblock = lz.compress(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
|
return cblock:len()+2
|
||||||
end
|
end
|
||||||
function stream.close()
|
function stream.close()
|
||||||
@ -54,14 +36,14 @@ local function writeBuffer(fo)
|
|||||||
return buffer.new("wb",stream)
|
return buffer.new("wb",stream)
|
||||||
end
|
end
|
||||||
|
|
||||||
function lz16.buffer(stream)
|
function lz16.buffer(stream) -- table -- table -- Wrap a stream to read or write LZ16.
|
||||||
if stream.mode.w then
|
if stream.mode.w then
|
||||||
return writeBuffer(stream)
|
return writeBuffer(stream)
|
||||||
end
|
end
|
||||||
return readBuffer(stream)
|
return readBuffer(stream)
|
||||||
end
|
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)
|
local f = io.open(fname, mode)
|
||||||
if not f then return false end
|
if not f then return false end
|
||||||
f.mode.b = true
|
f.mode.b = true
|
||||||
|
@ -7,18 +7,6 @@ local args, opts = shell.parse(...)
|
|||||||
local w, lz16 = pcall(require, "liblz16")
|
local w, lz16 = pcall(require, "liblz16")
|
||||||
if not w then lz16 = nil end
|
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)
|
local function vw(s)
|
||||||
if opts.v and s then
|
if opts.v and s then
|
||||||
io.write(tostring(s))
|
io.write(tostring(s))
|
||||||
@ -82,6 +70,9 @@ function open(fn,mode)
|
|||||||
local f
|
local f
|
||||||
if opts.z then
|
if opts.z then
|
||||||
f = lz16.open(fn, mode)
|
f = lz16.open(fn, mode)
|
||||||
|
if mode == "w" and tonumber(opts.csize) then
|
||||||
|
f.bufferSize = tonumber(opts.csize)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
f = io.open(fn, mode)
|
f = io.open(fn, mode)
|
||||||
end
|
end
|
||||||
@ -151,7 +142,7 @@ elseif opts.c then
|
|||||||
f:close()
|
f:close()
|
||||||
else
|
else
|
||||||
print([[Usage:
|
print([[Usage:
|
||||||
mtar -[crtxvz] <archivefile> [otherfile] [otherfile]
|
mtar -[crtxvz] <archivefile> [otherfile] [otherfile] [--csize=blocksize]
|
||||||
Operations:
|
Operations:
|
||||||
x: extract archive
|
x: extract archive
|
||||||
c: create archive
|
c: create archive
|
||||||
@ -159,5 +150,7 @@ Operations:
|
|||||||
Flags:
|
Flags:
|
||||||
v: be verbose
|
v: be verbose
|
||||||
z: use LZSS compression (requires liblz16)
|
z: use LZSS compression (requires liblz16)
|
||||||
r: append rather than overwrite]])
|
r: append rather than overwrite
|
||||||
|
Extended options:
|
||||||
|
csize: compression input block size]])
|
||||||
end
|
end
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# mtar - Minitel Archiver
|
# mtar - Minitel Archiver
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
mtar -[crtxvz] <archivefile> [otherfile] [otherfile]
|
mtar -[crtxvz] <archivefile> [otherfile] [otherfile] [--csize=block size]
|
||||||
|
|
||||||
## Operations
|
## Operations
|
||||||
|
|
||||||
@ -12,7 +12,10 @@
|
|||||||
## Flags
|
## Flags
|
||||||
|
|
||||||
v: be verbose
|
v: be verbose
|
||||||
z: use LZSS compression (requires liblz16)]])
|
z: use LZSS compression (requires liblz16)
|
||||||
|
|
||||||
|
## Extended options:
|
||||||
|
csize: compression input block size
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
@ -1,26 +1,24 @@
|
|||||||
local mtar = {}
|
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 function cleanPath(path)
|
||||||
local n = 0
|
local pt = {}
|
||||||
local i = 1
|
for segment in path:gmatch("[^/]+") do
|
||||||
for p in s:gmatch(".") do
|
if segment == ".." then
|
||||||
n = n << 8
|
pt[#pt] = nil
|
||||||
n = n | string.byte(p)
|
elseif segment ~= "." then
|
||||||
i=i+1
|
pt[#pt+1] = segment
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return n
|
return table.concat(pt,"/")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function cint(n,l)
|
function mtar.genHeader(fname,len,version) -- string number -- string -- generate a header for file *fname* when provided with file length *len*
|
||||||
local t={}
|
version=version or 1
|
||||||
for i = 0, 7 do
|
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))
|
||||||
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))
|
|
||||||
end
|
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.
|
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
|
return rb
|
||||||
end
|
end
|
||||||
return function()
|
return function()
|
||||||
stream:read(remain)
|
while remain > 0 do
|
||||||
local nlen = toint(stream:read(2) or "\0\0")
|
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
|
if nlen == 0 then
|
||||||
return
|
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
|
end
|
||||||
local name = stream:read(nlen)
|
local name = cleanPath(stream:read(nlen))
|
||||||
local fsize = toint(stream:read(2))
|
remain = string.unpack(versions[version].flf, stream:read(string.packsize(versions[version].flf)))
|
||||||
remain = fsize
|
return name, read, remain
|
||||||
return name, read, fsize
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user