annotate src/zlib-1.2.8/contrib/dotzlib/DotZLib/Inflater.cs @ 168:ceec0dd9ec9c

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