/src/glib/gio/gzlibdecompressor.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
36 | | enum { |
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 | | |
51 | | static void g_zlib_decompressor_iface_init (GConverterIface *iface); |
52 | | |
53 | | typedef struct { |
54 | | gz_header gzheader; |
55 | | char filename[257]; |
56 | | GFileInfo *file_info; |
57 | | } HeaderData; |
58 | | |
59 | | /** |
60 | | * GZlibDecompressor: |
61 | | * |
62 | | * Zlib decompression |
63 | | */ |
64 | | struct _GZlibDecompressor |
65 | | { |
66 | | GObject parent_instance; |
67 | | |
68 | | GZlibCompressorFormat format; |
69 | | z_stream zstream; |
70 | | HeaderData *header_data; |
71 | | }; |
72 | | |
73 | | static void |
74 | | g_zlib_decompressor_set_gzheader (GZlibDecompressor *decompressor) |
75 | 0 | { |
76 | | /* On win32, these functions were not exported before 1.2.4 */ |
77 | 0 | #if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240 |
78 | 0 | if (decompressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP) |
79 | 0 | return; |
80 | | |
81 | 0 | if (decompressor->header_data != NULL) |
82 | 0 | { |
83 | 0 | if (decompressor->header_data->file_info) |
84 | 0 | g_object_unref (decompressor->header_data->file_info); |
85 | |
|
86 | 0 | memset (decompressor->header_data, 0, sizeof (HeaderData)); |
87 | 0 | } |
88 | 0 | else |
89 | 0 | { |
90 | 0 | decompressor->header_data = g_new0 (HeaderData, 1); |
91 | 0 | } |
92 | |
|
93 | 0 | decompressor->header_data->gzheader.name = (Bytef*) &decompressor->header_data->filename; |
94 | | /* We keep one byte to guarantee the string is 0-terminated */ |
95 | 0 | decompressor->header_data->gzheader.name_max = 256; |
96 | |
|
97 | 0 | if (inflateGetHeader (&decompressor->zstream, &decompressor->header_data->gzheader) != Z_OK) |
98 | 0 | g_warning ("unexpected zlib error: %s", decompressor->zstream.msg); |
99 | 0 | #endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */ |
100 | 0 | } |
101 | | |
102 | | G_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 | | |
106 | | static void |
107 | | g_zlib_decompressor_finalize (GObject *object) |
108 | 0 | { |
109 | 0 | GZlibDecompressor *decompressor; |
110 | |
|
111 | 0 | decompressor = G_ZLIB_DECOMPRESSOR (object); |
112 | |
|
113 | 0 | inflateEnd (&decompressor->zstream); |
114 | |
|
115 | 0 | if (decompressor->header_data != NULL) |
116 | 0 | { |
117 | 0 | if (decompressor->header_data->file_info) |
118 | 0 | g_object_unref (decompressor->header_data->file_info); |
119 | 0 | g_free (decompressor->header_data); |
120 | 0 | } |
121 | |
|
122 | 0 | G_OBJECT_CLASS (g_zlib_decompressor_parent_class)->finalize (object); |
123 | 0 | } |
124 | | |
125 | | |
126 | | static void |
127 | | g_zlib_decompressor_set_property (GObject *object, |
128 | | guint prop_id, |
129 | | const GValue *value, |
130 | | GParamSpec *pspec) |
131 | 0 | { |
132 | 0 | GZlibDecompressor *decompressor; |
133 | |
|
134 | 0 | decompressor = G_ZLIB_DECOMPRESSOR (object); |
135 | |
|
136 | 0 | switch (prop_id) |
137 | 0 | { |
138 | 0 | case PROP_FORMAT: |
139 | 0 | decompressor->format = g_value_get_enum (value); |
140 | 0 | break; |
141 | | |
142 | 0 | default: |
143 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
144 | 0 | break; |
145 | 0 | } |
146 | |
|
147 | 0 | } |
148 | | |
149 | | static void |
150 | | g_zlib_decompressor_get_property (GObject *object, |
151 | | guint prop_id, |
152 | | GValue *value, |
153 | | GParamSpec *pspec) |
154 | 0 | { |
155 | 0 | GZlibDecompressor *decompressor; |
156 | |
|
157 | 0 | decompressor = G_ZLIB_DECOMPRESSOR (object); |
158 | |
|
159 | 0 | switch (prop_id) |
160 | 0 | { |
161 | 0 | case PROP_FORMAT: |
162 | 0 | g_value_set_enum (value, decompressor->format); |
163 | 0 | break; |
164 | | |
165 | 0 | case PROP_FILE_INFO: |
166 | 0 | if (decompressor->header_data) |
167 | 0 | g_value_set_object (value, decompressor->header_data->file_info); |
168 | 0 | else |
169 | 0 | g_value_set_object (value, NULL); |
170 | 0 | break; |
171 | | |
172 | 0 | default: |
173 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
174 | 0 | break; |
175 | 0 | } |
176 | 0 | } |
177 | | |
178 | | static void |
179 | | g_zlib_decompressor_init (GZlibDecompressor *decompressor) |
180 | 0 | { |
181 | 0 | } |
182 | | |
183 | | static void |
184 | | g_zlib_decompressor_constructed (GObject *object) |
185 | 0 | { |
186 | 0 | GZlibDecompressor *decompressor; |
187 | 0 | int res; |
188 | |
|
189 | 0 | decompressor = G_ZLIB_DECOMPRESSOR (object); |
190 | |
|
191 | 0 | if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP) |
192 | 0 | { |
193 | | /* + 16 for gzip */ |
194 | 0 | res = inflateInit2 (&decompressor->zstream, MAX_WBITS + 16); |
195 | 0 | } |
196 | 0 | else if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW) |
197 | 0 | { |
198 | | /* Negative for raw */ |
199 | 0 | res = inflateInit2 (&decompressor->zstream, -MAX_WBITS); |
200 | 0 | } |
201 | 0 | else /* ZLIB */ |
202 | 0 | res = inflateInit (&decompressor->zstream); |
203 | |
|
204 | 0 | if (res == Z_MEM_ERROR ) |
205 | 0 | g_error ("GZlibDecompressor: Not enough memory for zlib use"); |
206 | |
|
207 | 0 | if (res != Z_OK) |
208 | 0 | g_warning ("unexpected zlib error: %s", decompressor->zstream.msg); |
209 | |
|
210 | 0 | g_zlib_decompressor_set_gzheader (decompressor); |
211 | 0 | } |
212 | | |
213 | | static void |
214 | | g_zlib_decompressor_class_init (GZlibDecompressorClass *klass) |
215 | 0 | { |
216 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
217 | |
|
218 | 0 | gobject_class->finalize = g_zlib_decompressor_finalize; |
219 | 0 | gobject_class->constructed = g_zlib_decompressor_constructed; |
220 | 0 | gobject_class->get_property = g_zlib_decompressor_get_property; |
221 | 0 | gobject_class->set_property = g_zlib_decompressor_set_property; |
222 | |
|
223 | 0 | g_object_class_install_property (gobject_class, |
224 | 0 | PROP_FORMAT, |
225 | 0 | g_param_spec_enum ("format", |
226 | 0 | P_("compression format"), |
227 | 0 | P_("The format of the compressed data"), |
228 | 0 | G_TYPE_ZLIB_COMPRESSOR_FORMAT, |
229 | 0 | G_ZLIB_COMPRESSOR_FORMAT_ZLIB, |
230 | 0 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | |
231 | 0 | 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 | 0 | g_object_class_install_property (gobject_class, |
244 | 0 | PROP_FILE_INFO, |
245 | 0 | g_param_spec_object ("file-info", |
246 | 0 | P_("file info"), |
247 | 0 | P_("File info"), |
248 | 0 | G_TYPE_FILE_INFO, |
249 | 0 | G_PARAM_READABLE | |
250 | 0 | G_PARAM_STATIC_STRINGS)); |
251 | 0 | } |
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 | | **/ |
263 | | GZlibDecompressor * |
264 | | g_zlib_decompressor_new (GZlibCompressorFormat format) |
265 | 0 | { |
266 | 0 | GZlibDecompressor *decompressor; |
267 | |
|
268 | 0 | decompressor = g_object_new (G_TYPE_ZLIB_DECOMPRESSOR, |
269 | 0 | "format", format, |
270 | 0 | NULL); |
271 | |
|
272 | 0 | return decompressor; |
273 | 0 | } |
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 | | */ |
289 | | GFileInfo * |
290 | | g_zlib_decompressor_get_file_info (GZlibDecompressor *decompressor) |
291 | 0 | { |
292 | 0 | g_return_val_if_fail (G_IS_ZLIB_DECOMPRESSOR (decompressor), NULL); |
293 | | |
294 | 0 | if (decompressor->header_data) |
295 | 0 | return decompressor->header_data->file_info; |
296 | | |
297 | 0 | return NULL; |
298 | 0 | } |
299 | | |
300 | | static void |
301 | | g_zlib_decompressor_reset (GConverter *converter) |
302 | 0 | { |
303 | 0 | GZlibDecompressor *decompressor = G_ZLIB_DECOMPRESSOR (converter); |
304 | 0 | int res; |
305 | |
|
306 | 0 | res = inflateReset (&decompressor->zstream); |
307 | 0 | if (res != Z_OK) |
308 | 0 | g_warning ("unexpected zlib error: %s", decompressor->zstream.msg); |
309 | |
|
310 | 0 | g_zlib_decompressor_set_gzheader (decompressor); |
311 | 0 | } |
312 | | |
313 | | static GConverterResult |
314 | | g_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 | 0 | { |
324 | 0 | GZlibDecompressor *decompressor; |
325 | 0 | int res; |
326 | |
|
327 | 0 | decompressor = G_ZLIB_DECOMPRESSOR (converter); |
328 | |
|
329 | 0 | decompressor->zstream.next_in = (void *)inbuf; |
330 | 0 | decompressor->zstream.avail_in = inbuf_size; |
331 | |
|
332 | 0 | decompressor->zstream.next_out = outbuf; |
333 | 0 | decompressor->zstream.avail_out = outbuf_size; |
334 | |
|
335 | 0 | res = inflate (&decompressor->zstream, Z_NO_FLUSH); |
336 | |
|
337 | 0 | if (res == Z_DATA_ERROR || res == Z_NEED_DICT) |
338 | 0 | { |
339 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, |
340 | 0 | _("Invalid compressed data")); |
341 | 0 | return G_CONVERTER_ERROR; |
342 | 0 | } |
343 | | |
344 | 0 | if (res == Z_MEM_ERROR) |
345 | 0 | { |
346 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
347 | 0 | _("Not enough memory")); |
348 | 0 | return G_CONVERTER_ERROR; |
349 | 0 | } |
350 | | |
351 | 0 | if (res == Z_STREAM_ERROR) |
352 | 0 | { |
353 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
354 | 0 | _("Internal error: %s"), decompressor->zstream.msg); |
355 | 0 | return G_CONVERTER_ERROR; |
356 | 0 | } |
357 | | |
358 | 0 | if (res == Z_BUF_ERROR) |
359 | 0 | { |
360 | 0 | if (flags & G_CONVERTER_FLUSH) |
361 | 0 | 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 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT, |
368 | 0 | _("Need more input")); |
369 | 0 | return G_CONVERTER_ERROR; |
370 | 0 | } |
371 | | |
372 | 0 | g_assert (res == Z_OK || res == Z_STREAM_END); |
373 | | |
374 | 0 | *bytes_read = inbuf_size - decompressor->zstream.avail_in; |
375 | 0 | *bytes_written = outbuf_size - decompressor->zstream.avail_out; |
376 | |
|
377 | 0 | #if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240 |
378 | 0 | if (decompressor->header_data != NULL && |
379 | 0 | decompressor->header_data->gzheader.done == 1) |
380 | 0 | { |
381 | 0 | HeaderData *data = decompressor->header_data; |
382 | | |
383 | | /* So we don't notify again */ |
384 | 0 | data->gzheader.done = 2; |
385 | |
|
386 | 0 | data->file_info = g_file_info_new (); |
387 | 0 | g_file_info_set_attribute_uint64 (data->file_info, |
388 | 0 | G_FILE_ATTRIBUTE_TIME_MODIFIED, |
389 | 0 | data->gzheader.time); |
390 | 0 | g_file_info_set_attribute_uint32 (data->file_info, |
391 | 0 | G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, |
392 | 0 | 0); |
393 | |
|
394 | 0 | if (data->filename[0] != '\0') |
395 | 0 | g_file_info_set_attribute_byte_string (data->file_info, |
396 | 0 | G_FILE_ATTRIBUTE_STANDARD_NAME, |
397 | 0 | data->filename); |
398 | |
|
399 | 0 | g_object_notify (G_OBJECT (decompressor), "file-info"); |
400 | 0 | } |
401 | 0 | #endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */ |
402 | |
|
403 | 0 | if (res == Z_STREAM_END) |
404 | 0 | return G_CONVERTER_FINISHED; |
405 | 0 | return G_CONVERTER_CONVERTED; |
406 | 0 | } |
407 | | |
408 | | static void |
409 | | g_zlib_decompressor_iface_init (GConverterIface *iface) |
410 | 0 | { |
411 | 0 | iface->convert = g_zlib_decompressor_convert; |
412 | 0 | iface->reset = g_zlib_decompressor_reset; |
413 | 0 | } |