Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local trm = require("term")
- local shl = require("shell")
- local fls = require("filesystem")
- local b32 = require("bit32")
- local args = shl.parse(...)
- VALUE = 0 LEN = 0
- function AddBite(value, len)
- VALUE = b32.lshift(VALUE, len) + value
- LEN = LEN + len
- while LEN > 7 do
- file:write(string.char(b32.rshift(VALUE, LEN - 8)))
- VALUE = VALUE % (2 ^ (LEN - 8))
- LEN = LEN - 8
- end
- end
- function deflate()
- file = io.open(args[1], "rb")
- if file == nil then trm.write("file "..args[1].." not found") os.exit()
- end
- STR = file:read("*a")
- file:close()
- local dict = {[""] = 0}
- local DLEN = 1
- local word = ""
- file = io.open(args[2], "wb")
- if file == nil then trm.write("file "..args[2].." already in use") os.exit()
- end
- for i=1, #STR do
- word = word..STR:sub(i, i)
- if dict[word] == nil then
- AddBite(dict[word:sub(1, -2)], math.ceil(math.log(DLEN+1, 2)))
- AddBite(word:byte(-1), 8)
- dict[word] = DLEN DLEN = DLEN + 1
- word = ""
- end
- end
- if word ~= "" then
- AddBite(dict[word], math.ceil(math.log(DLEN+1, 2)))
- end
- if LEN ~= 0 then
- file:write(string.char(b32.lshift(VALUE, 8 - LEN)))
- end
- file:close()
- end
- function PutBite(len)
- while LEN < len do
- if POS > #STR1 then return false
- end
- VALUE = VALUE * 256 + STR1:byte(POS)
- POS = POS + 1
- LEN = LEN + 8
- end
- LEN = LEN - len
- local tmp = b32.rshift(VALUE, LEN)
- VALUE = VALUE % (2 ^ LEN)
- return tmp
- end
- function inflate()
- file = io.open(args[2], "rb")
- if file == nil then trm.write("file "..args[2].." file no found") os.exit()
- end
- STR1 = file:read("*a")
- file:close()
- LEN = 0
- POS = 1
- VALUE = 0
- local dict = {[0] = ""}
- local DLE = 1
- OUT = ""
- while true do
- local TEMP = PutBite(math.ceil(math.log(DLE+1, 2)))
- local symbol = PutBite(8)
- if TEMP == false then break
- end
- if symbol == false then
- OUT = OUT..dict[TEMP]
- break
- else
- symbol = string.char(symbol)
- OUT = OUT..dict[TEMP]..symbol
- dict[DLE] = dict[TEMP]..symbol DLE = DLE + 1
- end
- end
- end
- deflate()
- inflate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement