Advertisement
MeXaN1cK

LZW

Mar 7th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.37 KB | None | 0 0
  1. local fls = require("filesystem")
  2. local shl = require("shell")
  3. local b32 = require("bit32")
  4. local args = shl.parse(...)
  5. frompath, topath = args[1], args[2]
  6.  
  7. VALUE = 0 LEN = 0
  8. function AddBite(value, len)
  9.   VALUE = b32.lshift(VALUE, len) + value
  10.   LEN = LEN + len
  11.   while LEN > 7 do
  12.     file:write(string.char(b32.rshift(VALUE, LEN - 8)))
  13.     VALUE = VALUE % (2 ^ (LEN - 8))
  14.     LEN = LEN - 8
  15.   end
  16. end
  17.  
  18.  
  19. function inflate()
  20.   OUTPUT = {}
  21.   local dict = {}
  22.   for i=0, 255 do
  23.     dict[string.char(i)] = i
  24.   end
  25.   INDEX = 255
  26.  
  27.   file = io.open(frompath, "r")
  28.   STR = file:read("*a")
  29.   file:close()
  30.  
  31.   file = io.open(topath, "wb")
  32.   word = STR:sub(1, 1)
  33.   for i=2, #STR do
  34.     if dict[word] == nil then
  35.       OUTPUT[#OUTPUT + 1] = dict[word:sub(1, -2)]
  36.       AddBite(OUTPUT[#OUTPUT], math.ceil(math.log(INDEX + 1, 2)))
  37.  
  38.       dict[word] = INDEX + 1
  39.       INDEX = INDEX + 1
  40.       word = word:sub(-1)..STR:sub(i, i)
  41.     else
  42.       word = word..STR:sub(i, i)
  43.     end
  44.   end
  45.   if dict[word] == nil then
  46.     OUTPUT[#OUTPUT + 1] = dict[word:sub(1, -2)]
  47.     AddBite(OUTPUT[#OUTPUT], math.ceil(math.log(INDEX + 1, 2)))
  48.     dict[word] = INDEX + 1
  49.     INDEX = INDEX + 1
  50.     word = word:sub(-1)
  51.   end
  52.   OUTPUT[#OUTPUT + 1] = dict[word]
  53.   AddBite(OUTPUT[#OUTPUT], math.ceil(math.log(INDEX + 1, 2)))  
  54.   if LEN ~= 0 then
  55.     file:write(string.char(b32.lshift(VALUE, 8 - LEN)))
  56.   end
  57.  
  58.   file:close()
  59. end
  60.  
  61. function PutBite() local len = math.ceil(math.log(INDEX, 2))
  62.   while LEN < len do
  63.     if POS > #STR1 then return false
  64.     end
  65.     VALUE = VALUE * 256 + STR1:byte(POS)
  66.     POS = POS + 1
  67.     LEN = LEN + 8
  68.   end
  69.   LEN = LEN - len
  70.   local tmp = b32.rshift(VALUE, LEN)
  71.   VALUE = VALUE % (2 ^ LEN)
  72.   return tmp
  73. end
  74.  
  75.  
  76. function deflate()
  77.   dict1 = {}
  78.   for i=0, 255 do
  79.     dict1[i] = string.char(i)
  80.   end
  81.   INDEX = 256
  82.   INPUT = ""
  83.   VALUE, POS, LEN = 0, 1, 0  
  84.   file = io.open(topath, "rb")
  85.   STR1 = file:read("*a")
  86.   file:close()
  87.  
  88.   local TEMP = PutBite()
  89.   INPUT = dict1[TEMP]
  90.   dict1[INDEX] = dict1[TEMP]
  91.   INDEX = INDEX + 1
  92.   i = 2
  93.   while true do
  94.     TEMP = PutBite()
  95.     if TEMP == false then break end
  96.     dict1[INDEX - 1] = dict1[INDEX - 1]..dict1[TEMP]:sub(1, 1)
  97.     INPUT=INPUT..dict1[TEMP]
  98.     dict1[INDEX]=dict1[TEMP]
  99.     INDEX = INDEX + 1
  100.     i = i + 1
  101.   end
  102. --  INPUT = INPUT..dict1[INDEX - 1]
  103. end
  104.  
  105. inflate()
  106. deflate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement