Add GZlibCompressor
[platform/upstream/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
161     res = deflateInit (&compressor->zstream, compressor->level);
162
163   if (res == Z_MEM_ERROR )
164     g_error ("GZlibCompressor: Not enough memory for zlib use");
165
166   if (res != Z_OK)
167     g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
168 }
169
170 static void
171 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
172 {
173   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
174
175   gobject_class->finalize = g_zlib_compressor_finalize;
176   gobject_class->constructed = g_zlib_compressor_constructed;
177   gobject_class->get_property = g_zlib_compressor_get_property;
178   gobject_class->set_property = g_zlib_compressor_set_property;
179
180   g_object_class_install_property (gobject_class,
181                                    PROP_FORMAT,
182                                    g_param_spec_enum ("format",
183                                                       P_("compression format"),
184                                                       P_("The format of the compressed data"),
185                                                       G_TYPE_ZLIB_COMPRESSOR_FORMAT,
186                                                       G_ZLIB_COMPRESSOR_FORMAT_RAW,
187                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
188                                                       G_PARAM_STATIC_STRINGS));
189   g_object_class_install_property (gobject_class,
190                                    PROP_LEVEL,
191                                    g_param_spec_int ("level",
192                                                      P_("compression level"),
193                                                      P_("The level of compression from 0 (no compression) to 9 (most compression), -1 for the default level"),
194                                                      -1, 9,
195                                                      -1,
196                                                      G_PARAM_READWRITE |
197                                                      G_PARAM_CONSTRUCT_ONLY |
198                                                      G_PARAM_STATIC_STRINGS));
199 }
200
201 /**
202  * g_zlib_compressor_new:
203  * @format: The format to use for the compressed data
204  * @level: compression level (0-9), -1 for default
205  *
206  * Creates a new #GZlibCompressor.
207  *
208  * Returns: a new #GZlibCompressor
209  *
210  * Since: 2.24
211  **/
212 GZlibCompressor *
213 g_zlib_compressor_new (GZlibCompressorFormat format,
214                        int level)
215 {
216   GZlibCompressor *compressor;
217
218   compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
219                              "format", format,
220                              "level", level,
221                              NULL);
222
223   return compressor;
224 }
225
226 static void
227 g_zlib_compressor_reset (GConverter *converter)
228 {
229   GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
230   int res;
231
232   res = deflateReset (&compressor->zstream);
233   if (res != Z_OK)
234     g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
235 }
236
237 static GConverterResult
238 g_zlib_compressor_convert (GConverter *converter,
239                            const void *inbuf,
240                            gsize       inbuf_size,
241                            void       *outbuf,
242                            gsize       outbuf_size,
243                            GConverterFlags flags,
244                            gsize      *bytes_read,
245                            gsize      *bytes_written,
246                            GError    **error)
247 {
248   GZlibCompressor *compressor;
249   int res;
250   int flush;
251
252   compressor = G_ZLIB_COMPRESSOR (converter);
253
254   compressor->zstream.next_in = (void *)inbuf;
255   compressor->zstream.avail_in = inbuf_size;
256
257   compressor->zstream.next_out = outbuf;
258   compressor->zstream.avail_out = outbuf_size;
259
260   flush = Z_NO_FLUSH;
261   if (flags & G_CONVERTER_INPUT_AT_END)
262     flush = Z_FINISH;
263   else if (flags & G_CONVERTER_FLUSH)
264     flush = Z_SYNC_FLUSH;
265
266   res = deflate (&compressor->zstream, flush);
267
268   if (res == Z_MEM_ERROR)
269     {
270       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
271                            _("Not enough memory"));
272       return G_CONVERTER_ERROR;
273     }
274
275     if (res == Z_STREAM_ERROR)
276     {
277       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
278                    _("Internal error: %s"), compressor->zstream.msg);
279       return G_CONVERTER_ERROR;
280     }
281
282     if (res == Z_BUF_ERROR)
283       {
284         if (flags & G_CONVERTER_FLUSH)
285           return G_CONVERTER_FLUSHED;
286
287         /* We do have output space, so this should only happen if we
288            have no input but need some */
289
290         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
291                              _("Need more input"));
292         return G_CONVERTER_ERROR;
293       }
294
295   if (res == Z_OK || res == Z_STREAM_END)
296     {
297       *bytes_read = inbuf_size - compressor->zstream.avail_in;
298       *bytes_written = outbuf_size - compressor->zstream.avail_out;
299
300       if (res == Z_STREAM_END)
301         return G_CONVERTER_FINISHED;
302       return G_CONVERTER_CONVERTED;
303     }
304
305   g_assert_not_reached ();
306 }
307
308 static void
309 g_zlib_compressor_iface_init (GConverterIface *iface)
310 {
311   iface->convert = g_zlib_compressor_convert;
312   iface->reset = g_zlib_compressor_reset;
313 }
314
315 #define __G_ZLIB_COMPRESSOR_C__
316 #include "gioaliasdef.c"