annotate src/zlib-1.2.7/contrib/dotzlib/DotZLib/Inflater.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 decompressor, using the inflate algorithm in the ZLib dll
cannam@89 17 /// </summary>
cannam@89 18 public class Inflater : 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 inflateInit_(ref ZStream sz, string vs, int size);
cannam@89 23
cannam@89 24 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
cannam@89 25 private static extern int inflate(ref ZStream sz, int flush);
cannam@89 26
cannam@89 27 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
cannam@89 28 private static extern int inflateReset(ref ZStream sz);
cannam@89 29
cannam@89 30 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
cannam@89 31 private static extern int inflateEnd(ref ZStream sz);
cannam@89 32 #endregion
cannam@89 33
cannam@89 34 /// <summary>
cannam@89 35 /// Constructs an new instance of the <c>Inflater</c>
cannam@89 36 /// </summary>
cannam@89 37 public Inflater() : base()
cannam@89 38 {
cannam@89 39 int retval = inflateInit_(ref _ztream, Info.Version, Marshal.SizeOf(_ztream));
cannam@89 40 if (retval != 0)
cannam@89 41 throw new ZLibException(retval, "Could not initialize inflater");
cannam@89 42
cannam@89 43 resetOutput();
cannam@89 44 }
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 err = inflate(ref _ztream, (int)FlushTypes.None);
cannam@89 68 if (err == 0)
cannam@89 69 while (_ztream.avail_out == 0)
cannam@89 70 {
cannam@89 71 OnDataAvailable();
cannam@89 72 err = inflate(ref _ztream, (int)FlushTypes.None);
cannam@89 73 }
cannam@89 74
cannam@89 75 inputIndex += (int)_ztream.total_in;
cannam@89 76 }
cannam@89 77 setChecksum( _ztream.adler );
cannam@89 78 }
cannam@89 79
cannam@89 80
cannam@89 81 /// <summary>
cannam@89 82 /// Finishes up any pending data that needs to be processed and handled.
cannam@89 83 /// </summary>
cannam@89 84 public override void Finish()
cannam@89 85 {
cannam@89 86 int err;
cannam@89 87 do
cannam@89 88 {
cannam@89 89 err = inflate(ref _ztream, (int)FlushTypes.Finish);
cannam@89 90 OnDataAvailable();
cannam@89 91 }
cannam@89 92 while (err == 0);
cannam@89 93 setChecksum( _ztream.adler );
cannam@89 94 inflateReset(ref _ztream);
cannam@89 95 resetOutput();
cannam@89 96 }
cannam@89 97
cannam@89 98 /// <summary>
cannam@89 99 /// Closes the internal zlib inflate stream
cannam@89 100 /// </summary>
cannam@89 101 protected override void CleanUp() { inflateEnd(ref _ztream); }
cannam@89 102
cannam@89 103
cannam@89 104 }
cannam@89 105 }