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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "gzlibcompressor.h"
31 #include "gfileinfo.h"
34 #include "gioenumtypes.h"
46 * SECTION:gzcompressor
47 * @short_description: Zlib compressor
50 * #GZlibCompressor is an implementation of #GConverter that
51 * compresses data using zlib.
54 static void g_zlib_compressor_iface_init (GConverterIface *iface);
61 struct _GZlibCompressor
63 GObject parent_instance;
65 GZlibCompressorFormat format;
73 g_zlib_compressor_set_gzheader (GZlibCompressor *compressor)
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;
79 if (compressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP ||
80 compressor->file_info == NULL)
83 memset (&compressor->gzheader, 0, sizeof (gz_header));
84 compressor->gzheader.os = 0x03; /* Unix */
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;
90 compressor->gzheader.time =
91 (uLong) g_file_info_get_attribute_uint64 (compressor->file_info,
92 G_FILE_ATTRIBUTE_TIME_MODIFIED);
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 */
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))
104 g_zlib_compressor_finalize (GObject *object)
106 GZlibCompressor *compressor;
108 compressor = G_ZLIB_COMPRESSOR (object);
110 deflateEnd (&compressor->zstream);
112 if (compressor->file_info)
113 g_object_unref (compressor->file_info);
115 G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object);
120 g_zlib_compressor_set_property (GObject *object,
125 GZlibCompressor *compressor;
127 compressor = G_ZLIB_COMPRESSOR (object);
132 compressor->format = g_value_get_enum (value);
136 compressor->level = g_value_get_int (value);
140 g_zlib_compressor_set_file_info (compressor, g_value_get_object (value));
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 g_zlib_compressor_get_property (GObject *object,
156 GZlibCompressor *compressor;
158 compressor = G_ZLIB_COMPRESSOR (object);
163 g_value_set_enum (value, compressor->format);
167 g_value_set_int (value, compressor->level);
171 g_value_set_object (value, compressor->file_info);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 g_zlib_compressor_init (GZlibCompressor *compressor)
186 g_zlib_compressor_constructed (GObject *object)
188 GZlibCompressor *compressor;
191 compressor = G_ZLIB_COMPRESSOR (object);
193 if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
196 res = deflateInit2 (&compressor->zstream,
197 compressor->level, Z_DEFLATED,
201 else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
203 /* negative wbits for raw */
204 res = deflateInit2 (&compressor->zstream,
205 compressor->level, Z_DEFLATED,
210 res = deflateInit (&compressor->zstream, compressor->level);
212 if (res == Z_MEM_ERROR )
213 g_error ("GZlibCompressor: Not enough memory for zlib use");
216 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
218 g_zlib_compressor_set_gzheader (compressor);
222 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
224 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
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;
231 g_object_class_install_property (gobject_class,
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,
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"),
248 G_PARAM_CONSTRUCT_ONLY |
249 G_PARAM_STATIC_STRINGS));
252 * GZlibCompressor:file-info:
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 the GZIP header.
260 g_object_class_install_property (gobject_class,
262 g_param_spec_object ("file-info",
267 G_PARAM_STATIC_STRINGS));
271 * g_zlib_compressor_new:
272 * @format: The format to use for the compressed data
273 * @level: compression level (0-9), -1 for default
275 * Creates a new #GZlibCompressor.
277 * Returns: a new #GZlibCompressor
282 g_zlib_compressor_new (GZlibCompressorFormat format,
285 GZlibCompressor *compressor;
287 compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
296 * g_zlib_compressor_get_file_info:
297 * @compressor: a #GZlibCompressor
299 * Returns the #GZlibCompressor:file-info property.
301 * Returns: (transfer none): a #GFileInfo, or %NULL
306 g_zlib_compressor_get_file_info (GZlibCompressor *compressor)
308 g_return_val_if_fail (G_IS_ZLIB_COMPRESSOR (compressor), NULL);
310 return compressor->file_info;
314 * g_zlib_compressor_set_file_info:
315 * @compressor: a #GZlibCompressor
316 * @file_info: (allow-none): a #GFileInfo
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.
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().
330 g_zlib_compressor_set_file_info (GZlibCompressor *compressor,
331 GFileInfo *file_info)
333 g_return_if_fail (G_IS_ZLIB_COMPRESSOR (compressor));
335 if (file_info == compressor->file_info)
338 if (compressor->file_info)
339 g_object_unref (compressor->file_info);
341 g_object_ref (file_info);
342 compressor->file_info = file_info;
343 g_object_notify (G_OBJECT (compressor), "file-info");
345 g_zlib_compressor_set_gzheader (compressor);
349 g_zlib_compressor_reset (GConverter *converter)
351 GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
354 res = deflateReset (&compressor->zstream);
356 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
358 /* deflateReset reset the header too, so re-set it */
359 g_zlib_compressor_set_gzheader (compressor);
362 static GConverterResult
363 g_zlib_compressor_convert (GConverter *converter,
368 GConverterFlags flags,
370 gsize *bytes_written,
373 GZlibCompressor *compressor;
377 compressor = G_ZLIB_COMPRESSOR (converter);
379 compressor->zstream.next_in = (void *)inbuf;
380 compressor->zstream.avail_in = inbuf_size;
382 compressor->zstream.next_out = outbuf;
383 compressor->zstream.avail_out = outbuf_size;
386 if (flags & G_CONVERTER_INPUT_AT_END)
388 else if (flags & G_CONVERTER_FLUSH)
389 flush = Z_SYNC_FLUSH;
391 res = deflate (&compressor->zstream, flush);
393 if (res == Z_MEM_ERROR)
395 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
396 _("Not enough memory"));
397 return G_CONVERTER_ERROR;
400 if (res == Z_STREAM_ERROR)
402 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
403 _("Internal error: %s"), compressor->zstream.msg);
404 return G_CONVERTER_ERROR;
407 if (res == Z_BUF_ERROR)
409 if (flags & G_CONVERTER_FLUSH)
410 return G_CONVERTER_FLUSHED;
412 /* We do have output space, so this should only happen if we
413 have no input but need some */
415 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
416 _("Need more input"));
417 return G_CONVERTER_ERROR;
420 if (res == Z_OK || res == Z_STREAM_END)
422 *bytes_read = inbuf_size - compressor->zstream.avail_in;
423 *bytes_written = outbuf_size - compressor->zstream.avail_out;
425 if (res == Z_STREAM_END)
426 return G_CONVERTER_FINISHED;
427 return G_CONVERTER_CONVERTED;
430 g_assert_not_reached ();
434 g_zlib_compressor_iface_init (GConverterIface *iface)
436 iface->convert = g_zlib_compressor_convert;
437 iface->reset = g_zlib_compressor_reset;