Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function getL(tebr)
- local teb = {}
- for i,v in pairs(tebr) do
- teb[i] = v
- end
- local lowm
- local low2m
- local low
- local low2
- for i,v in pairs(teb) do
- if not low then
- low = i
- lowm = v[1]
- elseif v[1] < lowm then
- low = i
- lowm = v[1]
- end
- end
- teb[low] = nil
- for i,v in pairs(teb) do
- if not low2 then
- low2 = i
- low2m = v[1]
- elseif v[1] < low2m then
- low2 = i
- low2m = v[1]
- end
- end
- if low2 < low then
- local buf = low2
- low2 = low
- low = buf
- end
- return low, low2
- end
- local function it(com, cur, tab, hur)
- for i,v in pairs(tab) do
- if type(v[2]) == "table" then
- it(com .. i - 1, cur, v[2], hur + 1)
- else
- cur[v[2]] = com .. i - 1
- -- print(string.rep(" ", hur) .. v[2] .. " = " .. cur[v[2]])
- end
- end
- return com, cur
- end
- local function encode(text)
- local newt = {}
- local freq = {}
- for i = 1, #text do
- local char = string.byte(text:sub(i, i))
- if not freq[char] then
- freq[char] = 0
- end
- freq[char] = freq[char] + 1
- end
- for i,v in pairs(freq) do
- table.insert(newt, {v, i})
- end
- while #newt > 1 do
- local l1, l2 = getL(newt)
- local l1m = newt[l1]
- local l2m = newt[l2]
- table.remove(newt, l1)
- table.remove(newt, l2 - 1)
- table.insert(newt, {l1m[1] + l2m[1], {l1m, l2m}})
- end
- local d ,t = it("", {}, newt[1][2], 0)
- local new = ""
- for i = 1, #text do
- new = new .. t[text:sub(i, i):byte()]
- end
- return new, t
- end
- local function decode(text, dic)
- local new = ""
- for i = 1, #text do
- local got = false
- for m,v in pairs(dic) do
- if text:sub(1, #v) == v then
- new = new .. string.char(m)
- text = text:sub(#v + 1)
- break
- end
- end
- end
- return new
- end
- local tArgs = {...}
- if tArgs[1] == "e" then
- local file = fs.open(tArgs[2], "r")
- local dat = file.readAll()
- file.close()
- local t, d = encode(dat)
- local ard = textutils.serialize(d)
- local bw = ""
- local gof = {}
- for i = 1, #t do
- table.insert(gof, 1, tonumber(t:sub(i, i)))
- if #gof == 8 then
- local nby = 0
- for m,s in pairs(gof) do
- nby = nby + s * (2^(m - 1))
- end
- bw = bw .. string.char(nby)
- gof = {}
- end
- end
- local nby = 0
- for i = 1, 8-#gof do
- table.insert(gof, 1, 0)
- end
- for m,s in pairs(gof) do
- nby = nby + s * (2^(m - 1))
- end
- if nby ~= 0 then
- bw = bw .. string.char(nby)
- end
- file = fs.open(tArgs[3], "wb")
- local tow = #ard .. "a" .. ard .. #t .. "b" .. bw
- for i = 1, #tow do
- file.write(tow:sub(i, i):byte())
- end
- file.close()
- elseif tArgs[1] == "d" then
- local file = fs.open(tArgs[2], "rb")
- local newc = ""
- while true do
- local by = file.read()
- if by == nil then
- break
- else
- newc = newc .. string.char(by)
- end
- end
- local num = ""
- for i = 1, #newc do
- if newc:sub(i, i) == "a" then
- break
- else
- num = num .. newc:sub(i, i)
- end
- end
- num = tonumber(num)
- local nom = #tostring(num) + 1
- local toc = ""
- for i = 1 + nom, num + nom do
- toc = toc .. newc:sub(i, i)
- end
- nom = nom + #toc
- local num2 = ""
- for i = nom + 1, #newc do
- if newc:sub(i, i) == "b" then
- break
- else
- num2 = num2 .. newc:sub(i, i)
- end
- end
- num2 = tonumber(num2)
- nom = nom + #tostring(num2) + 1
- local newl = ""
- for i = 1 + nom, #newc do
- local by = newc:sub(i, i):byte()
- local newr = ""
- for i = 8, 1, -1 do
- if by >= 2^(i - 1) then
- newr = newr .. "1"
- by = by - 2^(i - 1)
- else
- newr = newr .. "0"
- end
- end
- newl = newl .. newr
- end
- newl = newl:sub(1, num2)
- local d = decode(newl, textutils.unserialize(toc))
- print(d)
- file.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement