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"
33 #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;
68 G_DEFINE_TYPE_WITH_CODE (GZlibCompressor, g_zlib_compressor, G_TYPE_OBJECT,
69 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
70 g_zlib_compressor_iface_init))
73 g_zlib_compressor_finalize (GObject *object)
75 GZlibCompressor *compressor;
77 compressor = G_ZLIB_COMPRESSOR (object);
79 deflateEnd (&compressor->zstream);
81 G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object);
86 g_zlib_compressor_set_property (GObject *object,
91 GZlibCompressor *compressor;
93 compressor = G_ZLIB_COMPRESSOR (object);
98 compressor->format = g_value_get_enum (value);
102 compressor->level = g_value_get_int (value);
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 g_zlib_compressor_get_property (GObject *object,
118 GZlibCompressor *compressor;
120 compressor = G_ZLIB_COMPRESSOR (object);
125 g_value_set_enum (value, compressor->format);
129 g_value_set_int (value, compressor->level);
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139 g_zlib_compressor_init (GZlibCompressor *compressor)
144 g_zlib_compressor_constructed (GObject *object)
146 GZlibCompressor *compressor;
149 compressor = G_ZLIB_COMPRESSOR (object);
151 if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
154 res = deflateInit2 (&compressor->zstream,
155 compressor->level, Z_DEFLATED,
159 else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
161 /* negative wbits for raw */
162 res = deflateInit2 (&compressor->zstream,
163 compressor->level, Z_DEFLATED,
168 res = deflateInit (&compressor->zstream, compressor->level);
170 if (res == Z_MEM_ERROR )
171 g_error ("GZlibCompressor: Not enough memory for zlib use");
174 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
178 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
180 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
182 gobject_class->finalize = g_zlib_compressor_finalize;
183 gobject_class->constructed = g_zlib_compressor_constructed;
184 gobject_class->get_property = g_zlib_compressor_get_property;
185 gobject_class->set_property = g_zlib_compressor_set_property;
187 g_object_class_install_property (gobject_class,
189 g_param_spec_enum ("format",
190 P_("compression format"),
191 P_("The format of the compressed data"),
192 G_TYPE_ZLIB_COMPRESSOR_FORMAT,
193 G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
194 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
195 G_PARAM_STATIC_STRINGS));
196 g_object_class_install_property (gobject_class,
198 g_param_spec_int ("level",
199 P_("compression level"),
200 P_("The level of compression from 0 (no compression) to 9 (most compression), -1 for the default level"),
204 G_PARAM_CONSTRUCT_ONLY |
205 G_PARAM_STATIC_STRINGS));
209 * g_zlib_compressor_new:
210 * @format: The format to use for the compressed data
211 * @level: compression level (0-9), -1 for default
213 * Creates a new #GZlibCompressor.
215 * Returns: a new #GZlibCompressor
220 g_zlib_compressor_new (GZlibCompressorFormat format,
223 GZlibCompressor *compressor;
225 compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
234 g_zlib_compressor_reset (GConverter *converter)
236 GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
239 res = deflateReset (&compressor->zstream);
241 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
244 static GConverterResult
245 g_zlib_compressor_convert (GConverter *converter,
250 GConverterFlags flags,
252 gsize *bytes_written,
255 GZlibCompressor *compressor;
259 compressor = G_ZLIB_COMPRESSOR (converter);
261 compressor->zstream.next_in = (void *)inbuf;
262 compressor->zstream.avail_in = inbuf_size;
264 compressor->zstream.next_out = outbuf;
265 compressor->zstream.avail_out = outbuf_size;
268 if (flags & G_CONVERTER_INPUT_AT_END)
270 else if (flags & G_CONVERTER_FLUSH)
271 flush = Z_SYNC_FLUSH;
273 res = deflate (&compressor->zstream, flush);
275 if (res == Z_MEM_ERROR)
277 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
278 _("Not enough memory"));
279 return G_CONVERTER_ERROR;
282 if (res == Z_STREAM_ERROR)
284 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
285 _("Internal error: %s"), compressor->zstream.msg);
286 return G_CONVERTER_ERROR;
289 if (res == Z_BUF_ERROR)
291 if (flags & G_CONVERTER_FLUSH)
292 return G_CONVERTER_FLUSHED;
294 /* We do have output space, so this should only happen if we
295 have no input but need some */
297 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
298 _("Need more input"));
299 return G_CONVERTER_ERROR;
302 if (res == Z_OK || res == Z_STREAM_END)
304 *bytes_read = inbuf_size - compressor->zstream.avail_in;
305 *bytes_written = outbuf_size - compressor->zstream.avail_out;
307 if (res == Z_STREAM_END)
308 return G_CONVERTER_FINISHED;
309 return G_CONVERTER_CONVERTED;
312 g_assert_not_reached ();
316 g_zlib_compressor_iface_init (GConverterIface *iface)
318 iface->convert = g_zlib_compressor_convert;
319 iface->reset = g_zlib_compressor_reset;