Fix GZlibCompressorFormat names
[dana/cg-glib.git] / gio / gzlibcompressor.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2009 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <zlib.h>
27 #include <string.h>
28
29 #include "gzlibcompressor.h"
30 #include "glib.h"
31 #include "gioerror.h"
32 #include "glibintl.h"
33 #include "gioenums.h"
34 #include "gioenumtypes.h"
35
36 #include "gioalias.h"
37
38 enum {
39   PROP_0,
40   PROP_FORMAT,
41   PROP_LEVEL
42 };
43
44 /**
45  * SECTION:gzcompressor
46  * @short_description: Zlib compressor
47  * @include: gio/gio.h
48  *
49  * #GZlibCompressor is an implementation of #GCompressor that
50  * decompresses data compressed with zlib.
51  */
52
53 static void g_zlib_compressor_iface_init          (GConverterIface *iface);
54
55 /**
56  * GZlibCompressor:
57  *
58  * Zlib decompression
59  */
60 struct _GZlibCompressor
61 {
62   GObject parent_instance;
63
64   GZlibCompressorFormat format;
65   int level;
66   z_stream zstream;
67 };
68
69 G_DEFINE_TYPE_WITH_CODE (GZlibCompressor, g_zlib_compressor, G_TYPE_OBJECT,
70                          G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
71                                                 g_zlib_compressor_iface_init))
72
73 static void
74 g_zlib_compressor_finalize (GObject *object)
75 {
76   GZlibCompressor *compressor;
77
78   compressor = G_ZLIB_COMPRESSOR (object);
79
80   deflateEnd (&compressor->zstream);
81
82   G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object);
83 }
84
85
86 static void
87 g_zlib_compressor_set_property (GObject      *object,
88                                   guint         prop_id,
89                                   const GValue *value,
90                                   GParamSpec   *pspec)
91 {
92   GZlibCompressor *compressor;
93
94   compressor = G_ZLIB_COMPRESSOR (object);
95
96   switch (prop_id)
97     {
98     case PROP_FORMAT:
99       compressor->format = g_value_get_enum (value);
100       break;
101
102     case PROP_LEVEL:
103       compressor->level = g_value_get_int (value);
104       break;
105
106     default:
107       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108       break;
109     }
110
111 }
112
113 static void
114 g_zlib_compressor_get_property (GObject    *object,
115                                   guint       prop_id,
116                                   GValue     *value,
117                                   GParamSpec *pspec)
118 {
119   GZlibCompressor *compressor;
120
121   compressor = G_ZLIB_COMPRESSOR (object);
122
123   switch (prop_id)
124     {
125     case PROP_FORMAT:
126       g_value_set_enum (value, compressor->format);
127       break;
128
129     case PROP_LEVEL:
130       g_value_set_int (value, compressor->level);
131       break;
132
133     default:
134       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135       break;
136     }
137 }
138
139 static void
140 g_zlib_compressor_init (GZlibCompressor *compressor)
141 {
142 }
143
144 static void
145 g_zlib_compressor_constructed (GObject *object)
146 {
147   GZlibCompressor *compressor;
148   int res;
149
150   compressor = G_ZLIB_COMPRESSOR (object);
151
152   if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
153     {
154       /* + 16 for gzip */
155       res = deflateInit2 (&compressor->zstream,
156                           compressor->level, Z_DEFLATED,
157                           MAX_WBITS + 16, 8,
158                           Z_DEFAULT_STRATEGY);
159     }
160   else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
161     {
162       /* negative wbits for raw */
163       res = deflateInit2 (&compressor->zstream,
164                           compressor->level, Z_DEFLATED,
165                           -MAX_WBITS, 8,
166                           Z_DEFAULT_STRATEGY);
167     }
168   else /* ZLIB */
169     res = deflateInit (&compressor->zstream, compressor->level);
170
171   if (res == Z_MEM_ERROR )
172     g_error ("GZlibCompressor: Not enough memory for zlib use");
173
174   if (res != Z_OK)
175     g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
176 }
177
178 static void
179 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
180 {
181   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
182
183   gobject_class->finalize = g_zlib_compressor_finalize;
184   gobject_class->constructed = g_zlib_compressor_constructed;
185   gobject_class->get_property = g_zlib_compressor_get_property;
186   gobject_class->set_property = g_zlib_compressor_set_property;
187
188   g_object_class_install_property (gobject_class,
189                                    PROP_FORMAT,
190                                    g_param_spec_enum ("format",
191                                                       P_("compression format"),
192                                                       P_("The format of the compressed data"),
193                                                       G_TYPE_ZLIB_COMPRESSOR_FORMAT,
194                                                       G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
195                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
196                                                       G_PARAM_STATIC_STRINGS));
197   g_object_class_install_property (gobject_class,
198                                    PROP_LEVEL,
199                                    g_param_spec_int ("level",
200                                                      P_("compression level"),
201                                                      P_("The level of compression from 0 (no compression) to 9 (most compression), -1 for the default level"),
202                                                      -1, 9,
203                                                      -1,
204                                                      G_PARAM_READWRITE |
205                                                      G_PARAM_CONSTRUCT_ONLY |
206                                                      G_PARAM_STATIC_STRINGS));
207 }
208
209 /**
210  * g_zlib_compressor_new:
211  * @format: The format to use for the compressed data
212  * @level: compression level (0-9), -1 for default
213  *
214  * Creates a new #GZlibCompressor.
215  *
216  * Returns: a new #GZlibCompressor
217  *
218  * Since: 2.24
219  **/
220 GZlibCompressor *
221 g_zlib_compressor_new (GZlibCompressorFormat format,
222                        int level)
223 {
224   GZlibCompressor *compressor;
225
226   compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
227                              "format", format,
228                              "level", level,
229                              NULL);
230
231   return compressor;
232 }
233
234 static void
235 g_zlib_compressor_reset (GConverter *converter)
236 {
237   GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
238   int res;
239
240   res = deflateReset (&compressor->zstream);
241   if (res != Z_OK)
242     g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
243 }
244
245 static GConverterResult
246 g_zlib_compressor_convert (GConverter *converter,
247                            const void *inbuf,
248                            gsize       inbuf_size,
249                            void       *outbuf,
250                            gsize       outbuf_size,
251                            GConverterFlags flags,
252                            gsize      *bytes_read,
253                            gsize      *bytes_written,
254                            GError    **error)
255 {
256   GZlibCompressor *compressor;
257   int res;
258   int flush;
259
260   compressor = G_ZLIB_COMPRESSOR (converter);
261
262   compressor->zstream.next_in = (void *)inbuf;
263   compressor->zstream.avail_in = inbuf_size;
264
265   compressor->zstream.next_out = outbuf;
266   compressor->zstream.avail_out = outbuf_size;
267
268   flush = Z_NO_FLUSH;
269   if (flags & G_CONVERTER_INPUT_AT_END)
270     flush = Z_FINISH;
271   else if (flags & G_CONVERTER_FLUSH)
272     flush = Z_SYNC_FLUSH;
273
274   res = deflate (&compressor->zstream, flush);
275
276   if (res == Z_MEM_ERROR)
277     {
278       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
279                            _("Not enough memory"));
280       return G_CONVERTER_ERROR;
281     }
282
283     if (res == Z_STREAM_ERROR)
284     {
285       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
286                    _("Internal error: %s"), compressor->zstream.msg);
287       return G_CONVERTER_ERROR;
288     }
289
290     if (res == Z_BUF_ERROR)
291       {
292         if (flags & G_CONVERTER_FLUSH)
293           return G_CONVERTER_FLUSHED;
294
295         /* We do have output space, so this should only happen if we
296            have no input but need some */
297
298         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
299                              _("Need more input"));
300         return G_CONVERTER_ERROR;
301       }
302
303   if (res == Z_OK || res == Z_STREAM_END)
304     {
305       *bytes_read = inbuf_size - compressor->zstream.avail_in;
306       *bytes_written = outbuf_size - compressor->zstream.avail_out;
307
308       if (res == Z_STREAM_END)
309         return G_CONVERTER_FINISHED;
310       return G_CONVERTER_CONVERTED;
311     }
312
313   g_assert_not_reached ();
314 }
315
316 static void
317 g_zlib_compressor_iface_init (GConverterIface *iface)
318 {
319   iface->convert = g_zlib_compressor_convert;
320   iface->reset = g_zlib_compressor_reset;
321 }
322
323 #define __G_ZLIB_COMPRESSOR_C__
324 #include "gioaliasdef.c"