| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | /* GIO - GLib Input, Output and Streaming Library
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2009 Red Hat, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2022-05-18 09:12:45 +01:00
										 |  |  |  * SPDX-License-Identifier: LGPL-2.1-or-later | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |  * This library is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU Lesser General Public | 
					
						
							|  |  |  |  * License as published by the Free Software Foundation; either | 
					
						
							| 
									
										
										
										
											2017-05-27 18:21:30 +02:00
										 |  |  |  * version 2.1 of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
					
						
							|  |  |  |  * Lesser General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Lesser General | 
					
						
							| 
									
										
										
										
											2014-01-23 12:58:29 +01:00
										 |  |  |  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Author: Alexander Larsson <alexl@redhat.com> | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "config.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-05 17:32:12 +02:00
										 |  |  | #include "gzlibcompressor.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <zlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  | #include "gfileinfo.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | #include "gioerror.h"
 | 
					
						
							|  |  |  | #include "gioenums.h"
 | 
					
						
							|  |  |  | #include "gioenumtypes.h"
 | 
					
						
							| 
									
										
										
										
											2010-05-05 17:32:12 +02:00
										 |  |  | #include "glibintl.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum { | 
					
						
							|  |  |  |   PROP_0, | 
					
						
							|  |  |  |   PROP_FORMAT, | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |   PROP_LEVEL, | 
					
						
							| 
									
										
										
										
											2025-04-12 21:06:21 +02:00
										 |  |  |   PROP_FILE_INFO, | 
					
						
							|  |  |  |   PROP_OS | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2023-11-02 16:29:26 +00:00
										 |  |  |  * GZlibCompressor: | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2023-11-02 16:29:26 +00:00
										 |  |  |  * `GZlibCompressor` is an implementation of [iface@Gio.Converter] that | 
					
						
							| 
									
										
										
										
											2010-02-24 12:54:17 +01:00
										 |  |  |  * compresses data using zlib. | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void g_zlib_compressor_iface_init          (GConverterIface *iface); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct _GZlibCompressor | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GObject parent_instance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   GZlibCompressorFormat format; | 
					
						
							|  |  |  |   int level; | 
					
						
							|  |  |  |   z_stream zstream; | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |   gz_header gzheader; | 
					
						
							|  |  |  |   GFileInfo *file_info; | 
					
						
							| 
									
										
										
										
											2025-04-12 21:06:21 +02:00
										 |  |  |   int os; | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  | static void | 
					
						
							|  |  |  | g_zlib_compressor_set_gzheader (GZlibCompressor *compressor) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-09-01 15:05:59 +02:00
										 |  |  |   /* On win32, these functions were not exported before 1.2.4 */ | 
					
						
							|  |  |  | #if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240
 | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |   const gchar *filename; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (compressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP || | 
					
						
							| 
									
										
										
										
											2025-04-12 21:06:21 +02:00
										 |  |  |       (compressor->file_info == NULL && | 
					
						
							|  |  |  |        compressor->os < 0)) | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   memset (&compressor->gzheader, 0, sizeof (gz_header)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-12 21:06:21 +02:00
										 |  |  |   if (compressor->os >= 0) | 
					
						
							|  |  |  |     compressor->gzheader.os = compressor->os; | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     compressor->gzheader.os = 0x03; /* Unix */ | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-12 21:06:21 +02:00
										 |  |  |   if (compressor->file_info) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       filename = g_file_info_get_name (compressor->file_info); | 
					
						
							|  |  |  |       compressor->gzheader.name = (Bytef *) filename; | 
					
						
							|  |  |  |       compressor->gzheader.name_max = filename ? strlen (filename) + 1 : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       compressor->gzheader.time = | 
					
						
							|  |  |  |           (uLong) g_file_info_get_attribute_uint64 (compressor->file_info, | 
					
						
							|  |  |  |                                                     G_FILE_ATTRIBUTE_TIME_MODIFIED); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (deflateSetHeader (&compressor->zstream, &compressor->gzheader) != Z_OK) | 
					
						
							| 
									
										
										
										
											2018-04-27 16:45:36 +01:00
										 |  |  |     g_warning ("unexpected zlib error: %s", compressor->zstream.msg); | 
					
						
							| 
									
										
										
										
											2010-09-01 15:05:59 +02:00
										 |  |  | #endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */
 | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | G_DEFINE_TYPE_WITH_CODE (GZlibCompressor, g_zlib_compressor, G_TYPE_OBJECT, | 
					
						
							|  |  |  | 			 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER, | 
					
						
							|  |  |  | 						g_zlib_compressor_iface_init)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_zlib_compressor_finalize (GObject *object) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GZlibCompressor *compressor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   compressor = G_ZLIB_COMPRESSOR (object); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   deflateEnd (&compressor->zstream); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |   if (compressor->file_info) | 
					
						
							|  |  |  |     g_object_unref (compressor->file_info); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |   G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_zlib_compressor_set_property (GObject      *object, | 
					
						
							|  |  |  | 				  guint         prop_id, | 
					
						
							|  |  |  | 				  const GValue *value, | 
					
						
							|  |  |  | 				  GParamSpec   *pspec) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GZlibCompressor *compressor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   compressor = G_ZLIB_COMPRESSOR (object); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   switch (prop_id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     case PROP_FORMAT: | 
					
						
							|  |  |  |       compressor->format = g_value_get_enum (value); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case PROP_LEVEL: | 
					
						
							|  |  |  |       compressor->level = g_value_get_int (value); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |     case PROP_FILE_INFO: | 
					
						
							|  |  |  |       g_zlib_compressor_set_file_info (compressor, g_value_get_object (value)); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-12 21:06:21 +02:00
										 |  |  |     case PROP_OS: | 
					
						
							|  |  |  |       g_zlib_compressor_set_os (compressor, g_value_get_int (value)); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |     default: | 
					
						
							|  |  |  |       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_zlib_compressor_get_property (GObject    *object, | 
					
						
							|  |  |  | 				  guint       prop_id, | 
					
						
							|  |  |  | 				  GValue     *value, | 
					
						
							|  |  |  | 				  GParamSpec *pspec) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GZlibCompressor *compressor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   compressor = G_ZLIB_COMPRESSOR (object); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   switch (prop_id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     case PROP_FORMAT: | 
					
						
							|  |  |  |       g_value_set_enum (value, compressor->format); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case PROP_LEVEL: | 
					
						
							|  |  |  |       g_value_set_int (value, compressor->level); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |     case PROP_FILE_INFO: | 
					
						
							|  |  |  |       g_value_set_object (value, compressor->file_info); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-12 21:06:21 +02:00
										 |  |  |     case PROP_OS: | 
					
						
							|  |  |  |       g_value_set_int (value, compressor->os); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |     default: | 
					
						
							|  |  |  |       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_zlib_compressor_init (GZlibCompressor *compressor) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-04-12 21:06:21 +02:00
										 |  |  |   compressor->os = -1; | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_zlib_compressor_constructed (GObject *object) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GZlibCompressor *compressor; | 
					
						
							|  |  |  |   int res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   compressor = G_ZLIB_COMPRESSOR (object); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       /* + 16 for gzip */ | 
					
						
							|  |  |  |       res = deflateInit2 (&compressor->zstream, | 
					
						
							|  |  |  | 			  compressor->level, Z_DEFLATED, | 
					
						
							|  |  |  | 			  MAX_WBITS + 16, 8, | 
					
						
							|  |  |  | 			  Z_DEFAULT_STRATEGY); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-11-24 13:02:05 +01:00
										 |  |  |   else if (compressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       /* negative wbits for raw */ | 
					
						
							|  |  |  |       res = deflateInit2 (&compressor->zstream, | 
					
						
							|  |  |  | 			  compressor->level, Z_DEFLATED, | 
					
						
							|  |  |  | 			  -MAX_WBITS, 8, | 
					
						
							|  |  |  | 			  Z_DEFAULT_STRATEGY); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else /* ZLIB */ | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |     res = deflateInit (&compressor->zstream, compressor->level); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (res == Z_MEM_ERROR ) | 
					
						
							|  |  |  |     g_error ("GZlibCompressor: Not enough memory for zlib use"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (res != Z_OK) | 
					
						
							| 
									
										
										
										
											2018-04-27 16:45:36 +01:00
										 |  |  |     g_warning ("unexpected zlib error: %s", compressor->zstream.msg); | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   g_zlib_compressor_set_gzheader (compressor); | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_zlib_compressor_class_init (GZlibCompressorClass *klass) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GObjectClass *gobject_class = G_OBJECT_CLASS (klass); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   gobject_class->finalize = g_zlib_compressor_finalize; | 
					
						
							|  |  |  |   gobject_class->constructed = g_zlib_compressor_constructed; | 
					
						
							|  |  |  |   gobject_class->get_property = g_zlib_compressor_get_property; | 
					
						
							|  |  |  |   gobject_class->set_property = g_zlib_compressor_set_property; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 13:23:09 +00:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * GZlibCompressor:format: | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * The format of the compressed data. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * Since: 2.24 | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |   g_object_class_install_property (gobject_class, | 
					
						
							|  |  |  | 				   PROP_FORMAT, | 
					
						
							| 
									
										
										
										
											2023-04-28 01:59:26 +02:00
										 |  |  | 				   g_param_spec_enum ("format", NULL, NULL, | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | 						      G_TYPE_ZLIB_COMPRESSOR_FORMAT, | 
					
						
							| 
									
										
										
										
											2009-11-24 13:02:05 +01:00
										 |  |  | 						      G_ZLIB_COMPRESSOR_FORMAT_ZLIB, | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | 						      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | | 
					
						
							|  |  |  | 						      G_PARAM_STATIC_STRINGS)); | 
					
						
							| 
									
										
										
										
											2023-11-29 13:23:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * GZlibCompressor:level: | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * The level of compression from `0` (no compression) to `9` (most | 
					
						
							| 
									
										
										
										
											2025-05-14 11:38:05 +01:00
										 |  |  |    * compression). | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * `-1` for the default level. | 
					
						
							| 
									
										
										
										
											2023-11-29 13:23:09 +00:00
										 |  |  |    * | 
					
						
							|  |  |  |    * Since: 2.24 | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |   g_object_class_install_property (gobject_class, | 
					
						
							|  |  |  | 				   PROP_LEVEL, | 
					
						
							| 
									
										
										
										
											2023-04-28 01:59:26 +02:00
										 |  |  | 				   g_param_spec_int ("level", NULL, NULL, | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | 						     -1, 9, | 
					
						
							|  |  |  | 						     -1, | 
					
						
							|  |  |  | 						     G_PARAM_READWRITE | | 
					
						
							|  |  |  | 						     G_PARAM_CONSTRUCT_ONLY | | 
					
						
							|  |  |  | 						     G_PARAM_STATIC_STRINGS)); | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * GZlibCompressor:file-info: | 
					
						
							|  |  |  |    * | 
					
						
							| 
									
										
										
										
											2025-05-14 11:38:05 +01:00
										 |  |  |    * A [class@Gio.FileInfo] containing file information to put into the gzip | 
					
						
							|  |  |  |    * header. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * The file name and modification time from the file info will be used. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * This will only be used if non-`NULL` and | 
					
						
							|  |  |  |    * [property@Gio.ZlibCompressor:format] is | 
					
						
							|  |  |  |    * [enum@Gio.ZlibCompressorFormat.GZIP]. | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |    * | 
					
						
							|  |  |  |    * Since: 2.26 | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   g_object_class_install_property (gobject_class, | 
					
						
							|  |  |  |                                    PROP_FILE_INFO, | 
					
						
							| 
									
										
										
										
											2023-04-28 01:59:26 +02:00
										 |  |  |                                    g_param_spec_object ("file-info", NULL, NULL, | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |                                                        G_TYPE_FILE_INFO, | 
					
						
							|  |  |  |                                                        G_PARAM_READWRITE | | 
					
						
							|  |  |  |                                                        G_PARAM_STATIC_STRINGS)); | 
					
						
							| 
									
										
										
										
											2025-04-12 21:06:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * GZlibCompressor:os: | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * The OS code of the gzip header. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * This will be used if set to a non-negative value, and if | 
					
						
							|  |  |  |    * [property@Gio.ZlibCompressor:format] is | 
					
						
							|  |  |  |    * [enum@Gio.ZlibCompressorFormat.GZIP], the compressor will set the OS code of | 
					
						
							|  |  |  |    * the gzip header to this value. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * If the value is unset, zlib will set the OS code depending on the platform. | 
					
						
							|  |  |  |    * This may be undesirable when reproducible output is desired. In that case setting | 
					
						
							|  |  |  |    * the OS code to `3` (for Unix) is recommended. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * Since: 2.86 | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   g_object_class_install_property (gobject_class, | 
					
						
							|  |  |  |                                    PROP_OS, | 
					
						
							|  |  |  |                                    g_param_spec_int  ("os", NULL, NULL, | 
					
						
							|  |  |  |                                                       -1, 255, | 
					
						
							|  |  |  |                                                       -1, | 
					
						
							|  |  |  |                                                       G_PARAM_READWRITE | | 
					
						
							|  |  |  |                                                       G_PARAM_STATIC_STRINGS |  | 
					
						
							|  |  |  |                                                       G_PARAM_EXPLICIT_NOTIFY)); | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_zlib_compressor_new: | 
					
						
							| 
									
										
										
										
											2025-05-14 11:38:05 +01:00
										 |  |  |  * @format: the format to use for the compressed data | 
					
						
							|  |  |  |  * @level: compression level (`0`-`9`), `-1` for default | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-05-14 11:38:05 +01:00
										 |  |  |  * Creates a compressor. | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-05-14 11:38:05 +01:00
										 |  |  |  * Returns: a new [class@Gio.ZlibCompressor] | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  |  * Since: 2.24 | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | GZlibCompressor * | 
					
						
							|  |  |  | g_zlib_compressor_new (GZlibCompressorFormat format, | 
					
						
							|  |  |  | 		       int level) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GZlibCompressor *compressor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   compressor = g_object_new (G_TYPE_ZLIB_COMPRESSOR, | 
					
						
							|  |  |  | 			     "format", format, | 
					
						
							|  |  |  | 			     "level", level, | 
					
						
							|  |  |  | 			     NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return compressor; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * g_zlib_compressor_get_file_info: | 
					
						
							| 
									
										
										
										
											2025-05-14 11:38:05 +01:00
										 |  |  |  * @compressor: a compressor | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-05-14 11:38:05 +01:00
										 |  |  |  * Gets the [property@Gio.ZlibCompressor:file-info] property. | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-05-14 11:38:05 +01:00
										 |  |  |  * Returns: (nullable) (transfer none): file info for the gzip header, if set | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |  * Since: 2.26 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | GFileInfo * | 
					
						
							|  |  |  | g_zlib_compressor_get_file_info (GZlibCompressor *compressor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_ZLIB_COMPRESSOR (compressor), NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return compressor->file_info; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_zlib_compressor_set_file_info: | 
					
						
							| 
									
										
										
										
											2025-05-14 11:38:05 +01:00
										 |  |  |  * @compressor: a compressor | 
					
						
							|  |  |  |  * @file_info: (nullable): file info for the gzip header | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-05-14 11:38:05 +01:00
										 |  |  |  * Sets the [property@Gio.ZlibCompressor:file-info] property. | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Note: it is an error to call this function while a compression is in | 
					
						
							|  |  |  |  * progress; it may only be called immediately after creation of @compressor, | 
					
						
							| 
									
										
										
										
											2025-05-14 11:38:05 +01:00
										 |  |  |  * or after resetting it with [method@Gio.Converter.reset]. | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.26 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | g_zlib_compressor_set_file_info (GZlibCompressor *compressor, | 
					
						
							|  |  |  |                                  GFileInfo       *file_info) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   g_return_if_fail (G_IS_ZLIB_COMPRESSOR (compressor)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (file_info == compressor->file_info) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (compressor->file_info) | 
					
						
							|  |  |  |     g_object_unref (compressor->file_info); | 
					
						
							|  |  |  |   if (file_info) | 
					
						
							|  |  |  |     g_object_ref (file_info); | 
					
						
							|  |  |  |   compressor->file_info = file_info; | 
					
						
							|  |  |  |   g_object_notify (G_OBJECT (compressor), "file-info"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_zlib_compressor_set_gzheader (compressor); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-12 21:06:21 +02:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * g_zlib_compressor_get_os: | 
					
						
							|  |  |  |  * @compressor: a compressor | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Gets the [property@Gio.ZlibCompressor:os] property. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Returns: the previously set OS value, or `-1` if unset | 
					
						
							|  |  |  |  * Since: 2.86 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | g_zlib_compressor_get_os (GZlibCompressor *compressor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_ZLIB_COMPRESSOR (compressor), -1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return compressor->os; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_zlib_compressor_set_os: | 
					
						
							|  |  |  |  * @compressor: a compressor | 
					
						
							|  |  |  |  * @os: the OS code to use, or `-1` to unset | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Sets the [property@Gio.ZlibCompressor:os] property. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Note: it is an error to call this function while a compression is in | 
					
						
							|  |  |  |  * progress; it may only be called immediately after creation of @compressor, | 
					
						
							|  |  |  |  * or after resetting it with [method@Gio.Converter.reset]. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.86 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | g_zlib_compressor_set_os (GZlibCompressor *compressor, | 
					
						
							|  |  |  |                           int              os) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   g_return_if_fail (G_IS_ZLIB_COMPRESSOR (compressor)); | 
					
						
							|  |  |  |   g_return_if_fail (os >= -1 && os < 256); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (os == compressor->os) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   compressor->os = os; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_object_notify (G_OBJECT (compressor), "os"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_zlib_compressor_set_gzheader (compressor); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | g_zlib_compressor_reset (GConverter *converter) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GZlibCompressor *compressor = G_ZLIB_COMPRESSOR (converter); | 
					
						
							|  |  |  |   int res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   res = deflateReset (&compressor->zstream); | 
					
						
							|  |  |  |   if (res != Z_OK) | 
					
						
							| 
									
										
										
										
											2018-04-27 16:45:36 +01:00
										 |  |  |     g_warning ("unexpected zlib error: %s", compressor->zstream.msg); | 
					
						
							| 
									
										
										
										
											2010-05-13 19:57:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* deflateReset reset the header too, so re-set it */ | 
					
						
							|  |  |  |   g_zlib_compressor_set_gzheader (compressor); | 
					
						
							| 
									
										
										
										
											2009-11-20 12:04:31 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static GConverterResult | 
					
						
							|  |  |  | g_zlib_compressor_convert (GConverter *converter, | 
					
						
							|  |  |  | 			   const void *inbuf, | 
					
						
							|  |  |  | 			   gsize       inbuf_size, | 
					
						
							|  |  |  | 			   void       *outbuf, | 
					
						
							|  |  |  | 			   gsize       outbuf_size, | 
					
						
							|  |  |  | 			   GConverterFlags flags, | 
					
						
							|  |  |  | 			   gsize      *bytes_read, | 
					
						
							|  |  |  | 			   gsize      *bytes_written, | 
					
						
							|  |  |  | 			   GError    **error) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GZlibCompressor *compressor; | 
					
						
							|  |  |  |   int res; | 
					
						
							|  |  |  |   int flush; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   compressor = G_ZLIB_COMPRESSOR (converter); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   compressor->zstream.next_in = (void *)inbuf; | 
					
						
							|  |  |  |   compressor->zstream.avail_in = inbuf_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   compressor->zstream.next_out = outbuf; | 
					
						
							|  |  |  |   compressor->zstream.avail_out = outbuf_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   flush = Z_NO_FLUSH; | 
					
						
							|  |  |  |   if (flags & G_CONVERTER_INPUT_AT_END) | 
					
						
							|  |  |  |     flush = Z_FINISH; | 
					
						
							|  |  |  |   else if (flags & G_CONVERTER_FLUSH) | 
					
						
							|  |  |  |     flush = Z_SYNC_FLUSH; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   res = deflate (&compressor->zstream, flush); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (res == Z_MEM_ERROR) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, | 
					
						
							|  |  |  | 			   _("Not enough memory")); | 
					
						
							|  |  |  |       return G_CONVERTER_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (res == Z_STREAM_ERROR) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, | 
					
						
							|  |  |  | 		   _("Internal error: %s"), compressor->zstream.msg); | 
					
						
							|  |  |  |       return G_CONVERTER_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (res == Z_BUF_ERROR) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  | 	if (flags & G_CONVERTER_FLUSH) | 
					
						
							|  |  |  | 	  return G_CONVERTER_FLUSHED; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* We do have output space, so this should only happen if we
 | 
					
						
							|  |  |  | 	   have no input but need some */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT, | 
					
						
							|  |  |  | 			     _("Need more input")); | 
					
						
							|  |  |  | 	return G_CONVERTER_ERROR; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (res == Z_OK || res == Z_STREAM_END) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       *bytes_read = inbuf_size - compressor->zstream.avail_in; | 
					
						
							|  |  |  |       *bytes_written = outbuf_size - compressor->zstream.avail_out; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (res == Z_STREAM_END) | 
					
						
							|  |  |  | 	return G_CONVERTER_FINISHED; | 
					
						
							|  |  |  |       return G_CONVERTER_CONVERTED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_assert_not_reached (); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_zlib_compressor_iface_init (GConverterIface *iface) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   iface->convert = g_zlib_compressor_convert; | 
					
						
							|  |  |  |   iface->reset = g_zlib_compressor_reset; | 
					
						
							|  |  |  | } |