Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. --[[
  2. MIT License
  3.  
  4. Copyright (c) 2016 Rochet2
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. SOFTWARE.
  23. ]]
  24.  
  25. local char = string.char
  26. local type = type
  27. local select = select
  28. local sub = string.sub
  29. local tconcat = table.concat
  30.  
  31. local basedictcompress = {}
  32. local basedictdecompress = {}
  33. for i = 0, 255 do
  34. local ic, iic = char(i), char(i, 0)
  35. basedictcompress[ic] = iic
  36. basedictdecompress[iic] = ic
  37. end
  38.  
  39. local function dictAddA(str, dict, a, b)
  40. if a >= 256 then
  41. a, b = 0, b+1
  42. if b >= 256 then
  43. dict = {}
  44. b = 1
  45. end
  46. end
  47. dict[str] = char(a,b)
  48. a = a+1
  49. return dict, a, b
  50. end
  51.  
  52. local function compress(input)
  53. if type(input) ~= "string" then
  54. return nil, "string expected, got "..type(input)
  55. end
  56. local len = #input
  57. if len <= 1 then
  58. return "u"..input
  59. end
  60.  
  61. local dict = {}
  62. local a, b = 0, 1
  63.  
  64. local result = {"c"}
  65. local resultlen = 1
  66. local n = 2
  67. local word = ""
  68. for i = 1, len do
  69. local c = sub(input, i, i)
  70. local wc = word..c
  71. if not (basedictcompress[wc] or dict[wc]) then
  72. local write = basedictcompress[word] or dict[word]
  73. if not write then
  74. return nil, "algorithm error, could not fetch word"
  75. end
  76. result[n] = write
  77. resultlen = resultlen + #write
  78. n = n+1
  79. if len <= resultlen then
  80. return "u"..input
  81. end
  82. dict, a, b = dictAddA(wc, dict, a, b)
  83. word = c
  84. else
  85. word = wc
  86. end
  87. end
  88. result[n] = basedictcompress[word] or dict[word]
  89. resultlen = resultlen+#result[n]
  90. n = n+1
  91. if len <= resultlen then
  92. return "u"..input
  93. end
  94. return tconcat(result)
  95. end
  96.  
  97. local function dictAddB(str, dict, a, b)
  98. if a >= 256 then
  99. a, b = 0, b+1
  100. if b >= 256 then
  101. dict = {}
  102. b = 1
  103. end
  104. end
  105. dict[char(a,b)] = str
  106. a = a+1
  107. return dict, a, b
  108. end
  109.  
  110. local function decompress(input)
  111. if type(input) ~= "string" then
  112. return nil, "string expected, got "..type(input)
  113. end
  114.  
  115. if #input < 1 then
  116. return nil, "invalid input - not a compressed string"
  117. end
  118.  
  119. local control = sub(input, 1, 1)
  120. if control == "u" then
  121. return sub(input, 2)
  122. elseif control ~= "c" then
  123. return nil, "invalid input - not a compressed string"
  124. end
  125. input = sub(input, 2)
  126. local len = #input
  127.  
  128. if len < 2 then
  129. return nil, "invalid input - not a compressed string"
  130. end
  131.  
  132. local dict = {}
  133. local a, b = 0, 1
  134.  
  135. local result = {}
  136. local n = 1
  137. local last = sub(input, 1, 2)
  138. result[n] = basedictdecompress[last] or dict[last]
  139. n = n+1
  140. for i = 3, len, 2 do
  141. local code = sub(input, i, i+1)
  142. local lastStr = basedictdecompress[last] or dict[last]
  143. if not lastStr then
  144. return nil, "could not find last from dict. Invalid input?"
  145. end
  146. local toAdd = basedictdecompress[code] or dict[code]
  147. if toAdd then
  148. result[n] = toAdd
  149. n = n+1
  150. dict, a, b = dictAddB(lastStr..sub(toAdd, 1, 1), dict, a, b)
  151. else
  152. local tmp = lastStr..sub(lastStr, 1, 1)
  153. result[n] = tmp
  154. n = n+1
  155. dict, a, b = dictAddB(tmp, dict, a, b)
  156. end
  157. last = code
  158. end
  159. return tconcat(result)
  160. end
  161.  
  162. return {
  163. compress = compress,
  164. decompress = decompress,
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement