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"
45 * SECTION:gzcompressor
46 * @short_description: Zlib compressor
49 * #GZlibCompressor is an implementation of #GConverter that
50 * compresses data using zlib.
53 static void g_zlib_compressor_iface_init (GConverterIface *iface);
60 struct _GZlibCompressor
62 GObject parent_instance;
64 GZlibCompressorFormat format;
69 G_DEFINE_TYPE_WITH_CODE (GZlibCompressor, g_zlib_compressor, G_TYPE_OBJECT,
70 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
71 g_zlib_compressor_iface_init))
74 g_zlib_compressor_finalize (GObject *object)
76 GZlibCompressor *compressor;
78 compressor = G_ZLIB_COMPRESSOR (object);
80 deflateEnd (&compressor->zstream);
82 G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object);
87 g_zlib_compressor_set_property (GObject *object,
92 GZlibCompressor *compressor;
94 compressor = G_ZLIB_COMPRESSOR (object);
99 compressor->format = g_value_get_enum (value);
103 compressor->level = g_value_get_int (value);
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114 g_zlib_compressor_get_property (GObject *object,
119 GZlibCompressor *compressor;
121 compressor = G_ZLIB_COMPRESSOR (object);
126 g_value_set_enum (value, compressor->format);
130 g_value_set_int (value, compressor->level);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140 g_zlib_compressor_init (GZlibCompressor *compressor)
145 g_zlib_compressor_constructed (GObject *object)
147 GZlibCompressor *compressor;
150 compressor = G_ZLIB_COMPRESSOR (object);
152 if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
155 res = deflateInit2 (&compressor->zstream,
156 compressor->level, Z_DEFLATED,
160 else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
162 /* negative wbits for raw */
163 res = deflateInit2 (&compressor->zstream,
164 compressor->level, Z_DEFLATED,
169 res = deflateInit (&compressor->zstream, compressor->level);
171 if (res == Z_MEM_ERROR )
172 g_error ("GZlibCompressor: Not enough memory for zlib use");
175 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
179 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
181 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
183 gobject_class->finalize = g_zlib_compressor_finalize;
184 gobject_class->constructed = g_zlib_compressor_constructed;
185 gobject_class->get_property = g_zlib_compressor_get_property;
186 gobject_class->set_property = g_zlib_compressor_set_property;
188 g_object_class_install_property (gobject_class,
190 g_param_spec_enum ("format",
191 P_("compression format"),
192 P_("The format of the compressed data"),
193 G_TYPE_ZLIB_COMPRESSOR_FORMAT,
194 G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
195 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
196 G_PARAM_STATIC_STRINGS));
197 g_object_class_install_property (gobject_class,
199 g_param_spec_int ("level",
200 P_("compression level"),
201 P_("The level of compression from 0 (no compression) to 9 (most compression), -1 for the default level"),
205 G_PARAM_CONSTRUCT_ONLY |
206 G_PARAM_STATIC_STRINGS));
210 * g_zlib_compressor_new:
211 * @format: The format to use for the compressed data
212 * @level: compression level (0-9), -1 for default
214 * Creates a new #GZlibCompressor.
216 * Returns: a new #GZlibCompressor
221 g_zlib_compressor_new (GZlibCompressorFormat format,
224 GZlibCompressor *compressor;
226 compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
235 g_zlib_compressor_reset (GConverter *converter)
237 GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
240 res = deflateReset (&compressor->zstream);
242 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
245 static GConverterResult
246 g_zlib_compressor_convert (GConverter *converter,
251 GConverterFlags flags,
253 gsize *bytes_written,
256 GZlibCompressor *compressor;
260 compressor = G_ZLIB_COMPRESSOR (converter);
262 compressor->zstream.next_in = (void *)inbuf;
263 compressor->zstream.avail_in = inbuf_size;
265 compressor->zstream.next_out = outbuf;
266 compressor->zstream.avail_out = outbuf_size;
269 if (flags & G_CONVERTER_INPUT_AT_END)
271 else if (flags & G_CONVERTER_FLUSH)
272 flush = Z_SYNC_FLUSH;
274 res = deflate (&compressor->zstream, flush);
276 if (res == Z_MEM_ERROR)
278 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
279 _("Not enough memory"));
280 return G_CONVERTER_ERROR;
283 if (res == Z_STREAM_ERROR)
285 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
286 _("Internal error: %s"), compressor->zstream.msg);
287 return G_CONVERTER_ERROR;
290 if (res == Z_BUF_ERROR)
292 if (flags & G_CONVERTER_FLUSH)
293 return G_CONVERTER_FLUSHED;
295 /* We do have output space, so this should only happen if we
296 have no input but need some */
298 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
299 _("Need more input"));
300 return G_CONVERTER_ERROR;
303 if (res == Z_OK || res == Z_STREAM_END)
305 *bytes_read = inbuf_size - compressor->zstream.avail_in;
306 *bytes_written = outbuf_size - compressor->zstream.avail_out;
308 if (res == Z_STREAM_END)
309 return G_CONVERTER_FINISHED;
310 return G_CONVERTER_CONVERTED;
313 g_assert_not_reached ();
317 g_zlib_compressor_iface_init (GConverterIface *iface)
319 iface->convert = g_zlib_compressor_convert;
320 iface->reset = g_zlib_compressor_reset;
323 #define __G_ZLIB_COMPRESSOR_C__
324 #include "gioaliasdef.c"