Advertisement
MeXaN1cK

LZW(lib)

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