gtestutils: Fix a typo in a documentation comment
[glib.git] / gio / gzlibcompressor.c
blob5e89017ad4e9542b4b7c012f704bf97ec9ff944f
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Red Hat, Inc.
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 of the License, or (at your option) any later version.
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.
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/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
23 #include "gzlibcompressor.h"
25 #include <errno.h>
26 #include <zlib.h>
27 #include <string.h>
29 #include "gfileinfo.h"
30 #include "gioerror.h"
31 #include "gioenums.h"
32 #include "gioenumtypes.h"
33 #include "glibintl.h"
36 enum {
37 PROP_0,
38 PROP_FORMAT,
39 PROP_LEVEL,
40 PROP_FILE_INFO
43 /**
44 * SECTION:gzcompressor
45 * @short_description: Zlib compressor
46 * @include: gio/gio.h
48 * #GZlibCompressor is an implementation of #GConverter that
49 * compresses data using zlib.
52 static void g_zlib_compressor_iface_init (GConverterIface *iface);
54 /**
55 * GZlibCompressor:
57 * Zlib decompression
59 struct _GZlibCompressor
61 GObject parent_instance;
63 GZlibCompressorFormat format;
64 int level;
65 z_stream zstream;
66 gz_header gzheader;
67 GFileInfo *file_info;
70 static void
71 g_zlib_compressor_set_gzheader (GZlibCompressor *compressor)
73 /* On win32, these functions were not exported before 1.2.4 */
74 #if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240
75 const gchar *filename;
77 if (compressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP ||
78 compressor->file_info == NULL)
79 return;
81 memset (&compressor->gzheader, 0, sizeof (gz_header));
82 compressor->gzheader.os = 0x03; /* Unix */
84 filename = g_file_info_get_name (compressor->file_info);
85 compressor->gzheader.name = (Bytef *) filename;
86 compressor->gzheader.name_max = filename ? strlen (filename) + 1 : 0;
88 compressor->gzheader.time =
89 (uLong) g_file_info_get_attribute_uint64 (compressor->file_info,
90 G_FILE_ATTRIBUTE_TIME_MODIFIED);
92 if (deflateSetHeader (&compressor->zstream, &compressor->gzheader) != Z_OK)
93 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
94 #endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */
97 G_DEFINE_TYPE_WITH_CODE (GZlibCompressor, g_zlib_compressor, G_TYPE_OBJECT,
98 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
99 g_zlib_compressor_iface_init))
101 static void
102 g_zlib_compressor_finalize (GObject *object)
104 GZlibCompressor *compressor;
106 compressor = G_ZLIB_COMPRESSOR (object);
108 deflateEnd (&compressor->zstream);
110 if (compressor->file_info)
111 g_object_unref (compressor->file_info);
113 G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object);
117 static void
118 g_zlib_compressor_set_property (GObject *object,
119 guint prop_id,
120 const GValue *value,
121 GParamSpec *pspec)
123 GZlibCompressor *compressor;
125 compressor = G_ZLIB_COMPRESSOR (object);
127 switch (prop_id)
129 case PROP_FORMAT:
130 compressor->format = g_value_get_enum (value);
131 break;
133 case PROP_LEVEL:
134 compressor->level = g_value_get_int (value);
135 break;
137 case PROP_FILE_INFO:
138 g_zlib_compressor_set_file_info (compressor, g_value_get_object (value));
139 break;
141 default:
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143 break;
148 static void
149 g_zlib_compressor_get_property (GObject *object,
150 guint prop_id,
151 GValue *value,
152 GParamSpec *pspec)
154 GZlibCompressor *compressor;
156 compressor = G_ZLIB_COMPRESSOR (object);
158 switch (prop_id)
160 case PROP_FORMAT:
161 g_value_set_enum (value, compressor->format);
162 break;
164 case PROP_LEVEL:
165 g_value_set_int (value, compressor->level);
166 break;
168 case PROP_FILE_INFO:
169 g_value_set_object (value, compressor->file_info);
170 break;
172 default:
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 break;
178 static void
179 g_zlib_compressor_init (GZlibCompressor *compressor)
183 static void
184 g_zlib_compressor_constructed (GObject *object)
186 GZlibCompressor *compressor;
187 int res;
189 compressor = G_ZLIB_COMPRESSOR (object);
191 if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
193 /* + 16 for gzip */
194 res = deflateInit2 (&compressor->zstream,
195 compressor->level, Z_DEFLATED,
196 MAX_WBITS + 16, 8,
197 Z_DEFAULT_STRATEGY);
199 else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
201 /* negative wbits for raw */
202 res = deflateInit2 (&compressor->zstream,
203 compressor->level, Z_DEFLATED,
204 -MAX_WBITS, 8,
205 Z_DEFAULT_STRATEGY);
207 else /* ZLIB */
208 res = deflateInit (&compressor->zstream, compressor->level);
210 if (res == Z_MEM_ERROR )
211 g_error ("GZlibCompressor: Not enough memory for zlib use");
213 if (res != Z_OK)
214 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
216 g_zlib_compressor_set_gzheader (compressor);
219 static void
220 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
222 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
224 gobject_class->finalize = g_zlib_compressor_finalize;
225 gobject_class->constructed = g_zlib_compressor_constructed;
226 gobject_class->get_property = g_zlib_compressor_get_property;
227 gobject_class->set_property = g_zlib_compressor_set_property;
229 g_object_class_install_property (gobject_class,
230 PROP_FORMAT,
231 g_param_spec_enum ("format",
232 P_("compression format"),
233 P_("The format of the compressed data"),
234 G_TYPE_ZLIB_COMPRESSOR_FORMAT,
235 G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
236 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
237 G_PARAM_STATIC_STRINGS));
238 g_object_class_install_property (gobject_class,
239 PROP_LEVEL,
240 g_param_spec_int ("level",
241 P_("compression level"),
242 P_("The level of compression from 0 (no compression) to 9 (most compression), -1 for the default level"),
243 -1, 9,
245 G_PARAM_READWRITE |
246 G_PARAM_CONSTRUCT_ONLY |
247 G_PARAM_STATIC_STRINGS));
250 * GZlibCompressor:file-info:
252 * If set to a non-%NULL #GFileInfo object, and #GZlibCompressor:format is
253 * %G_ZLIB_COMPRESSOR_FORMAT_GZIP, the compressor will write the file name
254 * and modification time from the file info to the GZIP header.
256 * Since: 2.26
258 g_object_class_install_property (gobject_class,
259 PROP_FILE_INFO,
260 g_param_spec_object ("file-info",
261 P_("file info"),
262 P_("File info"),
263 G_TYPE_FILE_INFO,
264 G_PARAM_READWRITE |
265 G_PARAM_STATIC_STRINGS));
269 * g_zlib_compressor_new:
270 * @format: The format to use for the compressed data
271 * @level: compression level (0-9), -1 for default
273 * Creates a new #GZlibCompressor.
275 * Returns: a new #GZlibCompressor
277 * Since: 2.24
279 GZlibCompressor *
280 g_zlib_compressor_new (GZlibCompressorFormat format,
281 int level)
283 GZlibCompressor *compressor;
285 compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
286 "format", format,
287 "level", level,
288 NULL);
290 return compressor;
294 * g_zlib_compressor_get_file_info:
295 * @compressor: a #GZlibCompressor
297 * Returns the #GZlibCompressor:file-info property.
299 * Returns: (transfer none): a #GFileInfo, or %NULL
301 * Since: 2.26
303 GFileInfo *
304 g_zlib_compressor_get_file_info (GZlibCompressor *compressor)
306 g_return_val_if_fail (G_IS_ZLIB_COMPRESSOR (compressor), NULL);
308 return compressor->file_info;
312 * g_zlib_compressor_set_file_info:
313 * @compressor: a #GZlibCompressor
314 * @file_info: (nullable): a #GFileInfo
316 * Sets @file_info in @compressor. If non-%NULL, and @compressor's
317 * #GZlibCompressor:format property is %G_ZLIB_COMPRESSOR_FORMAT_GZIP,
318 * it will be used to set the file name and modification time in
319 * the GZIP header of the compressed data.
321 * Note: it is an error to call this function while a compression is in
322 * progress; it may only be called immediately after creation of @compressor,
323 * or after resetting it with g_converter_reset().
325 * Since: 2.26
327 void
328 g_zlib_compressor_set_file_info (GZlibCompressor *compressor,
329 GFileInfo *file_info)
331 g_return_if_fail (G_IS_ZLIB_COMPRESSOR (compressor));
333 if (file_info == compressor->file_info)
334 return;
336 if (compressor->file_info)
337 g_object_unref (compressor->file_info);
338 if (file_info)
339 g_object_ref (file_info);
340 compressor->file_info = file_info;
341 g_object_notify (G_OBJECT (compressor), "file-info");
343 g_zlib_compressor_set_gzheader (compressor);
346 static void
347 g_zlib_compressor_reset (GConverter *converter)
349 GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
350 int res;
352 res = deflateReset (&compressor->zstream);
353 if (res != Z_OK)
354 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
356 /* deflateReset reset the header too, so re-set it */
357 g_zlib_compressor_set_gzheader (compressor);
360 static GConverterResult
361 g_zlib_compressor_convert (GConverter *converter,
362 const void *inbuf,
363 gsize inbuf_size,
364 void *outbuf,
365 gsize outbuf_size,
366 GConverterFlags flags,
367 gsize *bytes_read,
368 gsize *bytes_written,
369 GError **error)
371 GZlibCompressor *compressor;
372 int res;
373 int flush;
375 compressor = G_ZLIB_COMPRESSOR (converter);
377 compressor->zstream.next_in = (void *)inbuf;
378 compressor->zstream.avail_in = inbuf_size;
380 compressor->zstream.next_out = outbuf;
381 compressor->zstream.avail_out = outbuf_size;
383 flush = Z_NO_FLUSH;
384 if (flags & G_CONVERTER_INPUT_AT_END)
385 flush = Z_FINISH;
386 else if (flags & G_CONVERTER_FLUSH)
387 flush = Z_SYNC_FLUSH;
389 res = deflate (&compressor->zstream, flush);
391 if (res == Z_MEM_ERROR)
393 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
394 _("Not enough memory"));
395 return G_CONVERTER_ERROR;
398 if (res == Z_STREAM_ERROR)
400 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
401 _("Internal error: %s"), compressor->zstream.msg);
402 return G_CONVERTER_ERROR;
405 if (res == Z_BUF_ERROR)
407 if (flags & G_CONVERTER_FLUSH)
408 return G_CONVERTER_FLUSHED;
410 /* We do have output space, so this should only happen if we
411 have no input but need some */
413 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
414 _("Need more input"));
415 return G_CONVERTER_ERROR;
418 if (res == Z_OK || res == Z_STREAM_END)
420 *bytes_read = inbuf_size - compressor->zstream.avail_in;
421 *bytes_written = outbuf_size - compressor->zstream.avail_out;
423 if (res == Z_STREAM_END)
424 return G_CONVERTER_FINISHED;
425 return G_CONVERTER_CONVERTED;
428 g_assert_not_reached ();
431 static void
432 g_zlib_compressor_iface_init (GConverterIface *iface)
434 iface->convert = g_zlib_compressor_convert;
435 iface->reset = g_zlib_compressor_reset;