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