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 "gzlibdecompressor.h"
33 #include "gioenumtypes.h"
43 * SECTION:gzdecompressor
44 * @short_description: Zlib decompressor
47 * #GZlibDecompressor is an implementation of #GConverter that
48 * decompresses data compressed with zlib.
51 static void g_zlib_decompressor_iface_init (GConverterIface *iface);
58 struct _GZlibDecompressor
60 GObject parent_instance;
62 GZlibCompressorFormat format;
66 G_DEFINE_TYPE_WITH_CODE (GZlibDecompressor, g_zlib_decompressor, G_TYPE_OBJECT,
67 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
68 g_zlib_decompressor_iface_init))
71 g_zlib_decompressor_finalize (GObject *object)
73 GZlibDecompressor *decompressor;
75 decompressor = G_ZLIB_DECOMPRESSOR (object);
77 inflateEnd (&decompressor->zstream);
79 G_OBJECT_CLASS (g_zlib_decompressor_parent_class)->finalize (object);
84 g_zlib_decompressor_set_property (GObject *object,
89 GZlibDecompressor *decompressor;
91 decompressor = G_ZLIB_DECOMPRESSOR (object);
96 decompressor->format = g_value_get_enum (value);
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 g_zlib_decompressor_get_property (GObject *object,
112 GZlibDecompressor *decompressor;
114 decompressor = G_ZLIB_DECOMPRESSOR (object);
119 g_value_set_enum (value, decompressor->format);
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 g_zlib_decompressor_init (GZlibDecompressor *decompressor)
134 g_zlib_decompressor_constructed (GObject *object)
136 GZlibDecompressor *decompressor;
139 decompressor = G_ZLIB_DECOMPRESSOR (object);
141 if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
144 res = inflateInit2 (&decompressor->zstream, MAX_WBITS + 16);
146 else if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
148 /* Negative for gzip */
149 res = inflateInit2 (&decompressor->zstream, -MAX_WBITS);
152 res = inflateInit (&decompressor->zstream);
154 if (res == Z_MEM_ERROR )
155 g_error ("GZlibDecompressor: Not enough memory for zlib use");
158 g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
162 g_zlib_decompressor_class_init (GZlibDecompressorClass *klass)
164 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
166 gobject_class->finalize = g_zlib_decompressor_finalize;
167 gobject_class->constructed = g_zlib_decompressor_constructed;
168 gobject_class->get_property = g_zlib_decompressor_get_property;
169 gobject_class->set_property = g_zlib_decompressor_set_property;
171 g_object_class_install_property (gobject_class,
173 g_param_spec_enum ("format",
174 P_("compression format"),
175 P_("The format of the compressed data"),
176 G_TYPE_ZLIB_COMPRESSOR_FORMAT,
177 G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
178 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
179 G_PARAM_STATIC_STRINGS));
183 * g_zlib_decompressor_new:
184 * @format: The format to use for the compressed data
186 * Creates a new #GZlibDecompressor.
188 * Returns: a new #GZlibDecompressor
193 g_zlib_decompressor_new (GZlibCompressorFormat format)
195 GZlibDecompressor *decompressor;
197 decompressor = g_object_new (G_TYPE_ZLIB_DECOMPRESSOR,
205 g_zlib_decompressor_reset (GConverter *converter)
207 GZlibDecompressor *decompressor = G_ZLIB_DECOMPRESSOR (converter);
210 res = inflateReset (&decompressor->zstream);
212 g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
215 static GConverterResult
216 g_zlib_decompressor_convert (GConverter *converter,
221 GConverterFlags flags,
223 gsize *bytes_written,
226 GZlibDecompressor *decompressor;
229 decompressor = G_ZLIB_DECOMPRESSOR (converter);
231 decompressor->zstream.next_in = (void *)inbuf;
232 decompressor->zstream.avail_in = inbuf_size;
234 decompressor->zstream.next_out = outbuf;
235 decompressor->zstream.avail_out = outbuf_size;
237 res = inflate (&decompressor->zstream, Z_NO_FLUSH);
239 if (res == Z_DATA_ERROR || res == Z_NEED_DICT)
241 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
242 _("Invalid compressed data"));
243 return G_CONVERTER_ERROR;
246 if (res == Z_MEM_ERROR)
248 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
249 _("Not enough memory"));
250 return G_CONVERTER_ERROR;
253 if (res == Z_STREAM_ERROR)
255 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
256 _("Internal error: %s"), decompressor->zstream.msg);
257 return G_CONVERTER_ERROR;
260 if (res == Z_BUF_ERROR)
262 if (flags & G_CONVERTER_FLUSH)
263 return G_CONVERTER_FLUSHED;
265 /* Z_FINISH not set, so this means no progress could be made */
266 /* We do have output space, so this should only happen if we
267 have no input but need some */
269 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
270 _("Need more input"));
271 return G_CONVERTER_ERROR;
274 if (res == Z_OK || res == Z_STREAM_END)
276 *bytes_read = inbuf_size - decompressor->zstream.avail_in;
277 *bytes_written = outbuf_size - decompressor->zstream.avail_out;
279 if (res == Z_STREAM_END)
280 return G_CONVERTER_FINISHED;
281 return G_CONVERTER_CONVERTED;
284 g_assert_not_reached ();
288 g_zlib_decompressor_iface_init (GConverterIface *iface)
290 iface->convert = g_zlib_decompressor_convert;
291 iface->reset = g_zlib_decompressor_reset;