1/* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2009 Red Hat, Inc.
4 *
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.1 of the License, or (at your option) any later version.
9 *
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.
14 *
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Alexander Larsson <alexl@redhat.com>
19 */
20
21#include "config.h"
22
23#include "gzlibdecompressor.h"
24
25#include <errno.h>
26#include <zlib.h>
27#include <string.h>
28
29#include "gfileinfo.h"
30#include "gioerror.h"
31#include "gioenums.h"
32#include "gioenumtypes.h"
33#include "glibintl.h"
34
35
36enum {
37 PROP_0,
38 PROP_FORMAT,
39 PROP_FILE_INFO
40};
41
42/**
43 * SECTION:gzdecompressor
44 * @short_description: Zlib decompressor
45 * @include: gio/gio.h
46 *
47 * #GZlibDecompressor is an implementation of #GConverter that
48 * decompresses data compressed with zlib.
49 */
50
51static void g_zlib_decompressor_iface_init (GConverterIface *iface);
52
53typedef struct {
54 gz_header gzheader;
55 char filename[257];
56 GFileInfo *file_info;
57} HeaderData;
58
59/**
60 * GZlibDecompressor:
61 *
62 * Zlib decompression
63 */
64struct _GZlibDecompressor
65{
66 GObject parent_instance;
67
68 GZlibCompressorFormat format;
69 z_stream zstream;
70 HeaderData *header_data;
71};
72
73static void
74g_zlib_decompressor_set_gzheader (GZlibDecompressor *decompressor)
75{
76 /* On win32, these functions were not exported before 1.2.4 */
77#if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240
78 if (decompressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP)
79 return;
80
81 if (decompressor->header_data != NULL)
82 {
83 if (decompressor->header_data->file_info)
84 g_object_unref (object: decompressor->header_data->file_info);
85
86 memset (s: decompressor->header_data, c: 0, n: sizeof (HeaderData));
87 }
88 else
89 {
90 decompressor->header_data = g_new0 (HeaderData, 1);
91 }
92
93 decompressor->header_data->gzheader.name = (Bytef*) &decompressor->header_data->filename;
94 /* We keep one byte to guarantee the string is 0-terminated */
95 decompressor->header_data->gzheader.name_max = 256;
96
97 if (inflateGetHeader (strm: &decompressor->zstream, head: &decompressor->header_data->gzheader) != Z_OK)
98 g_warning ("unexpected zlib error: %s", decompressor->zstream.msg);
99#endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */
100}
101
102G_DEFINE_TYPE_WITH_CODE (GZlibDecompressor, g_zlib_decompressor, G_TYPE_OBJECT,
103 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
104 g_zlib_decompressor_iface_init))
105
106static void
107g_zlib_decompressor_finalize (GObject *object)
108{
109 GZlibDecompressor *decompressor;
110
111 decompressor = G_ZLIB_DECOMPRESSOR (object);
112
113 inflateEnd (strm: &decompressor->zstream);
114
115 if (decompressor->header_data != NULL)
116 {
117 if (decompressor->header_data->file_info)
118 g_object_unref (object: decompressor->header_data->file_info);
119 g_free (mem: decompressor->header_data);
120 }
121
122 G_OBJECT_CLASS (g_zlib_decompressor_parent_class)->finalize (object);
123}
124
125
126static void
127g_zlib_decompressor_set_property (GObject *object,
128 guint prop_id,
129 const GValue *value,
130 GParamSpec *pspec)
131{
132 GZlibDecompressor *decompressor;
133
134 decompressor = G_ZLIB_DECOMPRESSOR (object);
135
136 switch (prop_id)
137 {
138 case PROP_FORMAT:
139 decompressor->format = g_value_get_enum (value);
140 break;
141
142 default:
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144 break;
145 }
146
147}
148
149static void
150g_zlib_decompressor_get_property (GObject *object,
151 guint prop_id,
152 GValue *value,
153 GParamSpec *pspec)
154{
155 GZlibDecompressor *decompressor;
156
157 decompressor = G_ZLIB_DECOMPRESSOR (object);
158
159 switch (prop_id)
160 {
161 case PROP_FORMAT:
162 g_value_set_enum (value, v_enum: decompressor->format);
163 break;
164
165 case PROP_FILE_INFO:
166 if (decompressor->header_data)
167 g_value_set_object (value, v_object: decompressor->header_data->file_info);
168 else
169 g_value_set_object (value, NULL);
170 break;
171
172 default:
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 break;
175 }
176}
177
178static void
179g_zlib_decompressor_init (GZlibDecompressor *decompressor)
180{
181}
182
183static void
184g_zlib_decompressor_constructed (GObject *object)
185{
186 GZlibDecompressor *decompressor;
187 int res;
188
189 decompressor = G_ZLIB_DECOMPRESSOR (object);
190
191 if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
192 {
193 /* + 16 for gzip */
194 res = inflateInit2 (&decompressor->zstream, MAX_WBITS + 16);
195 }
196 else if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
197 {
198 /* Negative for raw */
199 res = inflateInit2 (&decompressor->zstream, -MAX_WBITS);
200 }
201 else /* ZLIB */
202 res = inflateInit (&decompressor->zstream);
203
204 if (res == Z_MEM_ERROR )
205 g_error ("GZlibDecompressor: Not enough memory for zlib use");
206
207 if (res != Z_OK)
208 g_warning ("unexpected zlib error: %s", decompressor->zstream.msg);
209
210 g_zlib_decompressor_set_gzheader (decompressor);
211}
212
213static void
214g_zlib_decompressor_class_init (GZlibDecompressorClass *klass)
215{
216 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
217
218 gobject_class->finalize = g_zlib_decompressor_finalize;
219 gobject_class->constructed = g_zlib_decompressor_constructed;
220 gobject_class->get_property = g_zlib_decompressor_get_property;
221 gobject_class->set_property = g_zlib_decompressor_set_property;
222
223 g_object_class_install_property (oclass: gobject_class,
224 property_id: PROP_FORMAT,
225 pspec: g_param_spec_enum (name: "format",
226 P_("compression format"),
227 P_("The format of the compressed data"),
228 enum_type: G_TYPE_ZLIB_COMPRESSOR_FORMAT,
229 default_value: G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
230 flags: G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
231 G_PARAM_STATIC_STRINGS));
232
233 /**
234 * GZlibDecompressor:file-info:
235 *
236 * A #GFileInfo containing the information found in the GZIP header
237 * of the data stream processed, or %NULL if the header was not yet
238 * fully processed, is not present at all, or the compressor's
239 * #GZlibDecompressor:format property is not %G_ZLIB_COMPRESSOR_FORMAT_GZIP.
240 *
241 * Since: 2.26
242 */
243 g_object_class_install_property (oclass: gobject_class,
244 property_id: PROP_FILE_INFO,
245 pspec: g_param_spec_object (name: "file-info",
246 P_("file info"),
247 P_("File info"),
248 G_TYPE_FILE_INFO,
249 flags: G_PARAM_READABLE |
250 G_PARAM_STATIC_STRINGS));
251}
252
253/**
254 * g_zlib_decompressor_new:
255 * @format: The format to use for the compressed data
256 *
257 * Creates a new #GZlibDecompressor.
258 *
259 * Returns: a new #GZlibDecompressor
260 *
261 * Since: 2.24
262 **/
263GZlibDecompressor *
264g_zlib_decompressor_new (GZlibCompressorFormat format)
265{
266 GZlibDecompressor *decompressor;
267
268 decompressor = g_object_new (G_TYPE_ZLIB_DECOMPRESSOR,
269 first_property_name: "format", format,
270 NULL);
271
272 return decompressor;
273}
274
275/**
276 * g_zlib_decompressor_get_file_info:
277 * @decompressor: a #GZlibDecompressor
278 *
279 * Retrieves the #GFileInfo constructed from the GZIP header data
280 * of compressed data processed by @compressor, or %NULL if @decompressor's
281 * #GZlibDecompressor:format property is not %G_ZLIB_COMPRESSOR_FORMAT_GZIP,
282 * or the header data was not fully processed yet, or it not present in the
283 * data stream at all.
284 *
285 * Returns: (nullable) (transfer none): a #GFileInfo, or %NULL
286 *
287 * Since: 2.26
288 */
289GFileInfo *
290g_zlib_decompressor_get_file_info (GZlibDecompressor *decompressor)
291{
292 g_return_val_if_fail (G_IS_ZLIB_DECOMPRESSOR (decompressor), NULL);
293
294 if (decompressor->header_data)
295 return decompressor->header_data->file_info;
296
297 return NULL;
298}
299
300static void
301g_zlib_decompressor_reset (GConverter *converter)
302{
303 GZlibDecompressor *decompressor = G_ZLIB_DECOMPRESSOR (converter);
304 int res;
305
306 res = inflateReset (strm: &decompressor->zstream);
307 if (res != Z_OK)
308 g_warning ("unexpected zlib error: %s", decompressor->zstream.msg);
309
310 g_zlib_decompressor_set_gzheader (decompressor);
311}
312
313static GConverterResult
314g_zlib_decompressor_convert (GConverter *converter,
315 const void *inbuf,
316 gsize inbuf_size,
317 void *outbuf,
318 gsize outbuf_size,
319 GConverterFlags flags,
320 gsize *bytes_read,
321 gsize *bytes_written,
322 GError **error)
323{
324 GZlibDecompressor *decompressor;
325 int res;
326
327 decompressor = G_ZLIB_DECOMPRESSOR (converter);
328
329 decompressor->zstream.next_in = (void *)inbuf;
330 decompressor->zstream.avail_in = inbuf_size;
331
332 decompressor->zstream.next_out = outbuf;
333 decompressor->zstream.avail_out = outbuf_size;
334
335 res = inflate (strm: &decompressor->zstream, Z_NO_FLUSH);
336
337 if (res == Z_DATA_ERROR || res == Z_NEED_DICT)
338 {
339 g_set_error_literal (err: error, G_IO_ERROR, code: G_IO_ERROR_INVALID_DATA,
340 _("Invalid compressed data"));
341 return G_CONVERTER_ERROR;
342 }
343
344 if (res == Z_MEM_ERROR)
345 {
346 g_set_error_literal (err: error, G_IO_ERROR, code: G_IO_ERROR_FAILED,
347 _("Not enough memory"));
348 return G_CONVERTER_ERROR;
349 }
350
351 if (res == Z_STREAM_ERROR)
352 {
353 g_set_error (err: error, G_IO_ERROR, code: G_IO_ERROR_FAILED,
354 _("Internal error: %s"), decompressor->zstream.msg);
355 return G_CONVERTER_ERROR;
356 }
357
358 if (res == Z_BUF_ERROR)
359 {
360 if (flags & G_CONVERTER_FLUSH)
361 return G_CONVERTER_FLUSHED;
362
363 /* Z_FINISH not set, so this means no progress could be made */
364 /* We do have output space, so this should only happen if we
365 have no input but need some */
366
367 g_set_error_literal (err: error, G_IO_ERROR, code: G_IO_ERROR_PARTIAL_INPUT,
368 _("Need more input"));
369 return G_CONVERTER_ERROR;
370 }
371
372 g_assert (res == Z_OK || res == Z_STREAM_END);
373
374 *bytes_read = inbuf_size - decompressor->zstream.avail_in;
375 *bytes_written = outbuf_size - decompressor->zstream.avail_out;
376
377#if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240
378 if (decompressor->header_data != NULL &&
379 decompressor->header_data->gzheader.done == 1)
380 {
381 HeaderData *data = decompressor->header_data;
382
383 /* So we don't notify again */
384 data->gzheader.done = 2;
385
386 data->file_info = g_file_info_new ();
387 g_file_info_set_attribute_uint64 (info: data->file_info,
388 G_FILE_ATTRIBUTE_TIME_MODIFIED,
389 attr_value: data->gzheader.time);
390 g_file_info_set_attribute_uint32 (info: data->file_info,
391 G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
392 attr_value: 0);
393
394 if (data->filename[0] != '\0')
395 g_file_info_set_attribute_byte_string (info: data->file_info,
396 G_FILE_ATTRIBUTE_STANDARD_NAME,
397 attr_value: data->filename);
398
399 g_object_notify (G_OBJECT (decompressor), property_name: "file-info");
400 }
401#endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */
402
403 if (res == Z_STREAM_END)
404 return G_CONVERTER_FINISHED;
405 return G_CONVERTER_CONVERTED;
406}
407
408static void
409g_zlib_decompressor_iface_init (GConverterIface *iface)
410{
411 iface->convert = g_zlib_decompressor_convert;
412 iface->reset = g_zlib_decompressor_reset;
413}
414

source code of gtk/subprojects/glib/gio/gzlibdecompressor.c