Blame gio/gzlibdecompressor.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2009 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gzlibdecompressor.h"
Packit ae235b
Packit ae235b
#include <errno.h>
Packit ae235b
#include <zlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gfileinfo.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "gioenums.h"
Packit ae235b
#include "gioenumtypes.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
enum {
Packit ae235b
  PROP_0,
Packit ae235b
  PROP_FORMAT,
Packit ae235b
  PROP_FILE_INFO
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gzdecompressor
Packit ae235b
 * @short_description: Zlib decompressor
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * #GZlibDecompressor is an implementation of #GConverter that
Packit ae235b
 * decompresses data compressed with zlib.
Packit ae235b
 */
Packit ae235b
Packit ae235b
static void g_zlib_decompressor_iface_init          (GConverterIface *iface);
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  gz_header gzheader;
Packit ae235b
  char filename[257];
Packit ae235b
  GFileInfo *file_info;
Packit ae235b
} HeaderData;
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GZlibDecompressor:
Packit ae235b
 *
Packit ae235b
 * Zlib decompression
Packit ae235b
 */
Packit ae235b
struct _GZlibDecompressor
Packit ae235b
{
Packit ae235b
  GObject parent_instance;
Packit ae235b
Packit ae235b
  GZlibCompressorFormat format;
Packit ae235b
  z_stream zstream;
Packit ae235b
  HeaderData *header_data;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_zlib_decompressor_set_gzheader (GZlibDecompressor *decompressor)
Packit ae235b
{
Packit ae235b
  /* On win32, these functions were not exported before 1.2.4 */
Packit ae235b
#if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240
Packit ae235b
  if (decompressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  if (decompressor->header_data != NULL)
Packit ae235b
    {
Packit ae235b
      if (decompressor->header_data->file_info)
Packit ae235b
        g_object_unref (decompressor->header_data->file_info);
Packit ae235b
Packit ae235b
      memset (decompressor->header_data, 0, sizeof (HeaderData));
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      decompressor->header_data = g_new0 (HeaderData, 1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  decompressor->header_data->gzheader.name = (Bytef*) &decompressor->header_data->filename;
Packit ae235b
  /* We keep one byte to guarantee the string is 0-terminated */
Packit ae235b
  decompressor->header_data->gzheader.name_max = 256;
Packit ae235b
Packit ae235b
  if (inflateGetHeader (&decompressor->zstream, &decompressor->header_data->gzheader) != Z_OK)
Packit ae235b
    g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
Packit ae235b
#endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */
Packit ae235b
}
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GZlibDecompressor, g_zlib_decompressor, G_TYPE_OBJECT,
Packit ae235b
			 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
Packit ae235b
						g_zlib_decompressor_iface_init))
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_zlib_decompressor_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GZlibDecompressor *decompressor;
Packit ae235b
Packit ae235b
  decompressor = G_ZLIB_DECOMPRESSOR (object);
Packit ae235b
Packit ae235b
  inflateEnd (&decompressor->zstream);
Packit ae235b
Packit ae235b
  if (decompressor->header_data != NULL)
Packit ae235b
    {
Packit ae235b
      if (decompressor->header_data->file_info)
Packit ae235b
        g_object_unref (decompressor->header_data->file_info);
Packit ae235b
      g_free (decompressor->header_data);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_zlib_decompressor_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_zlib_decompressor_set_property (GObject      *object,
Packit ae235b
				  guint         prop_id,
Packit ae235b
				  const GValue *value,
Packit ae235b
				  GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  GZlibDecompressor *decompressor;
Packit ae235b
Packit ae235b
  decompressor = G_ZLIB_DECOMPRESSOR (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_FORMAT:
Packit ae235b
      decompressor->format = g_value_get_enum (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_zlib_decompressor_get_property (GObject    *object,
Packit ae235b
				  guint       prop_id,
Packit ae235b
				  GValue     *value,
Packit ae235b
				  GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GZlibDecompressor *decompressor;
Packit ae235b
Packit ae235b
  decompressor = G_ZLIB_DECOMPRESSOR (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_FORMAT:
Packit ae235b
      g_value_set_enum (value, decompressor->format);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_FILE_INFO:
Packit ae235b
      if (decompressor->header_data)
Packit ae235b
        g_value_set_object (value, decompressor->header_data->file_info);
Packit ae235b
      else
Packit ae235b
        g_value_set_object (value, NULL);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_zlib_decompressor_init (GZlibDecompressor *decompressor)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_zlib_decompressor_constructed (GObject *object)
Packit ae235b
{
Packit ae235b
  GZlibDecompressor *decompressor;
Packit ae235b
  int res;
Packit ae235b
Packit ae235b
  decompressor = G_ZLIB_DECOMPRESSOR (object);
Packit ae235b
Packit ae235b
  if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_GZIP)
Packit ae235b
    {
Packit ae235b
      /* + 16 for gzip */
Packit ae235b
      res = inflateInit2 (&decompressor->zstream, MAX_WBITS + 16);
Packit ae235b
    }
Packit ae235b
  else if (decompressor->format == G_ZLIB_COMPRESSOR_FORMAT_RAW)
Packit ae235b
    {
Packit ae235b
      /* Negative for raw */
Packit ae235b
      res = inflateInit2 (&decompressor->zstream, -MAX_WBITS);
Packit ae235b
    }
Packit ae235b
  else /* ZLIB */
Packit ae235b
    res = inflateInit (&decompressor->zstream);
Packit ae235b
Packit ae235b
  if (res == Z_MEM_ERROR )
Packit ae235b
    g_error ("GZlibDecompressor: Not enough memory for zlib use");
Packit ae235b
Packit ae235b
  if (res != Z_OK)
Packit ae235b
    g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
Packit ae235b
Packit ae235b
  g_zlib_decompressor_set_gzheader (decompressor);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_zlib_decompressor_class_init (GZlibDecompressorClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_zlib_decompressor_finalize;
Packit ae235b
  gobject_class->constructed = g_zlib_decompressor_constructed;
Packit ae235b
  gobject_class->get_property = g_zlib_decompressor_get_property;
Packit ae235b
  gobject_class->set_property = g_zlib_decompressor_set_property;
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
				   PROP_FORMAT,
Packit ae235b
				   g_param_spec_enum ("format",
Packit ae235b
						      P_("compression format"),
Packit ae235b
						      P_("The format of the compressed data"),
Packit ae235b
						      G_TYPE_ZLIB_COMPRESSOR_FORMAT,
Packit ae235b
						      G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
Packit ae235b
						      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
						      G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GZlibDecompressor:file-info:
Packit ae235b
   *
Packit ae235b
   * A #GFileInfo containing the information found in the GZIP header
Packit ae235b
   * of the data stream processed, or %NULL if the header was not yet
Packit ae235b
   * fully processed, is not present at all, or the compressor's
Packit ae235b
   * #GZlibDecompressor:format property is not %G_ZLIB_COMPRESSOR_FORMAT_GZIP.
Packit ae235b
   *
Packit ae235b
   * Since: 2.26
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
                                   PROP_FILE_INFO,
Packit ae235b
                                   g_param_spec_object ("file-info",
Packit ae235b
                                                       P_("file info"),
Packit ae235b
                                                       P_("File info"),
Packit ae235b
                                                       G_TYPE_FILE_INFO,
Packit ae235b
                                                       G_PARAM_READABLE |
Packit ae235b
                                                       G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_zlib_decompressor_new:
Packit ae235b
 * @format: The format to use for the compressed data
Packit ae235b
 *
Packit ae235b
 * Creates a new #GZlibDecompressor.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GZlibDecompressor
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 **/
Packit ae235b
GZlibDecompressor *
Packit ae235b
g_zlib_decompressor_new (GZlibCompressorFormat format)
Packit ae235b
{
Packit ae235b
  GZlibDecompressor *decompressor;
Packit ae235b
Packit ae235b
  decompressor = g_object_new (G_TYPE_ZLIB_DECOMPRESSOR,
Packit ae235b
			       "format", format,
Packit ae235b
			       NULL);
Packit ae235b
Packit ae235b
  return decompressor;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_zlib_decompressor_get_file_info:
Packit ae235b
 * @decompressor: a #GZlibDecompressor
Packit ae235b
 *
Packit ae235b
 * Retrieves the #GFileInfo constructed from the GZIP header data
Packit ae235b
 * of compressed data processed by @compressor, or %NULL if @decompressor's
Packit ae235b
 * #GZlibDecompressor:format property is not %G_ZLIB_COMPRESSOR_FORMAT_GZIP,
Packit ae235b
 * or the header data was not fully processed yet, or it not present in the
Packit ae235b
 * data stream at all.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): a #GFileInfo, or %NULL
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GFileInfo *
Packit ae235b
g_zlib_decompressor_get_file_info (GZlibDecompressor *decompressor)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_ZLIB_DECOMPRESSOR (decompressor), NULL);
Packit ae235b
Packit ae235b
  if (decompressor->header_data)
Packit ae235b
    return decompressor->header_data->file_info;
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_zlib_decompressor_reset (GConverter *converter)
Packit ae235b
{
Packit ae235b
  GZlibDecompressor *decompressor = G_ZLIB_DECOMPRESSOR (converter);
Packit ae235b
  int res;
Packit ae235b
Packit ae235b
  res = inflateReset (&decompressor->zstream);
Packit ae235b
  if (res != Z_OK)
Packit ae235b
    g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
Packit ae235b
Packit ae235b
  g_zlib_decompressor_set_gzheader (decompressor);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GConverterResult
Packit ae235b
g_zlib_decompressor_convert (GConverter *converter,
Packit ae235b
			     const void *inbuf,
Packit ae235b
			     gsize       inbuf_size,
Packit ae235b
			     void       *outbuf,
Packit ae235b
			     gsize       outbuf_size,
Packit ae235b
			     GConverterFlags flags,
Packit ae235b
			     gsize      *bytes_read,
Packit ae235b
			     gsize      *bytes_written,
Packit ae235b
			     GError    **error)
Packit ae235b
{
Packit ae235b
  GZlibDecompressor *decompressor;
Packit ae235b
  int res;
Packit ae235b
Packit ae235b
  decompressor = G_ZLIB_DECOMPRESSOR (converter);
Packit ae235b
Packit ae235b
  decompressor->zstream.next_in = (void *)inbuf;
Packit ae235b
  decompressor->zstream.avail_in = inbuf_size;
Packit ae235b
Packit ae235b
  decompressor->zstream.next_out = outbuf;
Packit ae235b
  decompressor->zstream.avail_out = outbuf_size;
Packit ae235b
Packit ae235b
  res = inflate (&decompressor->zstream, Z_NO_FLUSH);
Packit ae235b
Packit ae235b
  if (res == Z_DATA_ERROR || res == Z_NEED_DICT)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
Packit ae235b
			   _("Invalid compressed data"));
Packit ae235b
      return G_CONVERTER_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (res == Z_MEM_ERROR)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
			   _("Not enough memory"));
Packit ae235b
      return G_CONVERTER_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
    if (res == Z_STREAM_ERROR)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
		   _("Internal error: %s"), decompressor->zstream.msg);
Packit ae235b
      return G_CONVERTER_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
    if (res == Z_BUF_ERROR)
Packit ae235b
      {
Packit ae235b
	if (flags & G_CONVERTER_FLUSH)
Packit ae235b
	  return G_CONVERTER_FLUSHED;
Packit ae235b
Packit ae235b
	/* Z_FINISH not set, so this means no progress could be made */
Packit ae235b
	/* We do have output space, so this should only happen if we
Packit ae235b
	   have no input but need some */
Packit ae235b
Packit ae235b
	g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
Packit ae235b
			     _("Need more input"));
Packit ae235b
	return G_CONVERTER_ERROR;
Packit ae235b
      }
Packit ae235b
Packit ae235b
  g_assert (res == Z_OK || res == Z_STREAM_END);
Packit ae235b
Packit ae235b
  *bytes_read = inbuf_size - decompressor->zstream.avail_in;
Packit ae235b
  *bytes_written = outbuf_size - decompressor->zstream.avail_out;
Packit ae235b
Packit ae235b
#if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240
Packit ae235b
  if (decompressor->header_data != NULL &&
Packit ae235b
      decompressor->header_data->gzheader.done == 1)
Packit ae235b
    {
Packit ae235b
      HeaderData *data = decompressor->header_data;
Packit ae235b
Packit ae235b
      /* So we don't notify again */
Packit ae235b
      data->gzheader.done = 2;
Packit ae235b
Packit ae235b
      data->file_info = g_file_info_new ();
Packit ae235b
      g_file_info_set_attribute_uint64 (data->file_info,
Packit ae235b
                                        G_FILE_ATTRIBUTE_TIME_MODIFIED,
Packit ae235b
                                        data->gzheader.time);
Packit ae235b
      g_file_info_set_attribute_uint32 (data->file_info,
Packit ae235b
                                        G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
Packit ae235b
                                        0);
Packit ae235b
Packit ae235b
      if (data->filename[0] != '\0')
Packit ae235b
        g_file_info_set_attribute_byte_string (data->file_info,
Packit ae235b
                                               G_FILE_ATTRIBUTE_STANDARD_NAME,
Packit ae235b
                                               data->filename);
Packit ae235b
Packit ae235b
      g_object_notify (G_OBJECT (decompressor), "file-info");
Packit ae235b
    }
Packit ae235b
#endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */
Packit ae235b
Packit ae235b
  if (res == Z_STREAM_END)
Packit ae235b
    return G_CONVERTER_FINISHED;
Packit ae235b
  return G_CONVERTER_CONVERTED;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_zlib_decompressor_iface_init (GConverterIface *iface)
Packit ae235b
{
Packit ae235b
  iface->convert = g_zlib_decompressor_convert;
Packit ae235b
  iface->reset = g_zlib_decompressor_reset;
Packit ae235b
}