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 "gzlibdecompressor.h"
34 #include "gioenumtypes.h"
44 * SECTION:gzdecompressor
45 * @short_description: Zlib decompressor
48 * #GZlibDecompressor is an implementation of #GConverter that
49 * decompresses data compressed with zlib.
52 static void g_zlib_decompressor_iface_init (GConverterIface *iface);
59 struct _GZlibDecompressor
61 GObject parent_instance;
63 GZlibCompressorFormat format;
67 G_DEFINE_TYPE_WITH_CODE (GZlibDecompressor, g_zlib_decompressor, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
69 g_zlib_decompressor_iface_init))
72 g_zlib_decompressor_finalize (GObject *object)
74 GZlibDecompressor *decompressor;
76 decompressor = G_ZLIB_DECOMPRESSOR (object);
78 inflateEnd (&decompressor->zstream);
80 G_OBJECT_CLASS (g_zlib_decompressor_parent_class)->finalize (object);
85 g_zlib_decompressor_set_property (GObject *object,
90 GZlibDecompressor *decompressor;
92 decompressor = G_ZLIB_DECOMPRESSOR (object);
97 decompressor->format = g_value_get_enum (value);
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 g_zlib_decompressor_get_property (GObject *object,
113 GZlibDecompressor *decompressor;
115 decompressor = G_ZLIB_DECOMPRESSOR (object);
120 g_value_set_enum (value, decompressor->format);
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
130 g_zlib_decompressor_init (GZlibDecompressor *decompressor)
135 g_zlib_decompressor_constructed (GObject *object)
137 GZlibDecompressor *decompressor;
140 decompressor = G_ZLIB_DECOMPRESSOR (object);
142 if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
145 res = inflateInit2 (&decompressor->zstream, MAX_WBITS + 16);
147 else if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
149 /* Negative for gzip */
150 res = inflateInit2 (&decompressor->zstream, -MAX_WBITS);
153 res = inflateInit (&decompressor->zstream);
155 if (res == Z_MEM_ERROR )
156 g_error ("GZlibDecompressor: Not enough memory for zlib use");
159 g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
163 g_zlib_decompressor_class_init (GZlibDecompressorClass *klass)
165 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
167 gobject_class->finalize = g_zlib_decompressor_finalize;
168 gobject_class->constructed = g_zlib_decompressor_constructed;
169 gobject_class->get_property = g_zlib_decompressor_get_property;
170 gobject_class->set_property = g_zlib_decompressor_set_property;
172 g_object_class_install_property (gobject_class,
174 g_param_spec_enum ("format",
175 P_("compression format"),
176 P_("The format of the compressed data"),
177 G_TYPE_ZLIB_COMPRESSOR_FORMAT,
178 G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
179 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
180 G_PARAM_STATIC_STRINGS));
184 * g_zlib_decompressor_new:
185 * @format: The format to use for the compressed data
187 * Creates a new #GZlibDecompressor.
189 * Returns: a new #GZlibDecompressor
194 g_zlib_decompressor_new (GZlibCompressorFormat format)
196 GZlibDecompressor *decompressor;
198 decompressor = g_object_new (G_TYPE_ZLIB_DECOMPRESSOR,
206 g_zlib_decompressor_reset (GConverter *converter)
208 GZlibDecompressor *decompressor = G_ZLIB_DECOMPRESSOR (converter);
211 res = inflateReset (&decompressor->zstream);
213 g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
216 static GConverterResult
217 g_zlib_decompressor_convert (GConverter *converter,
222 GConverterFlags flags,
224 gsize *bytes_written,
227 GZlibDecompressor *decompressor;
230 decompressor = G_ZLIB_DECOMPRESSOR (converter);
232 decompressor->zstream.next_in = (void *)inbuf;
233 decompressor->zstream.avail_in = inbuf_size;
235 decompressor->zstream.next_out = outbuf;
236 decompressor->zstream.avail_out = outbuf_size;
238 res = inflate (&decompressor->zstream, Z_NO_FLUSH);
240 if (res == Z_DATA_ERROR || res == Z_NEED_DICT)
242 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
243 _("Invalid compressed data"));
244 return G_CONVERTER_ERROR;
247 if (res == Z_MEM_ERROR)
249 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
250 _("Not enough memory"));
251 return G_CONVERTER_ERROR;
254 if (res == Z_STREAM_ERROR)
256 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
257 _("Internal error: %s"), decompressor->zstream.msg);
258 return G_CONVERTER_ERROR;
261 if (res == Z_BUF_ERROR)
263 if (flags & G_CONVERTER_FLUSH)
264 return G_CONVERTER_FLUSHED;
266 /* Z_FINISH not set, so this means no progress could be made */
267 /* We do have output space, so this should only happen if we
268 have no input but need some */
270 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
271 _("Need more input"));
272 return G_CONVERTER_ERROR;
275 if (res == Z_OK || res == Z_STREAM_END)
277 *bytes_read = inbuf_size - decompressor->zstream.avail_in;
278 *bytes_written = outbuf_size - decompressor->zstream.avail_out;
280 if (res == Z_STREAM_END)
281 return G_CONVERTER_FINISHED;
282 return G_CONVERTER_CONVERTED;
285 g_assert_not_reached ();
289 g_zlib_decompressor_iface_init (GConverterIface *iface)
291 iface->convert = g_zlib_decompressor_convert;
292 iface->reset = g_zlib_decompressor_reset;
295 #define __G_ZLIB_DECOMPRESSOR_C__
296 #include "gioaliasdef.c"