Updated FSF's address
[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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include "gzlibcompressor.h"
24
25 #include <errno.h>
26 #include <zlib.h>
27 #include <string.h>
28
29 #include "gfileinfo.h"
30 #include "gioerror.h"
31 #include "gioenums.h"
32 #include "gioenumtypes.h"
33 #include "glibintl.h"
34
35
36 enum {
37   PROP_0,
38   PROP_FORMAT,
39   PROP_LEVEL,
40   PROP_FILE_INFO
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   gz_header gzheader;
67   GFileInfo *file_info;
68 };
69
70 static void
71 g_zlib_compressor_set_gzheader (GZlibCompressor *compressor)
72 {
73   /* On win32, these functions were not exported before 1.2.4 */
74 #if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240
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 #endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */
95 }
96
97 G_DEFINE_TYPE_WITH_CODE (GZlibCompressor, g_zlib_compressor, G_TYPE_OBJECT,
98                          G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
99                                                 g_zlib_compressor_iface_init))
100
101 static void
102 g_zlib_compressor_finalize (GObject *object)
103 {
104   GZlibCompressor *compressor;
105
106   compressor = G_ZLIB_COMPRESSOR (object);
107
108   deflateEnd (&compressor->zstream);
109
110   if (compressor->file_info)
111     g_object_unref (compressor->file_info);
112
113   G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object);
114 }
115
116
117 static void
118 g_zlib_compressor_set_property (GObject      *object,
119                                   guint         prop_id,
120                                   const GValue *value,
121                                   GParamSpec   *pspec)
122 {
123   GZlibCompressor *compressor;
124
125   compressor = G_ZLIB_COMPRESSOR (object);
126
127   switch (prop_id)
128     {
129     case PROP_FORMAT:
130       compressor->format = g_value_get_enum (value);
131       break;
132
133     case PROP_LEVEL:
134       compressor->level = g_value_get_int (value);
135       break;
136
137     case PROP_FILE_INFO:
138       g_zlib_compressor_set_file_info (compressor, g_value_get_object (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_compressor_get_property (GObject    *object,
150                                   guint       prop_id,
151                                   GValue     *value,
152                                   GParamSpec *pspec)
153 {
154   GZlibCompressor *compressor;
155
156   compressor = G_ZLIB_COMPRESSOR (object);
157
158   switch (prop_id)
159     {
160     case PROP_FORMAT:
161       g_value_set_enum (value, compressor->format);
162       break;
163
164     case PROP_LEVEL:
165       g_value_set_int (value, compressor->level);
166       break;
167
168     case PROP_FILE_INFO:
169       g_value_set_object (value, compressor->file_info);
170       break;
171
172     default:
173       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174       break;
175     }
176 }
177
178 static void
179 g_zlib_compressor_init (GZlibCompressor *compressor)
180 {
181 }
182
183 static void
184 g_zlib_compressor_constructed (GObject *object)
185 {
186   GZlibCompressor *compressor;
187   int res;
188
189   compressor = G_ZLIB_COMPRESSOR (object);
190
191   if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
192     {
193       /* + 16 for gzip */
194       res = deflateInit2 (&compressor->zstream,
195                           compressor->level, Z_DEFLATED,
196                           MAX_WBITS + 16, 8,
197                           Z_DEFAULT_STRATEGY);
198     }
199   else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
200     {
201       /* negative wbits for raw */
202       res = deflateInit2 (&compressor->zstream,
203                           compressor->level, Z_DEFLATED,
204                           -MAX_WBITS, 8,
205                           Z_DEFAULT_STRATEGY);
206     }
207   else /* ZLIB */
208     res = deflateInit (&compressor->zstream, compressor->level);
209
210   if (res == Z_MEM_ERROR )
211     g_error ("GZlibCompressor: Not enough memory for zlib use");
212
213   if (res != Z_OK)
214     g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
215
216   g_zlib_compressor_set_gzheader (compressor);
217 }
218
219 static void
220 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
221 {
222   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
223
224   gobject_class->finalize = g_zlib_compressor_finalize;
225   gobject_class->constructed = g_zlib_compressor_constructed;
226   gobject_class->get_property = g_zlib_compressor_get_property;
227   gobject_class->set_property = g_zlib_compressor_set_property;
228
229   g_object_class_install_property (gobject_class,
230                                    PROP_FORMAT,
231                                    g_param_spec_enum ("format",
232                                                       P_("compression format"),
233                                                       P_("The format of the compressed data"),
234                                                       G_TYPE_ZLIB_COMPRESSOR_FORMAT,
235                                                       G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
236                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
237                                                       G_PARAM_STATIC_STRINGS));
238   g_object_class_install_property (gobject_class,
239                                    PROP_LEVEL,
240                                    g_param_spec_int ("level",
241                                                      P_("compression level"),
242                                                      P_("The level of compression from 0 (no compression) to 9 (most compression), -1 for the default level"),
243                                                      -1, 9,
244                                                      -1,
245                                                      G_PARAM_READWRITE |
246                                                      G_PARAM_CONSTRUCT_ONLY |
247                                                      G_PARAM_STATIC_STRINGS));
248
249   /**
250    * GZlibCompressor:file-info:
251    *
252    * If set to a non-%NULL #GFileInfo object, and #GZlibCompressor:format is
253    * %G_ZLIB_COMPRESSOR_FORMAT_GZIP, the compressor will write the file name
254    * and modification time from the file info to the GZIP header.
255    *
256    * Since: 2.26
257    */
258   g_object_class_install_property (gobject_class,
259                                    PROP_FILE_INFO,
260                                    g_param_spec_object ("file-info",
261                                                        P_("file info"),
262                                                        P_("File info"),
263                                                        G_TYPE_FILE_INFO,
264                                                        G_PARAM_READWRITE |
265                                                        G_PARAM_STATIC_STRINGS));
266 }
267
268 /**
269  * g_zlib_compressor_new:
270  * @format: The format to use for the compressed data
271  * @level: compression level (0-9), -1 for default
272  *
273  * Creates a new #GZlibCompressor.
274  *
275  * Returns: a new #GZlibCompressor
276  *
277  * Since: 2.24
278  **/
279 GZlibCompressor *
280 g_zlib_compressor_new (GZlibCompressorFormat format,
281                        int level)
282 {
283   GZlibCompressor *compressor;
284
285   compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
286                              "format", format,
287                              "level", level,
288                              NULL);
289
290   return compressor;
291 }
292
293 /**
294  * g_zlib_compressor_get_file_info:
295  * @compressor: a #GZlibCompressor
296  *
297  * Returns the #GZlibCompressor:file-info property.
298  *
299  * Returns: (transfer none): a #GFileInfo, or %NULL
300  *
301  * Since: 2.26
302  */
303 GFileInfo *
304 g_zlib_compressor_get_file_info (GZlibCompressor *compressor)
305 {
306   g_return_val_if_fail (G_IS_ZLIB_COMPRESSOR (compressor), NULL);
307
308   return compressor->file_info;
309 }
310
311 /**
312  * g_zlib_compressor_set_file_info:
313  * @compressor: a #GZlibCompressor
314  * @file_info: (allow-none): a #GFileInfo
315  *
316  * Sets @file_info in @compressor. If non-%NULL, and @compressor's
317  * #GZlibCompressor:format property is %G_ZLIB_COMPRESSOR_FORMAT_GZIP,
318  * it will be used to set the file name and modification time in
319  * the GZIP header of the compressed data.
320  *
321  * Note: it is an error to call this function while a compression is in
322  * progress; it may only be called immediately after creation of @compressor,
323  * or after resetting it with g_converter_reset().
324  *
325  * Since: 2.26
326  */
327 void
328 g_zlib_compressor_set_file_info (GZlibCompressor *compressor,
329                                  GFileInfo       *file_info)
330 {
331   g_return_if_fail (G_IS_ZLIB_COMPRESSOR (compressor));
332
333   if (file_info == compressor->file_info)
334     return;
335
336   if (compressor->file_info)
337     g_object_unref (compressor->file_info);
338   if (file_info)
339     g_object_ref (file_info);
340   compressor->file_info = file_info;
341   g_object_notify (G_OBJECT (compressor), "file-info");
342
343   g_zlib_compressor_set_gzheader (compressor);
344 }
345
346 static void
347 g_zlib_compressor_reset (GConverter *converter)
348 {
349   GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
350   int res;
351
352   res = deflateReset (&compressor->zstream);
353   if (res != Z_OK)
354     g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
355
356   /* deflateReset reset the header too, so re-set it */
357   g_zlib_compressor_set_gzheader (compressor);
358 }
359
360 static GConverterResult
361 g_zlib_compressor_convert (GConverter *converter,
362                            const void *inbuf,
363                            gsize       inbuf_size,
364                            void       *outbuf,
365                            gsize       outbuf_size,
366                            GConverterFlags flags,
367                            gsize      *bytes_read,
368                            gsize      *bytes_written,
369                            GError    **error)
370 {
371   GZlibCompressor *compressor;
372   int res;
373   int flush;
374
375   compressor = G_ZLIB_COMPRESSOR (converter);
376
377   compressor->zstream.next_in = (void *)inbuf;
378   compressor->zstream.avail_in = inbuf_size;
379
380   compressor->zstream.next_out = outbuf;
381   compressor->zstream.avail_out = outbuf_size;
382
383   flush = Z_NO_FLUSH;
384   if (flags & G_CONVERTER_INPUT_AT_END)
385     flush = Z_FINISH;
386   else if (flags & G_CONVERTER_FLUSH)
387     flush = Z_SYNC_FLUSH;
388
389   res = deflate (&compressor->zstream, flush);
390
391   if (res == Z_MEM_ERROR)
392     {
393       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
394                            _("Not enough memory"));
395       return G_CONVERTER_ERROR;
396     }
397
398     if (res == Z_STREAM_ERROR)
399     {
400       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
401                    _("Internal error: %s"), compressor->zstream.msg);
402       return G_CONVERTER_ERROR;
403     }
404
405     if (res == Z_BUF_ERROR)
406       {
407         if (flags & G_CONVERTER_FLUSH)
408           return G_CONVERTER_FLUSHED;
409
410         /* We do have output space, so this should only happen if we
411            have no input but need some */
412
413         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
414                              _("Need more input"));
415         return G_CONVERTER_ERROR;
416       }
417
418   if (res == Z_OK || res == Z_STREAM_END)
419     {
420       *bytes_read = inbuf_size - compressor->zstream.avail_in;
421       *bytes_written = outbuf_size - compressor->zstream.avail_out;
422
423       if (res == Z_STREAM_END)
424         return G_CONVERTER_FINISHED;
425       return G_CONVERTER_CONVERTED;
426     }
427
428   g_assert_not_reached ();
429 }
430
431 static void
432 g_zlib_compressor_iface_init (GConverterIface *iface)
433 {
434   iface->convert = g_zlib_compressor_convert;
435   iface->reset = g_zlib_compressor_reset;
436 }