annotate src/zlib-1.2.8/contrib/dotzlib/DotZLib/DotZLib.cs @ 83:ae30d91d2ffe

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
date Fri, 07 Feb 2020 11:51:13 +0000
parents 5ea0608b923f
children
rev   line source
Chris@43 1 //
Chris@43 2 // © Copyright Henrik Ravn 2004
Chris@43 3 //
Chris@43 4 // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
Chris@43 5 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@43 6 //
Chris@43 7
Chris@43 8 using System;
Chris@43 9 using System.IO;
Chris@43 10 using System.Runtime.InteropServices;
Chris@43 11 using System.Text;
Chris@43 12
Chris@43 13
Chris@43 14 namespace DotZLib
Chris@43 15 {
Chris@43 16
Chris@43 17 #region Internal types
Chris@43 18
Chris@43 19 /// <summary>
Chris@43 20 /// Defines constants for the various flush types used with zlib
Chris@43 21 /// </summary>
Chris@43 22 internal enum FlushTypes
Chris@43 23 {
Chris@43 24 None, Partial, Sync, Full, Finish, Block
Chris@43 25 }
Chris@43 26
Chris@43 27 #region ZStream structure
Chris@43 28 // internal mapping of the zlib zstream structure for marshalling
Chris@43 29 [StructLayoutAttribute(LayoutKind.Sequential, Pack=4, Size=0, CharSet=CharSet.Ansi)]
Chris@43 30 internal struct ZStream
Chris@43 31 {
Chris@43 32 public IntPtr next_in;
Chris@43 33 public uint avail_in;
Chris@43 34 public uint total_in;
Chris@43 35
Chris@43 36 public IntPtr next_out;
Chris@43 37 public uint avail_out;
Chris@43 38 public uint total_out;
Chris@43 39
Chris@43 40 [MarshalAs(UnmanagedType.LPStr)]
Chris@43 41 string msg;
Chris@43 42 uint state;
Chris@43 43
Chris@43 44 uint zalloc;
Chris@43 45 uint zfree;
Chris@43 46 uint opaque;
Chris@43 47
Chris@43 48 int data_type;
Chris@43 49 public uint adler;
Chris@43 50 uint reserved;
Chris@43 51 }
Chris@43 52
Chris@43 53 #endregion
Chris@43 54
Chris@43 55 #endregion
Chris@43 56
Chris@43 57 #region Public enums
Chris@43 58 /// <summary>
Chris@43 59 /// Defines constants for the available compression levels in zlib
Chris@43 60 /// </summary>
Chris@43 61 public enum CompressLevel : int
Chris@43 62 {
Chris@43 63 /// <summary>
Chris@43 64 /// The default compression level with a reasonable compromise between compression and speed
Chris@43 65 /// </summary>
Chris@43 66 Default = -1,
Chris@43 67 /// <summary>
Chris@43 68 /// No compression at all. The data are passed straight through.
Chris@43 69 /// </summary>
Chris@43 70 None = 0,
Chris@43 71 /// <summary>
Chris@43 72 /// The maximum compression rate available.
Chris@43 73 /// </summary>
Chris@43 74 Best = 9,
Chris@43 75 /// <summary>
Chris@43 76 /// The fastest available compression level.
Chris@43 77 /// </summary>
Chris@43 78 Fastest = 1
Chris@43 79 }
Chris@43 80 #endregion
Chris@43 81
Chris@43 82 #region Exception classes
Chris@43 83 /// <summary>
Chris@43 84 /// The exception that is thrown when an error occurs on the zlib dll
Chris@43 85 /// </summary>
Chris@43 86 public class ZLibException : ApplicationException
Chris@43 87 {
Chris@43 88 /// <summary>
Chris@43 89 /// Initializes a new instance of the <see cref="ZLibException"/> class with a specified
Chris@43 90 /// error message and error code
Chris@43 91 /// </summary>
Chris@43 92 /// <param name="errorCode">The zlib error code that caused the exception</param>
Chris@43 93 /// <param name="msg">A message that (hopefully) describes the error</param>
Chris@43 94 public ZLibException(int errorCode, string msg) : base(String.Format("ZLib error {0} {1}", errorCode, msg))
Chris@43 95 {
Chris@43 96 }
Chris@43 97
Chris@43 98 /// <summary>
Chris@43 99 /// Initializes a new instance of the <see cref="ZLibException"/> class with a specified
Chris@43 100 /// error code
Chris@43 101 /// </summary>
Chris@43 102 /// <param name="errorCode">The zlib error code that caused the exception</param>
Chris@43 103 public ZLibException(int errorCode) : base(String.Format("ZLib error {0}", errorCode))
Chris@43 104 {
Chris@43 105 }
Chris@43 106 }
Chris@43 107 #endregion
Chris@43 108
Chris@43 109 #region Interfaces
Chris@43 110
Chris@43 111 /// <summary>
Chris@43 112 /// Declares methods and properties that enables a running checksum to be calculated
Chris@43 113 /// </summary>
Chris@43 114 public interface ChecksumGenerator
Chris@43 115 {
Chris@43 116 /// <summary>
Chris@43 117 /// Gets the current value of the checksum
Chris@43 118 /// </summary>
Chris@43 119 uint Value { get; }
Chris@43 120
Chris@43 121 /// <summary>
Chris@43 122 /// Clears the current checksum to 0
Chris@43 123 /// </summary>
Chris@43 124 void Reset();
Chris@43 125
Chris@43 126 /// <summary>
Chris@43 127 /// Updates the current checksum with an array of bytes
Chris@43 128 /// </summary>
Chris@43 129 /// <param name="data">The data to update the checksum with</param>
Chris@43 130 void Update(byte[] data);
Chris@43 131
Chris@43 132 /// <summary>
Chris@43 133 /// Updates the current checksum with part of an array of bytes
Chris@43 134 /// </summary>
Chris@43 135 /// <param name="data">The data to update the checksum with</param>
Chris@43 136 /// <param name="offset">Where in <c>data</c> to start updating</param>
Chris@43 137 /// <param name="count">The number of bytes from <c>data</c> to use</param>
Chris@43 138 /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>
Chris@43 139 /// <exception cref="ArgumentNullException"><c>data</c> is a null reference</exception>
Chris@43 140 /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>
Chris@43 141 void Update(byte[] data, int offset, int count);
Chris@43 142
Chris@43 143 /// <summary>
Chris@43 144 /// Updates the current checksum with the data from a string
Chris@43 145 /// </summary>
Chris@43 146 /// <param name="data">The string to update the checksum with</param>
Chris@43 147 /// <remarks>The characters in the string are converted by the UTF-8 encoding</remarks>
Chris@43 148 void Update(string data);
Chris@43 149
Chris@43 150 /// <summary>
Chris@43 151 /// Updates the current checksum with the data from a string, using a specific encoding
Chris@43 152 /// </summary>
Chris@43 153 /// <param name="data">The string to update the checksum with</param>
Chris@43 154 /// <param name="encoding">The encoding to use</param>
Chris@43 155 void Update(string data, Encoding encoding);
Chris@43 156 }
Chris@43 157
Chris@43 158
Chris@43 159 /// <summary>
Chris@43 160 /// Represents the method that will be called from a codec when new data
Chris@43 161 /// are available.
Chris@43 162 /// </summary>
Chris@43 163 /// <paramref name="data">The byte array containing the processed data</paramref>
Chris@43 164 /// <paramref name="startIndex">The index of the first processed byte in <c>data</c></paramref>
Chris@43 165 /// <paramref name="count">The number of processed bytes available</paramref>
Chris@43 166 /// <remarks>On return from this method, the data may be overwritten, so grab it while you can.
Chris@43 167 /// You cannot assume that startIndex will be zero.
Chris@43 168 /// </remarks>
Chris@43 169 public delegate void DataAvailableHandler(byte[] data, int startIndex, int count);
Chris@43 170
Chris@43 171 /// <summary>
Chris@43 172 /// Declares methods and events for implementing compressors/decompressors
Chris@43 173 /// </summary>
Chris@43 174 public interface Codec
Chris@43 175 {
Chris@43 176 /// <summary>
Chris@43 177 /// Occurs when more processed data are available.
Chris@43 178 /// </summary>
Chris@43 179 event DataAvailableHandler DataAvailable;
Chris@43 180
Chris@43 181 /// <summary>
Chris@43 182 /// Adds more data to the codec to be processed.
Chris@43 183 /// </summary>
Chris@43 184 /// <param name="data">Byte array containing the data to be added to the codec</param>
Chris@43 185 /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>
Chris@43 186 void Add(byte[] data);
Chris@43 187
Chris@43 188 /// <summary>
Chris@43 189 /// Adds more data to the codec to be processed.
Chris@43 190 /// </summary>
Chris@43 191 /// <param name="data">Byte array containing the data to be added to the codec</param>
Chris@43 192 /// <param name="offset">The index of the first byte to add from <c>data</c></param>
Chris@43 193 /// <param name="count">The number of bytes to add</param>
Chris@43 194 /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>
Chris@43 195 void Add(byte[] data, int offset, int count);
Chris@43 196
Chris@43 197 /// <summary>
Chris@43 198 /// Finishes up any pending data that needs to be processed and handled.
Chris@43 199 /// </summary>
Chris@43 200 void Finish();
Chris@43 201
Chris@43 202 /// <summary>
Chris@43 203 /// Gets the checksum of the data that has been added so far
Chris@43 204 /// </summary>
Chris@43 205 uint Checksum { get; }
Chris@43 206
Chris@43 207
Chris@43 208 }
Chris@43 209
Chris@43 210 #endregion
Chris@43 211
Chris@43 212 #region Classes
Chris@43 213 /// <summary>
Chris@43 214 /// Encapsulates general information about the ZLib library
Chris@43 215 /// </summary>
Chris@43 216 public class Info
Chris@43 217 {
Chris@43 218 #region DLL imports
Chris@43 219 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
Chris@43 220 private static extern uint zlibCompileFlags();
Chris@43 221
Chris@43 222 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
Chris@43 223 private static extern string zlibVersion();
Chris@43 224 #endregion
Chris@43 225
Chris@43 226 #region Private stuff
Chris@43 227 private uint _flags;
Chris@43 228
Chris@43 229 // helper function that unpacks a bitsize mask
Chris@43 230 private static int bitSize(uint bits)
Chris@43 231 {
Chris@43 232 switch (bits)
Chris@43 233 {
Chris@43 234 case 0: return 16;
Chris@43 235 case 1: return 32;
Chris@43 236 case 2: return 64;
Chris@43 237 }
Chris@43 238 return -1;
Chris@43 239 }
Chris@43 240 #endregion
Chris@43 241
Chris@43 242 /// <summary>
Chris@43 243 /// Constructs an instance of the <c>Info</c> class.
Chris@43 244 /// </summary>
Chris@43 245 public Info()
Chris@43 246 {
Chris@43 247 _flags = zlibCompileFlags();
Chris@43 248 }
Chris@43 249
Chris@43 250 /// <summary>
Chris@43 251 /// True if the library is compiled with debug info
Chris@43 252 /// </summary>
Chris@43 253 public bool HasDebugInfo { get { return 0 != (_flags & 0x100); } }
Chris@43 254
Chris@43 255 /// <summary>
Chris@43 256 /// True if the library is compiled with assembly optimizations
Chris@43 257 /// </summary>
Chris@43 258 public bool UsesAssemblyCode { get { return 0 != (_flags & 0x200); } }
Chris@43 259
Chris@43 260 /// <summary>
Chris@43 261 /// Gets the size of the unsigned int that was compiled into Zlib
Chris@43 262 /// </summary>
Chris@43 263 public int SizeOfUInt { get { return bitSize(_flags & 3); } }
Chris@43 264
Chris@43 265 /// <summary>
Chris@43 266 /// Gets the size of the unsigned long that was compiled into Zlib
Chris@43 267 /// </summary>
Chris@43 268 public int SizeOfULong { get { return bitSize((_flags >> 2) & 3); } }
Chris@43 269
Chris@43 270 /// <summary>
Chris@43 271 /// Gets the size of the pointers that were compiled into Zlib
Chris@43 272 /// </summary>
Chris@43 273 public int SizeOfPointer { get { return bitSize((_flags >> 4) & 3); } }
Chris@43 274
Chris@43 275 /// <summary>
Chris@43 276 /// Gets the size of the z_off_t type that was compiled into Zlib
Chris@43 277 /// </summary>
Chris@43 278 public int SizeOfOffset { get { return bitSize((_flags >> 6) & 3); } }
Chris@43 279
Chris@43 280 /// <summary>
Chris@43 281 /// Gets the version of ZLib as a string, e.g. "1.2.1"
Chris@43 282 /// </summary>
Chris@43 283 public static string Version { get { return zlibVersion(); } }
Chris@43 284 }
Chris@43 285
Chris@43 286 #endregion
Chris@43 287
Chris@43 288 }