Add GZIP header processing to GZlibCompressor/GZlibDecompressor
[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 "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_LEVEL,
42   PROP_FILE_INFO
43 };
44
45 /**
46  * SECTION:gzcompressor
47  * @short_description: Zlib compressor
48  * @include: gio/gio.h
49  *
50  * #GZlibCompressor is an implementation of #GConverter that
51  * compresses data using zlib.
52  */
53
54 static void g_zlib_compressor_iface_init          (GConverterIface *iface);
55
56 /**
57  * GZlibCompressor:
58  *
59  * Zlib decompression
60  */
61 struct _GZlibCompressor
62 {
63   GObject parent_instance;
64
65   GZlibCompressorFormat format;
66   int level;
67   z_stream zstream;
68   gz_header gzheader;
69   GFileInfo *file_info;
70 };
71
72 static void
73 g_zlib_compressor_set_gzheader (GZlibCompressor *compressor)
74 {
75   const gchar *filename;
76
77   if (compressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP ||
78       compressor->file_info == NULL)
79     return;
80
81   memset (&compressor->gzheader, 0, sizeof (gz_header));
82   compressor->gzheader.os = 0x03; /* Unix */
83
84   filename = g_file_info_get_name (compressor->file_info);
85   compressor->gzheader.name = (Bytef *) filename;
86   compressor->gzheader.name_max = filename ? strlen (filename) + 1 : 0;
87
88   compressor->gzheader.time =
89       (uLong) g_file_info_get_attribute_uint64 (compressor->file_info,
90                                                 G_FILE_ATTRIBUTE_TIME_MODIFIED);
91
92   if (deflateSetHeader (&compressor->zstream, &compressor->gzheader) != Z_OK)
93     g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
94 }
95
96 G_DEFINE_TYPE_WITH_CODE (GZlibCompressor, g_zlib_compressor, G_TYPE_OBJECT,
97                          G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
98                                                 g_zlib_compressor_iface_init))
99
100 static void
101 g_zlib_compressor_finalize (GObject *object)
102 {
103   GZlibCompressor *compressor;
104
105   compressor = G_ZLIB_COMPRESSOR (object);
106
107   deflateEnd (&compressor->zstream);
108
109   if (compressor->file_info)
110     g_object_unref (compressor->file_info);
111
112   G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object);
113 }
114
115
116 static void
117 g_zlib_compressor_set_property (GObject      *object,
118                                   guint         prop_id,
119                                   const GValue *value,
120                                   GParamSpec   *pspec)
121 {
122   GZlibCompressor *compressor;
123
124   compressor = G_ZLIB_COMPRESSOR (object);
125
126   switch (prop_id)
127     {
128     case PROP_FORMAT:
129       compressor->format = g_value_get_enum (value);
130       break;
131
132     case PROP_LEVEL:
133       compressor->level = g_value_get_int (value);
134       break;
135
136     case PROP_FILE_INFO:
137       g_zlib_compressor_set_file_info (compressor, g_value_get_object (value));
138       break;
139
140     default:
141       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142       break;
143     }
144
145 }
146
147 static void
148 g_zlib_compressor_get_property (GObject    *object,
149                                   guint       prop_id,
150                                   GValue     *value,
151                                   GParamSpec *pspec)
152 {
153   GZlibCompressor *compressor;
154
155   compressor = G_ZLIB_COMPRESSOR (object);
156
157   switch (prop_id)
158     {
159     case PROP_FORMAT:
160       g_value_set_enum (value, compressor->format);
161       break;
162
163     case PROP_LEVEL:
164       g_value_set_int (value, compressor->level);
165       break;
166
167     case PROP_FILE_INFO:
168       g_value_set_object (value, compressor->file_info);
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_compressor_init (GZlibCompressor *compressor)
179 {
180 }
181
182 static void
183 g_zlib_compressor_constructed (GObject *object)
184 {
185   GZlibCompressor *compressor;
186   int res;
187
188   compressor = G_ZLIB_COMPRESSOR (object);
189
190   if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
191     {
192       /* + 16 for gzip */
193       res = deflateInit2 (&compressor->zstream,
194                           compressor->level, Z_DEFLATED,
195                           MAX_WBITS + 16, 8,
196                           Z_DEFAULT_STRATEGY);
197     }
198   else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
199     {
200       /* negative wbits for raw */
201       res = deflateInit2 (&compressor->zstream,
202                           compressor->level, Z_DEFLATED,
203                           -MAX_WBITS, 8,
204                           Z_DEFAULT_STRATEGY);
205     }
206   else /* ZLIB */
207     res = deflateInit (&compressor->zstream, compressor->level);
208
209   if (res == Z_MEM_ERROR )
210     g_error ("GZlibCompressor: Not enough memory for zlib use");
211
212   if (res != Z_OK)
213     g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
214
215   g_zlib_compressor_set_gzheader (compressor);
216 }
217
218 static void
219 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
220 {
221   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
222
223   gobject_class->finalize = g_zlib_compressor_finalize;
224   gobject_class->constructed = g_zlib_compressor_constructed;
225   gobject_class->get_property = g_zlib_compressor_get_property;
226   gobject_class->set_property = g_zlib_compressor_set_property;
227
228   g_object_class_install_property (gobject_class,
229                                    PROP_FORMAT,
230                                    g_param_spec_enum ("format",
231                                                       P_("compression format"),
232                                                       P_("The format of the compressed data"),
233                                                       G_TYPE_ZLIB_COMPRESSOR_FORMAT,
234                                                       G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
235                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
236                                                       G_PARAM_STATIC_STRINGS));
237   g_object_class_install_property (gobject_class,
238                                    PROP_LEVEL,
239                                    g_param_spec_int ("level",
240                                                      P_("compression level"),
241                                                      P_("The level of compression from 0 (no compression) to 9 (most compression), -1 for the default level"),
242                                                      -1, 9,
243                                                      -1,
244                                                      G_PARAM_READWRITE |
245                                                      G_PARAM_CONSTRUCT_ONLY |
246                                                      G_PARAM_STATIC_STRINGS));
247
248   /**
249    * GZlibCompressor:file-info:
250    *
251    * If set to a non-%NULL #GFileInfo object, and #GZlibCompressor:format is
252    * %G_ZLIB_COMPRESSOR_FORMAT_GZIP, the compressor will write the file name
253    * and modification time from the file info to the the GZIP header.
254    *
255    * Since: 2.26
256    */
257   g_object_class_install_property (gobject_class,
258                                    PROP_FILE_INFO,
259                                    g_param_spec_object ("file-info",
260                                                        P_("file info"),
261                                                        P_("File info"),
262                                                        G_TYPE_FILE_INFO,
263                                                        G_PARAM_READWRITE |
264                                                        G_PARAM_STATIC_STRINGS));
265 }
266
267 /**
268  * g_zlib_compressor_new:
269  * @format: The format to use for the compressed data
270  * @level: compression level (0-9), -1 for default
271  *
272  * Creates a new #GZlibCompressor.
273  *
274  * Returns: a new #GZlibCompressor
275  *
276  * Since: 2.24
277  **/
278 GZlibCompressor *
279 g_zlib_compressor_new (GZlibCompressorFormat format,
280                        int level)
281 {
282   GZlibCompressor *compressor;
283
284   compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
285                              "format", format,
286                              "level", level,
287                              NULL);
288
289   return compressor;
290 }
291
292 /**
293  * g_zlib_compressor_get_file_info:
294  * @compressor: a #GZlibCompressor
295  *
296  * Returns the #GZlibCompressor:file-info property.
297  *
298  * Returns: (transfer none): a #GFileInfo, or %NULL
299  *
300  * Since: 2.26
301  */
302 GFileInfo *
303 g_zlib_compressor_get_file_info (GZlibCompressor *compressor)
304 {
305   g_return_val_if_fail (G_IS_ZLIB_COMPRESSOR (compressor), NULL);
306
307   return compressor->file_info;
308 }
309
310 /**
311  * g_zlib_compressor_set_file_info:
312  * @compressor: a #GZlibCompressor
313  * @file_info: (allow-none): a #GFileInfo
314  *
315  * Sets @file_info in @compressor. If non-%NULL, and @compressor's
316  * #GZlibCompressor:format property is %G_ZLIB_COMPRESSOR_FORMAT_GZIP,
317  * it will be used to set the file name and modification time in
318  * the GZIP header of the compressed data.
319  *
320  * Note: it is an error to call this function while a compression is in
321  * progress; it may only be called immediately after creation of @compressor,
322  * or after resetting it with g_converter_reset().
323  *
324  * Since: 2.26
325  */
326 void
327 g_zlib_compressor_set_file_info (GZlibCompressor *compressor,
328                                  GFileInfo       *file_info)
329 {
330   g_return_if_fail (G_IS_ZLIB_COMPRESSOR (compressor));
331
332   if (file_info == compressor->file_info)
333     return;
334
335   if (compressor->file_info)
336     g_object_unref (compressor->file_info);
337   if (file_info)
338     g_object_ref (file_info);
339   compressor->file_info = file_info;
340   g_object_notify (G_OBJECT (compressor), "file-info");
341
342   g_zlib_compressor_set_gzheader (compressor);
343 }
344
345 static void
346 g_zlib_compressor_reset (GConverter *converter)
347 {
348   GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
349   int res;
350
351   res = deflateReset (&compressor->zstream);
352   if (res != Z_OK)
353     g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
354
355   /* deflateReset reset the header too, so re-set it */
356   g_zlib_compressor_set_gzheader (compressor);
357 }
358
359 static GConverterResult
360 g_zlib_compressor_convert (GConverter *converter,
361                            const void *inbuf,
362                            gsize       inbuf_size,
363                            void       *outbuf,
364                            gsize       outbuf_size,
365                            GConverterFlags flags,
366                            gsize      *bytes_read,
367                            gsize      *bytes_written,
368                            GError    **error)
369 {
370   GZlibCompressor *compressor;
371   int res;
372   int flush;
373
374   compressor = G_ZLIB_COMPRESSOR (converter);
375
376   compressor->zstream.next_in = (void *)inbuf;
377   compressor->zstream.avail_in = inbuf_size;
378
379   compressor->zstream.next_out = outbuf;
380   compressor->zstream.avail_out = outbuf_size;
381
382   flush = Z_NO_FLUSH;
383   if (flags & G_CONVERTER_INPUT_AT_END)
384     flush = Z_FINISH;
385   else if (flags & G_CONVERTER_FLUSH)
386     flush = Z_SYNC_FLUSH;
387
388   res = deflate (&compressor->zstream, flush);
389
390   if (res == Z_MEM_ERROR)
391     {
392       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
393                            _("Not enough memory"));
394       return G_CONVERTER_ERROR;
395     }
396
397     if (res == Z_STREAM_ERROR)
398     {
399       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
400                    _("Internal error: %s"), compressor->zstream.msg);
401       return G_CONVERTER_ERROR;
402     }
403
404     if (res == Z_BUF_ERROR)
405       {
406         if (flags & G_CONVERTER_FLUSH)
407           return G_CONVERTER_FLUSHED;
408
409         /* We do have output space, so this should only happen if we
410            have no input but need some */
411
412         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
413                              _("Need more input"));
414         return G_CONVERTER_ERROR;
415       }
416
417   if (res == Z_OK || res == Z_STREAM_END)
418     {
419       *bytes_read = inbuf_size - compressor->zstream.avail_in;
420       *bytes_written = outbuf_size - compressor->zstream.avail_out;
421
422       if (res == Z_STREAM_END)
423         return G_CONVERTER_FINISHED;
424       return G_CONVERTER_CONVERTED;
425     }
426
427   g_assert_not_reached ();
428 }
429
430 static void
431 g_zlib_compressor_iface_init (GConverterIface *iface)
432 {
433   iface->convert = g_zlib_compressor_convert;
434   iface->reset = g_zlib_compressor_reset;
435 }