Blame src/libostree/ostree-lzma-compressor.c

rpm-build 0fba15
/*
rpm-build 0fba15
 * Copyright (C) 2014 Colin Walters <walters@verbum.org>
rpm-build 0fba15
 *
rpm-build 0fba15
 * SPDX-License-Identifier: LGPL-2.0+
rpm-build 0fba15
 *
rpm-build 0fba15
 * This library is free software; you can redistribute it and/or
rpm-build 0fba15
 * modify it under the terms of the GNU Lesser General Public
rpm-build 0fba15
 * License as published by the Free Software Foundation; either
rpm-build 0fba15
 * version 2 of the License, or (at your option) any later version.
rpm-build 0fba15
 *
rpm-build 0fba15
 * This library is distributed in the hope that it will be useful,
rpm-build 0fba15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 0fba15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 0fba15
 * Lesser General Public License for more details.
rpm-build 0fba15
 *
rpm-build 0fba15
 * You should have received a copy of the GNU Lesser General
rpm-build 0fba15
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
rpm-build 0fba15
 */
rpm-build 0fba15
rpm-build 0fba15
#include "config.h"
rpm-build 0fba15
rpm-build 0fba15
#include "ostree-lzma-compressor.h"
rpm-build 0fba15
#include "ostree-lzma-common.h"
rpm-build 0fba15
rpm-build 0fba15
#include <errno.h>
rpm-build 0fba15
#include <lzma.h>
rpm-build 0fba15
#include <string.h>
rpm-build 0fba15
rpm-build 0fba15
enum {
rpm-build 0fba15
  PROP_0,
rpm-build 0fba15
  PROP_PARAMS
rpm-build 0fba15
};
rpm-build 0fba15
rpm-build 0fba15
/**
rpm-build 0fba15
 * SECTION:ostree-lzma-compressor
rpm-build 0fba15
 * @title: LZMA compressor
rpm-build 0fba15
 *
rpm-build 0fba15
 * An implementation of #GConverter that compresses data using
rpm-build 0fba15
 * LZMA.
rpm-build 0fba15
 */
rpm-build 0fba15
rpm-build 0fba15
static void _ostree_lzma_compressor_iface_init          (GConverterIface *iface);
rpm-build 0fba15
rpm-build 0fba15
/**
rpm-build 0fba15
 * OstreeLzmaCompressor:
rpm-build 0fba15
 *
rpm-build 0fba15
 * Zlib decompression
rpm-build 0fba15
 */
rpm-build 0fba15
struct _OstreeLzmaCompressor
rpm-build 0fba15
{
rpm-build 0fba15
  GObject parent_instance;
rpm-build 0fba15
rpm-build 0fba15
  GVariant *params;
rpm-build 0fba15
  lzma_stream lstream;
rpm-build 0fba15
  gboolean initialized;
rpm-build 0fba15
};
rpm-build 0fba15
rpm-build 0fba15
G_DEFINE_TYPE_WITH_CODE (OstreeLzmaCompressor, _ostree_lzma_compressor,
rpm-build 0fba15
			 G_TYPE_OBJECT,
rpm-build 0fba15
			 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
rpm-build 0fba15
						_ostree_lzma_compressor_iface_init))
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
_ostree_lzma_compressor_finalize (GObject *object)
rpm-build 0fba15
{
rpm-build 0fba15
  OstreeLzmaCompressor *self = OSTREE_LZMA_COMPRESSOR (object);
rpm-build 0fba15
rpm-build 0fba15
  lzma_end (&self->lstream);
rpm-build 0fba15
  g_clear_pointer (&self->params, (GDestroyNotify)g_variant_unref);
rpm-build 0fba15
rpm-build 0fba15
  G_OBJECT_CLASS (_ostree_lzma_compressor_parent_class)->finalize (object);
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
_ostree_lzma_compressor_set_property (GObject      *object,
rpm-build 0fba15
				      guint         prop_id,
rpm-build 0fba15
				      const GValue *value,
rpm-build 0fba15
				      GParamSpec   *pspec)
rpm-build 0fba15
{
rpm-build 0fba15
  OstreeLzmaCompressor *self = OSTREE_LZMA_COMPRESSOR (object);
rpm-build 0fba15
rpm-build 0fba15
  switch (prop_id)
rpm-build 0fba15
    {
rpm-build 0fba15
    case PROP_PARAMS:
rpm-build 0fba15
      self->params = g_value_get_variant (value);
rpm-build 0fba15
      break;
rpm-build 0fba15
rpm-build 0fba15
    default:
rpm-build 0fba15
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
rpm-build 0fba15
      break;
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
_ostree_lzma_compressor_get_property (GObject    *object,
rpm-build 0fba15
				      guint       prop_id,
rpm-build 0fba15
				      GValue     *value,
rpm-build 0fba15
				      GParamSpec *pspec)
rpm-build 0fba15
{
rpm-build 0fba15
  OstreeLzmaCompressor *self = OSTREE_LZMA_COMPRESSOR (object);
rpm-build 0fba15
rpm-build 0fba15
  switch (prop_id)
rpm-build 0fba15
    {
rpm-build 0fba15
    case PROP_PARAMS:
rpm-build 0fba15
      g_value_set_variant (value, self->params);
rpm-build 0fba15
      break;
rpm-build 0fba15
rpm-build 0fba15
    default:
rpm-build 0fba15
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
rpm-build 0fba15
      break;
rpm-build 0fba15
    }
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
_ostree_lzma_compressor_init (OstreeLzmaCompressor *self)
rpm-build 0fba15
{
rpm-build 0fba15
  lzma_stream tmp = LZMA_STREAM_INIT;
rpm-build 0fba15
  self->lstream = tmp;
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
_ostree_lzma_compressor_class_init (OstreeLzmaCompressorClass *klass)
rpm-build 0fba15
{
rpm-build 0fba15
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
rpm-build 0fba15
rpm-build 0fba15
  gobject_class->finalize = _ostree_lzma_compressor_finalize;
rpm-build 0fba15
  gobject_class->get_property = _ostree_lzma_compressor_get_property;
rpm-build 0fba15
  gobject_class->set_property = _ostree_lzma_compressor_set_property;
rpm-build 0fba15
rpm-build 0fba15
  g_object_class_install_property (gobject_class,
rpm-build 0fba15
				   PROP_PARAMS,
rpm-build 0fba15
				   g_param_spec_variant ("params", "", "",
rpm-build 0fba15
							 G_VARIANT_TYPE ("a{sv}"),
rpm-build 0fba15
							 NULL,
rpm-build 0fba15
							 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
rpm-build 0fba15
							 G_PARAM_STATIC_STRINGS));
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
OstreeLzmaCompressor *
rpm-build 0fba15
_ostree_lzma_compressor_new (GVariant *params)
rpm-build 0fba15
{
rpm-build 0fba15
  return g_object_new (OSTREE_TYPE_LZMA_COMPRESSOR,
rpm-build 0fba15
		       "params", params,
rpm-build 0fba15
		       NULL);
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
_ostree_lzma_compressor_reset (GConverter *converter)
rpm-build 0fba15
{
rpm-build 0fba15
  OstreeLzmaCompressor *self = OSTREE_LZMA_COMPRESSOR (converter);
rpm-build 0fba15
rpm-build 0fba15
  if (self->initialized)
rpm-build 0fba15
    {
rpm-build 0fba15
      lzma_stream tmp = LZMA_STREAM_INIT;
rpm-build 0fba15
      lzma_end (&self->lstream);
rpm-build 0fba15
      self->lstream = tmp;
rpm-build 0fba15
      self->initialized = FALSE;
rpm-build 0fba15
    }
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static GConverterResult
rpm-build 0fba15
_ostree_lzma_compressor_convert (GConverter *converter,
rpm-build 0fba15
				 const void *inbuf,
rpm-build 0fba15
				 gsize       inbuf_size,
rpm-build 0fba15
				 void       *outbuf,
rpm-build 0fba15
				 gsize       outbuf_size,
rpm-build 0fba15
				 GConverterFlags flags,
rpm-build 0fba15
				 gsize      *bytes_read,
rpm-build 0fba15
				 gsize      *bytes_written,
rpm-build 0fba15
				 GError    **error)
rpm-build 0fba15
{
rpm-build 0fba15
  OstreeLzmaCompressor *self = OSTREE_LZMA_COMPRESSOR (converter);
rpm-build 0fba15
  int res;
rpm-build 0fba15
  lzma_action action; 
rpm-build 0fba15
rpm-build 0fba15
  if (inbuf_size != 0 && outbuf_size == 0)
rpm-build 0fba15
    {
rpm-build 0fba15
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
rpm-build 0fba15
         "Output buffer too small");
rpm-build 0fba15
      return G_CONVERTER_ERROR;
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  if (!self->initialized)
rpm-build 0fba15
    {
rpm-build 0fba15
      res = lzma_easy_encoder (&self->lstream, 8, LZMA_CHECK_CRC64);
rpm-build 0fba15
      if (res != LZMA_OK)
rpm-build 0fba15
        goto out;
rpm-build 0fba15
      self->initialized = TRUE;
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  self->lstream.next_in = (void *)inbuf;
rpm-build 0fba15
  self->lstream.avail_in = inbuf_size;
rpm-build 0fba15
rpm-build 0fba15
  self->lstream.next_out = outbuf;
rpm-build 0fba15
  self->lstream.avail_out = outbuf_size;
rpm-build 0fba15
rpm-build 0fba15
  action = LZMA_RUN;
rpm-build 0fba15
  if (flags & G_CONVERTER_INPUT_AT_END)
rpm-build 0fba15
    action = LZMA_FINISH;
rpm-build 0fba15
  else if (flags & G_CONVERTER_FLUSH)
rpm-build 0fba15
    action = LZMA_SYNC_FLUSH;
rpm-build 0fba15
rpm-build 0fba15
  res = lzma_code (&self->lstream, action);
rpm-build 0fba15
  if (res != LZMA_OK && res != LZMA_STREAM_END)
rpm-build 0fba15
    goto out;
rpm-build 0fba15
rpm-build 0fba15
  *bytes_read = inbuf_size - self->lstream.avail_in;
rpm-build 0fba15
  *bytes_written = outbuf_size - self->lstream.avail_out;
rpm-build 0fba15
rpm-build 0fba15
 out:
rpm-build 0fba15
  return _ostree_lzma_return (res, error);
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
_ostree_lzma_compressor_iface_init (GConverterIface *iface)
rpm-build 0fba15
{
rpm-build 0fba15
  iface->convert = _ostree_lzma_compressor_convert;
rpm-build 0fba15
  iface->reset = _ostree_lzma_compressor_reset;
rpm-build 0fba15
}