Inflater.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. using System;
  8. using System.Diagnostics;
  9. using System.Runtime.InteropServices;
  10. namespace DotZLib
  11. {
  12. /// <summary>
  13. /// Implements a data decompressor, using the inflate algorithm in the ZLib dll
  14. /// </summary>
  15. public class Inflater : CodecBase
  16. {
  17. #region Dll imports
  18. [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
  19. private static extern int inflateInit_(ref ZStream sz, string vs, int size);
  20. [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
  21. private static extern int inflate(ref ZStream sz, int flush);
  22. [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
  23. private static extern int inflateReset(ref ZStream sz);
  24. [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
  25. private static extern int inflateEnd(ref ZStream sz);
  26. #endregion
  27. /// <summary>
  28. /// Constructs an new instance of the <c>Inflater</c>
  29. /// </summary>
  30. public Inflater() : base()
  31. {
  32. int retval = inflateInit_(ref _ztream, Info.Version, Marshal.SizeOf(_ztream));
  33. if (retval != 0)
  34. throw new ZLibException(retval, "Could not initialize inflater");
  35. resetOutput();
  36. }
  37. /// <summary>
  38. /// Adds more data to the codec to be processed.
  39. /// </summary>
  40. /// <param name="data">Byte array containing the data to be added to the codec</param>
  41. /// <param name="offset">The index of the first byte to add from <c>data</c></param>
  42. /// <param name="count">The number of bytes to add</param>
  43. /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>
  44. public override void Add(byte[] data, int offset, int count)
  45. {
  46. if (data == null) throw new ArgumentNullException();
  47. if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
  48. if ((offset+count) > data.Length) throw new ArgumentException();
  49. int total = count;
  50. int inputIndex = offset;
  51. int err = 0;
  52. while (err >= 0 && inputIndex < total)
  53. {
  54. copyInput(data, inputIndex, Math.Min(total - inputIndex, kBufferSize));
  55. err = inflate(ref _ztream, (int)FlushTypes.None);
  56. if (err == 0)
  57. while (_ztream.avail_out == 0)
  58. {
  59. OnDataAvailable();
  60. err = inflate(ref _ztream, (int)FlushTypes.None);
  61. }
  62. inputIndex += (int)_ztream.total_in;
  63. }
  64. setChecksum( _ztream.adler );
  65. }
  66. /// <summary>
  67. /// Finishes up any pending data that needs to be processed and handled.
  68. /// </summary>
  69. public override void Finish()
  70. {
  71. int err;
  72. do
  73. {
  74. err = inflate(ref _ztream, (int)FlushTypes.Finish);
  75. OnDataAvailable();
  76. }
  77. while (err == 0);
  78. setChecksum( _ztream.adler );
  79. inflateReset(ref _ztream);
  80. resetOutput();
  81. }
  82. /// <summary>
  83. /// Closes the internal zlib inflate stream
  84. /// </summary>
  85. protected override void CleanUp() { inflateEnd(ref _ztream); }
  86. }
  87. }