1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Red Hat, Inc.
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.
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.
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/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "gzlibcompressor.h"
29 #include "gfileinfo.h"
32 #include "gioenumtypes.h"
44 * SECTION:gzcompressor
45 * @short_description: Zlib compressor
48 * #GZlibCompressor is an implementation of #GConverter that
49 * compresses data using zlib.
52 static void g_zlib_compressor_iface_init (GConverterIface *iface);
59 struct _GZlibCompressor
61 GObject parent_instance;
63 GZlibCompressorFormat format;
71 g_zlib_compressor_set_gzheader (GZlibCompressor *compressor)
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;
77 if (compressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP ||
78 compressor->file_info == NULL)
81 memset (&compressor->gzheader, 0, sizeof (gz_header));
82 compressor->gzheader.os = 0x03; /* Unix */
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;
88 compressor->gzheader.time =
89 (uLong) g_file_info_get_attribute_uint64 (compressor->file_info,
90 G_FILE_ATTRIBUTE_TIME_MODIFIED);
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 */
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))
102 g_zlib_compressor_finalize (GObject *object)
104 GZlibCompressor *compressor;
106 compressor = G_ZLIB_COMPRESSOR (object);
108 deflateEnd (&compressor->zstream);
110 if (compressor->file_info)
111 g_object_unref (compressor->file_info);
113 G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object);
118 g_zlib_compressor_set_property (GObject *object,
123 GZlibCompressor *compressor;
125 compressor = G_ZLIB_COMPRESSOR (object);
130 compressor->format = g_value_get_enum (value);
134 compressor->level = g_value_get_int (value);
138 g_zlib_compressor_set_file_info (compressor, g_value_get_object (value));
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149 g_zlib_compressor_get_property (GObject *object,
154 GZlibCompressor *compressor;
156 compressor = G_ZLIB_COMPRESSOR (object);
161 g_value_set_enum (value, compressor->format);
165 g_value_set_int (value, compressor->level);
169 g_value_set_object (value, compressor->file_info);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 g_zlib_compressor_init (GZlibCompressor *compressor)
184 g_zlib_compressor_constructed (GObject *object)
186 GZlibCompressor *compressor;
189 compressor = G_ZLIB_COMPRESSOR (object);
191 if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
194 res = deflateInit2 (&compressor->zstream,
195 compressor->level, Z_DEFLATED,
199 else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
201 /* negative wbits for raw */
202 res = deflateInit2 (&compressor->zstream,
203 compressor->level, Z_DEFLATED,
208 res = deflateInit (&compressor->zstream, compressor->level);
210 if (res == Z_MEM_ERROR )
211 g_error ("GZlibCompressor: Not enough memory for zlib use");
214 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
216 g_zlib_compressor_set_gzheader (compressor);
220 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
222 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
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;
229 g_object_class_install_property (gobject_class,
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,
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"),
246 G_PARAM_CONSTRUCT_ONLY |
247 G_PARAM_STATIC_STRINGS));
250 * GZlibCompressor:file-info:
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.
258 g_object_class_install_property (gobject_class,
260 g_param_spec_object ("file-info",
265 G_PARAM_STATIC_STRINGS));
269 * g_zlib_compressor_new:
270 * @format: The format to use for the compressed data
271 * @level: compression level (0-9), -1 for default
273 * Creates a new #GZlibCompressor.
275 * Returns: a new #GZlibCompressor
280 g_zlib_compressor_new (GZlibCompressorFormat format,
283 GZlibCompressor *compressor;
285 compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
294 * g_zlib_compressor_get_file_info:
295 * @compressor: a #GZlibCompressor
297 * Returns the #GZlibCompressor:file-info property.
299 * Returns: (transfer none): a #GFileInfo, or %NULL
304 g_zlib_compressor_get_file_info (GZlibCompressor *compressor)
306 g_return_val_if_fail (G_IS_ZLIB_COMPRESSOR (compressor), NULL);
308 return compressor->file_info;
312 * g_zlib_compressor_set_file_info:
313 * @compressor: a #GZlibCompressor
314 * @file_info: (allow-none): a #GFileInfo
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.
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().
328 g_zlib_compressor_set_file_info (GZlibCompressor *compressor,
329 GFileInfo *file_info)
331 g_return_if_fail (G_IS_ZLIB_COMPRESSOR (compressor));
333 if (file_info == compressor->file_info)
336 if (compressor->file_info)
337 g_object_unref (compressor->file_info);
339 g_object_ref (file_info);
340 compressor->file_info = file_info;
341 g_object_notify (G_OBJECT (compressor), "file-info");
343 g_zlib_compressor_set_gzheader (compressor);
347 g_zlib_compressor_reset (GConverter *converter)
349 GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
352 res = deflateReset (&compressor->zstream);
354 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
356 /* deflateReset reset the header too, so re-set it */
357 g_zlib_compressor_set_gzheader (compressor);
360 static GConverterResult
361 g_zlib_compressor_convert (GConverter *converter,
366 GConverterFlags flags,
368 gsize *bytes_written,
371 GZlibCompressor *compressor;
375 compressor = G_ZLIB_COMPRESSOR (converter);
377 compressor->zstream.next_in = (void *)inbuf;
378 compressor->zstream.avail_in = inbuf_size;
380 compressor->zstream.next_out = outbuf;
381 compressor->zstream.avail_out = outbuf_size;
384 if (flags & G_CONVERTER_INPUT_AT_END)
386 else if (flags & G_CONVERTER_FLUSH)
387 flush = Z_SYNC_FLUSH;
389 res = deflate (&compressor->zstream, flush);
391 if (res == Z_MEM_ERROR)
393 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
394 _("Not enough memory"));
395 return G_CONVERTER_ERROR;
398 if (res == Z_STREAM_ERROR)
400 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
401 _("Internal error: %s"), compressor->zstream.msg);
402 return G_CONVERTER_ERROR;
405 if (res == Z_BUF_ERROR)
407 if (flags & G_CONVERTER_FLUSH)
408 return G_CONVERTER_FLUSHED;
410 /* We do have output space, so this should only happen if we
411 have no input but need some */
413 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
414 _("Need more input"));
415 return G_CONVERTER_ERROR;
418 if (res == Z_OK || res == Z_STREAM_END)
420 *bytes_read = inbuf_size - compressor->zstream.avail_in;
421 *bytes_written = outbuf_size - compressor->zstream.avail_out;
423 if (res == Z_STREAM_END)
424 return G_CONVERTER_FINISHED;
425 return G_CONVERTER_CONVERTED;
428 g_assert_not_reached ();
432 g_zlib_compressor_iface_init (GConverterIface *iface)
434 iface->convert = g_zlib_compressor_convert;
435 iface->reset = g_zlib_compressor_reset;