compression package - debugger fixes

This commit is contained in:
kepler155c@gmail.com
2020-06-01 16:54:28 -06:00
parent fb5e69f703
commit b289594c7f
10 changed files with 3676 additions and 443 deletions

3525
compress/apis/libdeflate.lua Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,7 @@
-- see: https://github.com/luarocks/luarocks/blob/master/src/luarocks/tools/tar.lua
-- A pure-Lua implementation of untar (unpacking .tar archives)
local tar = { }
local Util = require('opus.util')
local fs = _G.fs
@@ -54,7 +53,6 @@ local function checksum_header(block)
end
local function nullterm(s)
_G._zz = s
return s:match("^[^%z]*")
end
@@ -82,7 +80,7 @@ local function read_header_block(block)
return header
end
function tar.untar(filename, destdir, verbose)
local function untar(filename, destdir, verbose)
assert(type(filename) == "string")
assert(type(destdir) == "string")
@@ -165,7 +163,7 @@ function tar.untar(filename, destdir, verbose)
end
local function create_header_block(filename, abspath)
local block = ('\0'):rep(512)
local block = ('\0'):rep(blocksize)
local function number_to_octal(n)
return ('%o'):format(n)
@@ -185,7 +183,7 @@ local function create_header_block(filename, abspath)
end
-- the bare minimum for this program to untar
function tar.tar(filename, root, files)
local function tar(filename, root, files)
assert(type(filename) == "string")
assert(type(root) == "string")
assert(type(files) == "table")
@@ -197,15 +195,18 @@ function tar.tar(filename, root, files)
local abs = fs.combine(root, file)
local block = create_header_block(file, abs)
tar_handle:write(block)
local f = require('opus.util').readFile(abs, 'rb')
local f = Util.readFile(abs, 'rb')
tar_handle:write(f)
local padding = #f % 512
local padding = #f % blocksize
if padding > 0 then
tar_handle:write(('\0'):rep(512 - padding))
tar_handle:write(('\0'):rep(blocksize - padding))
end
end
tar_handle:close()
return true
end
return tar
return {
tar = tar,
untar = untar,
}

53
compress/compress.lua Normal file
View File

@@ -0,0 +1,53 @@
local LZW = require('compress.lzw')
local Tar = require('compress.tar')
local Util = require('opus.util')
local fs = _G.fs
local shell = _ENV.shell
local TMP_FILE = '.out.tar'
local args = { ... }
local files = { }
if not args[2] then
error('Syntax: tar OUTFILE DIR')
end
local file = shell.resolve(args[1])
local dir = shell.resolve(args[2])
local filetype = 'tar'
if file:match('(.+)%.tar$') then
filetype = 'tar'
elseif file:match('(.+)%.lzw$') then
filetype = 'lzw'
end
local function recurse(rel)
local abs = fs.combine(dir, rel)
for _,f in ipairs(fs.list(abs)) do
local fullName = fs.combine(abs, f)
if fs.native.isDir(fullName) then -- skip virtual dirs
recurse(fs.combine(rel, f))
else
table.insert(files, fs.combine(rel, f))
end
end
end
recurse('')
if filetype == 'tar' then
Tar.tar(file, dir, files)
elseif filetype == 'lzw' then
fs.mount(TMP_FILE, 'ramfs', 'file')
Tar.tar(TMP_FILE, dir, files)
local c = Util.readFile(TMP_FILE)
fs.delete(TMP_FILE)
c = LZW.compress(c)
Util.writeFile(file, c)
end

66
compress/uncompress.lua Normal file
View File

@@ -0,0 +1,66 @@
local DEFLATE = require('compress.deflatelua')
local LZW = require('compress.lzw')
local Tar = require('compress.tar')
local Util = require('opus.util')
local fs = _G.fs
local io = _G.io
local shell = _ENV.shell
local TMP_FILE = '.out.tar'
local args = { ... }
if not args[2] then
error('Syntax: tar FILE DESTDIR')
end
local inFile = shell.resolve(args[1])
local outDir = shell.resolve(args[2])
local s, m = pcall(function()
if inFile:match('(.+)%.[gG][zZ]$') then
local fh = io.open(inFile, 'rb') or error('Error opening ' .. inFile)
fs.mount(TMP_FILE, 'ramfs', 'file')
local ofh = io.open(TMP_FILE, 'wb')
DEFLATE.gunzip {input=fh, output=ofh, disable_crc=true}
fh:close()
ofh:close()
local s, m = Tar.untar(TMP_FILE, outDir, true)
if not s then
error(m)
end
elseif inFile:match('(.+)%.lzw$') then
local c = Util.readFile(inFile)
if not c then
error('Unable to open ' .. inFile)
end
fs.mount(TMP_FILE, 'ramfs', 'file')
Util.writeFile(TMP_FILE, LZW.decompress(c))
local s, m = Tar.untar(TMP_FILE, outDir, true)
if not s then
error(m)
end
else
local s, m = Tar.untar(inFile, outDir)
if not s then
error(m)
end
end
end)
fs.delete(TMP_FILE)
if not s then
error(m)
end

View File

@@ -1,46 +0,0 @@
-- see: https://github.com/luarocks/luarocks/blob/master/src/luarocks/tools/tar.lua
-- TODO: support normal tar syntax -- tar xvf
local DEFLATE = require('compress.deflatelua')
local tar = require('compress.tar')
local fs = _G.fs
local io = _G.io
local shell = _ENV.shell
local args = { ... }
if not args[2] then
error('Syntax: tar FILE DESTDIR')
end
local inFile = shell.resolve(args[1])
local outDir = shell.resolve(args[2])
if inFile:match('(.+)%.[gG][zZ]$') then
local TMP_FILE = '.out.tar'
local fh = io.open(inFile, 'rb') or error('Error opening ' .. inFile)
fs.mount(TMP_FILE, 'ramfs', 'file')
local ofh = io.open(TMP_FILE, 'wb')
DEFLATE.gunzip {input=fh, output=ofh, disable_crc=true}
fh:close()
ofh:close()
local s, m = tar.untar(TMP_FILE, outDir, true)
fs.delete(TMP_FILE)
if not s then
error(m)
end
else
local s, m = tar.untar(inFile, outDir)
if not s then
error(m)
end
end