Inflater.cs revision 1.1.1.1 1 1.1 christos //
2 1.1 christos // Copyright Henrik Ravn 2004
3 1.1 christos //
4 1.1 christos // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
5 1.1 christos // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 1.1 christos //
7 1.1 christos
8 1.1 christos using System;
9 1.1 christos using System.Diagnostics;
10 1.1 christos using System.Runtime.InteropServices;
11 1.1 christos
12 1.1 christos namespace DotZLib
13 1.1 christos {
14 1.1 christos
15 1.1 christos /// <summary>
16 1.1 christos /// Implements a data decompressor, using the inflate algorithm in the ZLib dll
17 1.1 christos /// </summary>
18 1.1 christos public class Inflater : CodecBase
19 1.1 christos {
20 1.1 christos #region Dll imports
21 1.1 christos [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
22 1.1 christos private static extern int inflateInit_(ref ZStream sz, string vs, int size);
23 1.1 christos
24 1.1 christos [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
25 1.1 christos private static extern int inflate(ref ZStream sz, int flush);
26 1.1 christos
27 1.1 christos [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
28 1.1 christos private static extern int inflateReset(ref ZStream sz);
29 1.1 christos
30 1.1 christos [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
31 1.1 christos private static extern int inflateEnd(ref ZStream sz);
32 1.1 christos #endregion
33 1.1 christos
34 1.1 christos /// <summary>
35 1.1 christos /// Constructs an new instance of the <c>Inflater</c>
36 1.1 christos /// </summary>
37 1.1 christos public Inflater() : base()
38 1.1 christos {
39 1.1 christos int retval = inflateInit_(ref _ztream, Info.Version, Marshal.SizeOf(_ztream));
40 1.1 christos if (retval != 0)
41 1.1 christos throw new ZLibException(retval, "Could not initialize inflater");
42 1.1 christos
43 1.1 christos resetOutput();
44 1.1 christos }
45 1.1 christos
46 1.1 christos
47 1.1 christos /// <summary>
48 1.1 christos /// Adds more data to the codec to be processed.
49 1.1 christos /// </summary>
50 1.1 christos /// <param name="data">Byte array containing the data to be added to the codec</param>
51 1.1 christos /// <param name="offset">The index of the first byte to add from <c>data</c></param>
52 1.1 christos /// <param name="count">The number of bytes to add</param>
53 1.1 christos /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>
54 1.1 christos public override void Add(byte[] data, int offset, int count)
55 1.1 christos {
56 1.1 christos if (data == null) throw new ArgumentNullException();
57 1.1 christos if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
58 1.1 christos if ((offset+count) > data.Length) throw new ArgumentException();
59 1.1 christos
60 1.1 christos int total = count;
61 1.1 christos int inputIndex = offset;
62 1.1 christos int err = 0;
63 1.1 christos
64 1.1 christos while (err >= 0 && inputIndex < total)
65 1.1 christos {
66 1.1 christos copyInput(data, inputIndex, Math.Min(total - inputIndex, kBufferSize));
67 1.1 christos err = inflate(ref _ztream, (int)FlushTypes.None);
68 1.1 christos if (err == 0)
69 1.1 christos while (_ztream.avail_out == 0)
70 1.1 christos {
71 1.1 christos OnDataAvailable();
72 1.1 christos err = inflate(ref _ztream, (int)FlushTypes.None);
73 1.1 christos }
74 1.1 christos
75 1.1 christos inputIndex += (int)_ztream.total_in;
76 1.1 christos }
77 1.1 christos setChecksum( _ztream.adler );
78 1.1 christos }
79 1.1 christos
80 1.1 christos
81 1.1 christos /// <summary>
82 1.1 christos /// Finishes up any pending data that needs to be processed and handled.
83 1.1 christos /// </summary>
84 1.1 christos public override void Finish()
85 1.1 christos {
86 1.1 christos int err;
87 1.1 christos do
88 1.1 christos {
89 1.1 christos err = inflate(ref _ztream, (int)FlushTypes.Finish);
90 1.1 christos OnDataAvailable();
91 1.1 christos }
92 1.1 christos while (err == 0);
93 1.1 christos setChecksum( _ztream.adler );
94 1.1 christos inflateReset(ref _ztream);
95 1.1 christos resetOutput();
96 1.1 christos }
97 1.1 christos
98 1.1 christos /// <summary>
99 1.1 christos /// Closes the internal zlib inflate stream
100 1.1 christos /// </summary>
101 1.1 christos protected override void CleanUp() { inflateEnd(ref _ztream); }
102 1.1 christos
103 1.1 christos
104 1.1 christos }
105 1.1 christos }
106