annotate src/zlib-1.2.7/contrib/dotzlib/DotZLib/DotZLib.cs @ 4:e13257ea84a4

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