Coverage Report

Created: 2025-07-01 07:09

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