OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [zlib/] [contrib/] [dotzlib/] [DotZLib/] [DotZLib.cs] - Blame information for rev 745

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.