Sync to trunk HEAD (r43416)
[reactos.git] / irc / TechBot / Compression / Deflater.cs
1 // Deflater.cs
2 // Copyright (C) 2001 Mike Krueger
3 //
4 // This file was translated from java, it was part of the GNU Classpath
5 // Copyright (C) 2001 Free Software Foundation, Inc.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 //
21 // Linking this library statically or dynamically with other modules is
22 // making a combined work based on this library. Thus, the terms and
23 // conditions of the GNU General Public License cover the whole
24 // combination.
25 //
26 // As a special exception, the copyright holders of this library give you
27 // permission to link this library with independent modules to produce an
28 // executable, regardless of the license terms of these independent
29 // modules, and to copy and distribute the resulting executable under
30 // terms of your choice, provided that you also meet, for each linked
31 // independent module, the terms and conditions of the license of that
32 // module. An independent module is a module which is not derived from
33 // or based on this library. If you modify this library, you may extend
34 // this exception to your version of the library, but you are not
35 // obligated to do so. If you do not wish to do so, delete this
36 // exception statement from your version.
37
38 using System;
39
40 namespace ICSharpCode.SharpZipLib.Zip.Compression
41 {
42
43 /// <summary>
44 /// This is the Deflater class. The deflater class compresses input
45 /// with the deflate algorithm described in RFC 1951. It has several
46 /// compression levels and three different strategies described below.
47 ///
48 /// This class is <i>not</i> thread safe. This is inherent in the API, due
49 /// to the split of deflate and setInput.
50 ///
51 /// author of the original java version : Jochen Hoenicke
52 /// </summary>
53 public class Deflater
54 {
55 /// <summary>
56 /// The best and slowest compression level. This tries to find very
57 /// long and distant string repetitions.
58 /// </summary>
59 public static int BEST_COMPRESSION = 9;
60
61 /// <summary>
62 /// The worst but fastest compression level.
63 /// </summary>
64 public static int BEST_SPEED = 1;
65
66 /// <summary>
67 /// The default compression level.
68 /// </summary>
69 public static int DEFAULT_COMPRESSION = -1;
70
71 /// <summary>
72 /// This level won't compress at all but output uncompressed blocks.
73 /// </summary>
74 public static int NO_COMPRESSION = 0;
75
76 /// <summary>
77 /// The compression method. This is the only method supported so far.
78 /// There is no need to use this constant at all.
79 /// </summary>
80 public static int DEFLATED = 8;
81
82 /*
83 * The Deflater can do the following state transitions:
84 *
85 * (1) -> INIT_STATE ----> INIT_FINISHING_STATE ---.
86 * / | (2) (5) |
87 * / v (5) |
88 * (3)| SETDICT_STATE ---> SETDICT_FINISHING_STATE |(3)
89 * \ | (3) | ,-------'
90 * | | | (3) /
91 * v v (5) v v
92 * (1) -> BUSY_STATE ----> FINISHING_STATE
93 * | (6)
94 * v
95 * FINISHED_STATE
96 * \_____________________________________/
97 * | (7)
98 * v
99 * CLOSED_STATE
100 *
101 * (1) If we should produce a header we start in INIT_STATE, otherwise
102 * we start in BUSY_STATE.
103 * (2) A dictionary may be set only when we are in INIT_STATE, then
104 * we change the state as indicated.
105 * (3) Whether a dictionary is set or not, on the first call of deflate
106 * we change to BUSY_STATE.
107 * (4) -- intentionally left blank -- :)
108 * (5) FINISHING_STATE is entered, when flush() is called to indicate that
109 * there is no more INPUT. There are also states indicating, that
110 * the header wasn't written yet.
111 * (6) FINISHED_STATE is entered, when everything has been flushed to the
112 * internal pending output buffer.
113 * (7) At any time (7)
114 *
115 */
116
117 private static int IS_SETDICT = 0x01;
118 private static int IS_FLUSHING = 0x04;
119 private static int IS_FINISHING = 0x08;
120
121 private static int INIT_STATE = 0x00;
122 private static int SETDICT_STATE = 0x01;
123 // private static int INIT_FINISHING_STATE = 0x08;
124 // private static int SETDICT_FINISHING_STATE = 0x09;
125 private static int BUSY_STATE = 0x10;
126 private static int FLUSHING_STATE = 0x14;
127 private static int FINISHING_STATE = 0x1c;
128 private static int FINISHED_STATE = 0x1e;
129 private static int CLOSED_STATE = 0x7f;
130
131 /// <summary>
132 /// Compression level.
133 /// </summary>
134 private int level;
135
136 /// <summary>
137 /// should we include a header.
138 /// </summary>
139 private bool noHeader;
140
141 // /// <summary>
142 // /// Compression strategy.
143 // /// </summary>
144 // private int strategy;
145
146 /// <summary>
147 /// The current state.
148 /// </summary>
149 private int state;
150
151 /// <summary>
152 /// The total bytes of output written.
153 /// </summary>
154 private int totalOut;
155
156 /// <summary>
157 /// The pending output.
158 /// </summary>
159 private DeflaterPending pending;
160
161 /// <summary>
162 /// The deflater engine.
163 /// </summary>
164 private DeflaterEngine engine;
165
166 /// <summary>
167 /// Creates a new deflater with default compression level.
168 /// </summary>
169 public Deflater() : this(DEFAULT_COMPRESSION, false)
170 {
171
172 }
173
174 /// <summary>
175 /// Creates a new deflater with given compression level.
176 /// </summary>
177 /// <param name="lvl">
178 /// the compression level, a value between NO_COMPRESSION
179 /// and BEST_COMPRESSION, or DEFAULT_COMPRESSION.
180 /// </param>
181 /// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>
182 public Deflater(int lvl) : this(lvl, false)
183 {
184
185 }
186
187 /// <summary>
188 /// Creates a new deflater with given compression level.
189 /// </summary>
190 /// <param name="lvl">
191 /// the compression level, a value between NO_COMPRESSION
192 /// and BEST_COMPRESSION.
193 /// </param>
194 /// <param name="nowrap">
195 /// true, if we should suppress the deflate header at the
196 /// beginning and the adler checksum at the end of the output. This is
197 /// useful for the GZIP format.
198 /// </param>
199 /// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>
200 public Deflater(int lvl, bool nowrap)
201 {
202 if (lvl == DEFAULT_COMPRESSION) {
203 lvl = 6;
204 } else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION) {
205 throw new ArgumentOutOfRangeException("lvl");
206 }
207
208 pending = new DeflaterPending();
209 engine = new DeflaterEngine(pending);
210 this.noHeader = nowrap;
211 SetStrategy(DeflateStrategy.Default);
212 SetLevel(lvl);
213 Reset();
214 }
215
216
217 /// <summary>
218 /// Resets the deflater. The deflater acts afterwards as if it was
219 /// just created with the same compression level and strategy as it
220 /// had before.
221 /// </summary>
222 public void Reset()
223 {
224 state = (noHeader ? BUSY_STATE : INIT_STATE);
225 totalOut = 0;
226 pending.Reset();
227 engine.Reset();
228 }
229
230 /// <summary>
231 /// Gets the current adler checksum of the data that was processed so far.
232 /// </summary>
233 public int Adler {
234 get {
235 return engine.Adler;
236 }
237 }
238
239 /// <summary>
240 /// Gets the number of input bytes processed so far.
241 /// </summary>
242 public int TotalIn {
243 get {
244 return engine.TotalIn;
245 }
246 }
247
248 /// <summary>
249 /// Gets the number of output bytes so far.
250 /// </summary>
251 public int TotalOut {
252 get {
253 return totalOut;
254 }
255 }
256
257 /// <summary>
258 /// Flushes the current input block. Further calls to deflate() will
259 /// produce enough output to inflate everything in the current input
260 /// block. This is not part of Sun's JDK so I have made it package
261 /// private. It is used by DeflaterOutputStream to implement
262 /// flush().
263 /// </summary>
264 public void Flush()
265 {
266 state |= IS_FLUSHING;
267 }
268
269 /// <summary>
270 /// Finishes the deflater with the current input block. It is an error
271 /// to give more input after this method was called. This method must
272 /// be called to force all bytes to be flushed.
273 /// </summary>
274 public void Finish()
275 {
276 state |= IS_FLUSHING | IS_FINISHING;
277 }
278
279 /// <summary>
280 /// Returns true if the stream was finished and no more output bytes
281 /// are available.
282 /// </summary>
283 public bool IsFinished {
284 get {
285 return state == FINISHED_STATE && pending.IsFlushed;
286 }
287 }
288
289 /// <summary>
290 /// Returns true, if the input buffer is empty.
291 /// You should then call setInput().
292 /// NOTE: This method can also return true when the stream
293 /// was finished.
294 /// </summary>
295 public bool IsNeedingInput {
296 get {
297 return engine.NeedsInput();
298 }
299 }
300
301 /// <summary>
302 /// Sets the data which should be compressed next. This should be only
303 /// called when needsInput indicates that more input is needed.
304 /// If you call setInput when needsInput() returns false, the
305 /// previous input that is still pending will be thrown away.
306 /// The given byte array should not be changed, before needsInput() returns
307 /// true again.
308 /// This call is equivalent to <code>setInput(input, 0, input.length)</code>.
309 /// </summary>
310 /// <param name="input">
311 /// the buffer containing the input data.
312 /// </param>
313 /// <exception cref="System.InvalidOperationException">
314 /// if the buffer was finished() or ended().
315 /// </exception>
316 public void SetInput(byte[] input)
317 {
318 SetInput(input, 0, input.Length);
319 }
320
321 /// <summary>
322 /// Sets the data which should be compressed next. This should be
323 /// only called when needsInput indicates that more input is needed.
324 /// The given byte array should not be changed, before needsInput() returns
325 /// true again.
326 /// </summary>
327 /// <param name="input">
328 /// the buffer containing the input data.
329 /// </param>
330 /// <param name="off">
331 /// the start of the data.
332 /// </param>
333 /// <param name="len">
334 /// the length of the data.
335 /// </param>
336 /// <exception cref="System.InvalidOperationException">
337 /// if the buffer was finished() or ended() or if previous input is still pending.
338 /// </exception>
339 public void SetInput(byte[] input, int off, int len)
340 {
341 if ((state & IS_FINISHING) != 0) {
342 throw new InvalidOperationException("finish()/end() already called");
343 }
344 engine.SetInput(input, off, len);
345 }
346
347 /// <summary>
348 /// Sets the compression level. There is no guarantee of the exact
349 /// position of the change, but if you call this when needsInput is
350 /// true the change of compression level will occur somewhere near
351 /// before the end of the so far given input.
352 /// </summary>
353 /// <param name="lvl">
354 /// the new compression level.
355 /// </param>
356 public void SetLevel(int lvl)
357 {
358 if (lvl == DEFAULT_COMPRESSION) {
359 lvl = 6;
360 } else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION) {
361 throw new ArgumentOutOfRangeException("lvl");
362 }
363
364 if (level != lvl) {
365 level = lvl;
366 engine.SetLevel(lvl);
367 }
368 }
369
370 /// <summary>
371 /// Sets the compression strategy. Strategy is one of
372 /// DEFAULT_STRATEGY, HUFFMAN_ONLY and FILTERED. For the exact
373 /// position where the strategy is changed, the same as for
374 /// setLevel() applies.
375 /// </summary>
376 /// <param name="stgy">
377 /// the new compression strategy.
378 /// </param>
379 public void SetStrategy(DeflateStrategy stgy)
380 {
381 engine.Strategy = stgy;
382 }
383
384 /// <summary>
385 /// Deflates the current input block to the given array. It returns
386 /// the number of bytes compressed, or 0 if either
387 /// needsInput() or finished() returns true or length is zero.
388 /// </summary>
389 /// <param name="output">
390 /// the buffer where to write the compressed data.
391 /// </param>
392 public int Deflate(byte[] output)
393 {
394 return Deflate(output, 0, output.Length);
395 }
396
397 /// <summary>
398 /// Deflates the current input block to the given array. It returns
399 /// the number of bytes compressed, or 0 if either
400 /// needsInput() or finished() returns true or length is zero.
401 /// </summary>
402 /// <param name="output">
403 /// the buffer where to write the compressed data.
404 /// </param>
405 /// <param name="offset">
406 /// the offset into the output array.
407 /// </param>
408 /// <param name="length">
409 /// the maximum number of bytes that may be written.
410 /// </param>
411 /// <exception cref="System.InvalidOperationException">
412 /// if end() was called.
413 /// </exception>
414 /// <exception cref="System.ArgumentOutOfRangeException">
415 /// if offset and/or length don't match the array length.
416 /// </exception>
417 public int Deflate(byte[] output, int offset, int length)
418 {
419 int origLength = length;
420
421 if (state == CLOSED_STATE) {
422 throw new InvalidOperationException("Deflater closed");
423 }
424
425 if (state < BUSY_STATE) {
426 /* output header */
427 int header = (DEFLATED +
428 ((DeflaterConstants.MAX_WBITS - 8) << 4)) << 8;
429 int level_flags = (level - 1) >> 1;
430 if (level_flags < 0 || level_flags > 3) {
431 level_flags = 3;
432 }
433 header |= level_flags << 6;
434 if ((state & IS_SETDICT) != 0) {
435 /* Dictionary was set */
436 header |= DeflaterConstants.PRESET_DICT;
437 }
438 header += 31 - (header % 31);
439
440
441 pending.WriteShortMSB(header);
442 if ((state & IS_SETDICT) != 0) {
443 int chksum = engine.Adler;
444 engine.ResetAdler();
445 pending.WriteShortMSB(chksum >> 16);
446 pending.WriteShortMSB(chksum & 0xffff);
447 }
448
449 state = BUSY_STATE | (state & (IS_FLUSHING | IS_FINISHING));
450 }
451
452 for (;;) {
453 int count = pending.Flush(output, offset, length);
454 offset += count;
455 totalOut += count;
456 length -= count;
457
458 if (length == 0 || state == FINISHED_STATE) {
459 break;
460 }
461
462 if (!engine.Deflate((state & IS_FLUSHING) != 0, (state & IS_FINISHING) != 0)) {
463 if (state == BUSY_STATE) {
464 /* We need more input now */
465 return origLength - length;
466 } else if (state == FLUSHING_STATE) {
467 if (level != NO_COMPRESSION) {
468 /* We have to supply some lookahead. 8 bit lookahead
469 * are needed by the zlib inflater, and we must fill
470 * the next byte, so that all bits are flushed.
471 */
472 int neededbits = 8 + ((-pending.BitCount) & 7);
473 while (neededbits > 0) {
474 /* write a static tree block consisting solely of
475 * an EOF:
476 */
477 pending.WriteBits(2, 10);
478 neededbits -= 10;
479 }
480 }
481 state = BUSY_STATE;
482 } else if (state == FINISHING_STATE) {
483 pending.AlignToByte();
484 /* We have completed the stream */
485 if (!noHeader) {
486 int adler = engine.Adler;
487 pending.WriteShortMSB(adler >> 16);
488 pending.WriteShortMSB(adler & 0xffff);
489 }
490 state = FINISHED_STATE;
491 }
492 }
493 }
494 return origLength - length;
495 }
496
497 /// <summary>
498 /// Sets the dictionary which should be used in the deflate process.
499 /// This call is equivalent to <code>setDictionary(dict, 0, dict.Length)</code>.
500 /// </summary>
501 /// <param name="dict">
502 /// the dictionary.
503 /// </param>
504 /// <exception cref="System.InvalidOperationException">
505 /// if setInput () or deflate () were already called or another dictionary was already set.
506 /// </exception>
507 public void SetDictionary(byte[] dict)
508 {
509 SetDictionary(dict, 0, dict.Length);
510 }
511
512 /// <summary>
513 /// Sets the dictionary which should be used in the deflate process.
514 /// The dictionary should be a byte array containing strings that are
515 /// likely to occur in the data which should be compressed. The
516 /// dictionary is not stored in the compressed output, only a
517 /// checksum. To decompress the output you need to supply the same
518 /// dictionary again.
519 /// </summary>
520 /// <param name="dict">
521 /// the dictionary.
522 /// </param>
523 /// <param name="offset">
524 /// an offset into the dictionary.
525 /// </param>
526 /// <param name="length">
527 /// the length of the dictionary.
528 /// </param>
529 /// <exception cref="System.InvalidOperationException">
530 /// if setInput () or deflate () were already called or another dictionary was already set.
531 /// </exception>
532 public void SetDictionary(byte[] dict, int offset, int length)
533 {
534 if (state != INIT_STATE) {
535 throw new InvalidOperationException();
536 }
537
538 state = SETDICT_STATE;
539 engine.SetDictionary(dict, offset, length);
540 }
541 }
542 }