/src/glib/gio/gzlibcompressor.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 "gzlibcompressor.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_LEVEL, |
42 | | PROP_FILE_INFO |
43 | | }; |
44 | | |
45 | | /** |
46 | | * SECTION:gzlibcompressor |
47 | | * @short_description: Zlib compressor |
48 | | * @include: gio/gio.h |
49 | | * |
50 | | * #GZlibCompressor is an implementation of #GConverter that |
51 | | * compresses data using zlib. |
52 | | */ |
53 | | |
54 | | static void g_zlib_compressor_iface_init (GConverterIface *iface); |
55 | | |
56 | | /** |
57 | | * GZlibCompressor: |
58 | | * |
59 | | * Zlib decompression |
60 | | */ |
61 | | struct _GZlibCompressor |
62 | | { |
63 | | GObject parent_instance; |
64 | | |
65 | | GZlibCompressorFormat format; |
66 | | int level; |
67 | | z_stream zstream; |
68 | | gz_header gzheader; |
69 | | GFileInfo *file_info; |
70 | | }; |
71 | | |
72 | | static void |
73 | | g_zlib_compressor_set_gzheader (GZlibCompressor *compressor) |
74 | 0 | { |
75 | | /* On win32, these functions were not exported before 1.2.4 */ |
76 | 0 | #if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240 |
77 | 0 | const gchar *filename; |
78 | |
|
79 | 0 | if (compressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP || |
80 | 0 | compressor->file_info == NULL) |
81 | 0 | return; |
82 | | |
83 | 0 | memset (&compressor->gzheader, 0, sizeof (gz_header)); |
84 | 0 | compressor->gzheader.os = 0x03; /* Unix */ |
85 | |
|
86 | 0 | filename = g_file_info_get_name (compressor->file_info); |
87 | 0 | compressor->gzheader.name = (Bytef *) filename; |
88 | 0 | compressor->gzheader.name_max = filename ? strlen (filename) + 1 : 0; |
89 | |
|
90 | 0 | compressor->gzheader.time = |
91 | 0 | (uLong) g_file_info_get_attribute_uint64 (compressor->file_info, |
92 | 0 | G_FILE_ATTRIBUTE_TIME_MODIFIED); |
93 | |
|
94 | 0 | if (deflateSetHeader (&compressor->zstream, &compressor->gzheader) != Z_OK) |
95 | 0 | g_warning ("unexpected zlib error: %s", compressor->zstream.msg); |
96 | 0 | #endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */ |
97 | 0 | } |
98 | | |
99 | | G_DEFINE_TYPE_WITH_CODE (GZlibCompressor, g_zlib_compressor, G_TYPE_OBJECT, |
100 | | G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER, |
101 | | g_zlib_compressor_iface_init)) |
102 | | |
103 | | static void |
104 | | g_zlib_compressor_finalize (GObject *object) |
105 | 0 | { |
106 | 0 | GZlibCompressor *compressor; |
107 | |
|
108 | 0 | compressor = G_ZLIB_COMPRESSOR (object); |
109 | |
|
110 | 0 | deflateEnd (&compressor->zstream); |
111 | |
|
112 | 0 | if (compressor->file_info) |
113 | 0 | g_object_unref (compressor->file_info); |
114 | |
|
115 | 0 | G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object); |
116 | 0 | } |
117 | | |
118 | | |
119 | | static void |
120 | | g_zlib_compressor_set_property (GObject *object, |
121 | | guint prop_id, |
122 | | const GValue *value, |
123 | | GParamSpec *pspec) |
124 | 0 | { |
125 | 0 | GZlibCompressor *compressor; |
126 | |
|
127 | 0 | compressor = G_ZLIB_COMPRESSOR (object); |
128 | |
|
129 | 0 | switch (prop_id) |
130 | 0 | { |
131 | 0 | case PROP_FORMAT: |
132 | 0 | compressor->format = g_value_get_enum (value); |
133 | 0 | break; |
134 | | |
135 | 0 | case PROP_LEVEL: |
136 | 0 | compressor->level = g_value_get_int (value); |
137 | 0 | break; |
138 | | |
139 | 0 | case PROP_FILE_INFO: |
140 | 0 | g_zlib_compressor_set_file_info (compressor, g_value_get_object (value)); |
141 | 0 | break; |
142 | | |
143 | 0 | default: |
144 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
145 | 0 | break; |
146 | 0 | } |
147 | |
|
148 | 0 | } |
149 | | |
150 | | static void |
151 | | g_zlib_compressor_get_property (GObject *object, |
152 | | guint prop_id, |
153 | | GValue *value, |
154 | | GParamSpec *pspec) |
155 | 0 | { |
156 | 0 | GZlibCompressor *compressor; |
157 | |
|
158 | 0 | compressor = G_ZLIB_COMPRESSOR (object); |
159 | |
|
160 | 0 | switch (prop_id) |
161 | 0 | { |
162 | 0 | case PROP_FORMAT: |
163 | 0 | g_value_set_enum (value, compressor->format); |
164 | 0 | break; |
165 | | |
166 | 0 | case PROP_LEVEL: |
167 | 0 | g_value_set_int (value, compressor->level); |
168 | 0 | break; |
169 | | |
170 | 0 | case PROP_FILE_INFO: |
171 | 0 | g_value_set_object (value, compressor->file_info); |
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_compressor_init (GZlibCompressor *compressor) |
182 | 0 | { |
183 | 0 | } |
184 | | |
185 | | static void |
186 | | g_zlib_compressor_constructed (GObject *object) |
187 | 0 | { |
188 | 0 | GZlibCompressor *compressor; |
189 | 0 | int res; |
190 | |
|
191 | 0 | compressor = G_ZLIB_COMPRESSOR (object); |
192 | |
|
193 | 0 | if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP) |
194 | 0 | { |
195 | | /* + 16 for gzip */ |
196 | 0 | res = deflateInit2 (&compressor->zstream, |
197 | 0 | compressor->level, Z_DEFLATED, |
198 | 0 | MAX_WBITS + 16, 8, |
199 | 0 | Z_DEFAULT_STRATEGY); |
200 | 0 | } |
201 | 0 | else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW) |
202 | 0 | { |
203 | | /* negative wbits for raw */ |
204 | 0 | res = deflateInit2 (&compressor->zstream, |
205 | 0 | compressor->level, Z_DEFLATED, |
206 | 0 | -MAX_WBITS, 8, |
207 | 0 | Z_DEFAULT_STRATEGY); |
208 | 0 | } |
209 | 0 | else /* ZLIB */ |
210 | 0 | res = deflateInit (&compressor->zstream, compressor->level); |
211 | |
|
212 | 0 | if (res == Z_MEM_ERROR ) |
213 | 0 | g_error ("GZlibCompressor: Not enough memory for zlib use"); |
214 | |
|
215 | 0 | if (res != Z_OK) |
216 | 0 | g_warning ("unexpected zlib error: %s", compressor->zstream.msg); |
217 | |
|
218 | 0 | g_zlib_compressor_set_gzheader (compressor); |
219 | 0 | } |
220 | | |
221 | | static void |
222 | | g_zlib_compressor_class_init (GZlibCompressorClass *klass) |
223 | 0 | { |
224 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
225 | |
|
226 | 0 | gobject_class->finalize = g_zlib_compressor_finalize; |
227 | 0 | gobject_class->constructed = g_zlib_compressor_constructed; |
228 | 0 | gobject_class->get_property = g_zlib_compressor_get_property; |
229 | 0 | gobject_class->set_property = g_zlib_compressor_set_property; |
230 | |
|
231 | 0 | g_object_class_install_property (gobject_class, |
232 | 0 | PROP_FORMAT, |
233 | 0 | g_param_spec_enum ("format", |
234 | 0 | P_("compression format"), |
235 | 0 | P_("The format of the compressed data"), |
236 | 0 | G_TYPE_ZLIB_COMPRESSOR_FORMAT, |
237 | 0 | G_ZLIB_COMPRESSOR_FORMAT_ZLIB, |
238 | 0 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | |
239 | 0 | G_PARAM_STATIC_STRINGS)); |
240 | 0 | g_object_class_install_property (gobject_class, |
241 | 0 | PROP_LEVEL, |
242 | 0 | g_param_spec_int ("level", |
243 | 0 | P_("compression level"), |
244 | 0 | P_("The level of compression from 0 (no compression) to 9 (most compression), -1 for the default level"), |
245 | 0 | -1, 9, |
246 | 0 | -1, |
247 | 0 | G_PARAM_READWRITE | |
248 | 0 | G_PARAM_CONSTRUCT_ONLY | |
249 | 0 | G_PARAM_STATIC_STRINGS)); |
250 | | |
251 | | /** |
252 | | * GZlibCompressor:file-info: |
253 | | * |
254 | | * If set to a non-%NULL #GFileInfo object, and #GZlibCompressor:format is |
255 | | * %G_ZLIB_COMPRESSOR_FORMAT_GZIP, the compressor will write the file name |
256 | | * and modification time from the file info to the GZIP header. |
257 | | * |
258 | | * Since: 2.26 |
259 | | */ |
260 | 0 | g_object_class_install_property (gobject_class, |
261 | 0 | PROP_FILE_INFO, |
262 | 0 | g_param_spec_object ("file-info", |
263 | 0 | P_("file info"), |
264 | 0 | P_("File info"), |
265 | 0 | G_TYPE_FILE_INFO, |
266 | 0 | G_PARAM_READWRITE | |
267 | 0 | G_PARAM_STATIC_STRINGS)); |
268 | 0 | } |
269 | | |
270 | | /** |
271 | | * g_zlib_compressor_new: |
272 | | * @format: The format to use for the compressed data |
273 | | * @level: compression level (0-9), -1 for default |
274 | | * |
275 | | * Creates a new #GZlibCompressor. |
276 | | * |
277 | | * Returns: a new #GZlibCompressor |
278 | | * |
279 | | * Since: 2.24 |
280 | | **/ |
281 | | GZlibCompressor * |
282 | | g_zlib_compressor_new (GZlibCompressorFormat format, |
283 | | int level) |
284 | 0 | { |
285 | 0 | GZlibCompressor *compressor; |
286 | |
|
287 | 0 | compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR, |
288 | 0 | "format", format, |
289 | 0 | "level", level, |
290 | 0 | NULL); |
291 | |
|
292 | 0 | return compressor; |
293 | 0 | } |
294 | | |
295 | | /** |
296 | | * g_zlib_compressor_get_file_info: |
297 | | * @compressor: a #GZlibCompressor |
298 | | * |
299 | | * Returns the #GZlibCompressor:file-info property. |
300 | | * |
301 | | * Returns: (nullable) (transfer none): a #GFileInfo, or %NULL |
302 | | * |
303 | | * Since: 2.26 |
304 | | */ |
305 | | GFileInfo * |
306 | | g_zlib_compressor_get_file_info (GZlibCompressor *compressor) |
307 | 0 | { |
308 | 0 | g_return_val_if_fail (G_IS_ZLIB_COMPRESSOR (compressor), NULL); |
309 | | |
310 | 0 | return compressor->file_info; |
311 | 0 | } |
312 | | |
313 | | /** |
314 | | * g_zlib_compressor_set_file_info: |
315 | | * @compressor: a #GZlibCompressor |
316 | | * @file_info: (nullable): a #GFileInfo |
317 | | * |
318 | | * Sets @file_info in @compressor. If non-%NULL, and @compressor's |
319 | | * #GZlibCompressor:format property is %G_ZLIB_COMPRESSOR_FORMAT_GZIP, |
320 | | * it will be used to set the file name and modification time in |
321 | | * the GZIP header of the compressed data. |
322 | | * |
323 | | * Note: it is an error to call this function while a compression is in |
324 | | * progress; it may only be called immediately after creation of @compressor, |
325 | | * or after resetting it with g_converter_reset(). |
326 | | * |
327 | | * Since: 2.26 |
328 | | */ |
329 | | void |
330 | | g_zlib_compressor_set_file_info (GZlibCompressor *compressor, |
331 | | GFileInfo *file_info) |
332 | 0 | { |
333 | 0 | g_return_if_fail (G_IS_ZLIB_COMPRESSOR (compressor)); |
334 | | |
335 | 0 | if (file_info == compressor->file_info) |
336 | 0 | return; |
337 | | |
338 | 0 | if (compressor->file_info) |
339 | 0 | g_object_unref (compressor->file_info); |
340 | 0 | if (file_info) |
341 | 0 | g_object_ref (file_info); |
342 | 0 | compressor->file_info = file_info; |
343 | 0 | g_object_notify (G_OBJECT (compressor), "file-info"); |
344 | |
|
345 | 0 | g_zlib_compressor_set_gzheader (compressor); |
346 | 0 | } |
347 | | |
348 | | static void |
349 | | g_zlib_compressor_reset (GConverter *converter) |
350 | 0 | { |
351 | 0 | GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter); |
352 | 0 | int res; |
353 | |
|
354 | 0 | res = deflateReset (&compressor->zstream); |
355 | 0 | if (res != Z_OK) |
356 | 0 | g_warning ("unexpected zlib error: %s", compressor->zstream.msg); |
357 | | |
358 | | /* deflateReset reset the header too, so re-set it */ |
359 | 0 | g_zlib_compressor_set_gzheader (compressor); |
360 | 0 | } |
361 | | |
362 | | static GConverterResult |
363 | | g_zlib_compressor_convert (GConverter *converter, |
364 | | const void *inbuf, |
365 | | gsize inbuf_size, |
366 | | void *outbuf, |
367 | | gsize outbuf_size, |
368 | | GConverterFlags flags, |
369 | | gsize *bytes_read, |
370 | | gsize *bytes_written, |
371 | | GError **error) |
372 | 0 | { |
373 | 0 | GZlibCompressor *compressor; |
374 | 0 | int res; |
375 | 0 | int flush; |
376 | |
|
377 | 0 | compressor = G_ZLIB_COMPRESSOR (converter); |
378 | |
|
379 | 0 | compressor->zstream.next_in = (void *)inbuf; |
380 | 0 | compressor->zstream.avail_in = inbuf_size; |
381 | |
|
382 | 0 | compressor->zstream.next_out = outbuf; |
383 | 0 | compressor->zstream.avail_out = outbuf_size; |
384 | |
|
385 | 0 | flush = Z_NO_FLUSH; |
386 | 0 | if (flags & G_CONVERTER_INPUT_AT_END) |
387 | 0 | flush = Z_FINISH; |
388 | 0 | else if (flags & G_CONVERTER_FLUSH) |
389 | 0 | flush = Z_SYNC_FLUSH; |
390 | |
|
391 | 0 | res = deflate (&compressor->zstream, flush); |
392 | |
|
393 | 0 | if (res == Z_MEM_ERROR) |
394 | 0 | { |
395 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
396 | 0 | _("Not enough memory")); |
397 | 0 | return G_CONVERTER_ERROR; |
398 | 0 | } |
399 | | |
400 | 0 | if (res == Z_STREAM_ERROR) |
401 | 0 | { |
402 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
403 | 0 | _("Internal error: %s"), compressor->zstream.msg); |
404 | 0 | return G_CONVERTER_ERROR; |
405 | 0 | } |
406 | | |
407 | 0 | if (res == Z_BUF_ERROR) |
408 | 0 | { |
409 | 0 | if (flags & G_CONVERTER_FLUSH) |
410 | 0 | return G_CONVERTER_FLUSHED; |
411 | | |
412 | | /* We do have output space, so this should only happen if we |
413 | | have no input but need some */ |
414 | | |
415 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT, |
416 | 0 | _("Need more input")); |
417 | 0 | return G_CONVERTER_ERROR; |
418 | 0 | } |
419 | | |
420 | 0 | if (res == Z_OK || res == Z_STREAM_END) |
421 | 0 | { |
422 | 0 | *bytes_read = inbuf_size - compressor->zstream.avail_in; |
423 | 0 | *bytes_written = outbuf_size - compressor->zstream.avail_out; |
424 | |
|
425 | 0 | if (res == Z_STREAM_END) |
426 | 0 | return G_CONVERTER_FINISHED; |
427 | 0 | return G_CONVERTER_CONVERTED; |
428 | 0 | } |
429 | | |
430 | 0 | g_assert_not_reached (); |
431 | 0 | } |
432 | | |
433 | | static void |
434 | | g_zlib_compressor_iface_init (GConverterIface *iface) |
435 | 0 | { |
436 | 0 | iface->convert = g_zlib_compressor_convert; |
437 | 0 | iface->reset = g_zlib_compressor_reset; |
438 | 0 | } |