summaryrefslogtreecommitdiffstats
path: root/compat/zlib/contrib/dotzlib/DotZLib/CodecBase.cs
blob: 954db7db05f0ef1a11012dc22573b2027b9548c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//

// © Copyright Henrik Ravn 2004

//

// Use, modification and distribution are subject to the Boost Software License, Version 1.0. 

// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

//


using System;
using System.Runtime.InteropServices;

namespace DotZLib
{
	/// <summary>

	/// Implements the common functionality needed for all <see cref="Codec"/>s

	/// </summary>

	public abstract class CodecBase : Codec, IDisposable
	{

        #region Data members


        /// <summary>

        /// Instance of the internal zlib buffer structure that is 

        /// passed to all functions in the zlib dll

        /// </summary>

        internal ZStream _ztream = new ZStream();

        /// <summary>

        /// True if the object instance has been disposed, false otherwise

        /// </summary>

        protected bool _isDisposed = false;

        /// <summary>

        /// The size of the internal buffers

        /// </summary>

        protected const int kBufferSize = 16384;

        private byte[] _outBuffer = new byte[kBufferSize];
        private byte[] _inBuffer = new byte[kBufferSize];

        private GCHandle _hInput;
        private GCHandle _hOutput;

        private uint _checksum = 0;

        #endregion


        /// <summary>

        /// Initializes a new instance of the <c>CodeBase</c> class. 

        /// </summary>

		public CodecBase()
		{
            try
            {
                _hInput = GCHandle.Alloc(_inBuffer, GCHandleType.Pinned);
                _hOutput = GCHandle.Alloc(_outBuffer, GCHandleType.Pinned);
            }
            catch (Exception)
            {
                CleanUp(false);
                throw;
            }
        }


        #region Codec Members


        /// <summary>

        /// Occurs when more processed data are available.

        /// </summary>

        public event DataAvailableHandler DataAvailable;

        /// <summary>

        /// Fires the <see cref="DataAvailable"/> event

        /// </summary>

        protected void OnDataAvailable()
        {
            if (_ztream.total_out > 0)
            {
                if (DataAvailable != null)
                    DataAvailable( _outBuffer, 0, (int)_ztream.total_out); 
                resetOutput();
            }
        }

        /// <summary>

        /// Adds more data to the codec to be processed.

        /// </summary>

        /// <param name="data">Byte array containing the data to be added to the codec</param>

        /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>

        public void Add(byte[] data)
        {
            Add(data,0,data.Length);
        }

        /// <summary>

        /// Adds more data to the codec to be processed.

        /// </summary>

        /// <param name="data">Byte array containing the data to be added to the codec</param>

        /// <param name="offset">The index of the first byte to add from <c>data</c></param>

        /// <param name="count">The number of bytes to add</param>

        /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>

        /// <remarks>This must be implemented by a derived class</remarks>

        public abstract void Add(byte[] data, int offset, int count);

        /// <summary>

        /// Finishes up any pending data that needs to be processed and handled.

        /// </summary>

        /// <remarks>This must be implemented by a derived class</remarks>

        public abstract void Finish();

        /// <summary>

        /// Gets the checksum of the data that has been added so far

        /// </summary>

        public uint Checksum { get { return _checksum; } }

        #endregion


        #region Destructor & IDisposable stuff


        /// <summary>

        /// Destroys this instance

        /// </summary>

        ~CodecBase()
        {
            CleanUp(false);
        }

        /// <summary>

        /// Releases any unmanaged resources and calls the <see cref="CleanUp()"/> method of the derived class

        /// </summary>

        public void Dispose()
        {
            CleanUp(true);
        }

        /// <summary>

        /// Performs any codec specific cleanup

        /// </summary>

        /// <remarks>This must be implemented by a derived class</remarks>

        protected abstract void CleanUp();

        // performs the release of the handles and calls the dereived CleanUp()

        private void CleanUp(bool isDisposing)
        {
            if (!_isDisposed)
            {
                CleanUp();
                if (_hInput.IsAllocated)
                    _hInput.Free();
                if (_hOutput.IsAllocated)
                    _hOutput.Free();

                _isDisposed = true;
            }
        }


        #endregion


        #region Helper methods


        /// <summary>

        /// Copies a number of bytes to the internal codec buffer - ready for proccesing

        /// </summary>

        /// <param name="data">The byte array that contains the data to copy</param>

        /// <param name="startIndex">The index of the first byte to copy</param>

        /// <param name="count">The number of bytes to copy from <c>data</c></param>

        protected void copyInput(byte[] data, int startIndex, int count)
        {
            Array.Copy(data, startIndex, _inBuffer,0, count);
            _ztream.next_in = _hInput.AddrOfPinnedObject();
            _ztream.total_in = 0;
            _ztream.avail_in = (uint)count;

        }

        /// <summary>

        /// Resets the internal output buffers to a known state - ready for processing

        /// </summary>

        protected void resetOutput()
        {
            _ztream.total_out = 0;
            _ztream.avail_out = kBufferSize;
            _ztream.next_out = _hOutput.AddrOfPinnedObject();
        }

        /// <summary>

        /// Updates the running checksum property

        /// </summary>

        /// <param name="newSum">The new checksum value</param>

        protected void setChecksum(uint newSum)
        {
            _checksum = newSum;
        }
        #endregion


    }
}