* signal.c: Mono_Unix_UnixSignal_WaitAny() semantic change: return the index
[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 <config.h>
10 #if defined (HAVE_ZLIB)
11 #include <zlib.h>
12 #else
13 #include "zlib.h"
14 #endif
15
16 #include <stdlib.h>
17
18 z_stream *
19 create_z_stream(int compress, unsigned char gzip)
20 {
21         z_stream *z;
22         int retval;
23
24 #if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1204)
25         /* Older versions of zlib do not support raw deflate or gzip */
26         return NULL;
27 #endif
28
29         z = malloc (sizeof (z_stream));
30         z->next_in = Z_NULL;
31         z->avail_in = 0;
32         z->next_out = Z_NULL;
33         z->avail_out = 0;
34         z->zalloc = Z_NULL;
35         z->zfree = Z_NULL;
36         z->opaque = NULL;
37         if (compress) {
38                 retval = deflateInit2 (z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, gzip ? 31 : -15, 8, Z_DEFAULT_STRATEGY);
39         } else {
40                 retval = inflateInit2 (z, gzip ? 31 : -15);
41         }
42
43         if (retval == Z_OK)
44                 return z;
45
46         free (z);
47         return NULL;
48 }
49
50 void
51 free_z_stream(z_stream *z, int compress)
52 {
53         if (compress) {
54                 deflateEnd (z);
55         } else {
56                 inflateEnd (z);
57         }
58         free (z);
59 }
60
61 void
62 z_stream_set_next_in(z_stream *z, unsigned char *next_in)
63 {
64         z->next_in = next_in;
65 }
66
67 void
68 z_stream_set_avail_in(z_stream *z, int avail_in)
69 {
70         z->avail_in = avail_in;
71 }
72
73 int
74 z_stream_get_avail_in(z_stream *z)
75 {
76         return z->avail_in;
77 }
78
79 void
80 z_stream_set_next_out(z_stream *z, unsigned char *next_out)
81 {
82         z->next_out = next_out;
83 }
84
85 void
86 z_stream_set_avail_out(z_stream *z, int avail_out)
87 {
88         z->avail_out = avail_out;
89 }
90
91 int
92 z_stream_deflate (z_stream *z, int flush, unsigned char *next_out, int *avail_out)
93 {
94         int ret_val;
95
96         z->next_out = next_out;
97         z->avail_out = *avail_out;
98
99         ret_val = deflate (z, flush);
100
101         *avail_out = z->avail_out;
102
103         return ret_val;
104 }
105
106 int
107 z_stream_inflate (z_stream *z, int *avail_out)
108 {
109         int ret_val;
110
111         z->avail_out = *avail_out;
112
113         ret_val = inflate (z, Z_NO_FLUSH);
114
115         *avail_out = z->avail_out;
116
117         return ret_val;
118 }