mirror of
https://github.com/20kdc/OC-KittenOS.git
synced 2024-11-15 15:08:05 +11:00
0d9583fcff
This isn't getting pushed until AmandaC's tested it. I'm not sure this is such a good idea, anyway ; if it's for load/save workflow improvements, wouldn't it be better to have a file access method that allows for file re-opening? That said, there's a limit to *that* before you just have to say, "Just use /data/".
50 lines
1.2 KiB
Lua
50 lines
1.2 KiB
Lua
-- This is released into the public domain.
|
|
-- No warranty is provided, implied or otherwise.
|
|
|
|
-- BDIVIDE r5 edition
|
|
-- Algorithm simplified for smaller implementation and potentially better compression
|
|
-- format:
|
|
-- 0-127 for constants
|
|
-- <128 + (length - 4)>, <position high>, <position low>
|
|
-- Position is where in the window it was found, minus 1.
|
|
-- windowSize must be the same between the encoder and decoder,
|
|
-- and is the amount of data preserved after cropping.
|
|
|
|
io.write("\x00") -- initiation character
|
|
|
|
local blk = io.read("*a")
|
|
local windowSize = 0x10000
|
|
local windowData = ("\x00"):rep(windowSize)
|
|
|
|
local function crop(data)
|
|
windowData = (windowData .. data):sub(-windowSize)
|
|
end
|
|
|
|
while blk ~= "" do
|
|
local bestData = blk:sub(1, 1)
|
|
local bestRes = bestData
|
|
for lm = 0, 127 do
|
|
local al = lm + 4
|
|
local pfx = blk:sub(1, al)
|
|
if #pfx ~= al then
|
|
break
|
|
end
|
|
local p = windowData:find(pfx, 1, true)
|
|
if not p then
|
|
break
|
|
end
|
|
local pm = p - 1
|
|
local thirdByte = pm % 256
|
|
-- anti ']'-corruption helper
|
|
if thirdByte ~= 93 then
|
|
bestData = string.char(128 + lm, math.floor(pm / 256), thirdByte)
|
|
bestRes = pfx
|
|
end
|
|
end
|
|
-- ok, encode!
|
|
io.write(bestData)
|
|
crop(bestRes)
|
|
blk = blk:sub(#bestRes + 1)
|
|
end
|
|
|