Rizin
unix-like reverse engineering framework and cli tools
Deflater.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.Diagnostics;
10 using System.Runtime.InteropServices;
11 
12 namespace DotZLib
13 {
14 
18  public sealed class Deflater : CodecBase
19  {
20  #region Dll imports
21  [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
22  private static extern int deflateInit_(ref ZStream sz, int level, string vs, int size);
23 
24  [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
25  private static extern int deflate(ref ZStream sz, int flush);
26 
27  [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
28  private static extern int deflateReset(ref ZStream sz);
29 
30  [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
31  private static extern int deflateEnd(ref ZStream sz);
32  #endregion
33 
38  public Deflater(CompressLevel level) : base()
39  {
40  int retval = deflateInit_(ref _ztream, (int)level, Info.Version, Marshal.SizeOf(_ztream));
41  if (retval != 0)
42  throw new ZLibException(retval, "Could not initialize deflater");
43 
44  resetOutput();
45  }
46 
54  public override void Add(byte[] data, int offset, int count)
55  {
56  if (data == null) throw new ArgumentNullException();
57  if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
58  if ((offset+count) > data.Length) throw new ArgumentException();
59 
60  int total = count;
61  int inputIndex = offset;
62  int err = 0;
63 
64  while (err >= 0 && inputIndex < total)
65  {
66  copyInput(data, inputIndex, Math.Min(total - inputIndex, kBufferSize));
67  while (err >= 0 && _ztream.avail_in > 0)
68  {
69  err = deflate(ref _ztream, (int)FlushTypes.None);
70  if (err == 0)
71  while (_ztream.avail_out == 0)
72  {
74  err = deflate(ref _ztream, (int)FlushTypes.None);
75  }
76  inputIndex += (int)_ztream.total_in;
77  }
78  }
80  }
81 
82 
86  public override void Finish()
87  {
88  int err;
89  do
90  {
91  err = deflate(ref _ztream, (int)FlushTypes.Finish);
93  }
94  while (err == 0);
96  deflateReset(ref _ztream);
97  resetOutput();
98  }
99 
103  protected override void CleanUp() { deflateEnd(ref _ztream); }
104 
105  }
106 }
static bool err
Definition: armass.c:435
Implements the common functionality needed for all Codecs
Definition: CodecBase.cs:17
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
Implements a data compressor, using the deflate algorithm in the ZLib dll
Definition: Deflater.cs:19
static int deflateEnd(ref ZStream sz)
override void Add(byte[] data, int offset, int count)
Adds more data to the codec to be processed.
Definition: Deflater.cs:54
Deflater(CompressLevel level)
Constructs an new instance of the Deflater
Definition: Deflater.cs:38
static int deflate(ref ZStream sz, int flush)
override void Finish()
Finishes up any pending data that needs to be processed and handled.
Definition: Deflater.cs:86
static int deflateInit_(ref ZStream sz, int level, string vs, int size)
static int deflateReset(ref ZStream sz)
override void CleanUp()
Closes the internal zlib deflate stream
Definition: Deflater.cs:103
Encapsulates general information about the ZLib library
Definition: DotZLib.cs:217
static string Version
Gets the version of ZLib as a string, e.g. "1.2.1"
Definition: DotZLib.cs:283
The exception that is thrown when an error occurs on the zlib dll
Definition: DotZLib.cs:87
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
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
CompressLevel
Defines constants for the available compression levels in zlib
Definition: DotZLib.cs:62
FlushTypes
Defines constants for the various flush types used with zlib
Definition: DotZLib.cs:23
static int
Definition: sfsocketcall.h:114
uint avail_in
Definition: DotZLib.cs:33
uint avail_out
Definition: DotZLib.cs:37
uint total_in
Definition: DotZLib.cs:34
static int level
Definition: vmenus.c:2424