Compare commits

...

2 Commits

Author SHA1 Message Date
a82ce51d05 added a lz16 stream library, depending on lzss 2020-03-04 15:32:40 +11:00
78385370fd added a minitel archive library 2020-03-04 15:26:49 +11:00
2 changed files with 167 additions and 0 deletions

78
lz16/liblz16.lua Normal file
View File

@ -0,0 +1,78 @@
local lz = require "lzss"
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)
s=s or ""
local n = 0
local i = 1
while true do
local p = s:sub(i,i)
if p == "" then break end
local b = string.byte(p)
n = n << 8
n = n | b
i=i+1
end
return n
end
local function readBuffer(fi)
local stream = {}
if fi:read(4) ~= "lz16" and not tO.f then
return false, "not an lz16 archive"
end
function stream.read()
local len = toint(fi:read(2) or "\0\0")
if len < 1 then
print("nothing")
coroutine.yield()
return nil
end
coroutine.yield()
return lz.decompress(fi:read(len))
end
function stream.close()
fi:close()
end
return buffer.new("r",stream)
end
local function writeBuffer(fo)
local stream = {}
function stream:write(data)
local cblock = lz.compress(data)
fo:write(cint(cblock:len(),2)..cblock)
return cblock:len()+2
end
function stream.close()
fo:close()
end
fo:write("lz16") -- write header
return buffer.new("w",stream)
end
function lz16.buffer(stream)
if stream.mode.w then
print("write")
return writeBuffer(stream)
end
return readBuffer(stream)
end
function lz16.open(fname, mode)
local f = io.open(fname, mode)
if not f then return false end
f.mode.b = true
return lz16.buffer(f)
end
return lz16

89
mtar/libmtar.lua Normal file
View File

@ -0,0 +1,89 @@
local mtar = {}
-- detect OS hopefully
if _OSVERSION then
if _OSVERSION:sub(1,8) == "OpenOS" then
OPENOS = true
elseif _OSVERSION:sub(1,9) == "PsychOS" then
PSYCHOS = true
end
else
LINUX = true
end
local function mkdir(dir)
if OPENOS or LINUX then
os.execute("mkdir "..dest.."/"..dir.." &> /dev/null")
elseif PSYCHOS then
-- todo: write PsychOS support
end
end
local function toint(s)
s=s or ""
local n = 0
local i = 1
while true do
local p = s:sub(i,i)
if p == "" then break end
local b = string.byte(p)
n = n << 8
n = n | b
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
if segment == ".." then
pt[#pt] = nil
elseif segment ~= "." then
pt[#pt+1] = segment
end
end
return table.concat(pt,"/")
end
function mtar.genHeader(fname,len) -- 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))
end
function mtar.unarchive(stream,dest)
dest = dest or "."
mkdir(dest)
while true do
local nlen = toint(stream:read(2))
if nlen == 0 then
break
end
local name = cleanPath(stream:read(nlen))
local fsize = toint(stream:read(2))
print(name,fsize)
local dir = name:match("(.+)/.*%.?.+")
if (dir) then
mkdir(dir)
end
local f = io.open(dest.."/"..name,"wb")
local rsize,buf = fsize, ""
if f then
repeat
buf = stream:read(math.min(rsize,1024))
f:write(buf)
rsize = rsize - buf:len()
until rsize <= 1
f:close()
end
end
end
return mtar