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>
29 #include "gzlibcompressor.h"
34 #include "gioenumtypes.h"
45 * SECTION:gzcompressor
46 * @short_description: Zlib compressor
49 * #GZlibCompressor is an implementation of #GCompressor that
50 * decompresses data compressed with 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,
161 res = deflateInit (&compressor->zstream, compressor->level);
163 if (res == Z_MEM_ERROR )
164 g_error ("GZlibCompressor: Not enough memory for zlib use");
167 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
171 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
173 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
175 gobject_class->finalize = g_zlib_compressor_finalize;
176 gobject_class->constructed = g_zlib_compressor_constructed;
177 gobject_class->get_property = g_zlib_compressor_get_property;
178 gobject_class->set_property = g_zlib_compressor_set_property;
180 g_object_class_install_property (gobject_class,
182 g_param_spec_enum ("format",
183 P_("compression format"),
184 P_("The format of the compressed data"),
185 G_TYPE_ZLIB_COMPRESSOR_FORMAT,
186 G_ZLIB_COMPRESSOR_FORMAT_RAW,
187 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
188 G_PARAM_STATIC_STRINGS));
189 g_object_class_install_property (gobject_class,
191 g_param_spec_int ("level",
192 P_("compression level"),
193 P_("The level of compression from 0 (no compression) to 9 (most compression), -1 for the default level"),
197 G_PARAM_CONSTRUCT_ONLY |
198 G_PARAM_STATIC_STRINGS));
202 * g_zlib_compressor_new:
203 * @format: The format to use for the compressed data
204 * @level: compression level (0-9), -1 for default
206 * Creates a new #GZlibCompressor.
208 * Returns: a new #GZlibCompressor
213 g_zlib_compressor_new (GZlibCompressorFormat format,
216 GZlibCompressor *compressor;
218 compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
227 g_zlib_compressor_reset (GConverter *converter)
229 GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
232 res = deflateReset (&compressor->zstream);
234 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
237 static GConverterResult
238 g_zlib_compressor_convert (GConverter *converter,
243 GConverterFlags flags,
245 gsize *bytes_written,
248 GZlibCompressor *compressor;
252 compressor = G_ZLIB_COMPRESSOR (converter);
254 compressor->zstream.next_in = (void *)inbuf;
255 compressor->zstream.avail_in = inbuf_size;
257 compressor->zstream.next_out = outbuf;
258 compressor->zstream.avail_out = outbuf_size;
261 if (flags & G_CONVERTER_INPUT_AT_END)
263 else if (flags & G_CONVERTER_FLUSH)
264 flush = Z_SYNC_FLUSH;
266 res = deflate (&compressor->zstream, flush);
268 if (res == Z_MEM_ERROR)
270 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
271 _("Not enough memory"));
272 return G_CONVERTER_ERROR;
275 if (res == Z_STREAM_ERROR)
277 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
278 _("Internal error: %s"), compressor->zstream.msg);
279 return G_CONVERTER_ERROR;
282 if (res == Z_BUF_ERROR)
284 if (flags & G_CONVERTER_FLUSH)
285 return G_CONVERTER_FLUSHED;
287 /* We do have output space, so this should only happen if we
288 have no input but need some */
290 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
291 _("Need more input"));
292 return G_CONVERTER_ERROR;
295 if (res == Z_OK || res == Z_STREAM_END)
297 *bytes_read = inbuf_size - compressor->zstream.avail_in;
298 *bytes_written = outbuf_size - compressor->zstream.avail_out;
300 if (res == Z_STREAM_END)
301 return G_CONVERTER_FINISHED;
302 return G_CONVERTER_CONVERTED;
305 g_assert_not_reached ();
309 g_zlib_compressor_iface_init (GConverterIface *iface)
311 iface->convert = g_zlib_compressor_convert;
312 iface->reset = g_zlib_compressor_reset;
315 #define __G_ZLIB_COMPRESSOR_C__
316 #include "gioaliasdef.c"