Skip to content

Commit

Permalink
Improve LZ4CAT with ability to write to destination file
Browse files Browse the repository at this point in the history
  • Loading branch information
dschmenk committed Nov 22, 2024
1 parent d3a418a commit 2ac3304
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
Binary file modified images/apple/DCGR-RGB.PO
Binary file not shown.
Binary file modified images/apple/DCGR-TK.PO
Binary file not shown.
82 changes: 62 additions & 20 deletions src/samplesrc/lz4cat.pla
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ include "inc/args.plh"
include "inc/fileio.plh"
include "inc/lz4.plh"

const ERASE_DST = 1
struc t_header
word magic[2]
byte FLG
byte BD
end
word arg
byte ref
byte ioflags
byte inref, outref
char[64] infile, outfile
word[2] eof

def lz4ReadBlock(flags)#2
word size[2], block, data, len
len = fileio:read(ref, @size, 4)

len = fileio:read(inref, @size, 4)
if len <> 4 or size[0] == 0 or size[1] & $7FFF
return NULL, 0
fin
block = heapalloc(size[0])
if block
len = fileio:read(ref, block, size[0])
len = fileio:read(inref, block, size[0])
if len <> size[0]
heaprelease(block)
return NULL, 0
Expand Down Expand Up @@ -49,15 +53,15 @@ def lz4ReadBlock(flags)#2
heaprelease(block + len)
fin
if flags & $10 // Block Checksum
fileio:read(ref, @size, 4)
fileio:read(inref, @size, 4)
fin
return data, len
end
def lz4ReadFrame#0
word data, len
byte header[t_header], opt

fileio:read(ref, @header, t_header)
fileio:read(inref, @header, t_header)
if header:magic[1] <> $184D or header:magic[0] <> $2204
puts("Not LZ4 file.\n")
return
Expand All @@ -77,31 +81,69 @@ def lz4ReadFrame#0
if header.FLG & $01 // Dictionary ID
opt = opt + 4
fin
fileio:read(ref, heapmark, opt) // Read rest of header and throw away
fileio:read(inref, heapmark, opt) // Read rest of header and throw away
repeat
data, len = lz4ReadBlock(header.FLG)
if len
while len
putc(^data <> $0A ?? ^data :: $0D)
data++
len--
loop
if outref
fileio:write(outref, data, len)
else
while len
putc(^data <> $0A ?? ^data :: $0D)
data++
len--
loop
fin
heaprelease(data)
fin
until not data
if header.FLG & $04 // Content Checksun
fileio:read(ref, heapmark, 4)
fileio:read(inref, heapmark, 4)
fin
end

arg = argNext(argFirst)
if ^arg
ref = fileio:open(arg)
if ref
lz4ReadFrame
fileio:close(ref)
else
puts("File not found.\n")
while ^(arg + 1) == '-'
when toupper(^(arg + 2))
is 'E'
ioflags = ioflags | ERASE_DST
break
wend
arg = argNext(arg)
loop
strcpy(@infile, arg)
arg = argNext(arg)
if ^arg
strcpy(@outfile, arg)
fin
if infile
fileio:iobufalloc(2) // Reserve two I/O buffers
inref = fileio:open(@infile)
if inref
if outfile
if ioflags & ERASE_DST
fileio:destroy(@outfile)
fin
outref = fileio:open(@outfile)
if outref
eof[0], eof[1] = fileio:geteof(outref)#2
fileio:setmark(outref, eof[0], eof[1])
else
if fileio:create(@outfile, 0, 0) == FILE_ERR_OK
outref = fileio:open(@outfile)
else
puts("Error creating:"); puts(@outfile); putln
fin
fin
fin
lz4ReadFrame
fileio:close(0)
return 0
else
puts("Error reading:"); puts(@infile); putln
fin
fin
fin

puts("Usage: lz4cat <lz4 File> [output file]\n")
done

0 comments on commit 2ac3304

Please sign in to comment.