Rizin
unix-like reverse engineering framework and cli tools
CodecBase.cs
Go to the documentation of this file.
1 //
2 // © Copyright Henrik Ravn 2004
3 //
4 // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 using System;
9 using System.Runtime.InteropServices;
10 
11 namespace DotZLib
12 {
16  public abstract class CodecBase : Codec, IDisposable
17  {
18 
19  #region Data members
20 
25  internal ZStream _ztream = new ZStream();
26 
30  protected bool _isDisposed = false;
31 
35  protected const int kBufferSize = 16384;
36 
37  private byte[] _outBuffer = new byte[kBufferSize];
38  private byte[] _inBuffer = new byte[kBufferSize];
39 
40  private GCHandle _hInput;
41  private GCHandle _hOutput;
42 
43  private uint _checksum = 0;
44 
45  #endregion
46 
50  public CodecBase()
51  {
52  try
53  {
54  _hInput = GCHandle.Alloc(_inBuffer, GCHandleType.Pinned);
55  _hOutput = GCHandle.Alloc(_outBuffer, GCHandleType.Pinned);
56  }
57  catch (Exception)
58  {
59  CleanUp(false);
60  throw;
61  }
62  }
63 
64 
65  #region Codec Members
66 
71 
75  protected void OnDataAvailable()
76  {
77  if (_ztream.total_out > 0)
78  {
79  if (DataAvailable != null)
81  resetOutput();
82  }
83  }
84 
90  public void Add(byte[] data)
91  {
92  Add(data,0,data.Length);
93  }
94 
103  public abstract void Add(byte[] data, int offset, int count);
104 
109  public abstract void Finish();
110 
114  public uint Checksum { get { return _checksum; } }
115 
116  #endregion
117 
118  #region Destructor & IDisposable stuff
119 
124  {
125  CleanUp(false);
126  }
127 
131  public void Dispose()
132  {
133  CleanUp(true);
134  }
135 
140  protected abstract void CleanUp();
141 
142  // performs the release of the handles and calls the dereived CleanUp()
143  private void CleanUp(bool isDisposing)
144  {
145  if (!_isDisposed)
146  {
147  CleanUp();
148  if (_hInput.IsAllocated)
149  _hInput.Free();
150  if (_hOutput.IsAllocated)
151  _hOutput.Free();
152 
153  _isDisposed = true;
154  }
155  }
156 
157 
158  #endregion
159 
160  #region Helper methods
161 
168  protected void copyInput(byte[] data, int startIndex, int count)
169  {
170  Array.Copy(data, startIndex, _inBuffer,0, count);
171  _ztream.next_in = _hInput.AddrOfPinnedObject();
172  _ztream.total_in = 0;
174 
175  }
176 
180  protected void resetOutput()
181  {
182  _ztream.total_out = 0;
184  _ztream.next_out = _hOutput.AddrOfPinnedObject();
185  }
186 
191  protected void setChecksum(uint newSum)
192  {
193  _checksum = newSum;
194  }
195  #endregion
196 
197  }
198 }
#define Array(T)
Definition: array.h:15
Implements the common functionality needed for all Codecs
Definition: CodecBase.cs:17
GCHandle _hOutput
Definition: CodecBase.cs:41
CodecBase()
Initializes a new instance of the CodeBase class.
Definition: CodecBase.cs:50
ZStream _ztream
Instance of the internal zlib buffer structure that is passed to all functions in the zlib dll
Definition: CodecBase.cs:25
void OnDataAvailable()
Fires the DataAvailable event
Definition: CodecBase.cs:75
void setChecksum(uint newSum)
Updates the running checksum property
Definition: CodecBase.cs:191
const int kBufferSize
The size of the internal buffers
Definition: CodecBase.cs:35
void resetOutput()
Resets the internal output buffers to a known state - ready for processing
Definition: CodecBase.cs:180
void copyInput(byte[] data, int startIndex, int count)
Copies a number of bytes to the internal codec buffer - ready for proccesing
Definition: CodecBase.cs:168
void Add(byte[] data)
Adds more data to the codec to be processed.
Definition: CodecBase.cs:90
byte[] _inBuffer
Definition: CodecBase.cs:38
DataAvailableHandler DataAvailable
Occurs when more processed data are available.
Definition: CodecBase.cs:70
bool _isDisposed
True if the object instance has been disposed, false otherwise
Definition: CodecBase.cs:30
abstract void CleanUp()
Performs any codec specific cleanup
uint Checksum
Gets the checksum of the data that has been added so far
Definition: CodecBase.cs:114
abstract void Add(byte[] data, int offset, int count)
Adds more data to the codec to be processed.
void Dispose()
Releases any unmanaged resources and calls the CleanUp() method of the derived class
Definition: CodecBase.cs:131
abstract void Finish()
Finishes up any pending data that needs to be processed and handled.
void CleanUp(bool isDisposing)
Definition: CodecBase.cs:143
byte[] _outBuffer
Definition: CodecBase.cs:37
~CodecBase()
Destroys this instance
Definition: CodecBase.cs:123
GCHandle _hInput
Definition: CodecBase.cs:40
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
unsigned int uint
Definition: gzlog.c:242
Declares methods and events for implementing compressors/decompressors
Definition: DotZLib.cs:175
voidpf uLong offset
Definition: ioapi.h:144
delegate void DataAvailableHandler(byte[] data, int startIndex, int count)
Represents the method that will be called from a codec when new data are available.
uint avail_in
Definition: DotZLib.cs:33
IntPtr next_in
Definition: DotZLib.cs:32
uint total_out
Definition: DotZLib.cs:38
uint avail_out
Definition: DotZLib.cs:37
IntPtr next_out
Definition: DotZLib.cs:36
uint total_in
Definition: DotZLib.cs:34