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