It is 'registered', not 'registred'
[glib.git] / gio / gzlibcompressor.c
blob51092e8197356d262440196112b3688a45459555
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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "config.h"
25 #include "gzlibcompressor.h"
27 #include <errno.h>
28 #include <zlib.h>
29 #include <string.h>
31 #include "gfileinfo.h"
32 #include "gioerror.h"
33 #include "gioenums.h"
34 #include "gioenumtypes.h"
35 #include "glibintl.h"
38 enum {
39 PROP_0,
40 PROP_FORMAT,
41 PROP_LEVEL,
42 PROP_FILE_INFO
45 /**
46 * SECTION:gzcompressor
47 * @short_description: Zlib compressor
48 * @include: gio/gio.h
50 * #GZlibCompressor is an implementation of #GConverter that
51 * compresses data using zlib.
54 static void g_zlib_compressor_iface_init (GConverterIface *iface);
56 /**
57 * GZlibCompressor:
59 * Zlib decompression
61 struct _GZlibCompressor
63 GObject parent_instance;
65 GZlibCompressorFormat format;
66 int level;
67 z_stream zstream;
68 gz_header gzheader;
69 GFileInfo *file_info;
72 static void
73 g_zlib_compressor_set_gzheader (GZlibCompressor *compressor)
75 /* On win32, these functions were not exported before 1.2.4 */
76 #if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240
77 const gchar *filename;
79 if (compressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP ||
80 compressor->file_info == NULL)
81 return;
83 memset (&compressor->gzheader, 0, sizeof (gz_header));
84 compressor->gzheader.os = 0x03; /* Unix */
86 filename = g_file_info_get_name (compressor->file_info);
87 compressor->gzheader.name = (Bytef *) filename;
88 compressor->gzheader.name_max = filename ? strlen (filename) + 1 : 0;
90 compressor->gzheader.time =
91 (uLong) g_file_info_get_attribute_uint64 (compressor->file_info,
92 G_FILE_ATTRIBUTE_TIME_MODIFIED);
94 if (deflateSetHeader (&compressor->zstream, &compressor->gzheader) != Z_OK)
95 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
96 #endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */
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))
103 static void
104 g_zlib_compressor_finalize (GObject *object)
106 GZlibCompressor *compressor;
108 compressor = G_ZLIB_COMPRESSOR (object);
110 deflateEnd (&compressor->zstream);
112 if (compressor->file_info)
113 g_object_unref (compressor->file_info);
115 G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object);
119 static void
120 g_zlib_compressor_set_property (GObject *object,
121 guint prop_id,
122 const GValue *value,
123 GParamSpec *pspec)
125 GZlibCompressor *compressor;
127 compressor = G_ZLIB_COMPRESSOR (object);
129 switch (prop_id)
131 case PROP_FORMAT:
132 compressor->format = g_value_get_enum (value);
133 break;
135 case PROP_LEVEL:
136 compressor->level = g_value_get_int (value);
137 break;
139 case PROP_FILE_INFO:
140 g_zlib_compressor_set_file_info (compressor, g_value_get_object (value));
141 break;
143 default:
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145 break;
150 static void
151 g_zlib_compressor_get_property (GObject *object,
152 guint prop_id,
153 GValue *value,
154 GParamSpec *pspec)
156 GZlibCompressor *compressor;
158 compressor = G_ZLIB_COMPRESSOR (object);
160 switch (prop_id)
162 case PROP_FORMAT:
163 g_value_set_enum (value, compressor->format);
164 break;
166 case PROP_LEVEL:
167 g_value_set_int (value, compressor->level);
168 break;
170 case PROP_FILE_INFO:
171 g_value_set_object (value, compressor->file_info);
172 break;
174 default:
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 break;
180 static void
181 g_zlib_compressor_init (GZlibCompressor *compressor)
185 static void
186 g_zlib_compressor_constructed (GObject *object)
188 GZlibCompressor *compressor;
189 int res;
191 compressor = G_ZLIB_COMPRESSOR (object);
193 if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
195 /* + 16 for gzip */
196 res = deflateInit2 (&compressor->zstream,
197 compressor->level, Z_DEFLATED,
198 MAX_WBITS + 16, 8,
199 Z_DEFAULT_STRATEGY);
201 else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
203 /* negative wbits for raw */
204 res = deflateInit2 (&compressor->zstream,
205 compressor->level, Z_DEFLATED,
206 -MAX_WBITS, 8,
207 Z_DEFAULT_STRATEGY);
209 else /* ZLIB */
210 res = deflateInit (&compressor->zstream, compressor->level);
212 if (res == Z_MEM_ERROR )
213 g_error ("GZlibCompressor: Not enough memory for zlib use");
215 if (res != Z_OK)
216 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
218 g_zlib_compressor_set_gzheader (compressor);
221 static void
222 g_zlib_compressor_class_init (GZlibCompressorClass *klass)
224 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
226 gobject_class->finalize = g_zlib_compressor_finalize;
227 gobject_class->constructed = g_zlib_compressor_constructed;
228 gobject_class->get_property = g_zlib_compressor_get_property;
229 gobject_class->set_property = g_zlib_compressor_set_property;
231 g_object_class_install_property (gobject_class,
232 PROP_FORMAT,
233 g_param_spec_enum ("format",
234 P_("compression format"),
235 P_("The format of the compressed data"),
236 G_TYPE_ZLIB_COMPRESSOR_FORMAT,
237 G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
238 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
239 G_PARAM_STATIC_STRINGS));
240 g_object_class_install_property (gobject_class,
241 PROP_LEVEL,
242 g_param_spec_int ("level",
243 P_("compression level"),
244 P_("The level of compression from 0 (no compression) to 9 (most compression), -1 for the default level"),
245 -1, 9,
247 G_PARAM_READWRITE |
248 G_PARAM_CONSTRUCT_ONLY |
249 G_PARAM_STATIC_STRINGS));
252 * GZlibCompressor:file-info:
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.
258 * Since: 2.26
260 g_object_class_install_property (gobject_class,
261 PROP_FILE_INFO,
262 g_param_spec_object ("file-info",
263 P_("file info"),
264 P_("File info"),
265 G_TYPE_FILE_INFO,
266 G_PARAM_READWRITE |
267 G_PARAM_STATIC_STRINGS));
271 * g_zlib_compressor_new:
272 * @format: The format to use for the compressed data
273 * @level: compression level (0-9), -1 for default
275 * Creates a new #GZlibCompressor.
277 * Returns: a new #GZlibCompressor
279 * Since: 2.24
281 GZlibCompressor *
282 g_zlib_compressor_new (GZlibCompressorFormat format,
283 int level)
285 GZlibCompressor *compressor;
287 compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR,
288 "format", format,
289 "level", level,
290 NULL);
292 return compressor;
296 * g_zlib_compressor_get_file_info:
297 * @compressor: a #GZlibCompressor
299 * Returns the #GZlibCompressor:file-info property.
301 * Returns: (transfer none): a #GFileInfo, or %NULL
303 * Since: 2.26
305 GFileInfo *
306 g_zlib_compressor_get_file_info (GZlibCompressor *compressor)
308 g_return_val_if_fail (G_IS_ZLIB_COMPRESSOR (compressor), NULL);
310 return compressor->file_info;
314 * g_zlib_compressor_set_file_info:
315 * @compressor: a #GZlibCompressor
316 * @file_info: (allow-none): a #GFileInfo
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.
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().
327 * Since: 2.26
329 void
330 g_zlib_compressor_set_file_info (GZlibCompressor *compressor,
331 GFileInfo *file_info)
333 g_return_if_fail (G_IS_ZLIB_COMPRESSOR (compressor));
335 if (file_info == compressor->file_info)
336 return;
338 if (compressor->file_info)
339 g_object_unref (compressor->file_info);
340 if (file_info)
341 g_object_ref (file_info);
342 compressor->file_info = file_info;
343 g_object_notify (G_OBJECT (compressor), "file-info");
345 g_zlib_compressor_set_gzheader (compressor);
348 static void
349 g_zlib_compressor_reset (GConverter *converter)
351 GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter);
352 int res;
354 res = deflateReset (&compressor->zstream);
355 if (res != Z_OK)
356 g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
358 /* deflateReset reset the header too, so re-set it */
359 g_zlib_compressor_set_gzheader (compressor);
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)
373 GZlibCompressor *compressor;
374 int res;
375 int flush;
377 compressor = G_ZLIB_COMPRESSOR (converter);
379 compressor->zstream.next_in = (void *)inbuf;
380 compressor->zstream.avail_in = inbuf_size;
382 compressor->zstream.next_out = outbuf;
383 compressor->zstream.avail_out = outbuf_size;
385 flush = Z_NO_FLUSH;
386 if (flags & G_CONVERTER_INPUT_AT_END)
387 flush = Z_FINISH;
388 else if (flags & G_CONVERTER_FLUSH)
389 flush = Z_SYNC_FLUSH;
391 res = deflate (&compressor->zstream, flush);
393 if (res == Z_MEM_ERROR)
395 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
396 _("Not enough memory"));
397 return G_CONVERTER_ERROR;
400 if (res == Z_STREAM_ERROR)
402 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
403 _("Internal error: %s"), compressor->zstream.msg);
404 return G_CONVERTER_ERROR;
407 if (res == Z_BUF_ERROR)
409 if (flags & G_CONVERTER_FLUSH)
410 return G_CONVERTER_FLUSHED;
412 /* We do have output space, so this should only happen if we
413 have no input but need some */
415 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
416 _("Need more input"));
417 return G_CONVERTER_ERROR;
420 if (res == Z_OK || res == Z_STREAM_END)
422 *bytes_read = inbuf_size - compressor->zstream.avail_in;
423 *bytes_written = outbuf_size - compressor->zstream.avail_out;
425 if (res == Z_STREAM_END)
426 return G_CONVERTER_FINISHED;
427 return G_CONVERTER_CONVERTED;
430 g_assert_not_reached ();
433 static void
434 g_zlib_compressor_iface_init (GConverterIface *iface)
436 iface->convert = g_zlib_compressor_convert;
437 iface->reset = g_zlib_compressor_reset;