92 public static class CLZF2
94 private static readonly uint HLOG = 14;
95 private static readonly uint HSIZE = (1 << 14);
96 private static readonly uint MAX_LIT = (1 << 5);
97 private static readonly uint MAX_OFF = (1 << 13);
98 private static readonly uint MAX_REF = ((1 << 8) + (1 << 3));
103 private static readonly
long[] HashTable =
new long[HSIZE];
106 public static byte[] Compress(
byte[] inputBytes)
109 int outputByteCountGuess = inputBytes.Length * 2;
110 byte[] tempBuffer =
new byte[outputByteCountGuess];
111 int byteCount = lzf_compress(inputBytes, ref tempBuffer);
114 while (byteCount == 0)
116 outputByteCountGuess *= 2;
117 tempBuffer =
new byte[outputByteCountGuess];
118 byteCount = lzf_compress(inputBytes, ref tempBuffer);
121 byte[] outputBytes =
new byte[byteCount];
122 Buffer.BlockCopy(tempBuffer, 0, outputBytes, 0, byteCount);
127 public static byte[] Decompress(
byte[] inputBytes)
130 int outputByteCountGuess = inputBytes.Length * 2;
131 byte[] tempBuffer =
new byte[outputByteCountGuess];
132 int byteCount = lzf_decompress(inputBytes, ref tempBuffer);
135 while (byteCount == 0)
137 outputByteCountGuess *= 2;
138 tempBuffer =
new byte[outputByteCountGuess];
139 byteCount = lzf_decompress(inputBytes, ref tempBuffer);
142 byte[] outputBytes =
new byte[byteCount];
143 Buffer.BlockCopy(tempBuffer, 0, outputBytes, 0, byteCount);
153 public static int lzf_compress(
byte[] input, ref
byte[] output)
155 int inputLength = input.Length;
156 int outputLength = output.Length;
158 Array.Clear(HashTable, 0, (
int)HSIZE);
165 uint hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]);
171 if (iidx < inputLength - 2)
173 hval = (hval << 8) | input[iidx + 2];
174 hslot = ((hval ^ (hval << 5)) >> (int)(((3 * 8 - HLOG)) - hval * 5) & (HSIZE - 1));
175 reference = HashTable[hslot];
176 HashTable[hslot] = (long)iidx;
179 if ((off = iidx - reference - 1) < MAX_OFF
180 && iidx + 4 < inputLength
182 && input[reference + 0] == input[iidx + 0]
183 && input[reference + 1] == input[iidx + 1]
184 && input[reference + 2] == input[iidx + 2]
189 uint maxlen = (uint)inputLength - iidx - len;
190 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
192 if (oidx + lit + 1 + 3 >= outputLength)
197 while (len < maxlen && input[reference + len] == input[iidx + len]);
201 output[oidx++] = (byte)(lit - 1);
204 output[oidx++] = input[iidx + lit];
205 while ((++lit) != 0);
213 output[oidx++] = (byte)((off >> 8) + (len << 5));
217 output[oidx++] = (byte)((off >> 8) + (7 << 5));
218 output[oidx++] = (byte)(len - 7);
221 output[oidx++] = (byte)off;
224 hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]);
226 hval = (hval << 8) | input[iidx + 2];
227 HashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - HLOG)) - hval * 5) & (HSIZE - 1))] = iidx;
230 hval = (hval << 8) | input[iidx + 2];
231 HashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - HLOG)) - hval * 5) & (HSIZE - 1))] = iidx;
236 else if (iidx == inputLength)
245 if (oidx + 1 + MAX_LIT >= outputLength)
248 output[oidx++] = (byte)(MAX_LIT - 1);
251 output[oidx++] = input[iidx + lit];
252 while ((++lit) != 0);
258 if (oidx + lit + 1 >= outputLength)
261 output[oidx++] = (byte)(lit - 1);
264 output[oidx++] = input[iidx + lit];
265 while ((++lit) != 0);
278 public static int lzf_decompress(
byte[] input, ref
byte[] output)
280 int inputLength = input.Length;
281 int outputLength = output.Length;
288 uint ctrl = input[iidx++];
294 if (oidx + ctrl > outputLength)
301 output[oidx++] = input[iidx++];
302 while ((--ctrl) != 0);
306 uint len = ctrl >> 5;
308 int reference = (int)(oidx - ((ctrl & 0x1f) << 8) - 1);
311 len += input[iidx++];
313 reference -= input[iidx++];
315 if (oidx + len + 2 > outputLength)
327 output[oidx++] = output[reference++];
328 output[oidx++] = output[reference++];
331 output[oidx++] = output[reference++];
332 while ((--len) != 0);
335 while (iidx < inputLength);
Credit Erdener Gonenc - @PixelEnvision.