Deflater.cs 3.9 KB

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