annotate src/zlib-1.2.7/contrib/dotzlib/DotZLib/Deflater.cs @ 169:223a55898ab9 tip default

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