Advertisement
osmarks

LibDeflate for potatOS

Apr 17th, 2020
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 96.09 KB | None | 0 0
  1. --[[--
  2. LibDeflate ported from here (https://github.com/MCJack123/CC-Archive/blob/master/LibDeflate.lua) to PotatOS
  3. ]]
  4.  
  5. --[[
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program.  If not, see https://www.gnu.org/licenses/.
  18.  
  19. Credits:
  20. 1. zlib, by Jean-loup Gailly (compression) and Mark Adler (decompression).
  21.     http://www.zlib.net/
  22.     Licensed under zlib License. http://www.zlib.net/zlib_license.html
  23.     For the compression algorithm.
  24. 2. puff, by Mark Adler. https://github.com/madler/zlib/tree/master/contrib/puff
  25.     Licensed under zlib License. http://www.zlib.net/zlib_license.html
  26.     For the decompression algorithm.
  27. 3. LibCompress, by jjsheets and Galmok of European Stormrage (Horde)
  28.     https://www.wowace.com/projects/libcompress
  29.     Licensed under GPLv2.
  30.     https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  31.     For the code to create customized codec.
  32. ]]
  33.  
  34. local LibDeflate
  35.  
  36. do
  37.     -- Semantic version. all lowercase.
  38.     -- Suffix can be alpha1, alpha2, beta1, beta2, rc1, rc2, etc.
  39.     -- NOTE: Two version numbers needs to modify.
  40.     -- 1. On the top of LibDeflate.lua
  41.     -- 2. HERE
  42.     local _VERSION = "1.0.0-release"
  43.  
  44.     local _COPYRIGHT =
  45.     "LibDeflate ".._VERSION
  46.     .." Copyright (C) 2018 Haoqian He."
  47.     .." License GPLv3+: GNU GPL version 3 or later"
  48.  
  49.     LibDeflate = {}
  50.  
  51.     LibDeflate._VERSION = _VERSION
  52.     LibDeflate._COPYRIGHT = _COPYRIGHT
  53. end
  54.  
  55. -- localize Lua api for faster access.
  56. local assert = assert
  57. local error = error
  58. local pairs = pairs
  59. local string_byte = string.byte
  60. local string_char = string.char
  61. local string_find = string.find
  62. local string_gsub = string.gsub
  63. local string_sub = string.sub
  64. local table_concat = table.concat
  65. local table_sort = table.sort
  66. local tostring = tostring
  67. local type = type
  68.  
  69. -- Converts i to 2^i, (0<=i<=32)
  70. -- This is used to implement bit left shift and bit right shift.
  71. -- "x >> y" in C:   "(x-x%_pow2[y])/_pow2[y]" in Lua
  72. -- "x << y" in C:   "x*_pow2[y]" in Lua
  73. local _pow2 = {}
  74.  
  75. -- Converts any byte to a character, (0<=byte<=255)
  76. local _byte_to_char = {}
  77.  
  78. -- _reverseBitsTbl[len][val] stores the bit reverse of
  79. -- the number with bit length "len" and value "val"
  80. -- For example, decimal number 6 with bits length 5 is binary 00110
  81. -- It's reverse is binary 01100,
  82. -- which is decimal 12 and 12 == _reverseBitsTbl[5][6]
  83. -- 1<=len<=9, 0<=val<=2^len-1
  84. -- The reason for 1<=len<=9 is that the max of min bitlen of huffman code
  85. -- of a huffman alphabet is 9?
  86. local _reverse_bits_tbl = {}
  87.  
  88. -- Convert a LZ77 length (3<=len<=258) to
  89. -- a deflate literal/LZ77_length code (257<=code<=285)
  90. local _length_to_deflate_code = {}
  91.  
  92. -- convert a LZ77 length (3<=len<=258) to
  93. -- a deflate literal/LZ77_length code extra bits.
  94. local _length_to_deflate_extra_bits = {}
  95.  
  96. -- Convert a LZ77 length (3<=len<=258) to
  97. -- a deflate literal/LZ77_length code extra bit length.
  98. local _length_to_deflate_extra_bitlen = {}
  99.  
  100. -- Convert a small LZ77 distance (1<=dist<=256) to a deflate code.
  101. local _dist256_to_deflate_code = {}
  102.  
  103. -- Convert a small LZ77 distance (1<=dist<=256) to
  104. -- a deflate distance code extra bits.
  105. local _dist256_to_deflate_extra_bits = {}
  106.  
  107. -- Convert a small LZ77 distance (1<=dist<=256) to
  108. -- a deflate distance code extra bit length.
  109. local _dist256_to_deflate_extra_bitlen = {}
  110.  
  111. -- Convert a literal/LZ77_length deflate code to LZ77 base length
  112. -- The key of the table is (code - 256), 257<=code<=285
  113. local _literal_deflate_code_to_base_len = {
  114.     3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  115.     35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258,
  116. }
  117.  
  118. -- Convert a literal/LZ77_length deflate code to base LZ77 length extra bits
  119. -- The key of the table is (code - 256), 257<=code<=285
  120. local _literal_deflate_code_to_extra_bitlen = {
  121.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  122.     3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0,
  123. }
  124.  
  125. -- Convert a distance deflate code to base LZ77 distance. (0<=code<=29)
  126. local _dist_deflate_code_to_base_dist = {
  127.     [0] = 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  128.     257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  129.     8193, 12289, 16385, 24577,
  130. }
  131.  
  132. -- Convert a distance deflate code to LZ77 bits length. (0<=code<=29)
  133. local _dist_deflate_code_to_extra_bitlen = {
  134.     [0] = 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  135.     7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
  136. }
  137.  
  138. -- The code order of the first huffman header in the dynamic deflate block.
  139. -- See the page 12 of RFC1951
  140. local _rle_codes_huffman_bitlen_order = {16, 17, 18,
  141.     0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
  142. }
  143.  
  144. -- The following tables are used by fixed deflate block.
  145. -- The value of these tables are assigned at the bottom of the source.
  146.  
  147. -- The huffman code of the literal/LZ77_length deflate codes,
  148. -- in fixed deflate block.
  149. local _fix_block_literal_huffman_code
  150.  
  151. -- Convert huffman code of the literal/LZ77_length to deflate codes,
  152. -- in fixed deflate block.
  153. local _fix_block_literal_huffman_to_deflate_code
  154.  
  155. -- The bit length of the huffman code of literal/LZ77_length deflate codes,
  156. -- in fixed deflate block.
  157. local _fix_block_literal_huffman_bitlen
  158.  
  159. -- The count of each bit length of the literal/LZ77_length deflate codes,
  160. -- in fixed deflate block.
  161. local _fix_block_literal_huffman_bitlen_count
  162.  
  163. -- The huffman code of the distance deflate codes,
  164. -- in fixed deflate block.
  165. local _fix_block_dist_huffman_code
  166.  
  167. -- Convert huffman code of the distance to deflate codes,
  168. -- in fixed deflate block.
  169. local _fix_block_dist_huffman_to_deflate_code
  170.  
  171. -- The bit length of the huffman code of the distance deflate codes,
  172. -- in fixed deflate block.
  173. local _fix_block_dist_huffman_bitlen
  174.  
  175. -- The count of each bit length of the huffman code of
  176. -- the distance deflate codes,
  177. -- in fixed deflate block.
  178. local _fix_block_dist_huffman_bitlen_count
  179.  
  180. for i = 0, 255 do
  181.     _byte_to_char[i] = string_char(i)
  182. end
  183.  
  184. do
  185.     local pow = 1
  186.     for i = 0, 32 do
  187.         _pow2[i] = pow
  188.         pow = pow * 2
  189.     end
  190. end
  191.  
  192. for i = 1, 9 do
  193.     _reverse_bits_tbl[i] = {}
  194.     for j=0, _pow2[i+1]-1 do
  195.         local reverse = 0
  196.         local value = j
  197.         for _ = 1, i do
  198.             -- The following line is equivalent to "res | (code %2)" in C.
  199.             reverse = reverse - reverse%2
  200.                 + (((reverse%2==1) or (value % 2) == 1) and 1 or 0)
  201.             value = (value-value%2)/2
  202.             reverse = reverse * 2
  203.         end
  204.         _reverse_bits_tbl[i][j] = (reverse-reverse%2)/2
  205.     end
  206. end
  207.  
  208. -- The source code is written according to the pattern in the numbers
  209. -- in RFC1951 Page10.
  210. do
  211.     local a = 18
  212.     local b = 16
  213.     local c = 265
  214.     local bitlen = 1
  215.     for len = 3, 258 do
  216.         if len <= 10 then
  217.             _length_to_deflate_code[len] = len + 254
  218.             _length_to_deflate_extra_bitlen[len] = 0
  219.         elseif len == 258 then
  220.             _length_to_deflate_code[len] = 285
  221.             _length_to_deflate_extra_bitlen[len] = 0
  222.         else
  223.             if len > a then
  224.                 a = a + b
  225.                 b = b * 2
  226.                 c = c + 4
  227.                 bitlen = bitlen + 1
  228.             end
  229.             local t = len-a-1+b/2
  230.             _length_to_deflate_code[len] = (t-(t%(b/8)))/(b/8) + c
  231.             _length_to_deflate_extra_bitlen[len] = bitlen
  232.             _length_to_deflate_extra_bits[len] = t % (b/8)
  233.         end
  234.     end
  235. end
  236.  
  237. -- The source code is written according to the pattern in the numbers
  238. -- in RFC1951 Page11.
  239. do
  240.     _dist256_to_deflate_code[1] = 0
  241.     _dist256_to_deflate_code[2] = 1
  242.     _dist256_to_deflate_extra_bitlen[1] = 0
  243.     _dist256_to_deflate_extra_bitlen[2] = 0
  244.  
  245.     local a = 3
  246.     local b = 4
  247.     local code = 2
  248.     local bitlen = 0
  249.     for dist = 3, 256 do
  250.         if dist > b then
  251.             a = a * 2
  252.             b = b * 2
  253.             code = code + 2
  254.             bitlen = bitlen + 1
  255.         end
  256.         _dist256_to_deflate_code[dist] = (dist <= a) and code or (code+1)
  257.         _dist256_to_deflate_extra_bitlen[dist] = (bitlen < 0) and 0 or bitlen
  258.         if b >= 8 then
  259.             _dist256_to_deflate_extra_bits[dist] = (dist-b/2-1) % (b/4)
  260.         end
  261.     end
  262. end
  263.  
  264. -- CRC-16/CRC-32 computation
  265. local band, bnot, xor, lshift, rshift
  266.  
  267. if bit ~= nil then
  268.     band = bit.band
  269.     bnot = bit.bnot
  270.     xor = bit.bxor
  271.     lshift = bit.blshift
  272.     rshift = bit.blogic_rshift
  273. elseif bit32 ~= nil then
  274.     band = bit32.band
  275.     bnot = bit32.bnot
  276.     xor = bit32.bxor
  277.     lshift = bit32.lshift
  278.     rshift = bit32.rshift
  279. else
  280.     error "Native bitops not available"
  281. end
  282.  
  283. --- Calculate the Adler-32 checksum of the string. <br>
  284. -- See RFC1950 Page 9 https://tools.ietf.org/html/rfc1950 for the
  285. -- definition of Adler-32 checksum.
  286. -- @param str [string] the input string to calcuate its Adler-32 checksum.
  287. -- @return [integer] The Adler-32 checksum, which is greater or equal to 0,
  288. -- and less than 2^32 (4294967296).
  289. function LibDeflate.Adler32(str)
  290.     -- This function is loop unrolled by better performance.
  291.     --
  292.     -- Here is the minimum code:
  293.     --
  294.     -- local a = 1
  295.     -- local b = 0
  296.     -- for i=1, #str do
  297.     --      local s = string.byte(str, i, i)
  298.     --      a = (a+s)%65521
  299.     --      b = (b+a)%65521
  300.     --      end
  301.     -- return b*65536+a
  302.     if type(str) ~= "string" then
  303.         error(("Usage: LibDeflate.Adler32(str):"
  304.             .." 'str' - string expected got '%s'."):format(type(str)), 2)
  305.     end
  306.     local strlen = #str
  307.  
  308.     local i = 1
  309.     local a = 1
  310.     local b = 0
  311.     while i <= strlen - 15 do
  312.         local x1, x2, x3, x4, x5, x6, x7, x8,
  313.             x9, x10, x11, x12, x13, x14, x15, x16 = string_byte(str, i, i+15)
  314.         b = (b+16*a+16*x1+15*x2+14*x3+13*x4+12*x5+11*x6+10*x7+9*x8+8*x9
  315.             +7*x10+6*x11+5*x12+4*x13+3*x14+2*x15+x16)%65521
  316.         a = (a+x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12+x13+x14+x15+x16)%65521
  317.         i =  i + 16
  318.     end
  319.     while (i <= strlen) do
  320.         local x = string_byte(str, i, i)
  321.         a = (a + x) % 65521
  322.         b = (b + a) % 65521
  323.         i = i + 1
  324.     end
  325.     return (b*65536+a) % 4294967296
  326. end
  327.  
  328. -- Compare adler32 checksum.
  329. -- adler32 should be compared with a mod to avoid sign problem
  330. -- 4072834167 (unsigned) is the same adler32 as -222133129
  331. local function IsEqualAdler32(actual, expected)
  332.     return (actual % 4294967296) == (expected % 4294967296)
  333. end
  334.  
  335. --- Create a preset dictionary.
  336. --
  337. -- This function is not fast, and the memory consumption of the produced
  338. -- dictionary is about 50 times of the input string. Therefore, it is suggestted
  339. -- to run this function only once in your program.
  340. --
  341. -- It is very important to know that if you do use a preset dictionary,
  342. -- compressors and decompressors MUST USE THE SAME dictionary. That is,
  343. -- dictionary must be created using the same string. If you update your program
  344. -- with a new dictionary, people with the old version won't be able to transmit
  345. -- data with people with the new version. Therefore, changing the dictionary
  346. -- must be very careful.
  347. --
  348. -- The parameters "strlen" and "adler32" add a layer of verification to ensure
  349. -- the parameter "str" is not modified unintentionally during the program
  350. -- development.
  351. --
  352. -- @usage local dict_str = "1234567890"
  353. --
  354. -- -- print(dict_str:len(), LibDeflate.Adler32(dict_str))
  355. -- -- Hardcode the print result below to verify it to avoid acciently
  356. -- -- modification of 'str' during the program development.
  357. -- -- string length: 10, Adler-32: 187433486,
  358. -- -- Don't calculate string length and its Adler-32 at run-time.
  359. --
  360. -- local dict = LibDeflate.CreateDictionary(dict_str, 10, 187433486)
  361. --
  362. -- @param str [string] The string used as the preset dictionary. <br>
  363. -- You should put stuffs that frequently appears in the dictionary
  364. -- string and preferablely put more frequently appeared stuffs toward the end
  365. -- of the string. <br>
  366. -- Empty string and string longer than 32768 bytes are not allowed.
  367. -- @param strlen [integer] The length of 'str'. Please pass in this parameter
  368. -- as a hardcoded constant, in order to verify the content of 'str'. The value
  369. -- of this parameter should be known before your program runs.
  370. -- @param adler32 [integer] The Adler-32 checksum of 'str'. Please pass in this
  371. -- parameter as a hardcoded constant, in order to verify the content of 'str'.
  372. -- The value of this parameter should be known before your program runs.
  373. -- @return  [table] The dictionary used for preset dictionary compression and
  374. -- decompression.
  375. -- @raise error if 'strlen' does not match the length of 'str',
  376. -- or if 'adler32' does not match the Adler-32 checksum of 'str'.
  377. function LibDeflate.CreateDictionary(str, strlen, adler32)
  378.     if type(str) ~= "string" then
  379.         error(("Usage: LibDeflate.CreateDictionary(str, strlen, adler32):"
  380.             .." 'str' - string expected got '%s'."):format(type(str)), 2)
  381.     end
  382.     if type(strlen) ~= "number" then
  383.         error(("Usage: LibDeflate.CreateDictionary(str, strlen, adler32):"
  384.             .." 'strlen' - number expected got '%s'."):format(
  385.             type(strlen)), 2)
  386.     end
  387.     if type(adler32) ~= "number" then
  388.         error(("Usage: LibDeflate.CreateDictionary(str, strlen, adler32):"
  389.             .." 'adler32' - number expected got '%s'."):format(
  390.             type(adler32)), 2)
  391.     end
  392.     if strlen ~= #str then
  393.         error(("Usage: LibDeflate.CreateDictionary(str, strlen, adler32):"
  394.                 .." 'strlen' does not match the actual length of 'str'."
  395.                 .." 'strlen': %u, '#str': %u ."
  396.                 .." Please check if 'str' is modified unintentionally.")
  397.             :format(strlen, #str))
  398.     end
  399.     if strlen == 0 then
  400.         error(("Usage: LibDeflate.CreateDictionary(str, strlen, adler32):"
  401.             .." 'str' - Empty string is not allowed."), 2)
  402.     end
  403.     if strlen > 32768 then
  404.         error(("Usage: LibDeflate.CreateDictionary(str, strlen, adler32):"
  405.             .." 'str' - string longer than 32768 bytes is not allowed."
  406.              .." Got %d bytes."):format(strlen), 2)
  407.     end
  408.     local actual_adler32 = self:Adler32(str)
  409.     if not IsEqualAdler32(adler32, actual_adler32) then
  410.         error(("Usage: LibDeflate.CreateDictionary(str, strlen, adler32):"
  411.                 .." 'adler32' does not match the actual adler32 of 'str'."
  412.                 .." 'adler32': %u, 'Adler32(str)': %u ."
  413.                 .." Please check if 'str' is modified unintentionally.")
  414.             :format(adler32, actual_adler32))
  415.     end
  416.  
  417.     local dictionary = {}
  418.     dictionary.adler32 = adler32
  419.     dictionary.hash_tables = {}
  420.     dictionary.string_table = {}
  421.     dictionary.strlen = strlen
  422.     local string_table = dictionary.string_table
  423.     local hash_tables = dictionary.hash_tables
  424.     string_table[1] = string_byte(str, 1, 1)
  425.     string_table[2] = string_byte(str, 2, 2)
  426.     if strlen >= 3 then
  427.         local i = 1
  428.         local hash = string_table[1]*256+string_table[2]
  429.         while i <= strlen - 2 - 3 do
  430.             local x1, x2, x3, x4 = string_byte(str, i+2, i+5)
  431.             string_table[i+2] = x1
  432.             string_table[i+3] = x2
  433.             string_table[i+4] = x3
  434.             string_table[i+5] = x4
  435.             hash = (hash*256+x1)%16777216
  436.             local t = hash_tables[hash]
  437.             if not t then t = {}; hash_tables[hash] = t end
  438.             t[#t+1] = i-strlen
  439.             i = i + 1
  440.             hash = (hash*256+x2)%16777216
  441.             t = hash_tables[hash]
  442.             if not t then t = {}; hash_tables[hash] = t end
  443.             t[#t+1] = i-strlen
  444.             i = i + 1
  445.             hash = (hash*256+x3)%16777216
  446.             t = hash_tables[hash]
  447.             if not t then t = {}; hash_tables[hash] = t end
  448.             t[#t+1] = i-strlen
  449.             i = i + 1
  450.             hash = (hash*256+x4)%16777216
  451.             t = hash_tables[hash]
  452.             if not t then t = {}; hash_tables[hash] = t end
  453.             t[#t+1] = i-strlen
  454.             i = i + 1
  455.         end
  456.         while i <= strlen - 2 do
  457.             local x = string_byte(str, i+2)
  458.             string_table[i+2] = x
  459.             hash = (hash*256+x)%16777216
  460.             local t = hash_tables[hash]
  461.             if not t then t = {}; hash_tables[hash] = t end
  462.             t[#t+1] = i-strlen
  463.             i = i + 1
  464.         end
  465.     end
  466.     return dictionary
  467. end
  468.  
  469. -- Check if the dictionary is valid.
  470. -- @param dictionary The preset dictionary for compression and decompression.
  471. -- @return true if valid, false if not valid.
  472. -- @return if not valid, the error message.
  473. local function IsValidDictionary(dictionary)
  474.     if type(dictionary) ~= "table" then
  475.         return false, ("'dictionary' - table expected got '%s'.")
  476.             :format(type(dictionary))
  477.     end
  478.     if type(dictionary.adler32) ~= "number"
  479.         or type(dictionary.string_table) ~= "table"
  480.         or type(dictionary.strlen) ~= "number"
  481.         or dictionary.strlen <= 0
  482.         or dictionary.strlen > 32768
  483.         or dictionary.strlen ~= #dictionary.string_table
  484.         or type(dictionary.hash_tables) ~= "table"
  485.         then
  486.         return false, ("'dictionary' - corrupted dictionary.")
  487.             :format(type(dictionary))
  488.     end
  489.     return true, ""
  490. end
  491.  
  492. --[[
  493.     key of the configuration table is the compression level,
  494.     and its value stores the compression setting.
  495.     These numbers come from zlib source code.
  496.  
  497.     Higher compression level usually means better compression.
  498.     (Because LibDeflate uses a simplified version of zlib algorithm,
  499.     there is no guarantee that higher compression level does not create
  500.     bigger file than lower level, but I can say it's 99% likely)
  501.  
  502.     Be careful with the high compression level. This is a pure lua
  503.     implementation compressor/decompressor, which is significant slower than
  504.     a C/C++ equivalant compressor/decompressor. Very high compression level
  505.     costs significant more CPU time, and usually compression size won't be
  506.     significant smaller when you increase compression level by 1, when the
  507.     level is already very high. Benchmark yourself if you can afford it.
  508.  
  509.     See also https://github.com/madler/zlib/blob/master/doc/algorithm.txt,
  510.     https://github.com/madler/zlib/blob/master/deflate.c for more information.
  511.  
  512.     The meaning of each field:
  513.     @field 1 use_lazy_evaluation:
  514.         true/false. Whether the program uses lazy evaluation.
  515.         See what is "lazy evaluation" in the link above.
  516.         lazy_evaluation improves ratio, but relatively slow.
  517.     @field 2 good_prev_length:
  518.         Only effective if lazy is set, Only use 1/4 of max_chain,
  519.         if prev length of lazy match is above this.
  520.     @field 3 max_insert_length/max_lazy_match:
  521.         If not using lazy evaluation,
  522.         insert new strings in the hash table only if the match length is not
  523.         greater than this length.
  524.         If using lazy evaluation, only continue lazy evaluation,
  525.         if previous match length is strictly smaller than this value.
  526.     @field 4 nice_length:
  527.         Number. Don't continue to go down the hash chain,
  528.         if match length is above this.
  529.     @field 5 max_chain:
  530.         Number. The maximum number of hash chains we look.
  531. --]]
  532. local _compression_level_configs = {
  533.     [0] = {false, nil, 0, 0, 0}, -- level 0, no compression
  534.     [1] = {false, nil, 4, 8, 4}, -- level 1, similar to zlib level 1
  535.     [2] = {false, nil, 5, 18, 8}, -- level 2, similar to zlib level 2
  536.     [3] = {false, nil, 6, 32, 32},  -- level 3, similar to zlib level 3
  537.     [4] = {true, 4, 4, 16, 16}, -- level 4, similar to zlib level 4
  538.     [5] = {true, 8, 16, 32, 32}, -- level 5, similar to zlib level 5
  539.     [6] = {true, 8, 16, 128, 128}, -- level 6, similar to zlib level 6
  540.     [7] = {true, 8, 32, 128, 256}, -- (SLOW) level 7, similar to zlib level 7
  541.     [8] = {true, 32, 128, 258, 1024} , --(SLOW) level 8,similar to zlib level 8
  542.     [9] = {true, 32, 258, 258, 4096},
  543.         -- (VERY SLOW) level 9, similar to zlib level 9
  544. }
  545.  
  546. -- Check if the compression/decompression arguments is valid
  547. -- @param str The input string.
  548. -- @param check_dictionary if true, check if dictionary is valid.
  549. -- @param dictionary The preset dictionary for compression and decompression.
  550. -- @param check_configs if true, check if config is valid.
  551. -- @param configs The compression configuration table
  552. -- @return true if valid, false if not valid.
  553. -- @return if not valid, the error message.
  554. local function IsValidArguments(str,
  555.     check_dictionary, dictionary,
  556.     check_configs, configs)
  557.  
  558.     if type(str) ~= "string" then
  559.         return false,
  560.             ("'str' - string expected got '%s'."):format(type(str))
  561.     end
  562.     if check_dictionary then
  563.         local dict_valid, dict_err = IsValidDictionary(dictionary)
  564.         if not dict_valid then
  565.             return false, dict_err
  566.         end
  567.     end
  568.     if check_configs then
  569.         local type_configs = type(configs)
  570.         if type_configs ~= "nil" and type_configs ~= "table" then
  571.             return false,
  572.             ("'configs' - nil or table expected got '%s'.")
  573.                 :format(type(configs))
  574.         end
  575.         if type_configs == "table" then
  576.             for k, v in pairs(configs) do
  577.                 if k ~= "level" and k ~= "strategy" then
  578.                     return false,
  579.                     ("'configs' - unsupported table key in the configs: '%s'.")
  580.                     :format(k)
  581.                 elseif k == "level" and not _compression_level_configs[v] then
  582.                     return false,
  583.                     ("'configs' - unsupported 'level': %s."):format(tostring(v))
  584.                 elseif k == "strategy" and v ~= "fixed" and v ~= "huffman_only"
  585.                         and v ~= "dynamic" then
  586.                         -- random_block_type is for testing purpose
  587.                     return false, ("'configs' - unsupported 'strategy': '%s'.")
  588.                         :format(tostring(v))
  589.                 end
  590.             end
  591.         end
  592.     end
  593.     return true, ""
  594. end
  595.  
  596.  
  597. --[[ --------------------------------------------------------------------------
  598.     Compress code
  599. --]] --------------------------------------------------------------------------
  600.  
  601. -- partial flush to save memory
  602. local _FLUSH_MODE_MEMORY_CLEANUP = 0
  603. -- full flush with partial bytes
  604. local _FLUSH_MODE_OUTPUT = 1
  605. -- write bytes to get to byte boundary
  606. local _FLUSH_MODE_BYTE_BOUNDARY = 2
  607. -- no flush, just get num of bits written so far
  608. local _FLUSH_MODE_NO_FLUSH = 3
  609.  
  610. --[[
  611.     Create an empty writer to easily write stuffs as the unit of bits.
  612.     Return values:
  613.     1. WriteBits(code, bitlen):
  614.     2. WriteString(str):
  615.     3. Flush(mode):
  616. --]]
  617. local function CreateWriter()
  618.     local buffer_size = 0
  619.     local cache = 0
  620.     local cache_bitlen = 0
  621.     local total_bitlen = 0
  622.     local buffer = {}
  623.     -- When buffer is big enough, flush into result_buffer to save memory.
  624.     local result_buffer = {}
  625.  
  626.     -- Write bits with value "value" and bit length of "bitlen" into writer.
  627.     -- @param value: The value being written
  628.     -- @param bitlen: The bit length of "value"
  629.     -- @return nil
  630.     local function WriteBits(value, bitlen)
  631.         cache = cache + value * _pow2[cache_bitlen]
  632.         cache_bitlen = cache_bitlen + bitlen
  633.         total_bitlen = total_bitlen + bitlen
  634.         -- Only bulk to buffer every 4 bytes. This is quicker.
  635.         if cache_bitlen >= 32 then
  636.             buffer_size = buffer_size + 1
  637.             buffer[buffer_size] =
  638.                 _byte_to_char[cache % 256]
  639.                 .._byte_to_char[((cache-cache%256)/256 % 256)]
  640.                 .._byte_to_char[((cache-cache%65536)/65536 % 256)]
  641.                 .._byte_to_char[((cache-cache%16777216)/16777216 % 256)]
  642.             local rshift_mask = _pow2[32 - cache_bitlen + bitlen]
  643.             cache = (value - value%rshift_mask)/rshift_mask
  644.             cache_bitlen = cache_bitlen - 32
  645.         end
  646.     end
  647.  
  648.     -- Write the entire string into the writer.
  649.     -- @param str The string being written
  650.     -- @return nil
  651.     local function WriteString(str)
  652.         for _ = 1, cache_bitlen, 8 do
  653.             buffer_size = buffer_size + 1
  654.             buffer[buffer_size] = string_char(cache % 256)
  655.             cache = (cache-cache%256)/256
  656.         end
  657.         cache_bitlen = 0
  658.         buffer_size = buffer_size + 1
  659.         buffer[buffer_size] = str
  660.         total_bitlen = total_bitlen + #str*8
  661.     end
  662.  
  663.     -- Flush current stuffs in the writer and return it.
  664.     -- This operation will free most of the memory.
  665.     -- @param mode See the descrtion of the constant and the source code.
  666.     -- @return The total number of bits stored in the writer right now.
  667.     -- for byte boundary mode, it includes the padding bits.
  668.     -- for output mode, it does not include padding bits.
  669.     -- @return Return the outputs if mode is output.
  670.     local function FlushWriter(mode)
  671.         if mode == _FLUSH_MODE_NO_FLUSH then
  672.             return total_bitlen
  673.         end
  674.  
  675.         if mode == _FLUSH_MODE_OUTPUT
  676.             or mode == _FLUSH_MODE_BYTE_BOUNDARY then
  677.             -- Full flush, also output cache.
  678.             -- Need to pad some bits if cache_bitlen is not multiple of 8.
  679.             local padding_bitlen = (8 - cache_bitlen % 8) % 8
  680.  
  681.             if cache_bitlen > 0 then
  682.                 -- padding with all 1 bits, mainly because "\000" is not
  683.                 -- good to be tranmitted. I do this so "\000" is a little bit
  684.                 -- less frequent.
  685.                 cache = cache - _pow2[cache_bitlen]
  686.                     + _pow2[cache_bitlen+padding_bitlen]
  687.                 for _ = 1, cache_bitlen, 8 do
  688.                     buffer_size = buffer_size + 1
  689.                     buffer[buffer_size] = _byte_to_char[cache % 256]
  690.                     cache = (cache-cache%256)/256
  691.                 end
  692.  
  693.                 cache = 0
  694.                 cache_bitlen = 0
  695.             end
  696.             if mode == _FLUSH_MODE_BYTE_BOUNDARY then
  697.                 total_bitlen = total_bitlen + padding_bitlen
  698.                 return total_bitlen
  699.             end
  700.         end
  701.  
  702.         local flushed = table_concat(buffer)
  703.         buffer = {}
  704.         buffer_size = 0
  705.         result_buffer[#result_buffer+1] = flushed
  706.  
  707.         if mode == _FLUSH_MODE_MEMORY_CLEANUP then
  708.             return total_bitlen
  709.         else
  710.             return total_bitlen, table_concat(result_buffer)
  711.         end
  712.     end
  713.  
  714.     return WriteBits, WriteString, FlushWriter
  715. end
  716.  
  717. -- Push an element into a max heap
  718. -- @param heap A max heap whose max element is at index 1.
  719. -- @param e The element to be pushed. Assume element "e" is a table
  720. --  and comparison is done via its first entry e[1]
  721. -- @param heap_size current number of elements in the heap.
  722. --  NOTE: There may be some garbage stored in
  723. --  heap[heap_size+1], heap[heap_size+2], etc..
  724. -- @return nil
  725. local function MinHeapPush(heap, e, heap_size)
  726.     heap_size = heap_size + 1
  727.     heap[heap_size] = e
  728.     local value = e[1]
  729.     local pos = heap_size
  730.     local parent_pos = (pos-pos%2)/2
  731.  
  732.     while (parent_pos >= 1 and heap[parent_pos][1] > value) do
  733.         local t = heap[parent_pos]
  734.         heap[parent_pos] = e
  735.         heap[pos] = t
  736.         pos = parent_pos
  737.         parent_pos = (parent_pos-parent_pos%2)/2
  738.     end
  739. end
  740.  
  741. -- Pop an element from a max heap
  742. -- @param heap A max heap whose max element is at index 1.
  743. -- @param heap_size current number of elements in the heap.
  744. -- @return the poped element
  745. -- Note: This function does not change table size of "heap" to save CPU time.
  746. local function MinHeapPop(heap, heap_size)
  747.     local top = heap[1]
  748.     local e = heap[heap_size]
  749.     local value = e[1]
  750.     heap[1] = e
  751.     heap[heap_size] = top
  752.     heap_size = heap_size - 1
  753.  
  754.     local pos = 1
  755.     local left_child_pos = pos * 2
  756.     local right_child_pos = left_child_pos + 1
  757.  
  758.     while (left_child_pos <= heap_size) do
  759.         local left_child = heap[left_child_pos]
  760.         if (right_child_pos <= heap_size
  761.             and heap[right_child_pos][1] < left_child[1]) then
  762.             local right_child = heap[right_child_pos]
  763.             if right_child[1] < value then
  764.                 heap[right_child_pos] = e
  765.                 heap[pos] = right_child
  766.                 pos = right_child_pos
  767.                 left_child_pos = pos * 2
  768.                 right_child_pos = left_child_pos + 1
  769.             else
  770.                 break
  771.             end
  772.         else
  773.             if left_child[1] < value then
  774.                 heap[left_child_pos] = e
  775.                 heap[pos] = left_child
  776.                 pos = left_child_pos
  777.                 left_child_pos = pos * 2
  778.                 right_child_pos = left_child_pos + 1
  779.             else
  780.                 break
  781.             end
  782.         end
  783.     end
  784.  
  785.     return top
  786. end
  787.  
  788. -- Deflate defines a special huffman tree, which is unique once the bit length
  789. -- of huffman code of all symbols are known.
  790. -- @param bitlen_count Number of symbols with a specific bitlen
  791. -- @param symbol_bitlen The bit length of a symbol
  792. -- @param max_symbol The max symbol among all symbols,
  793. --      which is (number of symbols - 1)
  794. -- @param max_bitlen The max huffman bit length among all symbols.
  795. -- @return The huffman code of all symbols.
  796. local function GetHuffmanCodeFromBitlen(bitlen_counts, symbol_bitlens
  797.         , max_symbol, max_bitlen)
  798.     local huffman_code = 0
  799.     local next_codes = {}
  800.     local symbol_huffman_codes = {}
  801.     for bitlen = 1, max_bitlen do
  802.         huffman_code = (huffman_code+(bitlen_counts[bitlen-1] or 0))*2
  803.         next_codes[bitlen] = huffman_code
  804.     end
  805.     for symbol = 0, max_symbol do
  806.         local bitlen = symbol_bitlens[symbol]
  807.         if bitlen then
  808.             huffman_code = next_codes[bitlen]
  809.             next_codes[bitlen] = huffman_code + 1
  810.  
  811.             -- Reverse the bits of huffman code,
  812.             -- because most signifant bits of huffman code
  813.             -- is stored first into the compressed data.
  814.             -- @see RFC1951 Page5 Section 3.1.1
  815.             if bitlen <= 9 then -- Have cached reverse for small bitlen.
  816.                 symbol_huffman_codes[symbol] =
  817.                     _reverse_bits_tbl[bitlen][huffman_code]
  818.             else
  819.                 local reverse = 0
  820.                 for _ = 1, bitlen do
  821.                     reverse = reverse - reverse%2
  822.                         + (((reverse%2==1)
  823.                             or (huffman_code % 2) == 1) and 1 or 0)
  824.                     huffman_code = (huffman_code-huffman_code%2)/2
  825.                     reverse = reverse*2
  826.                 end
  827.                 symbol_huffman_codes[symbol] = (reverse-reverse%2)/2
  828.             end
  829.         end
  830.     end
  831.     return symbol_huffman_codes
  832. end
  833.  
  834. -- A helper function to sort heap elements
  835. -- a[1], b[1] is the huffman frequency
  836. -- a[2], b[2] is the symbol value.
  837. local function SortByFirstThenSecond(a, b)
  838.     return a[1] < b[1] or
  839.         (a[1] == b[1] and a[2] < b[2])
  840. end
  841.  
  842. -- Calculate the huffman bit length and huffman code.
  843. -- @param symbol_count: A table whose table key is the symbol, and table value
  844. --      is the symbol frenquency (nil means 0 frequency).
  845. -- @param max_bitlen: See description of return value.
  846. -- @param max_symbol: The maximum symbol
  847. -- @return a table whose key is the symbol, and the value is the huffman bit
  848. --      bit length. We guarantee that all bit length <= max_bitlen.
  849. --      For 0<=symbol<=max_symbol, table value could be nil if the frequency
  850. --      of the symbol is 0 or nil.
  851. -- @return a table whose key is the symbol, and the value is the huffman code.
  852. -- @return a number indicating the maximum symbol whose bitlen is not 0.
  853. local function GetHuffmanBitlenAndCode(symbol_counts, max_bitlen, max_symbol)
  854.     local heap_size
  855.     local max_non_zero_bitlen_symbol = -1
  856.     local leafs = {}
  857.     local heap = {}
  858.     local symbol_bitlens = {}
  859.     local symbol_codes = {}
  860.     local bitlen_counts = {}
  861.  
  862.     --[[
  863.         tree[1]: weight, temporarily used as parent and bitLengths
  864.         tree[2]: symbol
  865.         tree[3]: left child
  866.         tree[4]: right child
  867.     --]]
  868.     local number_unique_symbols = 0
  869.     for symbol, count in pairs(symbol_counts) do
  870.         number_unique_symbols = number_unique_symbols + 1
  871.         leafs[number_unique_symbols] = {count, symbol}
  872.     end
  873.  
  874.     if (number_unique_symbols == 0) then
  875.         -- no code.
  876.         return {}, {}, -1
  877.     elseif (number_unique_symbols == 1) then
  878.         -- Only one code. In this case, its huffman code
  879.         -- needs to be assigned as 0, and bit length is 1.
  880.         -- This is the only case that the return result
  881.         -- represents an imcomplete huffman tree.
  882.         local symbol = leafs[1][2]
  883.         symbol_bitlens[symbol] = 1
  884.         symbol_codes[symbol] = 0
  885.         return symbol_bitlens, symbol_codes, symbol
  886.     else
  887.         table_sort(leafs, SortByFirstThenSecond)
  888.         heap_size = number_unique_symbols
  889.         for i = 1, heap_size do
  890.             heap[i] = leafs[i]
  891.         end
  892.  
  893.         while (heap_size > 1) do
  894.             -- Note: pop does not change table size of heap
  895.             local leftChild = MinHeapPop(heap, heap_size)
  896.             heap_size = heap_size - 1
  897.             local rightChild = MinHeapPop(heap, heap_size)
  898.             heap_size = heap_size - 1
  899.             local newNode =
  900.                 {leftChild[1]+rightChild[1], -1, leftChild, rightChild}
  901.             MinHeapPush(heap, newNode, heap_size)
  902.             heap_size = heap_size + 1
  903.         end
  904.  
  905.         -- Number of leafs whose bit length is greater than max_len.
  906.         local number_bitlen_overflow = 0
  907.  
  908.         -- Calculate bit length of all nodes
  909.         local fifo = {heap[1], 0, 0, 0} -- preallocate some spaces.
  910.         local fifo_size = 1
  911.         local index = 1
  912.         heap[1][1] = 0
  913.         while (index <= fifo_size) do -- Breath first search
  914.             local e = fifo[index]
  915.             local bitlen = e[1]
  916.             local symbol = e[2]
  917.             local left_child = e[3]
  918.             local right_child = e[4]
  919.             if left_child then
  920.                 fifo_size = fifo_size + 1
  921.                 fifo[fifo_size] = left_child
  922.                 left_child[1] = bitlen + 1
  923.             end
  924.             if right_child then
  925.                 fifo_size = fifo_size + 1
  926.                 fifo[fifo_size] = right_child
  927.                 right_child[1] = bitlen + 1
  928.             end
  929.             index = index + 1
  930.  
  931.             if (bitlen > max_bitlen) then
  932.                 number_bitlen_overflow = number_bitlen_overflow + 1
  933.                 bitlen = max_bitlen
  934.             end
  935.             if symbol >= 0 then
  936.                 symbol_bitlens[symbol] = bitlen
  937.                 max_non_zero_bitlen_symbol =
  938.                     (symbol > max_non_zero_bitlen_symbol)
  939.                     and symbol or max_non_zero_bitlen_symbol
  940.                 bitlen_counts[bitlen] = (bitlen_counts[bitlen] or 0) + 1
  941.             end
  942.         end
  943.  
  944.         -- Resolve bit length overflow
  945.         -- @see ZLib/trees.c:gen_bitlen(s, desc), for reference
  946.         if (number_bitlen_overflow > 0) then
  947.             repeat
  948.                 local bitlen = max_bitlen - 1
  949.                 while ((bitlen_counts[bitlen] or 0) == 0) do
  950.                     bitlen = bitlen - 1
  951.                 end
  952.                 -- move one leaf down the tree
  953.                 bitlen_counts[bitlen] = bitlen_counts[bitlen] - 1
  954.                 -- move one overflow item as its brother
  955.                 bitlen_counts[bitlen+1] = (bitlen_counts[bitlen+1] or 0) + 2
  956.                 bitlen_counts[max_bitlen] = bitlen_counts[max_bitlen] - 1
  957.                 number_bitlen_overflow = number_bitlen_overflow - 2
  958.             until (number_bitlen_overflow <= 0)
  959.  
  960.             index = 1
  961.             for bitlen = max_bitlen, 1, -1 do
  962.                 local n = bitlen_counts[bitlen] or 0
  963.                 while (n > 0) do
  964.                     local symbol = leafs[index][2]
  965.                     symbol_bitlens[symbol] = bitlen
  966.                     n = n - 1
  967.                     index = index + 1
  968.                 end
  969.             end
  970.         end
  971.  
  972.         symbol_codes = GetHuffmanCodeFromBitlen(bitlen_counts, symbol_bitlens,
  973.                 max_symbol, max_bitlen)
  974.         return symbol_bitlens, symbol_codes, max_non_zero_bitlen_symbol
  975.     end
  976. end
  977.  
  978. -- Calculate the first huffman header in the dynamic huffman block
  979. -- @see RFC1951 Page 12
  980. -- @param lcode_bitlen: The huffman bit length of literal/LZ77_length.
  981. -- @param max_non_zero_bitlen_lcode: The maximum literal/LZ77_length symbol
  982. --      whose huffman bit length is not zero.
  983. -- @param dcode_bitlen: The huffman bit length of LZ77 distance.
  984. -- @param max_non_zero_bitlen_dcode: The maximum LZ77 distance symbol
  985. --      whose huffman bit length is not zero.
  986. -- @return The run length encoded codes.
  987. -- @return The extra bits. One entry for each rle code that needs extra bits.
  988. --      (code == 16 or 17 or 18).
  989. -- @return The count of appearance of each rle codes.
  990. local function RunLengthEncodeHuffmanBitlen(
  991.         lcode_bitlens,
  992.         max_non_zero_bitlen_lcode,
  993.         dcode_bitlens,
  994.         max_non_zero_bitlen_dcode)
  995.     local rle_code_tblsize = 0
  996.     local rle_codes = {}
  997.     local rle_code_counts = {}
  998.     local rle_extra_bits_tblsize = 0
  999.     local rle_extra_bits = {}
  1000.     local prev = nil
  1001.     local count = 0
  1002.  
  1003.     -- If there is no distance code, assume one distance code of bit length 0.
  1004.     -- RFC1951: One distance code of zero bits means that
  1005.     -- there are no distance codes used at all (the data is all literals).
  1006.     max_non_zero_bitlen_dcode = (max_non_zero_bitlen_dcode < 0)
  1007.             and 0 or max_non_zero_bitlen_dcode
  1008.     local max_code = max_non_zero_bitlen_lcode+max_non_zero_bitlen_dcode+1
  1009.  
  1010.     for code = 0, max_code+1 do
  1011.         local len = (code <= max_non_zero_bitlen_lcode)
  1012.             and (lcode_bitlens[code] or 0)
  1013.             or ((code <= max_code)
  1014.             and (dcode_bitlens[code-max_non_zero_bitlen_lcode-1] or 0) or nil)
  1015.         if len == prev then
  1016.             count = count + 1
  1017.             if len ~= 0 and count == 6 then
  1018.                 rle_code_tblsize = rle_code_tblsize + 1
  1019.                 rle_codes[rle_code_tblsize] = 16
  1020.                 rle_extra_bits_tblsize = rle_extra_bits_tblsize + 1
  1021.                 rle_extra_bits[rle_extra_bits_tblsize] = 3
  1022.                 rle_code_counts[16] = (rle_code_counts[16] or 0) + 1
  1023.                 count = 0
  1024.             elseif len == 0 and count == 138 then
  1025.                 rle_code_tblsize = rle_code_tblsize + 1
  1026.                 rle_codes[rle_code_tblsize] = 18
  1027.                 rle_extra_bits_tblsize = rle_extra_bits_tblsize + 1
  1028.                 rle_extra_bits[rle_extra_bits_tblsize] = 127
  1029.                 rle_code_counts[18] = (rle_code_counts[18] or 0) + 1
  1030.                 count = 0
  1031.             end
  1032.         else
  1033.             if count == 1 then
  1034.                 rle_code_tblsize = rle_code_tblsize + 1
  1035.                 rle_codes[rle_code_tblsize] = prev
  1036.                 rle_code_counts[prev] = (rle_code_counts[prev] or 0) + 1
  1037.             elseif count == 2 then
  1038.                 rle_code_tblsize = rle_code_tblsize + 1
  1039.                 rle_codes[rle_code_tblsize] = prev
  1040.                 rle_code_tblsize = rle_code_tblsize + 1
  1041.                 rle_codes[rle_code_tblsize] = prev
  1042.                 rle_code_counts[prev] = (rle_code_counts[prev] or 0) + 2
  1043.             elseif count >= 3 then
  1044.                 rle_code_tblsize = rle_code_tblsize + 1
  1045.                 local rleCode = (prev ~= 0) and 16 or (count <= 10 and 17 or 18)
  1046.                 rle_codes[rle_code_tblsize] = rleCode
  1047.                 rle_code_counts[rleCode] = (rle_code_counts[rleCode] or 0) + 1
  1048.                 rle_extra_bits_tblsize = rle_extra_bits_tblsize + 1
  1049.                 rle_extra_bits[rle_extra_bits_tblsize] =
  1050.                     (count <= 10) and (count - 3) or (count - 11)
  1051.             end
  1052.  
  1053.             prev = len
  1054.             if len and len ~= 0 then
  1055.                 rle_code_tblsize = rle_code_tblsize + 1
  1056.                 rle_codes[rle_code_tblsize] = len
  1057.                 rle_code_counts[len] = (rle_code_counts[len] or 0) + 1
  1058.                 count = 0
  1059.             else
  1060.                 count = 1
  1061.             end
  1062.         end
  1063.     end
  1064.  
  1065.     return rle_codes, rle_extra_bits, rle_code_counts
  1066. end
  1067.  
  1068. -- Load the string into a table, in order to speed up LZ77.
  1069. -- Loop unrolled 16 times to speed this function up.
  1070. -- @param str The string to be loaded.
  1071. -- @param t The load destination
  1072. -- @param start str[index] will be the first character to be loaded.
  1073. -- @param end str[index] will be the last character to be loaded
  1074. -- @param offset str[index] will be loaded into t[index-offset]
  1075. -- @return t
  1076. local function LoadStringToTable(str, t, start, stop, offset)
  1077.     local i = start - offset
  1078.     while i <= stop - 15 - offset do
  1079.         t[i], t[i+1], t[i+2], t[i+3], t[i+4], t[i+5], t[i+6], t[i+7], t[i+8],
  1080.         t[i+9], t[i+10], t[i+11], t[i+12], t[i+13], t[i+14], t[i+15] =
  1081.             string_byte(str, i + offset, i + 15 + offset)
  1082.         i = i + 16
  1083.     end
  1084.     while (i <= stop - offset) do
  1085.         t[i] = string_byte(str, i + offset, i + offset)
  1086.         i = i + 1
  1087.     end
  1088.     return t
  1089. end
  1090.  
  1091. -- Do LZ77 process. This function uses the majority of the CPU time.
  1092. -- @see zlib/deflate.c:deflate_fast(), zlib/deflate.c:deflate_slow()
  1093. -- @see https://github.com/madler/zlib/blob/master/doc/algorithm.txt
  1094. -- This function uses the algorithms used above. You should read the
  1095. -- algorithm.txt above to understand what is the hash function and the
  1096. -- lazy evaluation.
  1097. --
  1098. -- The special optimization used here is hash functions used here.
  1099. -- The hash function is just the multiplication of the three consective
  1100. -- characters. So if the hash matches, it guarantees 3 characters are matched.
  1101. -- This optimization can be implemented because Lua table is a hash table.
  1102. --
  1103. -- @param level integer that describes compression level.
  1104. -- @param string_table table that stores the value of string to be compressed.
  1105. --          The index of this table starts from 1.
  1106. --          The caller needs to make sure all values needed by this function
  1107. --          are loaded.
  1108. --          Assume "str" is the origin input string into the compressor
  1109. --          str[block_start]..str[block_end+3] needs to be loaded into
  1110. --          string_table[block_start-offset]..string_table[block_end-offset]
  1111. --          If dictionary is presented, the last 258 bytes of the dictionary
  1112. --          needs to be loaded into sing_table[-257..0]
  1113. --          (See more in the description of offset.)
  1114. -- @param hash_tables. The table key is the hash value (0<=hash<=16777216=256^3)
  1115. --          The table value is an array0 that stores the indexes of the
  1116. --          input data string to be compressed, such that
  1117. --          hash == str[index]*str[index+1]*str[index+2]
  1118. --          Indexes are ordered in this array.
  1119. -- @param block_start The indexes of the input data string to be compressed.
  1120. --              that starts the LZ77 block.
  1121. -- @param block_end The indexes of the input data string to be compressed.
  1122. --              that stores the LZ77 block.
  1123. -- @param offset str[index] is stored in string_table[index-offset],
  1124. --          This offset is mainly an optimization to limit the index
  1125. --          of string_table, so lua can access this table quicker.
  1126. -- @param dictionary See LibDeflate.CreateDictionary
  1127. -- @return literal/LZ77_length deflate codes.
  1128. -- @return the extra bits of literal/LZ77_length deflate codes.
  1129. -- @return the count of each literal/LZ77 deflate code.
  1130. -- @return LZ77 distance deflate codes.
  1131. -- @return the extra bits of LZ77 distance deflate codes.
  1132. -- @return the count of each LZ77 distance deflate code.
  1133. local function GetBlockLZ77Result(level, string_table, hash_tables, block_start,
  1134.         block_end, offset, dictionary)
  1135.     local config = _compression_level_configs[level]
  1136.     local config_use_lazy
  1137.         , config_good_prev_length
  1138.         , config_max_lazy_match
  1139.         , config_nice_length
  1140.         , config_max_hash_chain =
  1141.             config[1], config[2], config[3], config[4], config[5]
  1142.  
  1143.     local config_max_insert_length = (not config_use_lazy)
  1144.         and config_max_lazy_match or 2147483646
  1145.     local config_good_hash_chain =
  1146.         (config_max_hash_chain-config_max_hash_chain%4/4)
  1147.  
  1148.     local hash
  1149.  
  1150.     local dict_hash_tables
  1151.     local dict_string_table
  1152.     local dict_string_len = 0
  1153.  
  1154.     if dictionary then
  1155.         dict_hash_tables = dictionary.hash_tables
  1156.         dict_string_table = dictionary.string_table
  1157.         dict_string_len = dictionary.strlen
  1158.         assert(block_start == 1)
  1159.         if block_end >= block_start and dict_string_len >= 2 then
  1160.             hash = dict_string_table[dict_string_len-1]*65536
  1161.                 + dict_string_table[dict_string_len]*256 + string_table[1]
  1162.             local t = hash_tables[hash]
  1163.             if not t then t = {}; hash_tables[hash] = t end
  1164.             t[#t+1] = -1
  1165.         end
  1166.         if block_end >= block_start+1 and dict_string_len >= 1 then
  1167.             hash = dict_string_table[dict_string_len]*65536
  1168.                 + string_table[1]*256 + string_table[2]
  1169.             local t = hash_tables[hash]
  1170.             if not t then t = {}; hash_tables[hash] = t end
  1171.             t[#t+1] = 0
  1172.         end
  1173.     end
  1174.  
  1175.     hash = (string_table[block_start-offset] or 0)*256
  1176.         + (string_table[block_start+1-offset] or 0)
  1177.  
  1178.     local lcodes = {}
  1179.     local lcode_tblsize = 0
  1180.     local lcodes_counts = {}
  1181.     local dcodes = {}
  1182.     local dcodes_tblsize = 0
  1183.     local dcodes_counts = {}
  1184.  
  1185.     local lextra_bits = {}
  1186.     local lextra_bits_tblsize = 0
  1187.     local dextra_bits = {}
  1188.     local dextra_bits_tblsize = 0
  1189.  
  1190.     local match_available = false
  1191.     local prev_len
  1192.     local prev_dist
  1193.     local cur_len = 0
  1194.     local cur_dist = 0
  1195.  
  1196.     local index = block_start
  1197.     local index_end = block_end + (config_use_lazy and 1 or 0)
  1198.  
  1199.     -- the zlib source code writes separate code for lazy evaluation and
  1200.     -- not lazy evaluation, which is easier to understand.
  1201.     -- I put them together, so it is a bit harder to understand.
  1202.     -- because I think this is easier for me to maintain it.
  1203.     while (index <= index_end) do
  1204.         local string_table_index = index - offset
  1205.         prev_len = cur_len
  1206.         prev_dist = cur_dist
  1207.         cur_len = 0
  1208.  
  1209.         hash = (hash*256+(string_table[string_table_index+2] or 0))%16777216
  1210.  
  1211.         local chain_index
  1212.         local cur_chain
  1213.         local hash_chain = hash_tables[hash]
  1214.         local chain_old_size
  1215.         if not hash_chain then
  1216.             chain_old_size = 0
  1217.             hash_chain = {}
  1218.             hash_tables[hash] = hash_chain
  1219.             if dict_hash_tables then
  1220.                 cur_chain = dict_hash_tables[hash]
  1221.                 chain_index = cur_chain and #cur_chain or 0
  1222.             else
  1223.                 chain_index = 0
  1224.             end
  1225.         else
  1226.             chain_old_size = #hash_chain
  1227.             cur_chain = hash_chain
  1228.             chain_index = chain_old_size
  1229.         end
  1230.  
  1231.         if index <= block_end then
  1232.             hash_chain[chain_old_size+1] = index
  1233.         end
  1234.  
  1235.         if (chain_index > 0 and index + 2 <= block_end
  1236.             and (not config_use_lazy or prev_len < config_max_lazy_match)) then
  1237.  
  1238.             local depth =
  1239.                 (config_use_lazy and prev_len >= config_good_prev_length)
  1240.                 and config_good_hash_chain or config_max_hash_chain
  1241.  
  1242.             while chain_index >= 1 and depth > 0 do
  1243.                 local prev = cur_chain[chain_index]
  1244.  
  1245.                 if index - prev > 32768 then
  1246.                     break
  1247.                 end
  1248.                 if prev < index then
  1249.                     local j = 3
  1250.  
  1251.                     if prev >= -257 then
  1252.                         local prev_table_index = prev-offset
  1253.                         -- NOTE for author:
  1254.                         -- j < 258 and index + j <= block_end
  1255.                         -- This is the right condition
  1256.                         while (j < 258 and index + j <= block_end) do
  1257.                             if (string_table[prev_table_index+j]
  1258.                                 == string_table[string_table_index+j]) then
  1259.                                 j = j + 1
  1260.                             else
  1261.                                 break
  1262.                             end
  1263.                         end
  1264.                     else
  1265.                         local prev_table_index = dict_string_len+prev
  1266.                         -- NOTE for author:
  1267.                         -- j < 258 and index + j <= block_end
  1268.                         -- This is the right condition
  1269.                         while (j < 258 and index + j <= block_end) do
  1270.                             if (dict_string_table[prev_table_index+j]
  1271.                                 == string_table[string_table_index+j]) then
  1272.                                 j = j + 1
  1273.                             else
  1274.                                 break
  1275.                             end
  1276.                         end
  1277.                     end
  1278.                     if j > cur_len then
  1279.                         cur_len = j
  1280.                         cur_dist = index - prev
  1281.                     end
  1282.                     if cur_len >= config_nice_length then
  1283.                         break
  1284.                     end
  1285.                 end
  1286.  
  1287.                 chain_index = chain_index - 1
  1288.                 depth = depth - 1
  1289.                 if chain_index == 0 and prev > 0 and dict_hash_tables then
  1290.                     cur_chain = dict_hash_tables[hash]
  1291.                     chain_index = cur_chain and #cur_chain or 0
  1292.                 end
  1293.             end
  1294.         end
  1295.  
  1296.         if not config_use_lazy then
  1297.             prev_len, prev_dist = cur_len, cur_dist
  1298.         end
  1299.         if ((not config_use_lazy or match_available)
  1300.             and (prev_len > 3 or (prev_len == 3 and prev_dist < 4096))
  1301.             and cur_len <= prev_len )then
  1302.             local code = _length_to_deflate_code[prev_len]
  1303.             local length_extra_bits_bitlen =
  1304.                 _length_to_deflate_extra_bitlen[prev_len]
  1305.             local dist_code, dist_extra_bits_bitlen, dist_extra_bits
  1306.             if prev_dist <= 256 then -- have cached code for small distance.
  1307.                 dist_code = _dist256_to_deflate_code[prev_dist]
  1308.                 dist_extra_bits = _dist256_to_deflate_extra_bits[prev_dist]
  1309.                 dist_extra_bits_bitlen =
  1310.                     _dist256_to_deflate_extra_bitlen[prev_dist]
  1311.             else
  1312.                 dist_code = 16
  1313.                 dist_extra_bits_bitlen = 7
  1314.                 local a = 384
  1315.                 local b = 512
  1316.  
  1317.                 while true do
  1318.                     if prev_dist <= a then
  1319.                         dist_extra_bits = (prev_dist-(b/2)-1) % (b/4)
  1320.                         break
  1321.                     elseif prev_dist <= b then
  1322.                         dist_extra_bits = (prev_dist-(b/2)-1) % (b/4)
  1323.                         dist_code = dist_code + 1
  1324.                         break
  1325.                     else
  1326.                         dist_code = dist_code + 2
  1327.                         dist_extra_bits_bitlen = dist_extra_bits_bitlen + 1
  1328.                         a = a*2
  1329.                         b = b*2
  1330.                     end
  1331.                 end
  1332.             end
  1333.             lcode_tblsize = lcode_tblsize + 1
  1334.             lcodes[lcode_tblsize] = code
  1335.             lcodes_counts[code] = (lcodes_counts[code] or 0) + 1
  1336.  
  1337.             dcodes_tblsize = dcodes_tblsize + 1
  1338.             dcodes[dcodes_tblsize] = dist_code
  1339.             dcodes_counts[dist_code] = (dcodes_counts[dist_code] or 0) + 1
  1340.  
  1341.             if length_extra_bits_bitlen > 0 then
  1342.                 local lenExtraBits = _length_to_deflate_extra_bits[prev_len]
  1343.                 lextra_bits_tblsize = lextra_bits_tblsize + 1
  1344.                 lextra_bits[lextra_bits_tblsize] = lenExtraBits
  1345.             end
  1346.             if dist_extra_bits_bitlen > 0 then
  1347.                 dextra_bits_tblsize = dextra_bits_tblsize + 1
  1348.                 dextra_bits[dextra_bits_tblsize] = dist_extra_bits
  1349.             end
  1350.  
  1351.             for i=index+1, index+prev_len-(config_use_lazy and 2 or 1) do
  1352.                 hash = (hash*256+(string_table[i-offset+2] or 0))%16777216
  1353.                 if prev_len <= config_max_insert_length then
  1354.                     hash_chain = hash_tables[hash]
  1355.                     if not hash_chain then
  1356.                         hash_chain = {}
  1357.                         hash_tables[hash] = hash_chain
  1358.                     end
  1359.                     hash_chain[#hash_chain+1] = i
  1360.                 end
  1361.             end
  1362.             index = index + prev_len - (config_use_lazy and 1 or 0)
  1363.             match_available = false
  1364.         elseif (not config_use_lazy) or match_available then
  1365.             local code = string_table[config_use_lazy
  1366.                 and (string_table_index-1) or string_table_index]
  1367.             lcode_tblsize = lcode_tblsize + 1
  1368.             lcodes[lcode_tblsize] = code
  1369.             lcodes_counts[code] = (lcodes_counts[code] or 0) + 1
  1370.             index = index + 1
  1371.         else
  1372.             match_available = true
  1373.             index = index + 1
  1374.         end
  1375.     end
  1376.  
  1377.     -- Write "end of block" symbol
  1378.     lcode_tblsize = lcode_tblsize + 1
  1379.     lcodes[lcode_tblsize] = 256
  1380.     lcodes_counts[256] = (lcodes_counts[256] or 0) + 1
  1381.  
  1382.     return lcodes, lextra_bits, lcodes_counts, dcodes, dextra_bits
  1383.         , dcodes_counts
  1384. end
  1385.  
  1386. -- Get the header data of dynamic block.
  1387. -- @param lcodes_count The count of each literal/LZ77_length codes.
  1388. -- @param dcodes_count The count of each Lz77 distance codes.
  1389. -- @return a lots of stuffs.
  1390. -- @see RFC1951 Page 12
  1391. local function GetBlockDynamicHuffmanHeader(lcodes_counts, dcodes_counts)
  1392.     local lcodes_huffman_bitlens, lcodes_huffman_codes
  1393.         , max_non_zero_bitlen_lcode =
  1394.         GetHuffmanBitlenAndCode(lcodes_counts, 15, 285)
  1395.     local dcodes_huffman_bitlens, dcodes_huffman_codes
  1396.         , max_non_zero_bitlen_dcode =
  1397.         GetHuffmanBitlenAndCode(dcodes_counts, 15, 29)
  1398.  
  1399.     local rle_deflate_codes, rle_extra_bits, rle_codes_counts =
  1400.         RunLengthEncodeHuffmanBitlen(lcodes_huffman_bitlens
  1401.         ,max_non_zero_bitlen_lcode, dcodes_huffman_bitlens
  1402.         , max_non_zero_bitlen_dcode)
  1403.  
  1404.     local rle_codes_huffman_bitlens, rle_codes_huffman_codes =
  1405.         GetHuffmanBitlenAndCode(rle_codes_counts, 7, 18)
  1406.  
  1407.     local HCLEN = 0
  1408.     for i = 1, 19 do
  1409.         local symbol = _rle_codes_huffman_bitlen_order[i]
  1410.         local length = rle_codes_huffman_bitlens[symbol] or 0
  1411.         if length ~= 0 then
  1412.             HCLEN = i
  1413.         end
  1414.     end
  1415.  
  1416.     HCLEN = HCLEN - 4
  1417.     local HLIT = max_non_zero_bitlen_lcode + 1 - 257
  1418.     local HDIST = max_non_zero_bitlen_dcode + 1 - 1
  1419.     if HDIST < 0 then HDIST = 0 end
  1420.  
  1421.     return HLIT, HDIST, HCLEN, rle_codes_huffman_bitlens
  1422.         , rle_codes_huffman_codes, rle_deflate_codes, rle_extra_bits
  1423.         , lcodes_huffman_bitlens, lcodes_huffman_codes
  1424.         , dcodes_huffman_bitlens, dcodes_huffman_codes
  1425. end
  1426.  
  1427. -- Get the size of dynamic block without writing any bits into the writer.
  1428. -- @param ... Read the source code of GetBlockDynamicHuffmanHeader()
  1429. -- @return the bit length of the dynamic block
  1430. local function GetDynamicHuffmanBlockSize(lcodes, dcodes, HCLEN
  1431.     , rle_codes_huffman_bitlens, rle_deflate_codes
  1432.     , lcodes_huffman_bitlens, dcodes_huffman_bitlens)
  1433.  
  1434.     local block_bitlen = 17 -- 1+2+5+5+4
  1435.     block_bitlen = block_bitlen + (HCLEN+4)*3
  1436.  
  1437.     for i = 1, #rle_deflate_codes do
  1438.         local code = rle_deflate_codes[i]
  1439.         block_bitlen = block_bitlen + rle_codes_huffman_bitlens[code]
  1440.         if code >= 16 then
  1441.             block_bitlen = block_bitlen +
  1442.             ((code == 16) and 2 or (code == 17 and 3 or 7))
  1443.         end
  1444.     end
  1445.  
  1446.     local length_code_count = 0
  1447.     for i = 1, #lcodes do
  1448.         local code = lcodes[i]
  1449.         local huffman_bitlen = lcodes_huffman_bitlens[code]
  1450.         block_bitlen = block_bitlen + huffman_bitlen
  1451.         if code > 256 then -- Length code
  1452.             length_code_count = length_code_count + 1
  1453.             if code > 264 and code < 285 then -- Length code with extra bits
  1454.                 local extra_bits_bitlen =
  1455.                     _literal_deflate_code_to_extra_bitlen[code-256]
  1456.                 block_bitlen = block_bitlen + extra_bits_bitlen
  1457.             end
  1458.             local dist_code = dcodes[length_code_count]
  1459.             local dist_huffman_bitlen = dcodes_huffman_bitlens[dist_code]
  1460.             block_bitlen = block_bitlen + dist_huffman_bitlen
  1461.  
  1462.             if dist_code > 3 then -- dist code with extra bits
  1463.                 local dist_extra_bits_bitlen = (dist_code-dist_code%2)/2 - 1
  1464.                 block_bitlen = block_bitlen + dist_extra_bits_bitlen
  1465.             end
  1466.         end
  1467.     end
  1468.     return block_bitlen
  1469. end
  1470.  
  1471. -- Write dynamic block.
  1472. -- @param ... Read the source code of GetBlockDynamicHuffmanHeader()
  1473. local function CompressDynamicHuffmanBlock(WriteBits, is_last_block
  1474.         , lcodes, lextra_bits, dcodes, dextra_bits, HLIT, HDIST, HCLEN
  1475.         , rle_codes_huffman_bitlens, rle_codes_huffman_codes
  1476.         , rle_deflate_codes, rle_extra_bits
  1477.         , lcodes_huffman_bitlens, lcodes_huffman_codes
  1478.         , dcodes_huffman_bitlens, dcodes_huffman_codes)
  1479.  
  1480.     WriteBits(is_last_block and 1 or 0, 1) -- Last block identifier
  1481.     WriteBits(2, 2) -- Dynamic Huffman block identifier
  1482.  
  1483.     WriteBits(HLIT, 5)
  1484.     WriteBits(HDIST, 5)
  1485.     WriteBits(HCLEN, 4)
  1486.  
  1487.     for i = 1, HCLEN+4 do
  1488.         local symbol = _rle_codes_huffman_bitlen_order[i]
  1489.         local length = rle_codes_huffman_bitlens[symbol] or 0
  1490.         WriteBits(length, 3)
  1491.     end
  1492.  
  1493.     local rleExtraBitsIndex = 1
  1494.     for i=1, #rle_deflate_codes do
  1495.         local code = rle_deflate_codes[i]
  1496.         WriteBits(rle_codes_huffman_codes[code]
  1497.             , rle_codes_huffman_bitlens[code])
  1498.         if code >= 16 then
  1499.             local extraBits = rle_extra_bits[rleExtraBitsIndex]
  1500.             WriteBits(extraBits, (code == 16) and 2 or (code == 17 and 3 or 7))
  1501.             rleExtraBitsIndex = rleExtraBitsIndex + 1
  1502.         end
  1503.     end
  1504.  
  1505.     local length_code_count = 0
  1506.     local length_code_with_extra_count = 0
  1507.     local dist_code_with_extra_count = 0
  1508.  
  1509.     for i=1, #lcodes do
  1510.         local deflate_codee = lcodes[i]
  1511.         local huffman_code = lcodes_huffman_codes[deflate_codee]
  1512.         local huffman_bitlen = lcodes_huffman_bitlens[deflate_codee]
  1513.         WriteBits(huffman_code, huffman_bitlen)
  1514.         if deflate_codee > 256 then -- Length code
  1515.             length_code_count = length_code_count + 1
  1516.             if deflate_codee > 264 and deflate_codee < 285 then
  1517.                 -- Length code with extra bits
  1518.                 length_code_with_extra_count = length_code_with_extra_count + 1
  1519.                 local extra_bits = lextra_bits[length_code_with_extra_count]
  1520.                 local extra_bits_bitlen =
  1521.                     _literal_deflate_code_to_extra_bitlen[deflate_codee-256]
  1522.                 WriteBits(extra_bits, extra_bits_bitlen)
  1523.             end
  1524.             -- Write distance code
  1525.             local dist_deflate_code = dcodes[length_code_count]
  1526.             local dist_huffman_code = dcodes_huffman_codes[dist_deflate_code]
  1527.             local dist_huffman_bitlen =
  1528.                 dcodes_huffman_bitlens[dist_deflate_code]
  1529.             WriteBits(dist_huffman_code, dist_huffman_bitlen)
  1530.  
  1531.             if dist_deflate_code > 3 then -- dist code with extra bits
  1532.                 dist_code_with_extra_count = dist_code_with_extra_count + 1
  1533.                 local dist_extra_bits = dextra_bits[dist_code_with_extra_count]
  1534.                 local dist_extra_bits_bitlen =
  1535.                     (dist_deflate_code-dist_deflate_code%2)/2 - 1
  1536.                 WriteBits(dist_extra_bits, dist_extra_bits_bitlen)
  1537.             end
  1538.         end
  1539.     end
  1540. end
  1541.  
  1542. -- Get the size of fixed block without writing any bits into the writer.
  1543. -- @param lcodes literal/LZ77_length deflate codes
  1544. -- @param decodes LZ77 distance deflate codes
  1545. -- @return the bit length of the fixed block
  1546. local function GetFixedHuffmanBlockSize(lcodes, dcodes)
  1547.     local block_bitlen = 3
  1548.     local length_code_count = 0
  1549.     for i=1, #lcodes do
  1550.         local code = lcodes[i]
  1551.         local huffman_bitlen = _fix_block_literal_huffman_bitlen[code]
  1552.         block_bitlen = block_bitlen + huffman_bitlen
  1553.         if code > 256 then -- Length code
  1554.             length_code_count = length_code_count + 1
  1555.             if code > 264 and code < 285 then -- Length code with extra bits
  1556.                 local extra_bits_bitlen =
  1557.                     _literal_deflate_code_to_extra_bitlen[code-256]
  1558.                 block_bitlen = block_bitlen + extra_bits_bitlen
  1559.             end
  1560.             local dist_code = dcodes[length_code_count]
  1561.             block_bitlen = block_bitlen + 5
  1562.  
  1563.             if dist_code > 3 then -- dist code with extra bits
  1564.                 local dist_extra_bits_bitlen =
  1565.                     (dist_code-dist_code%2)/2 - 1
  1566.                 block_bitlen = block_bitlen + dist_extra_bits_bitlen
  1567.             end
  1568.         end
  1569.     end
  1570.     return block_bitlen
  1571. end
  1572.  
  1573. -- Write fixed block.
  1574. -- @param lcodes literal/LZ77_length deflate codes
  1575. -- @param decodes LZ77 distance deflate codes
  1576. local function CompressFixedHuffmanBlock(WriteBits, is_last_block,
  1577.         lcodes, lextra_bits, dcodes, dextra_bits)
  1578.     WriteBits(is_last_block and 1 or 0, 1) -- Last block identifier
  1579.     WriteBits(1, 2) -- Fixed Huffman block identifier
  1580.     local length_code_count = 0
  1581.     local length_code_with_extra_count = 0
  1582.     local dist_code_with_extra_count = 0
  1583.     for i=1, #lcodes do
  1584.         local deflate_code = lcodes[i]
  1585.         local huffman_code = _fix_block_literal_huffman_code[deflate_code]
  1586.         local huffman_bitlen = _fix_block_literal_huffman_bitlen[deflate_code]
  1587.         WriteBits(huffman_code, huffman_bitlen)
  1588.         if deflate_code > 256 then -- Length code
  1589.             length_code_count = length_code_count + 1
  1590.             if deflate_code > 264 and deflate_code < 285 then
  1591.                 -- Length code with extra bits
  1592.                 length_code_with_extra_count = length_code_with_extra_count + 1
  1593.                 local extra_bits = lextra_bits[length_code_with_extra_count]
  1594.                 local extra_bits_bitlen =
  1595.                     _literal_deflate_code_to_extra_bitlen[deflate_code-256]
  1596.                 WriteBits(extra_bits, extra_bits_bitlen)
  1597.             end
  1598.             -- Write distance code
  1599.             local dist_code = dcodes[length_code_count]
  1600.             local dist_huffman_code = _fix_block_dist_huffman_code[dist_code]
  1601.             WriteBits(dist_huffman_code, 5)
  1602.  
  1603.             if dist_code > 3 then -- dist code with extra bits
  1604.                 dist_code_with_extra_count = dist_code_with_extra_count + 1
  1605.                 local dist_extra_bits = dextra_bits[dist_code_with_extra_count]
  1606.                 local dist_extra_bits_bitlen = (dist_code-dist_code%2)/2 - 1
  1607.                 WriteBits(dist_extra_bits, dist_extra_bits_bitlen)
  1608.             end
  1609.         end
  1610.     end
  1611. end
  1612.  
  1613. -- Get the size of store block without writing any bits into the writer.
  1614. -- @param block_start The start index of the origin input string
  1615. -- @param block_end The end index of the origin input string
  1616. -- @param Total bit lens had been written into the compressed result before,
  1617. -- because store block needs to shift to byte boundary.
  1618. -- @return the bit length of the fixed block
  1619. local function GetStoreBlockSize(block_start, block_end, total_bitlen)
  1620.     assert(block_end-block_start+1 <= 65535)
  1621.     local block_bitlen = 3
  1622.     total_bitlen = total_bitlen + 3
  1623.     local padding_bitlen = (8-total_bitlen%8)%8
  1624.     block_bitlen = block_bitlen + padding_bitlen
  1625.     block_bitlen = block_bitlen + 32
  1626.     block_bitlen = block_bitlen + (block_end - block_start + 1) * 8
  1627.     return block_bitlen
  1628. end
  1629.  
  1630. -- Write the store block.
  1631. -- @param ... lots of stuffs
  1632. -- @return nil
  1633. local function CompressStoreBlock(WriteBits, WriteString, is_last_block, str
  1634.     , block_start, block_end, total_bitlen)
  1635.     assert(block_end-block_start+1 <= 65535)
  1636.     WriteBits(is_last_block and 1 or 0, 1) -- Last block identifer.
  1637.     WriteBits(0, 2) -- Store block identifier.
  1638.     total_bitlen = total_bitlen + 3
  1639.     local padding_bitlen = (8-total_bitlen%8)%8
  1640.     if padding_bitlen > 0 then
  1641.         WriteBits(_pow2[padding_bitlen]-1, padding_bitlen)
  1642.     end
  1643.     local size = block_end - block_start + 1
  1644.     WriteBits(size, 16)
  1645.  
  1646.     -- Write size's one's complement
  1647.     local comp = (255 - size % 256) + (255 - (size-size%256)/256)*256
  1648.     WriteBits(comp, 16)
  1649.  
  1650.     WriteString(str:sub(block_start, block_end))
  1651. end
  1652.  
  1653. -- Do the deflate
  1654. -- Currently using a simple way to determine the block size
  1655. -- (This is why the compression ratio is little bit worse than zlib when
  1656. -- the input size is very large
  1657. -- The first block is 64KB, the following block is 32KB.
  1658. -- After each block, there is a memory cleanup operation.
  1659. -- This is not a fast operation, but it is needed to save memory usage, so
  1660. -- the memory usage does not grow unboundly. If the data size is less than
  1661. -- 64KB, then memory cleanup won't happen.
  1662. -- This function determines whether to use store/fixed/dynamic blocks by
  1663. -- calculating the block size of each block type and chooses the smallest one.
  1664. local function Deflate(configs, WriteBits, WriteString, FlushWriter, str
  1665.     , dictionary)
  1666.     local string_table = {}
  1667.     local hash_tables = {}
  1668.     local is_last_block = nil
  1669.     local block_start
  1670.     local block_end
  1671.     local bitlen_written
  1672.     local total_bitlen = FlushWriter(_FLUSH_MODE_NO_FLUSH)
  1673.     local strlen = #str
  1674.     local offset
  1675.  
  1676.     local level
  1677.     local strategy
  1678.     if configs then
  1679.         if configs.level then
  1680.             level = configs.level
  1681.         end
  1682.         if configs.strategy then
  1683.             strategy = configs.strategy
  1684.         end
  1685.     end
  1686.  
  1687.     if not level then
  1688.         if strlen < 2048 then
  1689.             level = 7
  1690.         elseif strlen > 65536 then
  1691.             level = 3
  1692.         else
  1693.             level = 5
  1694.         end
  1695.     end
  1696.  
  1697.     while not is_last_block do
  1698.         if not block_start then
  1699.             block_start = 1
  1700.             block_end = 64*1024 - 1
  1701.             offset = 0
  1702.         else
  1703.             block_start = block_end + 1
  1704.             block_end = block_end + 32*1024
  1705.             offset = block_start - 32*1024 - 1
  1706.         end
  1707.  
  1708.         if block_end >= strlen then
  1709.             block_end = strlen
  1710.             is_last_block = true
  1711.         else
  1712.             is_last_block = false
  1713.         end
  1714.  
  1715.         local lcodes, lextra_bits, lcodes_counts, dcodes, dextra_bits
  1716.             , dcodes_counts
  1717.  
  1718.         local HLIT, HDIST, HCLEN, rle_codes_huffman_bitlens
  1719.             , rle_codes_huffman_codes, rle_deflate_codes
  1720.             , rle_extra_bits, lcodes_huffman_bitlens, lcodes_huffman_codes
  1721.             , dcodes_huffman_bitlens, dcodes_huffman_codes
  1722.  
  1723.         local dynamic_block_bitlen
  1724.         local fixed_block_bitlen
  1725.         local store_block_bitlen
  1726.  
  1727.         if level ~= 0 then
  1728.  
  1729.             -- GetBlockLZ77 needs block_start to block_end+3 to be loaded.
  1730.             LoadStringToTable(str, string_table, block_start, block_end + 3
  1731.                 , offset)
  1732.             if block_start == 1 and dictionary then
  1733.                 local dict_string_table = dictionary.string_table
  1734.                 local dict_strlen = dictionary.strlen
  1735.                 for i=0, (-dict_strlen+1)<-257
  1736.                     and -257 or (-dict_strlen+1), -1 do
  1737.                     string_table[i] = dict_string_table[dict_strlen+i]
  1738.                 end
  1739.             end
  1740.  
  1741.             if strategy == "huffman_only" then
  1742.                 lcodes = {}
  1743.                 LoadStringToTable(str, lcodes, block_start, block_end
  1744.                     , block_start-1)
  1745.                 lextra_bits = {}
  1746.                 lcodes_counts = {}
  1747.                 lcodes[block_end - block_start+2] = 256 -- end of block
  1748.                 for i=1, block_end - block_start+2 do
  1749.                     local code = lcodes[i]
  1750.                     lcodes_counts[code] = (lcodes_counts[code] or 0) + 1
  1751.                 end
  1752.                 dcodes = {}
  1753.                 dextra_bits = {}
  1754.                 dcodes_counts = {}
  1755.             else
  1756.                 lcodes, lextra_bits, lcodes_counts, dcodes, dextra_bits
  1757.                 , dcodes_counts = GetBlockLZ77Result(level, string_table
  1758.                 , hash_tables, block_start, block_end, offset, dictionary
  1759.                 )
  1760.             end
  1761.  
  1762.             HLIT, HDIST, HCLEN, rle_codes_huffman_bitlens
  1763.                 , rle_codes_huffman_codes, rle_deflate_codes
  1764.                 , rle_extra_bits, lcodes_huffman_bitlens, lcodes_huffman_codes
  1765.                 , dcodes_huffman_bitlens, dcodes_huffman_codes =
  1766.                 GetBlockDynamicHuffmanHeader(lcodes_counts, dcodes_counts)
  1767.             dynamic_block_bitlen = GetDynamicHuffmanBlockSize(
  1768.                     lcodes, dcodes, HCLEN, rle_codes_huffman_bitlens
  1769.                     , rle_deflate_codes, lcodes_huffman_bitlens
  1770.                     , dcodes_huffman_bitlens)
  1771.             fixed_block_bitlen = GetFixedHuffmanBlockSize(lcodes, dcodes)
  1772.         end
  1773.  
  1774.         store_block_bitlen = GetStoreBlockSize(block_start, block_end
  1775.             , total_bitlen)
  1776.  
  1777.         local min_bitlen = store_block_bitlen
  1778.         min_bitlen = (fixed_block_bitlen and fixed_block_bitlen < min_bitlen)
  1779.             and fixed_block_bitlen or min_bitlen
  1780.         min_bitlen = (dynamic_block_bitlen
  1781.             and dynamic_block_bitlen < min_bitlen)
  1782.             and dynamic_block_bitlen or min_bitlen
  1783.  
  1784.         if level == 0 or (strategy ~= "fixed" and strategy ~= "dynamic" and
  1785.             store_block_bitlen == min_bitlen) then
  1786.             CompressStoreBlock(WriteBits, WriteString, is_last_block
  1787.                 , str, block_start, block_end, total_bitlen)
  1788.             total_bitlen = total_bitlen + store_block_bitlen
  1789.         elseif strategy ~= "dynamic" and (
  1790.             strategy == "fixed" or fixed_block_bitlen == min_bitlen) then
  1791.             CompressFixedHuffmanBlock(WriteBits, is_last_block,
  1792.                     lcodes, lextra_bits, dcodes, dextra_bits)
  1793.             total_bitlen = total_bitlen + fixed_block_bitlen
  1794.         elseif strategy == "dynamic" or dynamic_block_bitlen == min_bitlen then
  1795.             CompressDynamicHuffmanBlock(WriteBits, is_last_block, lcodes
  1796.                 , lextra_bits, dcodes, dextra_bits, HLIT, HDIST, HCLEN
  1797.                 , rle_codes_huffman_bitlens, rle_codes_huffman_codes
  1798.                 , rle_deflate_codes, rle_extra_bits
  1799.                 , lcodes_huffman_bitlens, lcodes_huffman_codes
  1800.                 , dcodes_huffman_bitlens, dcodes_huffman_codes)
  1801.             total_bitlen = total_bitlen + dynamic_block_bitlen
  1802.         end
  1803.  
  1804.         if is_last_block then
  1805.             bitlen_written = FlushWriter(_FLUSH_MODE_NO_FLUSH)
  1806.         else
  1807.             bitlen_written = FlushWriter(_FLUSH_MODE_MEMORY_CLEANUP)
  1808.         end
  1809.  
  1810.         assert(bitlen_written == total_bitlen)
  1811.  
  1812.         -- Memory clean up, so memory consumption does not always grow linearly
  1813.         -- , even if input string is > 64K.
  1814.         -- Not a very efficient operation, but this operation won't happen
  1815.         -- when the input data size is less than 64K.
  1816.         if not is_last_block then
  1817.             local j
  1818.             if dictionary and block_start == 1 then
  1819.                 j = 0
  1820.                 while (string_table[j]) do
  1821.                     string_table[j] = nil
  1822.                     j = j - 1
  1823.                 end
  1824.             end
  1825.             dictionary = nil
  1826.             j = 1
  1827.             for i = block_end-32767, block_end do
  1828.                 string_table[j] = string_table[i-offset]
  1829.                 j = j + 1
  1830.             end
  1831.             for k, t in pairs(hash_tables) do
  1832.                 local tSize = #t
  1833.                 if tSize > 0 and block_end+1 - t[1] > 32768 then
  1834.                     if tSize == 1 then
  1835.                         --hash_tables[k] = nil -- seems to be causing problems
  1836.                     else
  1837.                         local new = {}
  1838.                         local newSize = 0
  1839.                         for i = 2, tSize do
  1840.                             j = t[i]
  1841.                             if block_end+1 - j <= 32768 then
  1842.                                 newSize = newSize + 1
  1843.                                 new[newSize] = j
  1844.                             end
  1845.                         end
  1846.                         hash_tables[k] = new
  1847.                     end
  1848.                 end
  1849.             end
  1850.         end
  1851.  
  1852.         if os and os.pullEvent then -- ComputerCraft requires this for long-running processes
  1853.             os.queueEvent "nosleep"
  1854.             os.pullEvent()
  1855.         end
  1856.     end
  1857. end
  1858.  
  1859. --- The description to compression configuration table. <br>
  1860. -- Any field can be nil to use its default. <br>
  1861. -- Table with keys other than those below is an invalid table.
  1862. -- @class table
  1863. -- @name compression_configs
  1864. -- @field level The compression level ranged from 0 to 9. 0 is no compression.
  1865. -- 9 is the slowest but best compression. Use nil for default level.
  1866. -- @field strategy The compression strategy. "fixed" to only use fixed deflate
  1867. -- compression block. "dynamic" to only use dynamic block. "huffman_only" to
  1868. -- do no LZ77 compression. Only do huffman compression.
  1869.  
  1870.  
  1871. -- @see LibDeflate.CompressDeflate(str, configs)
  1872. -- @see LibDeflate.CompressDeflateWithDict(str, dictionary, configs)
  1873. local function CompressDeflateInternal(str, dictionary, configs)
  1874.     local WriteBits, WriteString, FlushWriter = CreateWriter()
  1875.     Deflate(configs, WriteBits, WriteString, FlushWriter, str, dictionary)
  1876.     local total_bitlen, result = FlushWriter(_FLUSH_MODE_OUTPUT)
  1877.     local padding_bitlen = (8-total_bitlen%8)%8
  1878.     return result, padding_bitlen
  1879. end
  1880.  
  1881. -- @see LibDeflate.CompressZlib
  1882. -- @see LibDeflate.CompressZlibWithDict
  1883. local function CompressZlibInternal(str, dictionary, configs)
  1884.     local WriteBits, WriteString, FlushWriter = CreateWriter()
  1885.  
  1886.     local CM = 8 -- Compression method
  1887.     local CINFO = 7 --Window Size = 32K
  1888.     local CMF = CINFO*16+CM
  1889.     WriteBits(CMF, 8)
  1890.  
  1891.     local FDIST = dictionary and 1 or 0
  1892.     local FLEVEL = 2 -- Default compression
  1893.     local FLG = FLEVEL*64+FDIST*32
  1894.     local FCHECK = (31-(CMF*256+FLG)%31)
  1895.     -- The FCHECK value must be such that CMF and FLG,
  1896.     -- when viewed as a 16-bit unsigned integer stored
  1897.     -- in MSB order (CMF*256 + FLG), is a multiple of 31.
  1898.     FLG = FLG + FCHECK
  1899.     WriteBits(FLG, 8)
  1900.  
  1901.     if FDIST == 1 then
  1902.         local adler32 = dictionary.adler32
  1903.         local byte0 = adler32 % 256
  1904.         adler32 = (adler32 - byte0) / 256
  1905.         local byte1 = adler32 % 256
  1906.         adler32 = (adler32 - byte1) / 256
  1907.         local byte2 = adler32 % 256
  1908.         adler32 = (adler32 - byte2) / 256
  1909.         local byte3 = adler32 % 256
  1910.         WriteBits(byte3, 8)
  1911.         WriteBits(byte2, 8)
  1912.         WriteBits(byte1, 8)
  1913.         WriteBits(byte0, 8)
  1914.     end
  1915.  
  1916.     Deflate(configs, WriteBits, WriteString, FlushWriter, str, dictionary)
  1917.     FlushWriter(_FLUSH_MODE_BYTE_BOUNDARY)
  1918.  
  1919.     local adler32 = LibDeflate.Adler32(str)
  1920.  
  1921.     -- Most significant byte first
  1922.     local byte3 = adler32%256
  1923.     adler32 = (adler32 - byte3) / 256
  1924.     local byte2 = adler32%256
  1925.     adler32 = (adler32 - byte2) / 256
  1926.     local byte1 = adler32%256
  1927.     adler32 = (adler32 - byte1) / 256
  1928.     local byte0 = adler32%256
  1929.  
  1930.     WriteBits(byte0, 8)
  1931.     WriteBits(byte1, 8)
  1932.     WriteBits(byte2, 8)
  1933.     WriteBits(byte3, 8)
  1934.     local total_bitlen, result = FlushWriter(_FLUSH_MODE_OUTPUT)
  1935.     local padding_bitlen = (8-total_bitlen%8)%8
  1936.     return result, padding_bitlen
  1937. end
  1938.  
  1939. --- Compress using the raw deflate format.
  1940. -- @param str [string] The data to be compressed.
  1941. -- @param configs [table/nil] The configuration table to control the compression
  1942. -- . If nil, use the default configuration.
  1943. -- @return [string] The compressed data.
  1944. -- @return [integer] The number of bits padded at the end of output.
  1945. -- 0 <= bits < 8  <br>
  1946. -- This means the most significant "bits" of the last byte of the returned
  1947. -- compressed data are padding bits and they don't affect decompression.
  1948. -- You don't need to use this value unless you want to do some postprocessing
  1949. -- to the compressed data.
  1950. -- @see compression_configs
  1951. -- @see LibDeflate.DecompressDeflate
  1952. function LibDeflate.CompressDeflate(str, configs)
  1953.     local arg_valid, arg_err = IsValidArguments(str, false, nil, true, configs)
  1954.     if not arg_valid then
  1955.         error(("Usage: LibDeflate.CompressDeflate(str, configs): "
  1956.             ..arg_err), 2)
  1957.     end
  1958.     return CompressDeflateInternal(str, nil, configs)
  1959. end
  1960.  
  1961. --- Compress using the raw deflate format with a preset dictionary.
  1962. -- @param str [string] The data to be compressed.
  1963. -- @param dictionary [table] The preset dictionary produced by
  1964. -- LibDeflate.CreateDictionary
  1965. -- @param configs [table/nil] The configuration table to control the compression
  1966. -- . If nil, use the default configuration.
  1967. -- @return [string] The compressed data.
  1968. -- @return [integer] The number of bits padded at the end of output.
  1969. -- 0 <= bits < 8  <br>
  1970. -- This means the most significant "bits" of the last byte of the returned
  1971. -- compressed data are padding bits and they don't affect decompression.
  1972. -- You don't need to use this value unless you want to do some postprocessing
  1973. -- to the compressed data.
  1974. -- @see compression_configs
  1975. -- @see LibDeflate.CreateDictionary
  1976. -- @see LibDeflate.DecompressDeflateWithDict
  1977. function LibDeflate.CompressDeflateWithDict(str, dictionary, configs)
  1978.     local arg_valid, arg_err = IsValidArguments(str, true, dictionary
  1979.         , true, configs)
  1980.     if not arg_valid then
  1981.         error(("Usage: LibDeflate.CompressDeflateWithDict"
  1982.             .."(str, dictionary, configs): "
  1983.             ..arg_err), 2)
  1984.     end
  1985.     return CompressDeflateInternal(str, dictionary, configs)
  1986. end
  1987.  
  1988. --- Compress using the zlib format.
  1989. -- @param str [string] the data to be compressed.
  1990. -- @param configs [table/nil] The configuration table to control the compression
  1991. -- . If nil, use the default configuration.
  1992. -- @return [string] The compressed data.
  1993. -- @return [integer] The number of bits padded at the end of output.
  1994. -- Should always be 0.
  1995. -- Zlib formatted compressed data never has padding bits at the end.
  1996. -- @see compression_configs
  1997. -- @see LibDeflate.DecompressZlib
  1998. function LibDeflate.CompressZlib(str, configs)
  1999.     local arg_valid, arg_err = IsValidArguments(str, false, nil, true, configs)
  2000.     if not arg_valid then
  2001.         error(("Usage: LibDeflate.CompressZlib(str, configs): "
  2002.             ..arg_err), 2)
  2003.     end
  2004.     return CompressZlibInternal(str, nil, configs)
  2005. end
  2006.  
  2007. --- Compress using the zlib format with a preset dictionary.
  2008. -- @param str [string] the data to be compressed.
  2009. -- @param dictionary [table] A preset dictionary produced
  2010. -- by LibDeflate.CreateDictionary()
  2011. -- @param configs [table/nil] The configuration table to control the compression
  2012. -- . If nil, use the default configuration.
  2013. -- @return [string] The compressed data.
  2014. -- @return [integer] The number of bits padded at the end of output.
  2015. -- Should always be 0.
  2016. -- Zlib formatted compressed data never has padding bits at the end.
  2017. -- @see compression_configs
  2018. -- @see LibDeflate.CreateDictionary
  2019. -- @see LibDeflate.DecompressZlibWithDict
  2020. function LibDeflate.CompressZlibWithDict(str, dictionary, configs)
  2021.     local arg_valid, arg_err = IsValidArguments(str, true, dictionary
  2022.         , true, configs)
  2023.     if not arg_valid then
  2024.         error(("Usage: LibDeflate.CompressZlibWithDict"
  2025.             .."(str, dictionary, configs): "
  2026.             ..arg_err), 2)
  2027.     end
  2028.     return CompressZlibInternal(str, dictionary, configs)
  2029. end
  2030.  
  2031. --[[ --------------------------------------------------------------------------
  2032.     Decompress code
  2033. --]] --------------------------------------------------------------------------
  2034.  
  2035. --[[
  2036.     Create a reader to easily reader stuffs as the unit of bits.
  2037.     Return values:
  2038.     1. ReadBits(bitlen)
  2039.     2. ReadBytes(bytelen, buffer, buffer_size)
  2040.     3. Decode(huffman_bitlen_count, huffman_symbol, min_bitlen)
  2041.     4. ReaderBitlenLeft()
  2042.     5. SkipToByteBoundary()
  2043. --]]
  2044. local function CreateReader(input_string)
  2045.     local input = input_string
  2046.     local input_strlen = #input_string
  2047.     local input_next_byte_pos = 1
  2048.     local cache_bitlen = 0
  2049.     local cache = 0
  2050.  
  2051.     -- Read some bits.
  2052.     -- To improve speed, this function does not
  2053.     -- check if the input has been exhausted.
  2054.     -- Use ReaderBitlenLeft() < 0 to check it.
  2055.     -- @param bitlen the number of bits to read
  2056.     -- @return the data is read.
  2057.     local function ReadBits(bitlen)
  2058.         local rshift_mask = _pow2[bitlen]
  2059.         local code
  2060.         if bitlen <= cache_bitlen then
  2061.             code = cache % rshift_mask
  2062.             cache = (cache - code) / rshift_mask
  2063.             cache_bitlen = cache_bitlen - bitlen
  2064.         else -- Whether input has been exhausted is not checked.
  2065.             local lshift_mask = _pow2[cache_bitlen]
  2066.             local byte1, byte2, byte3, byte4 = string_byte(input
  2067.                 , input_next_byte_pos, input_next_byte_pos+3)
  2068.             -- This requires lua number to be at least double ()
  2069.             cache = cache + ((byte1 or 0)+(byte2 or 0)*256
  2070.                 + (byte3 or 0)*65536+(byte4 or 0)*16777216)*lshift_mask
  2071.             input_next_byte_pos = input_next_byte_pos + 4
  2072.             cache_bitlen = cache_bitlen + 32 - bitlen
  2073.             code = cache % rshift_mask
  2074.             cache = (cache - code) / rshift_mask
  2075.         end
  2076.         return code
  2077.     end
  2078.  
  2079.     -- Read some bytes from the reader.
  2080.     -- Assume reader is on the byte boundary.
  2081.     -- @param bytelen The number of bytes to be read.
  2082.     -- @param buffer The byte read will be stored into this buffer.
  2083.     -- @param buffer_size The buffer will be modified starting from
  2084.     --  buffer[buffer_size+1], ending at buffer[buffer_size+bytelen-1]
  2085.     -- @return the new buffer_size
  2086.     local function ReadBytes(bytelen, buffer, buffer_size)
  2087.         assert(cache_bitlen % 8 == 0)
  2088.  
  2089.         local byte_from_cache = (cache_bitlen/8 < bytelen)
  2090.             and (cache_bitlen/8) or bytelen
  2091.         for _=1, byte_from_cache do
  2092.             local byte = cache % 256
  2093.             buffer_size = buffer_size + 1
  2094.             buffer[buffer_size] = string_char(byte)
  2095.             cache = (cache - byte) / 256
  2096.         end
  2097.         cache_bitlen = cache_bitlen - byte_from_cache*8
  2098.         bytelen = bytelen - byte_from_cache
  2099.         if (input_strlen - input_next_byte_pos - bytelen + 1) * 8
  2100.             + cache_bitlen < 0 then
  2101.             return -1 -- out of input
  2102.         end
  2103.         for i=input_next_byte_pos, input_next_byte_pos+bytelen-1 do
  2104.             buffer_size = buffer_size + 1
  2105.             buffer[buffer_size] = string_sub(input, i, i)
  2106.         end
  2107.  
  2108.         input_next_byte_pos = input_next_byte_pos + bytelen
  2109.         return buffer_size
  2110.     end
  2111.  
  2112.     -- Decode huffman code
  2113.     -- To improve speed, this function does not check
  2114.     -- if the input has been exhausted.
  2115.     -- Use ReaderBitlenLeft() < 0 to check it.
  2116.     -- Credits for Mark Adler. This code is from puff:Decode()
  2117.     -- @see puff:Decode(...)
  2118.     -- @param huffman_bitlen_count
  2119.     -- @param huffman_symbol
  2120.     -- @param min_bitlen The minimum huffman bit length of all symbols
  2121.     -- @return The decoded deflate code.
  2122.     --  Negative value is returned if decoding fails.
  2123.     local function Decode(huffman_bitlen_counts, huffman_symbols, min_bitlen)
  2124.         local code = 0
  2125.         local first = 0
  2126.         local index = 0
  2127.         local count
  2128.         if min_bitlen > 0 then
  2129.             if cache_bitlen < 15 and input then
  2130.                 local lshift_mask = _pow2[cache_bitlen]
  2131.                 local byte1, byte2, byte3, byte4 =
  2132.                     string_byte(input, input_next_byte_pos
  2133.                     , input_next_byte_pos+3)
  2134.                 -- This requires lua number to be at least double ()
  2135.                 cache = cache + ((byte1 or 0)+(byte2 or 0)*256
  2136.                     +(byte3 or 0)*65536+(byte4 or 0)*16777216)*lshift_mask
  2137.                 input_next_byte_pos = input_next_byte_pos + 4
  2138.                 cache_bitlen = cache_bitlen + 32
  2139.             end
  2140.  
  2141.             local rshift_mask = _pow2[min_bitlen]
  2142.             cache_bitlen = cache_bitlen - min_bitlen
  2143.             code = cache % rshift_mask
  2144.             cache = (cache - code) / rshift_mask
  2145.             -- Reverse the bits
  2146.             code = _reverse_bits_tbl[min_bitlen][code]
  2147.  
  2148.             count = huffman_bitlen_counts[min_bitlen]
  2149.             if code < count then
  2150.                 return huffman_symbols[code]
  2151.             end
  2152.             index = count
  2153.             first = count * 2
  2154.             code = code * 2
  2155.         end
  2156.  
  2157.         for bitlen = min_bitlen+1, 15 do
  2158.             local bit
  2159.             bit = cache % 2
  2160.             cache = (cache - bit) / 2
  2161.             cache_bitlen = cache_bitlen - 1
  2162.  
  2163.             code = (bit==1) and (code + 1 - code % 2) or code
  2164.             count = huffman_bitlen_counts[bitlen] or 0
  2165.             local diff = code - first
  2166.             if diff < count then
  2167.                 return huffman_symbols[index + diff]
  2168.             end
  2169.             index = index + count
  2170.             first = first + count
  2171.             first = first * 2
  2172.             code = code * 2
  2173.         end
  2174.         -- invalid literal/length or distance code
  2175.         -- in fixed or dynamic block (run out of code)
  2176.         return -10
  2177.     end
  2178.  
  2179.     local function ReaderBitlenLeft()
  2180.         return (input_strlen - input_next_byte_pos + 1) * 8 + cache_bitlen
  2181.     end
  2182.  
  2183.     local function SkipToByteBoundary()
  2184.         local skipped_bitlen = cache_bitlen%8
  2185.         local rshift_mask = _pow2[skipped_bitlen]
  2186.         cache_bitlen = cache_bitlen - skipped_bitlen
  2187.         cache = (cache - cache % rshift_mask) / rshift_mask
  2188.     end
  2189.  
  2190.     return ReadBits, ReadBytes, Decode, ReaderBitlenLeft, SkipToByteBoundary
  2191. end
  2192.  
  2193. -- Create a deflate state, so I can pass in less arguments to functions.
  2194. -- @param str the whole string to be decompressed.
  2195. -- @param dictionary The preset dictionary. nil if not provided.
  2196. --      This dictionary should be produced by LibDeflate.CreateDictionary(str)
  2197. -- @return The decomrpess state.
  2198. local function CreateDecompressState(str, dictionary)
  2199.     local ReadBits, ReadBytes, Decode, ReaderBitlenLeft
  2200.         , SkipToByteBoundary = CreateReader(str)
  2201.     local state =
  2202.     {
  2203.         ReadBits = ReadBits,
  2204.         ReadBytes = ReadBytes,
  2205.         Decode = Decode,
  2206.         ReaderBitlenLeft = ReaderBitlenLeft,
  2207.         SkipToByteBoundary = SkipToByteBoundary,
  2208.         buffer_size = 0,
  2209.         buffer = {},
  2210.         result_buffer = {},
  2211.         dictionary = dictionary,
  2212.     }
  2213.     return state
  2214. end
  2215.  
  2216. -- Get the stuffs needed to decode huffman codes
  2217. -- @see puff.c:construct(...)
  2218. -- @param huffman_bitlen The huffman bit length of the huffman codes.
  2219. -- @param max_symbol The maximum symbol
  2220. -- @param max_bitlen The min huffman bit length of all codes
  2221. -- @return zero or positive for success, negative for failure.
  2222. -- @return The count of each huffman bit length.
  2223. -- @return A table to convert huffman codes to deflate codes.
  2224. -- @return The minimum huffman bit length.
  2225. local function GetHuffmanForDecode(huffman_bitlens, max_symbol, max_bitlen)
  2226.     local huffman_bitlen_counts = {}
  2227.     local min_bitlen = max_bitlen
  2228.     for symbol = 0, max_symbol do
  2229.         local bitlen = huffman_bitlens[symbol] or 0
  2230.         min_bitlen = (bitlen > 0 and bitlen < min_bitlen)
  2231.             and bitlen or min_bitlen
  2232.         huffman_bitlen_counts[bitlen] = (huffman_bitlen_counts[bitlen] or 0)+1
  2233.     end
  2234.  
  2235.     if huffman_bitlen_counts[0] == max_symbol+1 then -- No Codes
  2236.         return 0, huffman_bitlen_counts, {}, 0 -- Complete, but decode will fail
  2237.     end
  2238.  
  2239.     local left = 1
  2240.     for len = 1, max_bitlen do
  2241.         left = left * 2
  2242.         left = left - (huffman_bitlen_counts[len] or 0)
  2243.         if left < 0 then
  2244.             return left -- Over-subscribed, return negative
  2245.         end
  2246.     end
  2247.  
  2248.     -- Generate offsets info symbol table for each length for sorting
  2249.     local offsets = {}
  2250.     offsets[1] = 0
  2251.     for len = 1, max_bitlen-1 do
  2252.         offsets[len + 1] = offsets[len] + (huffman_bitlen_counts[len] or 0)
  2253.     end
  2254.  
  2255.     local huffman_symbols = {}
  2256.     for symbol = 0, max_symbol do
  2257.         local bitlen = huffman_bitlens[symbol] or 0
  2258.         if bitlen ~= 0 then
  2259.             local offset = offsets[bitlen]
  2260.             huffman_symbols[offset] = symbol
  2261.             offsets[bitlen] = offsets[bitlen] + 1
  2262.         end
  2263.     end
  2264.  
  2265.     -- Return zero for complete set, positive for incomplete set.
  2266.     return left, huffman_bitlen_counts, huffman_symbols, min_bitlen
  2267. end
  2268.  
  2269. -- Decode a fixed or dynamic huffman blocks, excluding last block identifier
  2270. -- and block type identifer.
  2271. -- @see puff.c:codes()
  2272. -- @param state decompression state that will be modified by this function.
  2273. --  @see CreateDecompressState
  2274. -- @param ... Read the source code
  2275. -- @return 0 on success, other value on failure.
  2276. local function DecodeUntilEndOfBlock(state, lcodes_huffman_bitlens
  2277.     , lcodes_huffman_symbols, lcodes_huffman_min_bitlen
  2278.     , dcodes_huffman_bitlens, dcodes_huffman_symbols
  2279.     , dcodes_huffman_min_bitlen)
  2280.     local buffer, buffer_size, ReadBits, Decode, ReaderBitlenLeft
  2281.         , result_buffer =
  2282.         state.buffer, state.buffer_size, state.ReadBits, state.Decode
  2283.         , state.ReaderBitlenLeft, state.result_buffer
  2284.     local dictionary = state.dictionary
  2285.     local dict_string_table
  2286.     local dict_strlen
  2287.  
  2288.     local buffer_end = 1
  2289.     if dictionary and not buffer[0] then
  2290.         -- If there is a dictionary, copy the last 258 bytes into
  2291.         -- the string_table to make the copy in the main loop quicker.
  2292.         -- This is done only once per decompression.
  2293.         dict_string_table = dictionary.string_table
  2294.         dict_strlen = dictionary.strlen
  2295.         buffer_end = -dict_strlen + 1
  2296.         for i=0, (-dict_strlen+1)<-257 and -257 or (-dict_strlen+1), -1 do
  2297.             buffer[i] = _byte_to_char[dict_string_table[dict_strlen+i]]
  2298.         end
  2299.     end
  2300.  
  2301.     repeat
  2302.         local symbol = Decode(lcodes_huffman_bitlens
  2303.             , lcodes_huffman_symbols, lcodes_huffman_min_bitlen)
  2304.         if symbol < 0 or symbol > 285 then
  2305.         -- invalid literal/length or distance code in fixed or dynamic block
  2306.             return -10
  2307.         elseif symbol < 256 then -- Literal
  2308.             buffer_size = buffer_size + 1
  2309.             buffer[buffer_size] = _byte_to_char[symbol]
  2310.         elseif symbol > 256 then -- Length code
  2311.             symbol = symbol - 256
  2312.             local bitlen = _literal_deflate_code_to_base_len[symbol]
  2313.             bitlen = (symbol >= 8)
  2314.                  and (bitlen
  2315.                  + ReadBits(_literal_deflate_code_to_extra_bitlen[symbol]))
  2316.                     or bitlen
  2317.             symbol = Decode(dcodes_huffman_bitlens, dcodes_huffman_symbols
  2318.                 , dcodes_huffman_min_bitlen)
  2319.             if symbol < 0 or symbol > 29 then
  2320.             -- invalid literal/length or distance code in fixed or dynamic block
  2321.                 return -10
  2322.             end
  2323.             local dist = _dist_deflate_code_to_base_dist[symbol]
  2324.             dist = (dist > 4) and (dist
  2325.                 + ReadBits(_dist_deflate_code_to_extra_bitlen[symbol])) or dist
  2326.  
  2327.             local char_buffer_index = buffer_size-dist+1
  2328.             if char_buffer_index < buffer_end then
  2329.             -- distance is too far back in fixed or dynamic block
  2330.                 return -11
  2331.             end
  2332.             if char_buffer_index >= -257 then
  2333.                 for _=1, bitlen do
  2334.                     buffer_size = buffer_size + 1
  2335.                     buffer[buffer_size] = buffer[char_buffer_index]
  2336.                     char_buffer_index = char_buffer_index + 1
  2337.                 end
  2338.             else
  2339.                 char_buffer_index = dict_strlen + char_buffer_index
  2340.                 for _=1, bitlen do
  2341.                     buffer_size = buffer_size + 1
  2342.                     buffer[buffer_size] =
  2343.                     _byte_to_char[dict_string_table[char_buffer_index]]
  2344.                     char_buffer_index = char_buffer_index + 1
  2345.                 end
  2346.             end
  2347.         end
  2348.  
  2349.         if ReaderBitlenLeft() < 0 then
  2350.             return 2 -- available inflate data did not terminate
  2351.         end
  2352.  
  2353.         if buffer_size >= 65536 then
  2354.             result_buffer[#result_buffer+1] =
  2355.                 table_concat(buffer, "", 1, 32768)
  2356.             for i=32769, buffer_size do
  2357.                 buffer[i-32768] = buffer[i]
  2358.             end
  2359.             buffer_size = buffer_size - 32768
  2360.             buffer[buffer_size+1] = nil
  2361.             -- NOTE: buffer[32769..end] and buffer[-257..0] are not cleared.
  2362.             -- This is why "buffer_size" variable is needed.
  2363.         end
  2364.     until symbol == 256
  2365.  
  2366.     state.buffer_size = buffer_size
  2367.  
  2368.     return 0
  2369. end
  2370.  
  2371. -- Decompress a store block
  2372. -- @param state decompression state that will be modified by this function.
  2373. -- @return 0 if succeeds, other value if fails.
  2374. local function DecompressStoreBlock(state)
  2375.     local buffer, buffer_size, ReadBits, ReadBytes, ReaderBitlenLeft
  2376.         , SkipToByteBoundary, result_buffer =
  2377.         state.buffer, state.buffer_size, state.ReadBits, state.ReadBytes
  2378.         , state.ReaderBitlenLeft, state.SkipToByteBoundary, state.result_buffer
  2379.  
  2380.     SkipToByteBoundary()
  2381.     local bytelen = ReadBits(16)
  2382.     if ReaderBitlenLeft() < 0 then
  2383.         return 2 -- available inflate data did not terminate
  2384.     end
  2385.     local bytelenComp = ReadBits(16)
  2386.     if ReaderBitlenLeft() < 0 then
  2387.         return 2 -- available inflate data did not terminate
  2388.     end
  2389.  
  2390.     if bytelen % 256 + bytelenComp % 256 ~= 255 then
  2391.         return -2 -- Not one's complement
  2392.     end
  2393.     if (bytelen-bytelen % 256)/256
  2394.         + (bytelenComp-bytelenComp % 256)/256 ~= 255 then
  2395.         return -2 -- Not one's complement
  2396.     end
  2397.  
  2398.     -- Note that ReadBytes will skip to the next byte boundary first.
  2399.     buffer_size = ReadBytes(bytelen, buffer, buffer_size)
  2400.     if buffer_size < 0 then
  2401.         return 2 -- available inflate data did not terminate
  2402.     end
  2403.  
  2404.     -- memory clean up when there are enough bytes in the buffer.
  2405.     if buffer_size >= 65536 then
  2406.         result_buffer[#result_buffer+1] = table_concat(buffer, "", 1, 32768)
  2407.         for i=32769, buffer_size do
  2408.             buffer[i-32768] = buffer[i]
  2409.         end
  2410.         buffer_size = buffer_size - 32768
  2411.         buffer[buffer_size+1] = nil
  2412.     end
  2413.     state.buffer_size = buffer_size
  2414.     return 0
  2415. end
  2416.  
  2417. -- Decompress a fixed block
  2418. -- @param state decompression state that will be modified by this function.
  2419. -- @return 0 if succeeds other value if fails.
  2420. local function DecompressFixBlock(state)
  2421.     return DecodeUntilEndOfBlock(state
  2422.         , _fix_block_literal_huffman_bitlen_count
  2423.         , _fix_block_literal_huffman_to_deflate_code, 7
  2424.         , _fix_block_dist_huffman_bitlen_count
  2425.         , _fix_block_dist_huffman_to_deflate_code, 5)
  2426. end
  2427.  
  2428. -- Decompress a dynamic block
  2429. -- @param state decompression state that will be modified by this function.
  2430. -- @return 0 if success, other value if fails.
  2431. local function DecompressDynamicBlock(state)
  2432.     local ReadBits, Decode = state.ReadBits, state.Decode
  2433.     local nlen = ReadBits(5) + 257
  2434.     local ndist = ReadBits(5) + 1
  2435.     local ncode = ReadBits(4) + 4
  2436.     if nlen > 286 or ndist > 30 then
  2437.         -- dynamic block code description: too many length or distance codes
  2438.         return -3
  2439.     end
  2440.  
  2441.     local rle_codes_huffman_bitlens = {}
  2442.  
  2443.     for i = 1, ncode do
  2444.         rle_codes_huffman_bitlens[_rle_codes_huffman_bitlen_order[i]] =
  2445.             ReadBits(3)
  2446.     end
  2447.  
  2448.     local rle_codes_err, rle_codes_huffman_bitlen_counts,
  2449.         rle_codes_huffman_symbols, rle_codes_huffman_min_bitlen =
  2450.         GetHuffmanForDecode(rle_codes_huffman_bitlens, 18, 7)
  2451.     if rle_codes_err ~= 0 then -- Require complete code set here
  2452.         -- dynamic block code description: code lengths codes incomplete
  2453.         return -4
  2454.     end
  2455.  
  2456.     local lcodes_huffman_bitlens = {}
  2457.     local dcodes_huffman_bitlens = {}
  2458.     -- Read length/literal and distance code length tables
  2459.     local index = 0
  2460.     while index < nlen + ndist do
  2461.         local symbol -- Decoded value
  2462.         local bitlen -- Last length to repeat
  2463.  
  2464.         symbol = Decode(rle_codes_huffman_bitlen_counts
  2465.             , rle_codes_huffman_symbols, rle_codes_huffman_min_bitlen)
  2466.  
  2467.         if symbol < 0 then
  2468.             return symbol -- Invalid symbol
  2469.         elseif symbol < 16 then
  2470.             if index < nlen then
  2471.                 lcodes_huffman_bitlens[index] = symbol
  2472.             else
  2473.                 dcodes_huffman_bitlens[index-nlen] = symbol
  2474.             end
  2475.             index = index + 1
  2476.         else
  2477.             bitlen = 0
  2478.             if symbol == 16 then
  2479.                 if index == 0 then
  2480.                     -- dynamic block code description: repeat lengths
  2481.                     -- with no first length
  2482.                     return -5
  2483.                 end
  2484.                 if index-1 < nlen then
  2485.                     bitlen = lcodes_huffman_bitlens[index-1]
  2486.                 else
  2487.                     bitlen = dcodes_huffman_bitlens[index-nlen-1]
  2488.                 end
  2489.                 symbol = 3 + ReadBits(2)
  2490.             elseif symbol == 17 then -- Repeat zero 3..10 times
  2491.                 symbol = 3 + ReadBits(3)
  2492.             else -- == 18, repeat zero 11.138 times
  2493.                 symbol = 11 + ReadBits(7)
  2494.             end
  2495.             if index + symbol > nlen + ndist then
  2496.                 -- dynamic block code description:
  2497.                 -- repeat more than specified lengths
  2498.                 return -6
  2499.             end
  2500.             while symbol > 0 do -- Repeat last or zero symbol times
  2501.                 symbol = symbol - 1
  2502.                 if index < nlen then
  2503.                     lcodes_huffman_bitlens[index] = bitlen
  2504.                 else
  2505.                     dcodes_huffman_bitlens[index-nlen] = bitlen
  2506.                 end
  2507.                 index = index + 1
  2508.             end
  2509.         end
  2510.     end
  2511.  
  2512.     if (lcodes_huffman_bitlens[256] or 0) == 0 then
  2513.         -- dynamic block code description: missing end-of-block code
  2514.         return -9
  2515.     end
  2516.  
  2517.     local lcodes_err, lcodes_huffman_bitlen_counts
  2518.         , lcodes_huffman_symbols, lcodes_huffman_min_bitlen =
  2519.         GetHuffmanForDecode(lcodes_huffman_bitlens, nlen-1, 15)
  2520.     --dynamic block code description: invalid literal/length code lengths,
  2521.     -- Incomplete code ok only for single length 1 code
  2522.     if (lcodes_err ~=0 and (lcodes_err < 0
  2523.         or nlen ~= (lcodes_huffman_bitlen_counts[0] or 0)
  2524.             +(lcodes_huffman_bitlen_counts[1] or 0))) then
  2525.         return -7
  2526.     end
  2527.  
  2528.     local dcodes_err, dcodes_huffman_bitlen_counts
  2529.         , dcodes_huffman_symbols, dcodes_huffman_min_bitlen =
  2530.         GetHuffmanForDecode(dcodes_huffman_bitlens, ndist-1, 15)
  2531.     -- dynamic block code description: invalid distance code lengths,
  2532.     -- Incomplete code ok only for single length 1 code
  2533.     if (dcodes_err ~=0 and (dcodes_err < 0
  2534.         or ndist ~= (dcodes_huffman_bitlen_counts[0] or 0)
  2535.             + (dcodes_huffman_bitlen_counts[1] or 0))) then
  2536.         return -8
  2537.     end
  2538.  
  2539.     -- Build buffman table for literal/length codes
  2540.     return DecodeUntilEndOfBlock(state, lcodes_huffman_bitlen_counts
  2541.         , lcodes_huffman_symbols, lcodes_huffman_min_bitlen
  2542.         , dcodes_huffman_bitlen_counts, dcodes_huffman_symbols
  2543.         , dcodes_huffman_min_bitlen)
  2544. end
  2545.  
  2546. -- Decompress a deflate stream
  2547. -- @param state: a decompression state
  2548. -- @return the decompressed string if succeeds. nil if fails.
  2549. local function Inflate(state)
  2550.     local ReadBits = state.ReadBits
  2551.  
  2552.     local is_last_block
  2553.     while not is_last_block do
  2554.         is_last_block = (ReadBits(1) == 1)
  2555.         local block_type = ReadBits(2)
  2556.         local status
  2557.         if block_type == 0 then
  2558.             status = DecompressStoreBlock(state)
  2559.         elseif block_type == 1 then
  2560.             status = DecompressFixBlock(state)
  2561.         elseif block_type == 2 then
  2562.             status = DecompressDynamicBlock(state)
  2563.         else
  2564.             return nil, -1 -- invalid block type (type == 3)
  2565.         end
  2566.         if status ~= 0 then
  2567.             return nil, status
  2568.         end
  2569.         -- ComputerCraft requires this for long-running processes
  2570.         if os and os.pullEvent then
  2571.             os.queueEvent("nosleep")
  2572.             os.pullEvent()
  2573.         end
  2574.     end
  2575.  
  2576.     state.result_buffer[#state.result_buffer+1] =
  2577.         table_concat(state.buffer, "", 1, state.buffer_size)
  2578.     local result = table_concat(state.result_buffer)
  2579.     return result
  2580. end
  2581.  
  2582. -- @see LibDeflate.DecompressDeflate(str)
  2583. -- @see LibDeflate.DecompressDeflateWithDict(str, dictionary)
  2584. local function DecompressDeflateInternal(str, dictionary)
  2585.     local state = CreateDecompressState(str, dictionary)
  2586.     local result, status = Inflate(state)
  2587.     if not result then
  2588.         return nil, status
  2589.     end
  2590.  
  2591.     local bitlen_left = state.ReaderBitlenLeft()
  2592.     local bytelen_left = (bitlen_left - bitlen_left % 8) / 8
  2593.     return result, bytelen_left
  2594. end
  2595.  
  2596. -- @see LibDeflate.DecompressZlib(str)
  2597. -- @see LibDeflate.DecompressZlibWithDict(str)
  2598. local function DecompressZlibInternal(str, dictionary)
  2599.     local state = CreateDecompressState(str, dictionary)
  2600.     local ReadBits = state.ReadBits
  2601.  
  2602.     local CMF = ReadBits(8)
  2603.     if state.ReaderBitlenLeft() < 0 then
  2604.         return nil, 2 -- available inflate data did not terminate
  2605.     end
  2606.     local CM = CMF % 16
  2607.     local CINFO = (CMF - CM) / 16
  2608.     if CM ~= 8 then
  2609.         return nil, -12 -- invalid compression method
  2610.     end
  2611.     if CINFO > 7 then
  2612.         return nil, -13 -- invalid window size
  2613.     end
  2614.  
  2615.     local FLG = ReadBits(8)
  2616.     if state.ReaderBitlenLeft() < 0 then
  2617.         return nil, 2 -- available inflate data did not terminate
  2618.     end
  2619.     if (CMF*256+FLG)%31 ~= 0 then
  2620.         return nil, -14 -- invalid header checksum
  2621.     end
  2622.  
  2623.     local FDIST = ((FLG-FLG%32)/32 % 2)
  2624.     local FLEVEL = ((FLG-FLG%64)/64 % 4) -- luacheck: ignore FLEVEL
  2625.  
  2626.     if FDIST == 1 then
  2627.         if not dictionary then
  2628.             return nil, -16 -- need dictonary, but dictionary is not provided.
  2629.         end
  2630.         local byte3 = ReadBits(8)
  2631.         local byte2 = ReadBits(8)
  2632.         local byte1 = ReadBits(8)
  2633.         local byte0 = ReadBits(8)
  2634.         local actual_adler32 = byte3*16777216+byte2*65536+byte1*256+byte0
  2635.         if state.ReaderBitlenLeft() < 0 then
  2636.             return nil, 2 -- available inflate data did not terminate
  2637.         end
  2638.         if not IsEqualAdler32(actual_adler32, dictionary.adler32) then
  2639.             return nil, -17 -- dictionary adler32 does not match
  2640.         end
  2641.     end
  2642.     local result, status = Inflate(state)
  2643.     if not result then
  2644.         return nil, status
  2645.     end
  2646.     state.SkipToByteBoundary()
  2647.  
  2648.     local adler_byte0 = ReadBits(8)
  2649.     local adler_byte1 = ReadBits(8)
  2650.     local adler_byte2 = ReadBits(8)
  2651.     local adler_byte3 = ReadBits(8)
  2652.     if state.ReaderBitlenLeft() < 0 then
  2653.         return nil, 2 -- available inflate data did not terminate
  2654.     end
  2655.  
  2656.     local adler32_expected = adler_byte0*16777216
  2657.         + adler_byte1*65536 + adler_byte2*256 + adler_byte3
  2658.     local adler32_actual = LibDeflate.Adler32(result)
  2659.     if not IsEqualAdler32(adler32_expected, adler32_actual) then
  2660.         return nil, -15 -- Adler32 checksum does not match
  2661.     end
  2662.  
  2663.     local bitlen_left = state.ReaderBitlenLeft()
  2664.     local bytelen_left = (bitlen_left - bitlen_left % 8) / 8
  2665.     return result, bytelen_left
  2666. end
  2667.  
  2668. --- Decompress a raw deflate compressed data.
  2669. -- @param str [string] The data to be decompressed.
  2670. -- @return [string/nil] If the decompression succeeds, return the decompressed
  2671. -- data. If the decompression fails, return nil. You should check if this return
  2672. -- value is non-nil to know if the decompression succeeds.
  2673. -- @return [integer] If the decompression succeeds, return the number of
  2674. -- unprocessed bytes in the input compressed data. This return value is a
  2675. -- positive integer if the input data is a valid compressed data appended by an
  2676. -- arbitary non-empty string. This return value is 0 if the input data does not
  2677. -- contain any extra bytes.<br>
  2678. -- If the decompression fails (The first return value of this function is nil),
  2679. -- this return value is undefined.
  2680. -- @see LibDeflate.CompressDeflate
  2681. function LibDeflate.DecompressDeflate(str)
  2682.     local arg_valid, arg_err = IsValidArguments(str)
  2683.     if not arg_valid then
  2684.         error(("Usage: LibDeflate.DecompressDeflate(str): "
  2685.             ..arg_err), 2)
  2686.     end
  2687.     return DecompressDeflateInternal(str)
  2688. end
  2689.  
  2690. --- Decompress a raw deflate compressed data with a preset dictionary.
  2691. -- @param str [string] The data to be decompressed.
  2692. -- @param dictionary [table] The preset dictionary used by
  2693. -- LibDeflate.CompressDeflateWithDict when the compressed data is produced.
  2694. -- Decompression and compression must use the same dictionary.
  2695. -- Otherwise wrong decompressed data could be produced without generating any
  2696. -- error.
  2697. -- @return [string/nil] If the decompression succeeds, return the decompressed
  2698. -- data. If the decompression fails, return nil. You should check if this return
  2699. -- value is non-nil to know if the decompression succeeds.
  2700. -- @return [integer] If the decompression succeeds, return the number of
  2701. -- unprocessed bytes in the input compressed data. This return value is a
  2702. -- positive integer if the input data is a valid compressed data appended by an
  2703. -- arbitary non-empty string. This return value is 0 if the input data does not
  2704. -- contain any extra bytes.<br>
  2705. -- If the decompression fails (The first return value of this function is nil),
  2706. -- this return value is undefined.
  2707. -- @see LibDeflate.CompressDeflateWithDict
  2708. function LibDeflate.DecompressDeflateWithDict(str, dictionary)
  2709.     local arg_valid, arg_err = IsValidArguments(str, true, dictionary)
  2710.     if not arg_valid then
  2711.         error(("Usage: LibDeflate.DecompressDeflateWithDict(str, dictionary): "
  2712.             ..arg_err), 2)
  2713.     end
  2714.     return DecompressDeflateInternal(str, dictionary)
  2715. end
  2716.  
  2717. --- Decompress a zlib compressed data.
  2718. -- @param str [string] The data to be decompressed
  2719. -- @return [string/nil] If the decompression succeeds, return the decompressed
  2720. -- data. If the decompression fails, return nil. You should check if this return
  2721. -- value is non-nil to know if the decompression succeeds.
  2722. -- @return [integer] If the decompression succeeds, return the number of
  2723. -- unprocessed bytes in the input compressed data. This return value is a
  2724. -- positive integer if the input data is a valid compressed data appended by an
  2725. -- arbitary non-empty string. This return value is 0 if the input data does not
  2726. -- contain any extra bytes.<br>
  2727. -- If the decompression fails (The first return value of this function is nil),
  2728. -- this return value is undefined.
  2729. -- @see LibDeflate.CompressZlib
  2730. function LibDeflate.DecompressZlib(str)
  2731.     local arg_valid, arg_err = IsValidArguments(str)
  2732.     if not arg_valid then
  2733.         error(("Usage: LibDeflate.DecompressZlib(str): "
  2734.             ..arg_err), 2)
  2735.     end
  2736.     return DecompressZlibInternal(str)
  2737. end
  2738.  
  2739. --- Decompress a zlib compressed data with a preset dictionary.
  2740. -- @param str [string] The data to be decompressed
  2741. -- @param dictionary [table] The preset dictionary used by
  2742. -- LibDeflate.CompressDeflateWithDict when the compressed data is produced.
  2743. -- Decompression and compression must use the same dictionary.
  2744. -- Otherwise wrong decompressed data could be produced without generating any
  2745. -- error.
  2746. -- @return [string/nil] If the decompression succeeds, return the decompressed
  2747. -- data. If the decompression fails, return nil. You should check if this return
  2748. -- value is non-nil to know if the decompression succeeds.
  2749. -- @return [integer] If the decompression succeeds, return the number of
  2750. -- unprocessed bytes in the input compressed data. This return value is a
  2751. -- positive integer if the input data is a valid compressed data appended by an
  2752. -- arbitary non-empty string. This return value is 0 if the input data does not
  2753. -- contain any extra bytes.<br>
  2754. -- If the decompression fails (The first return value of this function is nil),
  2755. -- this return value is undefined.
  2756. -- @see LibDeflate.CompressZlibWithDict
  2757. function LibDeflate.DecompressZlibWithDict(str, dictionary)
  2758.     local arg_valid, arg_err = IsValidArguments(str, true, dictionary)
  2759.     if not arg_valid then
  2760.         error(("Usage: LibDeflate.DecompressZlibWithDict(str, dictionary): "
  2761.             ..arg_err), 2)
  2762.     end
  2763.     return DecompressZlibInternal(str, dictionary)
  2764. end
  2765.  
  2766. -- Calculate the huffman code of fixed block
  2767. do
  2768.     _fix_block_literal_huffman_bitlen = {}
  2769.     for sym=0, 143 do
  2770.         _fix_block_literal_huffman_bitlen[sym] = 8
  2771.     end
  2772.     for sym=144, 255 do
  2773.         _fix_block_literal_huffman_bitlen[sym] = 9
  2774.     end
  2775.     for sym=256, 279 do
  2776.         _fix_block_literal_huffman_bitlen[sym] = 7
  2777.     end
  2778.     for sym=280, 287 do
  2779.         _fix_block_literal_huffman_bitlen[sym] = 8
  2780.     end
  2781.  
  2782.     _fix_block_dist_huffman_bitlen = {}
  2783.     for dist=0, 31 do
  2784.         _fix_block_dist_huffman_bitlen[dist] = 5
  2785.     end
  2786.     local status
  2787.     status, _fix_block_literal_huffman_bitlen_count
  2788.         , _fix_block_literal_huffman_to_deflate_code =
  2789.         GetHuffmanForDecode(_fix_block_literal_huffman_bitlen, 287, 9)
  2790.     assert(status == 0)
  2791.     status, _fix_block_dist_huffman_bitlen_count,
  2792.         _fix_block_dist_huffman_to_deflate_code =
  2793.         GetHuffmanForDecode(_fix_block_dist_huffman_bitlen, 31, 5)
  2794.     assert(status == 0)
  2795.  
  2796.     _fix_block_literal_huffman_code =
  2797.         GetHuffmanCodeFromBitlen(_fix_block_literal_huffman_bitlen_count
  2798.         , _fix_block_literal_huffman_bitlen, 287, 9)
  2799.     _fix_block_dist_huffman_code =
  2800.         GetHuffmanCodeFromBitlen(_fix_block_dist_huffman_bitlen_count
  2801.         , _fix_block_dist_huffman_bitlen, 31, 5)
  2802. end
  2803.  
  2804. -- For test. Don't use the functions in this table for real application.
  2805. -- Stuffs in this table is subject to change.
  2806. LibDeflate.internals = {
  2807.     LoadStringToTable = LoadStringToTable,
  2808.     IsValidDictionary = IsValidDictionary,
  2809.     IsEqualAdler32 = IsEqualAdler32
  2810. }
  2811.  
  2812. return LibDeflate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement