Coverage Report

Created: 2025-07-23 08:13

/src/pango/subprojects/glib/gio/gzlibdecompressor.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
 * 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 "gzlibdecompressor.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_FILE_INFO
42
};
43
44
/**
45
 * GZlibDecompressor:
46
 *
47
 * `GZlibDecompressor` is an implementation of [iface@Gio.Converter] that
48
 * decompresses data compressed with zlib.
49
 */
50
51
static void g_zlib_decompressor_iface_init          (GConverterIface *iface);
52
53
typedef struct {
54
  gz_header gzheader;
55
  char filename[257];
56
  GFileInfo *file_info;
57
} HeaderData;
58
59
struct _GZlibDecompressor
60
{
61
  GObject parent_instance;
62
63
  GZlibCompressorFormat format;
64
  z_stream zstream;
65
  HeaderData *header_data;
66
};
67
68
static void
69
g_zlib_decompressor_set_gzheader (GZlibDecompressor *decompressor)
70
0
{
71
  /* On win32, these functions were not exported before 1.2.4 */
72
0
#if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240
73
0
  if (decompressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP)
74
0
    return;
75
76
0
  if (decompressor->header_data != NULL)
77
0
    {
78
0
      if (decompressor->header_data->file_info)
79
0
        g_object_unref (decompressor->header_data->file_info);
80
81
0
      memset (decompressor->header_data, 0, sizeof (HeaderData));
82
0
    }
83
0
  else
84
0
    {
85
0
      decompressor->header_data = g_new0 (HeaderData, 1);
86
0
    }
87
88
0
  decompressor->header_data->gzheader.name = (Bytef*) &decompressor->header_data->filename;
89
  /* We keep one byte to guarantee the string is 0-terminated */
90
0
  decompressor->header_data->gzheader.name_max = 256;
91
92
0
  if (inflateGetHeader (&decompressor->zstream, &decompressor->header_data->gzheader) != Z_OK)
93
0
    g_warning ("unexpected zlib error: %s", decompressor->zstream.msg);
94
0
#endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */
95
0
}
96
97
G_DEFINE_TYPE_WITH_CODE (GZlibDecompressor, g_zlib_decompressor, G_TYPE_OBJECT,
98
       G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
99
            g_zlib_decompressor_iface_init))
100
101
static void
102
g_zlib_decompressor_finalize (GObject *object)
103
0
{
104
0
  GZlibDecompressor *decompressor;
105
106
0
  decompressor = G_ZLIB_DECOMPRESSOR (object);
107
108
0
  inflateEnd (&decompressor->zstream);
109
110
0
  if (decompressor->header_data != NULL)
111
0
    {
112
0
      if (decompressor->header_data->file_info)
113
0
        g_object_unref (decompressor->header_data->file_info);
114
0
      g_free (decompressor->header_data);
115
0
    }
116
117
0
  G_OBJECT_CLASS (g_zlib_decompressor_parent_class)->finalize (object);
118
0
}
119
120
121
static void
122
g_zlib_decompressor_set_property (GObject      *object,
123
          guint         prop_id,
124
          const GValue *value,
125
          GParamSpec   *pspec)
126
0
{
127
0
  GZlibDecompressor *decompressor;
128
129
0
  decompressor = G_ZLIB_DECOMPRESSOR (object);
130
131
0
  switch (prop_id)
132
0
    {
133
0
    case PROP_FORMAT:
134
0
      decompressor->format = g_value_get_enum (value);
135
0
      break;
136
137
0
    default:
138
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139
0
      break;
140
0
    }
141
142
0
}
143
144
static void
145
g_zlib_decompressor_get_property (GObject    *object,
146
          guint       prop_id,
147
          GValue     *value,
148
          GParamSpec *pspec)
149
0
{
150
0
  GZlibDecompressor *decompressor;
151
152
0
  decompressor = G_ZLIB_DECOMPRESSOR (object);
153
154
0
  switch (prop_id)
155
0
    {
156
0
    case PROP_FORMAT:
157
0
      g_value_set_enum (value, decompressor->format);
158
0
      break;
159
160
0
    case PROP_FILE_INFO:
161
0
      if (decompressor->header_data)
162
0
        g_value_set_object (value, decompressor->header_data->file_info);
163
0
      else
164
0
        g_value_set_object (value, NULL);
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_decompressor_init (GZlibDecompressor *decompressor)
175
0
{
176
0
}
177
178
static void
179
g_zlib_decompressor_constructed (GObject *object)
180
0
{
181
0
  GZlibDecompressor *decompressor;
182
0
  int res;
183
184
0
  decompressor = G_ZLIB_DECOMPRESSOR (object);
185
186
0
  if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
187
0
    {
188
      /* + 16 for gzip */
189
0
      res = inflateInit2 (&decompressor->zstream, MAX_WBITS + 16);
190
0
    }
191
0
  else if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
192
0
    {
193
      /* Negative for raw */
194
0
      res = inflateInit2 (&decompressor->zstream, -MAX_WBITS);
195
0
    }
196
0
  else /* ZLIB */
197
0
    res = inflateInit (&decompressor->zstream);
198
199
0
  if (res == Z_MEM_ERROR )
200
0
    g_error ("GZlibDecompressor: Not enough memory for zlib use");
201
202
0
  if (res != Z_OK)
203
0
    g_warning ("unexpected zlib error: %s", decompressor->zstream.msg);
204
205
0
  g_zlib_decompressor_set_gzheader (decompressor);
206
0
}
207
208
static void
209
g_zlib_decompressor_class_init (GZlibDecompressorClass *klass)
210
0
{
211
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
212
213
0
  gobject_class->finalize = g_zlib_decompressor_finalize;
214
0
  gobject_class->constructed = g_zlib_decompressor_constructed;
215
0
  gobject_class->get_property = g_zlib_decompressor_get_property;
216
0
  gobject_class->set_property = g_zlib_decompressor_set_property;
217
218
  /**
219
   * GZlibDecompressor:format:
220
   *
221
   * The format of the compressed data.
222
   *
223
   * Since: 2.24
224
   */
225
0
  g_object_class_install_property (gobject_class,
226
0
           PROP_FORMAT,
227
0
           g_param_spec_enum ("format", NULL, NULL,
228
0
                  G_TYPE_ZLIB_COMPRESSOR_FORMAT,
229
0
                  G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
230
0
                  G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
231
0
                  G_PARAM_STATIC_STRINGS));
232
233
  /**
234
   * GZlibDecompressor:file-info:
235
   *
236
   * A [class@Gio.FileInfo] containing the information found in the gzip header
237
   * of the data stream processed.
238
   *
239
   * This will be `NULL` if the header was not yet fully processed, is not
240
   * present at all, or the compressor’s [property@Gio.ZlibDecompressor:format]
241
   * property is not [enum@Gio.ZlibCompressorFormat.GZIP].
242
   *
243
   * Since: 2.26
244
   */
245
0
  g_object_class_install_property (gobject_class,
246
0
                                   PROP_FILE_INFO,
247
0
                                   g_param_spec_object ("file-info", NULL, NULL,
248
0
                                                       G_TYPE_FILE_INFO,
249
0
                                                       G_PARAM_READABLE |
250
0
                                                       G_PARAM_STATIC_STRINGS));
251
0
}
252
253
/**
254
 * g_zlib_decompressor_new:
255
 * @format: the format to use for the compressed data
256
 *
257
 * Creates a new decompressor.
258
 *
259
 * Returns: a new [class@Gio.ZlibDecompressor]
260
 * Since: 2.24
261
 **/
262
GZlibDecompressor *
263
g_zlib_decompressor_new (GZlibCompressorFormat format)
264
0
{
265
0
  GZlibDecompressor *decompressor;
266
267
0
  decompressor = g_object_new (G_TYPE_ZLIB_DECOMPRESSOR,
268
0
             "format", format,
269
0
             NULL);
270
271
0
  return decompressor;
272
0
}
273
274
/**
275
 * g_zlib_decompressor_get_file_info:
276
 * @decompressor: a #GZlibDecompressor
277
 *
278
 * Gets the [property@Gio.ZlibDecompressor:file-info] property.
279
 *
280
 * Returns: (nullable) (transfer none): file info from the gzip header, if available
281
 * Since: 2.26
282
 */
283
GFileInfo *
284
g_zlib_decompressor_get_file_info (GZlibDecompressor *decompressor)
285
0
{
286
0
  g_return_val_if_fail (G_IS_ZLIB_DECOMPRESSOR (decompressor), NULL);
287
288
0
  if (decompressor->header_data)
289
0
    return decompressor->header_data->file_info;
290
291
0
  return NULL;
292
0
}
293
294
static void
295
g_zlib_decompressor_reset (GConverter *converter)
296
0
{
297
0
  GZlibDecompressor *decompressor = G_ZLIB_DECOMPRESSOR (converter);
298
0
  int res;
299
300
0
  res = inflateReset (&decompressor->zstream);
301
0
  if (res != Z_OK)
302
0
    g_warning ("unexpected zlib error: %s", decompressor->zstream.msg);
303
304
0
  g_zlib_decompressor_set_gzheader (decompressor);
305
0
}
306
307
static GConverterResult
308
g_zlib_decompressor_convert (GConverter *converter,
309
           const void *inbuf,
310
           gsize       inbuf_size,
311
           void       *outbuf,
312
           gsize       outbuf_size,
313
           GConverterFlags flags,
314
           gsize      *bytes_read,
315
           gsize      *bytes_written,
316
           GError    **error)
317
0
{
318
0
  GZlibDecompressor *decompressor;
319
0
  int res;
320
321
0
  decompressor = G_ZLIB_DECOMPRESSOR (converter);
322
323
0
  decompressor->zstream.next_in = (void *)inbuf;
324
0
  decompressor->zstream.avail_in = inbuf_size;
325
326
0
  decompressor->zstream.next_out = outbuf;
327
0
  decompressor->zstream.avail_out = outbuf_size;
328
329
0
  res = inflate (&decompressor->zstream, Z_NO_FLUSH);
330
331
0
  if (res == Z_DATA_ERROR || res == Z_NEED_DICT)
332
0
    {
333
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
334
0
         _("Invalid compressed data"));
335
0
      return G_CONVERTER_ERROR;
336
0
    }
337
338
0
  if (res == Z_MEM_ERROR)
339
0
    {
340
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
341
0
         _("Not enough memory"));
342
0
      return G_CONVERTER_ERROR;
343
0
    }
344
345
0
    if (res == Z_STREAM_ERROR)
346
0
    {
347
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
348
0
       _("Internal error: %s"), decompressor->zstream.msg);
349
0
      return G_CONVERTER_ERROR;
350
0
    }
351
352
0
    if (res == Z_BUF_ERROR)
353
0
      {
354
0
  if (flags & G_CONVERTER_FLUSH)
355
0
    return G_CONVERTER_FLUSHED;
356
357
  /* Z_FINISH not set, so this means no progress could be made */
358
  /* We do have output space, so this should only happen if we
359
     have no input but need some */
360
361
0
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
362
0
           _("Need more input"));
363
0
  return G_CONVERTER_ERROR;
364
0
      }
365
366
0
  g_assert (res == Z_OK || res == Z_STREAM_END);
367
368
0
  *bytes_read = inbuf_size - decompressor->zstream.avail_in;
369
0
  *bytes_written = outbuf_size - decompressor->zstream.avail_out;
370
371
0
#if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240
372
0
  if (decompressor->header_data != NULL &&
373
0
      decompressor->header_data->gzheader.done == 1)
374
0
    {
375
0
      HeaderData *data = decompressor->header_data;
376
377
      /* So we don't notify again */
378
0
      data->gzheader.done = 2;
379
380
0
      data->file_info = g_file_info_new ();
381
0
      g_file_info_set_attribute_uint64 (data->file_info,
382
0
                                        G_FILE_ATTRIBUTE_TIME_MODIFIED,
383
0
                                        data->gzheader.time);
384
0
      g_file_info_set_attribute_uint32 (data->file_info,
385
0
                                        G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
386
0
                                        0);
387
0
      g_file_info_set_attribute_uint32 (data->file_info,
388
0
                                        G_FILE_ATTRIBUTE_TIME_MODIFIED_NSEC,
389
0
                                        0);
390
391
0
      if (data->filename[0] != '\0')
392
0
        g_file_info_set_attribute_byte_string (data->file_info,
393
0
                                               G_FILE_ATTRIBUTE_STANDARD_NAME,
394
0
                                               data->filename);
395
396
0
      g_object_notify (G_OBJECT (decompressor), "file-info");
397
0
    }
398
0
#endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */
399
400
0
  if (res == Z_STREAM_END)
401
0
    return G_CONVERTER_FINISHED;
402
0
  return G_CONVERTER_CONVERTED;
403
0
}
404
405
static void
406
g_zlib_decompressor_iface_init (GConverterIface *iface)
407
0
{
408
0
  iface->convert = g_zlib_decompressor_convert;
409
0
  iface->reset = g_zlib_decompressor_reset;
410
0
}