Registry server LDB backend: Don't make copies of the same type
[kai/samba-autobuild/.git] / lib / zlib / contrib / dotzlib / DotZLib / Deflater.cs
1 //\r
2 // © Copyright Henrik Ravn 2004\r
3 //\r
4 // Use, modification and distribution are subject to the Boost Software License, Version 1.0. \r
5 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)\r
6 //\r
7 \r
8 using System;\r
9 using System.Diagnostics;\r
10 using System.Runtime.InteropServices;\r
11 \r
12 namespace DotZLib\r
13 {\r
14 \r
15     /// <summary>\r
16     /// Implements a data compressor, using the deflate algorithm in the ZLib dll\r
17     /// </summary>\r
18         public sealed class Deflater : CodecBase\r
19         {\r
20         #region Dll imports\r
21         [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]\r
22         private static extern int deflateInit_(ref ZStream sz, int level, string vs, int size);\r
23 \r
24         [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]\r
25         private static extern int deflate(ref ZStream sz, int flush);\r
26 \r
27         [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]\r
28         private static extern int deflateReset(ref ZStream sz);\r
29 \r
30         [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]\r
31         private static extern int deflateEnd(ref ZStream sz);\r
32         #endregion\r
33 \r
34         /// <summary>\r
35         /// Constructs an new instance of the <c>Deflater</c>\r
36         /// </summary>\r
37         /// <param name="level">The compression level to use for this <c>Deflater</c></param>\r
38                 public Deflater(CompressLevel level) : base()\r
39                 {\r
40             int retval = deflateInit_(ref _ztream, (int)level, Info.Version, Marshal.SizeOf(_ztream));\r
41             if (retval != 0)\r
42                 throw new ZLibException(retval, "Could not initialize deflater");\r
43 \r
44             resetOutput();\r
45                 }\r
46 \r
47         /// <summary>\r
48         /// Adds more data to the codec to be processed.\r
49         /// </summary>\r
50         /// <param name="data">Byte array containing the data to be added to the codec</param>\r
51         /// <param name="offset">The index of the first byte to add from <c>data</c></param>\r
52         /// <param name="count">The number of bytes to add</param>\r
53         /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>\r
54         public override void Add(byte[] data, int offset, int count)\r
55         {\r
56             if (data == null) throw new ArgumentNullException();\r
57             if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();\r
58             if ((offset+count) > data.Length) throw new ArgumentException();\r
59             \r
60             int total = count;\r
61             int inputIndex = offset;\r
62             int err = 0;\r
63 \r
64             while (err >= 0 && inputIndex < total)\r
65             {\r
66                 copyInput(data, inputIndex, Math.Min(total - inputIndex, kBufferSize));\r
67                 while (err >= 0 && _ztream.avail_in > 0)\r
68                 {\r
69                     err = deflate(ref _ztream, (int)FlushTypes.None);\r
70                     if (err == 0)\r
71                         while (_ztream.avail_out == 0)\r
72                         {\r
73                             OnDataAvailable();\r
74                             err = deflate(ref _ztream, (int)FlushTypes.None);\r
75                         }\r
76                     inputIndex += (int)_ztream.total_in;\r
77                 }\r
78             }\r
79             setChecksum( _ztream.adler );\r
80         }\r
81 \r
82 \r
83         /// <summary>\r
84         /// Finishes up any pending data that needs to be processed and handled.\r
85         /// </summary>\r
86         public override void Finish()\r
87         {\r
88             int err;\r
89             do \r
90             {\r
91                 err = deflate(ref _ztream, (int)FlushTypes.Finish);\r
92                 OnDataAvailable();\r
93             }\r
94             while (err == 0);\r
95             setChecksum( _ztream.adler );\r
96             deflateReset(ref _ztream);\r
97             resetOutput();\r
98         }\r
99 \r
100         /// <summary>\r
101         /// Closes the internal zlib deflate stream\r
102         /// </summary>\r
103         protected override void CleanUp() { deflateEnd(ref _ztream); }\r
104 \r
105     }\r
106 }\r