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