Add GZIP header processing to GZlibCompressor/GZlibDecompressor
[platform/upstream/glib.git] / gio / gzlibdecompressor.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 "gzlibdecompressor.h"
26
27 #include <errno.h>
28 #include <zlib.h>
29 #include <string.h>
30
31 #include "gfileinfo.h"
32 #include "gioerror.h"
33 #include "gioenums.h"
34 #include "gioenumtypes.h"
35 #include "glibintl.h"
36
37
38 enum {
39   PROP_0,
40   PROP_FORMAT,
41   PROP_FILE_INFO
42 };
43
44 /**
45  * SECTION:gzdecompressor
46  * @short_description: Zlib decompressor
47  * @include: gio/gio.h
48  *
49  * #GZlibDecompressor is an implementation of #GConverter that
50  * decompresses data compressed with zlib.
51  */
52
53 static void g_zlib_decompressor_iface_init          (GConverterIface *iface);
54
55 typedef struct {
56   gz_header gzheader;
57   char filename[257];
58   GFileInfo *file_info;
59 } HeaderData;
60
61 /**
62  * GZlibDecompressor:
63  *
64  * Zlib decompression
65  */
66 struct _GZlibDecompressor
67 {
68   GObject parent_instance;
69
70   GZlibCompressorFormat format;
71   z_stream zstream;
72   HeaderData *header_data;
73 };
74
75 static void
76 g_zlib_decompressor_set_gzheader (GZlibDecompressor *decompressor)
77 {
78   if (decompressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP)
79     return;
80
81   if (decompressor->header_data != NULL)
82     {
83       if (decompressor->header_data->file_info)
84         g_object_unref (decompressor->header_data->file_info);
85
86       memset (decompressor->header_data, 0, sizeof (HeaderData));
87     }
88   else
89     {
90       decompressor->header_data = g_new0 (HeaderData, 1);
91     }
92
93   decompressor->header_data->gzheader.name = (Bytef*) &decompressor->header_data->filename;
94   /* We keep one byte to guarantee the string is 0-terminated */
95   decompressor->header_data->gzheader.name_max = 256;
96
97   if (inflateGetHeader (&decompressor->zstream, &decompressor->header_data->gzheader) != Z_OK)
98     g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
99 }
100
101 G_DEFINE_TYPE_WITH_CODE (GZlibDecompressor, g_zlib_decompressor, G_TYPE_OBJECT,
102                          G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
103                                                 g_zlib_decompressor_iface_init))
104
105 static void
106 g_zlib_decompressor_finalize (GObject *object)
107 {
108   GZlibDecompressor *decompressor;
109
110   decompressor = G_ZLIB_DECOMPRESSOR (object);
111
112   inflateEnd (&decompressor->zstream);
113
114   if (decompressor->header_data != NULL)
115     {
116       if (decompressor->header_data->file_info)
117         g_object_unref (decompressor->header_data->file_info);
118       g_free (decompressor->header_data);
119     }
120
121   G_OBJECT_CLASS (g_zlib_decompressor_parent_class)->finalize (object);
122 }
123
124
125 static void
126 g_zlib_decompressor_set_property (GObject      *object,
127                                   guint         prop_id,
128                                   const GValue *value,
129                                   GParamSpec   *pspec)
130 {
131   GZlibDecompressor *decompressor;
132
133   decompressor = G_ZLIB_DECOMPRESSOR (object);
134
135   switch (prop_id)
136     {
137     case PROP_FORMAT:
138       decompressor->format = g_value_get_enum (value);
139       break;
140
141     default:
142       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143       break;
144     }
145
146 }
147
148 static void
149 g_zlib_decompressor_get_property (GObject    *object,
150                                   guint       prop_id,
151                                   GValue     *value,
152                                   GParamSpec *pspec)
153 {
154   GZlibDecompressor *decompressor;
155
156   decompressor = G_ZLIB_DECOMPRESSOR (object);
157
158   switch (prop_id)
159     {
160     case PROP_FORMAT:
161       g_value_set_enum (value, decompressor->format);
162       break;
163
164     case PROP_FILE_INFO:
165       if (decompressor->header_data)
166         g_value_set_object (value, decompressor->header_data->file_info);
167       else
168         g_value_set_object (value, NULL);
169       break;
170
171     default:
172       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173       break;
174     }
175 }
176
177 static void
178 g_zlib_decompressor_init (GZlibDecompressor *decompressor)
179 {
180 }
181
182 static void
183 g_zlib_decompressor_constructed (GObject *object)
184 {
185   GZlibDecompressor *decompressor;
186   int res;
187
188   decompressor = G_ZLIB_DECOMPRESSOR (object);
189
190   if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
191     {
192       /* + 16 for gzip */
193       res = inflateInit2 (&decompressor->zstream, MAX_WBITS + 16);
194     }
195   else if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
196     {
197       /* Negative for gzip */
198       res = inflateInit2 (&decompressor->zstream, -MAX_WBITS);
199     }
200   else /* ZLIB */
201     res = inflateInit (&decompressor->zstream);
202
203   if (res == Z_MEM_ERROR )
204     g_error ("GZlibDecompressor: Not enough memory for zlib use");
205
206   if (res != Z_OK)
207     g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
208
209   g_zlib_decompressor_set_gzheader (decompressor);
210 }
211
212 static void
213 g_zlib_decompressor_class_init (GZlibDecompressorClass *klass)
214 {
215   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
216
217   gobject_class->finalize = g_zlib_decompressor_finalize;
218   gobject_class->constructed = g_zlib_decompressor_constructed;
219   gobject_class->get_property = g_zlib_decompressor_get_property;
220   gobject_class->set_property = g_zlib_decompressor_set_property;
221
222   g_object_class_install_property (gobject_class,
223                                    PROP_FORMAT,
224                                    g_param_spec_enum ("format",
225                                                       P_("compression format"),
226                                                       P_("The format of the compressed data"),
227                                                       G_TYPE_ZLIB_COMPRESSOR_FORMAT,
228                                                       G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
229                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
230                                                       G_PARAM_STATIC_STRINGS));
231
232   /**
233    * GZlibDecompressor:file-info:
234    *
235    * A #GFileInfo containing the information found in the GZIP header
236    * of the data stream processed, or %NULL if the header was not yet
237    * fully processed, is not present at all, or the compressor's
238    * #GZlibDecompressor:format property is not %G_ZLIB_COMPRESSOR_FORMAT_GZIP.
239    *
240    * Since: 2.26
241    */
242   g_object_class_install_property (gobject_class,
243                                    PROP_FILE_INFO,
244                                    g_param_spec_object ("file-info",
245                                                        P_("file info"),
246                                                        P_("File info"),
247                                                        G_TYPE_FILE_INFO,
248                                                        G_PARAM_READABLE |
249                                                        G_PARAM_STATIC_STRINGS));
250 }
251
252 /**
253  * g_zlib_decompressor_new:
254  * @format: The format to use for the compressed data
255  *
256  * Creates a new #GZlibDecompressor.
257  *
258  * Returns: a new #GZlibDecompressor
259  *
260  * Since: 2.24
261  **/
262 GZlibDecompressor *
263 g_zlib_decompressor_new (GZlibCompressorFormat format)
264 {
265   GZlibDecompressor *decompressor;
266
267   decompressor = g_object_new (G_TYPE_ZLIB_DECOMPRESSOR,
268                                "format", format,
269                                NULL);
270
271   return decompressor;
272 }
273
274 /**
275  * g_zlib_decompressor_get_file_info:
276  * @decompressor: a #GZlibDecompressor
277  *
278  * Retrieves the #GFileInfo constructed from the GZIP header data
279  * of compressed data processed by @compressor, or %NULL if @decompressor's
280  * #GZlibDecompressor:format property is not %G_ZLIB_COMPRESSOR_FORMAT_GZIP,
281  * or the header data was not fully processed yet, or it not present in the
282  * data stream at all.
283  *
284  * Returns: (transfer none): a #GFileInfo, or %NULL
285  *
286  * Since: 2.26
287  */
288 GFileInfo *
289 g_zlib_decompressor_get_file_info (GZlibDecompressor *decompressor)
290 {
291   g_return_val_if_fail (G_IS_ZLIB_DECOMPRESSOR (decompressor), NULL);
292
293   if (decompressor->header_data)
294     return decompressor->header_data->file_info;
295
296   return NULL;
297 }
298
299 static void
300 g_zlib_decompressor_reset (GConverter *converter)
301 {
302   GZlibDecompressor *decompressor = G_ZLIB_DECOMPRESSOR (converter);
303   int res;
304
305   res = inflateReset (&decompressor->zstream);
306   if (res != Z_OK)
307     g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
308
309   g_zlib_decompressor_set_gzheader (decompressor);
310 }
311
312 static GConverterResult
313 g_zlib_decompressor_convert (GConverter *converter,
314                              const void *inbuf,
315                              gsize       inbuf_size,
316                              void       *outbuf,
317                              gsize       outbuf_size,
318                              GConverterFlags flags,
319                              gsize      *bytes_read,
320                              gsize      *bytes_written,
321                              GError    **error)
322 {
323   GZlibDecompressor *decompressor;
324   int res;
325
326   decompressor = G_ZLIB_DECOMPRESSOR (converter);
327
328   decompressor->zstream.next_in = (void *)inbuf;
329   decompressor->zstream.avail_in = inbuf_size;
330
331   decompressor->zstream.next_out = outbuf;
332   decompressor->zstream.avail_out = outbuf_size;
333
334   res = inflate (&decompressor->zstream, Z_NO_FLUSH);
335
336   if (res == Z_DATA_ERROR || res == Z_NEED_DICT)
337     {
338       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
339                            _("Invalid compressed data"));
340       return G_CONVERTER_ERROR;
341     }
342
343   if (res == Z_MEM_ERROR)
344     {
345       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
346                            _("Not enough memory"));
347       return G_CONVERTER_ERROR;
348     }
349
350     if (res == Z_STREAM_ERROR)
351     {
352       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
353                    _("Internal error: %s"), decompressor->zstream.msg);
354       return G_CONVERTER_ERROR;
355     }
356
357     if (res == Z_BUF_ERROR)
358       {
359         if (flags & G_CONVERTER_FLUSH)
360           return G_CONVERTER_FLUSHED;
361
362         /* Z_FINISH not set, so this means no progress could be made */
363         /* We do have output space, so this should only happen if we
364            have no input but need some */
365
366         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
367                              _("Need more input"));
368         return G_CONVERTER_ERROR;
369       }
370
371   g_assert (res == Z_OK || res == Z_STREAM_END);
372
373   *bytes_read = inbuf_size - decompressor->zstream.avail_in;
374   *bytes_written = outbuf_size - decompressor->zstream.avail_out;
375
376   if (decompressor->header_data != NULL &&
377       decompressor->header_data->gzheader.done == 1)
378     {
379       HeaderData *data = decompressor->header_data;
380
381       /* So we don't notify again */
382       data->gzheader.done = 2;
383
384       data->file_info = g_file_info_new ();
385       g_file_info_set_attribute_uint64 (data->file_info,
386                                         G_FILE_ATTRIBUTE_TIME_MODIFIED,
387                                         data->gzheader.time);
388       g_file_info_set_attribute_uint32 (data->file_info,
389                                         G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
390                                         0);
391
392       if (data->filename[0] != '\0')
393         g_file_info_set_attribute_byte_string (data->file_info,
394                                                G_FILE_ATTRIBUTE_STANDARD_NAME,
395                                                data->filename);
396
397       g_object_notify (G_OBJECT (decompressor), "file-info");
398     }
399
400   if (res == Z_STREAM_END)
401     return G_CONVERTER_FINISHED;
402   return G_CONVERTER_CONVERTED;
403 }
404
405 static void
406 g_zlib_decompressor_iface_init (GConverterIface *iface)
407 {
408   iface->convert = g_zlib_decompressor_convert;
409   iface->reset = g_zlib_decompressor_reset;
410 }