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