Adding various attributes inheriting from MonoTODO.
[mono.git] / support / zlib_macros.c
1 /*
2  * Helper routines to use Zlib
3  *
4  * Author:
5  *   Christopher Lahey (clahey@ximian.co)
6  *
7  * (C) 2004 Novell, Inc.
8  */
9 #include <zlib.h>
10 #include <stdlib.h>
11
12 z_stream *
13 create_z_stream(int compress, unsigned char gzip)
14 {
15         z_stream *z;
16         int retval;
17
18 #if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1204)
19         /* Older versions of zlib do not support raw deflate or gzip */
20         return NULL;
21 #endif
22
23         z = malloc (sizeof (z_stream));
24         z->next_in = Z_NULL;
25         z->avail_in = 0;
26         z->next_out = Z_NULL;
27         z->avail_out = 0;
28         z->zalloc = Z_NULL;
29         z->zfree = Z_NULL;
30         z->opaque = NULL;
31         if (compress) {
32                 retval = deflateInit2 (z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, gzip ? 31 : -15, 8, Z_DEFAULT_STRATEGY);
33         } else {
34                 retval = inflateInit2 (z, gzip ? 31 : -15);
35         }
36
37         if (retval == Z_OK)
38                 return z;
39
40         free (z);
41         return NULL;
42 }
43
44 void
45 free_z_stream(z_stream *z, int compress)
46 {
47         if (compress) {
48                 deflateEnd (z);
49         } else {
50                 inflateEnd (z);
51         }
52         free (z);
53 }
54
55 void
56 z_stream_set_next_in(z_stream *z, unsigned char *next_in)
57 {
58         z->next_in = next_in;
59 }
60
61 void
62 z_stream_set_avail_in(z_stream *z, int avail_in)
63 {
64         z->avail_in = avail_in;
65 }
66
67 int
68 z_stream_get_avail_in(z_stream *z)
69 {
70         return z->avail_in;
71 }
72
73 void
74 z_stream_set_next_out(z_stream *z, unsigned char *next_out)
75 {
76         z->next_out = next_out;
77 }
78
79 void
80 z_stream_set_avail_out(z_stream *z, int avail_out)
81 {
82         z->avail_out = avail_out;
83 }
84
85 int
86 z_stream_deflate (z_stream *z, int flush, unsigned char *next_out, int *avail_out)
87 {
88         int ret_val;
89
90         z->next_out = next_out;
91         z->avail_out = *avail_out;
92
93         ret_val = deflate (z, flush);
94
95         *avail_out = z->avail_out;
96
97         return ret_val;
98 }
99
100 int
101 z_stream_inflate (z_stream *z, int *avail_out)
102 {
103         int ret_val;
104
105         z->avail_out = *avail_out;
106
107         ret_val = inflate (z, Z_NO_FLUSH);
108
109         *avail_out = z->avail_out;
110
111         return ret_val;
112 }