Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fls = require("filesystem")
- local shl = require("shell")
- local b32 = require("bit32")
- LZW = {}
- function LZW.inflate(frompath, topath) local VALUE, LEN, OUTPUT, dict, INDEX = 0, 0, {}, {}, 255
- local 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
- for i=0, 255 do
- dict[string.char(i)] = i
- end
- file = io.open(frompath, "r")
- local STR = file:read("*a")
- file:close()
- file = io.open(topath, "wb")
- local word = STR:sub(1, 1)
- for i=2, #STR do
- if dict[word] == nil then
- OUTPUT[#OUTPUT + 1] = dict[word:sub(1, -2)]
- AddBite(OUTPUT[#OUTPUT], math.ceil(math.log(INDEX + 1, 2)))
- dict[word] = INDEX + 1
- INDEX = INDEX + 1
- word = word:sub(-1)..STR:sub(i, i)
- else
- word = word..STR:sub(i, i)
- end
- end
- if dict[word] == nil then
- OUTPUT[#OUTPUT + 1] = dict[word:sub(1, -2)]
- AddBite(OUTPUT[#OUTPUT], math.ceil(math.log(INDEX + 1, 2)))
- dict[word] = INDEX + 1
- INDEX = INDEX + 1
- word = word:sub(-1)
- end
- OUTPUT[#OUTPUT + 1] = dict[word]
- AddBite(OUTPUT[#OUTPUT], math.ceil(math.log(INDEX + 1, 2)))
- if LEN ~= 0 then
- file:write(string.char(b32.lshift(VALUE, 8 - LEN)))
- end
- file:close()
- end
- function LZW.deflate(frompath, topath) local dict1, INDEX, INPUT, POS, VALUE, LEN, STR1
- function PutBite() local len = math.ceil(math.log(INDEX, 2))
- 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
- dict1 = {}
- for i=0, 255 do
- dict1[i] = string.char(i)
- end
- INDEX = 256
- INPUT = ""
- VALUE, POS, LEN = 0, 1, 0
- file = io.open(frompath, "rb")
- STR1 = file:read("*a")
- file:close()
- local TEMP = PutBite()
- INPUT = dict1[TEMP]
- dict1[INDEX] = dict1[TEMP]
- INDEX = INDEX + 1
- i = 2
- while true do
- TEMP = PutBite()
- if TEMP == false then break end
- dict1[INDEX - 1] = dict1[INDEX - 1]..dict1[TEMP]:sub(1, 1)
- INPUT=INPUT..dict1[TEMP]
- dict1[INDEX]=dict1[TEMP]
- INDEX = INDEX + 1
- i = i + 1
- end
- file = io.open(topath, "wb")
- file:write(INPUT)
- file:close()
- end
- return LZW
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement