Advertisement
Guest User

CLZF2

a guest
Aug 17th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.71 KB | None | 0 0
  1. /*
  2. * Improved version to C# LibLZF Port:
  3. * Copyright (c) 2010 Roman Atachiants <kelindar@gmail.com>
  4. *
  5. * Original CLZF Port:
  6. * Copyright (c) 2005 Oren J. Maurice <oymaurice@hazorea.org.il>
  7. *
  8. * Original LibLZF Library & Algorithm:
  9. * Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modifica-
  12. * tion, are permitted provided that the following conditions are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * 3. The name of the author may not be used to endorse or promote products
  22. * derived from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
  26. * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  27. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
  28. * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  30. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
  32. * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  33. * OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * Alternatively, the contents of this file may be used under the terms of
  36. * the GNU General Public License version 2 (the "GPL"), in which case the
  37. * provisions of the GPL are applicable instead of the above. If you wish to
  38. * allow the use of your version of this file only under the terms of the
  39. * GPL and not to allow others to use your version of this file under the
  40. * BSD license, indicate your decision by deleting the provisions above and
  41. * replace them with the notice and other provisions required by the GPL. If
  42. * you do not delete the provisions above, a recipient may use your version
  43. * of this file under either the BSD or the GPL.
  44. */
  45. using System;
  46.  
  47. /* Benchmark with Alice29 Canterbury Corpus
  48. ---------------------------------------
  49. (Compression) Original CLZF C#
  50. Raw = 152089, Compressed = 101092
  51. 8292,4743 ms.
  52. ---------------------------------------
  53. (Compression) My LZF C#
  54. Raw = 152089, Compressed = 101092
  55. 33,0019 ms.
  56. ---------------------------------------
  57. (Compression) Zlib using SharpZipLib
  58. Raw = 152089, Compressed = 54388
  59. 8389,4799 ms.
  60. ---------------------------------------
  61. (Compression) QuickLZ C#
  62. Raw = 152089, Compressed = 83494
  63. 80,0046 ms.
  64. ---------------------------------------
  65. (Decompression) Original CLZF C#
  66. Decompressed = 152089
  67. 16,0009 ms.
  68. ---------------------------------------
  69. (Decompression) My LZF C#
  70. Decompressed = 152089
  71. 15,0009 ms.
  72. ---------------------------------------
  73. (Decompression) Zlib using SharpZipLib
  74. Decompressed = 152089
  75. 3577,2046 ms.
  76. ---------------------------------------
  77. (Decompression) QuickLZ C#
  78. Decompressed = 152089
  79. 21,0012 ms.
  80. */
  81.  
  82.  
  83. /// <summary>
  84. /// Improved C# LZF Compressor, a very small data compression library. The compression algorithm is extremely fast.
  85. public static class CLZF2
  86. {
  87. private static readonly uint HLOG = 14;
  88. private static readonly uint HSIZE = (1 << 14);
  89. private static readonly uint MAX_LIT = (1 << 5);
  90. private static readonly uint MAX_OFF = (1 << 13);
  91. private static readonly uint MAX_REF = ((1 << 8) + (1 << 3));
  92.  
  93. /// <summary>
  94. /// Hashtable, that can be allocated only once
  95. /// </summary>
  96. private static readonly long[] HashTable = new long[HSIZE];
  97.  
  98. // Compresses inputBytes
  99. public static byte[] Compress(byte[] inputBytes)
  100. {
  101. // Starting guess, increase it later if needed
  102. int outputByteCountGuess = inputBytes.Length * 2;
  103. byte[] tempBuffer = new byte[outputByteCountGuess];
  104. int byteCount = lzf_compress (inputBytes, ref tempBuffer);
  105.  
  106. // If byteCount is 0, then increase buffer and try again
  107. while (byteCount == 0)
  108. {
  109. outputByteCountGuess *=2;
  110. tempBuffer = new byte[outputByteCountGuess];
  111. byteCount = lzf_compress (inputBytes, ref tempBuffer);
  112. }
  113.  
  114. byte[] outputBytes = new byte[byteCount];
  115. Buffer.BlockCopy(tempBuffer, 0, outputBytes, 0, byteCount);
  116. return outputBytes;
  117. }
  118.  
  119. // Decompress outputBytes
  120. public static byte[] Decompress(byte[] inputBytes)
  121. {
  122. // Starting guess, increase it later if needed
  123. int outputByteCountGuess = inputBytes.Length * 2;
  124. byte[] tempBuffer = new byte[outputByteCountGuess];
  125. int byteCount = lzf_decompress (inputBytes, ref tempBuffer);
  126.  
  127. // If byteCount is 0, then increase buffer and try again
  128. while (byteCount == 0)
  129. {
  130. outputByteCountGuess *=2;
  131. tempBuffer = new byte[outputByteCountGuess];
  132. byteCount = lzf_decompress (inputBytes, ref tempBuffer);
  133. }
  134.  
  135. byte[] outputBytes = new byte[byteCount];
  136. Buffer.BlockCopy(tempBuffer, 0, outputBytes, 0, byteCount);
  137. return outputBytes;
  138. }
  139.  
  140. /// <summary>
  141. /// Compresses the data using LibLZF algorithm
  142. /// </summary>
  143. /// <param name="input">Reference to the data to compress</param>
  144. /// <param name="output">Reference to a buffer which will contain the compressed data</param>
  145. /// <returns>The size of the compressed archive in the output buffer</returns>
  146. public static int lzf_compress(byte[] input, ref byte[] output)
  147. {
  148. int inputLength = input.Length;
  149. int outputLength = output.Length;
  150.  
  151. Array.Clear(HashTable, 0, (int)HSIZE);
  152.  
  153. long hslot;
  154. uint iidx = 0;
  155. uint oidx = 0;
  156. long reference;
  157.  
  158. uint hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]); // FRST(in_data, iidx);
  159. long off;
  160. int lit = 0;
  161.  
  162. for (; ; )
  163. {
  164. if (iidx < inputLength - 2)
  165. {
  166. hval = (hval << 8) | input[iidx + 2];
  167. hslot = ((hval ^ (hval << 5)) >> (int)(((3 * 8 - HLOG)) - hval * 5) & (HSIZE - 1));
  168. reference = HashTable[hslot];
  169. HashTable[hslot] = (long)iidx;
  170.  
  171.  
  172. if ((off = iidx - reference - 1) < MAX_OFF
  173. && iidx + 4 < inputLength
  174. && reference > 0
  175. && input[reference + 0] == input[iidx + 0]
  176. && input[reference + 1] == input[iidx + 1]
  177. && input[reference + 2] == input[iidx + 2]
  178. )
  179. {
  180. /* match found at *reference++ */
  181. uint len = 2;
  182. uint maxlen = (uint)inputLength - iidx - len;
  183. maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
  184.  
  185. if (oidx + lit + 1 + 3 >= outputLength)
  186. return 0;
  187.  
  188. do
  189. len++;
  190. while (len < maxlen && input[reference + len] == input[iidx + len]);
  191.  
  192. if (lit != 0)
  193. {
  194. output[oidx++] = (byte)(lit - 1);
  195. lit = -lit;
  196. do
  197. output[oidx++] = input[iidx + lit];
  198. while ((++lit) != 0);
  199. }
  200.  
  201. len -= 2;
  202. iidx++;
  203.  
  204. if (len < 7)
  205. {
  206. output[oidx++] = (byte)((off >> 8) + (len << 5));
  207. }
  208. else
  209. {
  210. output[oidx++] = (byte)((off >> 8) + (7 << 5));
  211. output[oidx++] = (byte)(len - 7);
  212. }
  213.  
  214. output[oidx++] = (byte)off;
  215.  
  216. iidx += len - 1;
  217. hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]);
  218.  
  219. hval = (hval << 8) | input[iidx + 2];
  220. HashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - HLOG)) - hval * 5) & (HSIZE - 1))] = iidx;
  221. iidx++;
  222.  
  223. hval = (hval << 8) | input[iidx + 2];
  224. HashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - HLOG)) - hval * 5) & (HSIZE - 1))] = iidx;
  225. iidx++;
  226. continue;
  227. }
  228. }
  229. else if (iidx == inputLength)
  230. break;
  231.  
  232. /* one more literal byte we must copy */
  233. lit++;
  234. iidx++;
  235.  
  236. if (lit == MAX_LIT)
  237. {
  238. if (oidx + 1 + MAX_LIT >= outputLength)
  239. return 0;
  240.  
  241. output[oidx++] = (byte)(MAX_LIT - 1);
  242. lit = -lit;
  243. do
  244. output[oidx++] = input[iidx + lit];
  245. while ((++lit) != 0);
  246. }
  247. }
  248.  
  249. if (lit != 0)
  250. {
  251. if (oidx + lit + 1 >= outputLength)
  252. return 0;
  253.  
  254. output[oidx++] = (byte)(lit - 1);
  255. lit = -lit;
  256. do
  257. output[oidx++] = input[iidx + lit];
  258. while ((++lit) != 0);
  259. }
  260.  
  261. return (int)oidx;
  262. }
  263.  
  264.  
  265. /// <summary>
  266. /// Decompresses the data using LibLZF algorithm
  267. /// </summary>
  268. /// <param name="input">Reference to the data to decompress</param>
  269. /// <param name="output">Reference to a buffer which will contain the decompressed data</param>
  270. /// <returns>Returns decompressed size</returns>
  271. public static int lzf_decompress(byte[] input, ref byte[] output)
  272. {
  273. int inputLength = input.Length;
  274. int outputLength = output.Length;
  275.  
  276. uint iidx = 0;
  277. uint oidx = 0;
  278.  
  279. do
  280. {
  281. uint ctrl = input[iidx++];
  282.  
  283. if (ctrl < (1 << 5)) /* literal run */
  284. {
  285. ctrl++;
  286.  
  287. if (oidx + ctrl > outputLength)
  288. {
  289. //SET_ERRNO (E2BIG);
  290. return 0;
  291. }
  292.  
  293. do
  294. output[oidx++] = input[iidx++];
  295. while ((--ctrl) != 0);
  296. }
  297. else /* back reference */
  298. {
  299. uint len = ctrl >> 5;
  300.  
  301. int reference = (int)(oidx - ((ctrl & 0x1f) << 8) - 1);
  302.  
  303.  
  304. if (len == 7)
  305.  
  306. len += input[iidx++];
  307.  
  308.  
  309.  
  310. reference -= input[iidx++];
  311.  
  312.  
  313.  
  314. if (oidx + len + 2 > outputLength)
  315.  
  316. {
  317.  
  318. //SET_ERRNO (E2BIG);
  319.  
  320. return 0;
  321.  
  322. }
  323.  
  324.  
  325.  
  326. if (reference < 0)
  327.  
  328. {
  329.  
  330. //SET_ERRNO (EINVAL);
  331.  
  332. return 0;
  333.  
  334. }
  335.  
  336.  
  337.  
  338. output[oidx++] = output[reference++];
  339.  
  340. output[oidx++] = output[reference++];
  341.  
  342.  
  343.  
  344. do
  345.  
  346. output[oidx++] = output[reference++];
  347.  
  348. while ((--len) != 0);
  349.  
  350. }
  351.  
  352. }
  353.  
  354. while (iidx < inputLength);
  355.  
  356.  
  357.  
  358. return (int)oidx;
  359.  
  360. }
  361.  
  362.  
  363.  
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement