annotate src/zlib-1.2.8/contrib/dotzlib/DotZLib/DotZLib.cs @ 148:b4bfdf10c4b3

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