gio/: fully remove gioalias hacks
[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 "gzlibcompressor.h"
26
27 #include <errno.h>
28 #include <zlib.h>
29 #include <string.h>
30
31 #include "gioerror.h"
32 #include "gioenums.h"
33 #include "gioenumtypes.h"
34 #include "glibintl.h"
35
36
37 enum {
38   PROP_0,
39   PROP_FORMAT,
40   PROP_LEVEL
41 };
42
43 /**
44  * SECTION:gzcompressor
45  * @short_description: Zlib compressor
46  * @include: gio/gio.h
47  *
48  * #GZlibCompressor is an implementation of #GConverter that
49  * compresses data using zlib.
50  */
51
52 static void g_zlib_compressor_iface_init          (GConverterIface *iface);
53
54 /**
55  * GZlibCompressor:
56  *
57  * Zlib decompression
58  */
59 struct _GZlibCompressor
60 {
61   GObject parent_instance;
62
63   GZlibCompressorFormat format;
64   int level;
65   z_stream zstream;
66 };
67
68 G_DEFINE_TYPE_WITH_CODE (GZlibCompressor, g_zlib_compressor, G_TYPE_OBJECT,
69                          G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
70                                                 g_zlib_compressor_iface_init))
71
72 static void
73 g_zlib_compressor_finalize (GObject *object)
74 {
75   GZlibCompressor *compressor;
76
77   compressor = G_ZLIB_COMPRESSOR (object);
78
79   deflateEnd (&compressor->zstream);
80
81   G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object);
82 }
83
84
85 static void
86 g_zlib_compressor_set_property (GObject      *object,
87                                   guint         prop_id,
88                                   const GValue *value,
89                                   GParamSpec   *pspec)
90 {
91   GZlibCompressor *compressor;
92
93   compressor = G_ZLIB_COMPRESSOR (object);
94
95   switch (prop_id)
96     {
97     case PROP_FORMAT:
98       compressor->format = g_value_get_enum (value);
99       break;
100
101     case PROP_LEVEL:
102       compressor->level = g_value_get_int (value);
103       break;
104
105     default:
106       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107       break;
108     }
109
110 }
111
112 static void
113 g_zlib_compressor_get_property (GObject    *object,
114                                   guint       prop_id,
115                                   GValue     *value,
116                                   GParamSpec *pspec)
117 {
118   GZlibCompressor *compressor;
119
120   compressor = G_ZLIB_COMPRESSOR (object);
121
122   switch (prop_id)
123     {
124     case PROP_FORMAT:
125       g_value_set_enum (value, compressor->format);
126       break;
127
128     case PROP_LEVEL:
129       g_value_set_int (value, compressor->level);
130       break;
131
132     default:
133       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134       break;
135     }
136 }
137
138 static void
139 g_zlib_compressor_init (GZlibCompressor *compressor)
140 {
141 }
142
143 static void
144 g_zlib_compressor_constructed (GObject *object)
145 {
146   GZlibCompressor *compressor;
147   int res;
148
149   compressor = G_ZLIB_COMPRESSOR (object);
150
151   if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
152     {
153       /* + 16 for gzip */
154       res = deflateInit2 (&compressor->zstream,
155                           compressor->level, Z_DEFLATED,
156                           MAX_WBITS + 16, 8,
157                           Z_DEFAULT_STRATEGY);
158     }
159   else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
160     {
161       /* negative wbits for raw */
162       res = deflateInit2 (&compressor->zstream,
163                           compressor->level, Z_DEFLATED,
164                           -MAX_WBITS, 8,
165                           Z_DEFAULT_STRATEGY);
166     }
167   else /* ZLIB */
168     res = deflateInit (&compressor->zstream, compressor->level);
169
170   if (res == Z_MEM_ERROR )
171     g_error ("GZlibCompressor: Not enough memory for zlib use");
172
173   if (res != Z_OK)
174     g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
175 }
176
177 static void
178 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
179 {
180   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
181
182   gobject_class->finalize = g_zlib_compressor_finalize;
183   gobject_class->constructed = g_zlib_compressor_constructed;
184   gobject_class->get_property = g_zlib_compressor_get_property;
185   gobject_class->set_property = g_zlib_compressor_set_property;
186
187   g_object_class_install_property (gobject_class,
188                                    PROP_FORMAT,
189                                    g_param_spec_enum ("format",
190                                                       P_("compression format"),
191                                                       P_("The format of the compressed data"),
192                                                       G_TYPE_ZLIB_COMPRESSOR_FORMAT,
193                                                       G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
194                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
195                                                       G_PARAM_STATIC_STRINGS));
196   g_object_class_install_property (gobject_class,
197                                    PROP_LEVEL,
198                                    g_param_spec_int ("level",
199                                                      P_("compression level"),
200                                                      P_("The level of compression from 0 (no compression) to 9 (most compression), -1 for the default level"),
201                                                      -1, 9,
202                                                      -1,
203                                                      G_PARAM_READWRITE |
204                                                      G_PARAM_CONSTRUCT_ONLY |
205                                                      G_PARAM_STATIC_STRINGS));
206 }
207
208 /**
209  * g_zlib_compressor_new:
210  * @format: The format to use for the compressed data
211  * @level: compression level (0-9), -1 for default
212  *
213  * Creates a new #GZlibCompressor.
214  *
215  * Returns: a new #GZlibCompressor
216  *
217  * Since: 2.24
218  **/
219 GZlibCompressor *
220 g_zlib_compressor_new (GZlibCompressorFormat format,
221                        int level)
222 {
223   GZlibCompressor *compressor;
224
225   compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
226                              "format", format,
227                              "level", level,
228                              NULL);
229
230   return compressor;
231 }
232
233 static void
234 g_zlib_compressor_reset (GConverter *converter)
235 {
236   GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
237   int res;
238
239   res = deflateReset (&compressor->zstream);
240   if (res != Z_OK)
241     g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
242 }
243
244 static GConverterResult
245 g_zlib_compressor_convert (GConverter *converter,
246                            const void *inbuf,
247                            gsize       inbuf_size,
248                            void       *outbuf,
249                            gsize       outbuf_size,
250                            GConverterFlags flags,
251                            gsize      *bytes_read,
252                            gsize      *bytes_written,
253                            GError    **error)
254 {
255   GZlibCompressor *compressor;
256   int res;
257   int flush;
258
259   compressor = G_ZLIB_COMPRESSOR (converter);
260
261   compressor->zstream.next_in = (void *)inbuf;
262   compressor->zstream.avail_in = inbuf_size;
263
264   compressor->zstream.next_out = outbuf;
265   compressor->zstream.avail_out = outbuf_size;
266
267   flush = Z_NO_FLUSH;
268   if (flags & G_CONVERTER_INPUT_AT_END)
269     flush = Z_FINISH;
270   else if (flags & G_CONVERTER_FLUSH)
271     flush = Z_SYNC_FLUSH;
272
273   res = deflate (&compressor->zstream, flush);
274
275   if (res == Z_MEM_ERROR)
276     {
277       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
278                            _("Not enough memory"));
279       return G_CONVERTER_ERROR;
280     }
281
282     if (res == Z_STREAM_ERROR)
283     {
284       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
285                    _("Internal error: %s"), compressor->zstream.msg);
286       return G_CONVERTER_ERROR;
287     }
288
289     if (res == Z_BUF_ERROR)
290       {
291         if (flags & G_CONVERTER_FLUSH)
292           return G_CONVERTER_FLUSHED;
293
294         /* We do have output space, so this should only happen if we
295            have no input but need some */
296
297         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
298                              _("Need more input"));
299         return G_CONVERTER_ERROR;
300       }
301
302   if (res == Z_OK || res == Z_STREAM_END)
303     {
304       *bytes_read = inbuf_size - compressor->zstream.avail_in;
305       *bytes_written = outbuf_size - compressor->zstream.avail_out;
306
307       if (res == Z_STREAM_END)
308         return G_CONVERTER_FINISHED;
309       return G_CONVERTER_CONVERTED;
310     }
311
312   g_assert_not_reached ();
313 }
314
315 static void
316 g_zlib_compressor_iface_init (GConverterIface *iface)
317 {
318   iface->convert = g_zlib_compressor_convert;
319   iface->reset = g_zlib_compressor_reset;
320 }