local T=string.char local N=type local Z=select local E=string.sub local S=table.concat local t={}local g={}for N=0,255,1 do local Z,E=T(N),T(N,0)t[Z]=E g[E]=Z end local function Y(N,Z,E,S)if E>=256 then E,S=0,S+1 if S>=256 then Z={}S=1 end end Z[N]=T(E,S)E=E+1 return Z,E,S end local function P(T)if N(T)~="\115\116\114\105\110\103"then return nil,"\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\044 \103\111\116 "..N(T)end local Z=#T if Z<=1 then return"\117"..T end local g={}local P,O=0,1 local G={"\099"}local v=1 local p=2 local K=""for N=1,Z,1 do local S=E(T,N,N)local C=K..S if not(t[C]or g[C])then local N=t[K]or g[K]if not N then return nil,"\097\108\103\111\114\105\116\104\109 \101\114\114\111\114\044 \099\111\117\108\100 \110\111\116 \102\101\116\099\104 \119\111\114\100"end G[p]=N v=v+#N p=p+1 if Z<=v then return"\117"..T end g,P,O=Y(C,g,P,O)K=S else K=C end end G[p]=t[K]or g[K]v=v+#G[p]p=p+1 if Z<=v then return"\117"..T end return S(G)end local function O(N,Z,E,S)if E>=256 then E,S=0,S+1 if S>=256 then Z={}S=1 end end Z[T(E,S)]=N E=E+1 return Z,E,S end local function G(T)if N(T)~="\115\116\114\105\110\103"then return nil,"\115\116\114\105\110\103 \101\120\112\101\099\116\101\100\044 \103\111\116 "..N(T)end if#T<1 then return nil,"\105\110\118\097\108\105\100 \105\110\112\117\116 - \110\111\116 \097 \099\111\109\112\114\101\115\115\101\100 \115\116\114\105\110\103"end local Z=E(T,1,1)if Z=="\117"then return E(T,2)elseif Z~="\099"then return nil,"\105\110\118\097\108\105\100 \105\110\112\117\116 - \110\111\116 \097 \099\111\109\112\114\101\115\115\101\100 \115\116\114\105\110\103"end T=E(T,2)local t=#T if t<2 then return nil,"\105\110\118\097\108\105\100 \105\110\112\117\116 - \110\111\116 \097 \099\111\109\112\114\101\115\115\101\100 \115\116\114\105\110\103"end local Y={}local P,G=0,1 local v={}local p=1 local K=E(T,1,2)v[p]=g[K]or Y[K]p=p+1 for N=3,t,2 do local Z=E(T,N,N+1)local S=g[K]or Y[K]if not S then return nil,"\099\111\117\108\100 \110\111\116 \102\105\110\100 \108\097\115\116 \102\114\111\109 \100\105\099\116\046 \073\110\118\097\108\105\100 \105\110\112\117\116\063"end local t=g[Z]or Y[Z]if t then v[p]=t p=p+1 Y,P,G=O(S..E(t,1,1),Y,P,G)else local T=S..E(S,1,1)v[p]=T p=p+1 Y,P,G=O(T,Y,P,G)end K=Z end return S(v)end return{Compress=P;Decompress=G}
--[[
MIT License
Copyright (c) 2016 Rochet2
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]

local char = string.char
local type = type
local select = select
local sub = string.sub
local tconcat = table.concat

local basedictcompress = {}
local basedictdecompress = {}
for i = 0, 255 do
    local ic, iic = char(i), char(i, 0)
    basedictcompress[ic] = iic
    basedictdecompress[iic] = ic
end

local function dictAddA(str, dict, a, b)
    if a >= 256 then
        a, b = 0, b+1
        if b >= 256 then
            dict = {}
            b = 1
        end
    end
    dict[str] = char(a,b)
    a = a+1
    return dict, a, b
end

local function compress(input)
    if type(input) ~= "string" then
        return nil, "string expected, got "..type(input)
    end
    local len = #input
    if len <= 1 then
        return "u"..input
    end

    local dict = {}
    local a, b = 0, 1

    local result = {"c"}
    local resultlen = 1
    local n = 2
    local word = ""
    for i = 1, len do
        local c = sub(input, i, i)
        local wc = word..c
        if not (basedictcompress[wc] or dict[wc]) then
            local write = basedictcompress[word] or dict[word]
            if not write then
                return nil, "algorithm error, could not fetch word"
            end
            result[n] = write
            resultlen = resultlen + #write
            n = n+1
            if  len <= resultlen then
                return "u"..input
            end
            dict, a, b = dictAddA(wc, dict, a, b)
            word = c
        else
            word = wc
        end
    end
    result[n] = basedictcompress[word] or dict[word]
    resultlen = resultlen+#result[n]
    n = n+1
    if  len <= resultlen then
        return "u"..input
    end
    return tconcat(result)
end

local function dictAddB(str, dict, a, b)
    if a >= 256 then
        a, b = 0, b+1
        if b >= 256 then
            dict = {}
            b = 1
        end
    end
    dict[char(a,b)] = str
    a = a+1
    return dict, a, b
end

local function decompress(input)
    if type(input) ~= "string" then
        return nil, "string expected, got "..type(input)
    end

    if #input < 1 then
        return nil, "invalid input - not a compressed string"
    end

    local control = sub(input, 1, 1)
    if control == "u" then
        return sub(input, 2)
    elseif control ~= "c" then
        return nil, "invalid input - not a compressed string"
    end
    input = sub(input, 2)
    local len = #input

    if len < 2 then
        return nil, "invalid input - not a compressed string"
    end

    local dict = {}
    local a, b = 0, 1

    local result = {}
    local n = 1
    local last = sub(input, 1, 2)
    result[n] = basedictdecompress[last] or dict[last]
    n = n+1
    for i = 3, len, 2 do
        local code = sub(input, i, i+1)
        local lastStr = basedictdecompress[last] or dict[last]
        if not lastStr then
            return nil, "could not find last from dict. Invalid input?"
        end
        local toAdd = basedictdecompress[code] or dict[code]
        if toAdd then
            result[n] = toAdd
            n = n+1
            dict, a, b = dictAddB(lastStr..sub(toAdd, 1, 1), dict, a, b)
        else
            local tmp = lastStr..sub(lastStr, 1, 1)
            result[n] = tmp
            n = n+1
            dict, a, b = dictAddB(tmp, dict, a, b)
        end
        last = code
    end
    return tconcat(result)
end

return {
    Compress = compress,
    Decompress = decompress,
}