summaryrefslogtreecommitdiff
path: root/src/libostree/ostree-lzma-compressor.c
blob: cefd256ff1d88a738127fef81cbe25e5d9a75cdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright (C) 2014 Colin Walters <walters@verbum.org>
 * Copyright (C) 2022 Igalia S.L.
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * 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
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "ostree-lzma-common.h"
#include "ostree-lzma-compressor.h"

#include <errno.h>
#include <lzma.h>
#include <string.h>

enum
{
  PROP_0,
  PROP_PARAMS
};

/**
 * SECTION:ostree-lzma-compressor
 * @title: LZMA compressor
 *
 * An implementation of #GConverter that compresses data using
 * LZMA.
 */

static void _ostree_lzma_compressor_iface_init (GConverterIface *iface);

/**
 * OstreeLzmaCompressor:
 *
 * Zlib decompression
 */
struct _OstreeLzmaCompressor
{
  GObject parent_instance;

  GVariant *params;
  lzma_stream lstream;
  gboolean initialized;
};

G_DEFINE_TYPE_WITH_CODE (OstreeLzmaCompressor, _ostree_lzma_compressor, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
                                                _ostree_lzma_compressor_iface_init))

static void
_ostree_lzma_compressor_finalize (GObject *object)
{
  OstreeLzmaCompressor *self = OSTREE_LZMA_COMPRESSOR (object);

  lzma_end (&self->lstream);
  g_clear_pointer (&self->params, g_variant_unref);

  G_OBJECT_CLASS (_ostree_lzma_compressor_parent_class)->finalize (object);
}

static void
_ostree_lzma_compressor_set_property (GObject *object, guint prop_id, const GValue *value,
                                      GParamSpec *pspec)
{
  OstreeLzmaCompressor *self = OSTREE_LZMA_COMPRESSOR (object);

  switch (prop_id)
    {
    case PROP_PARAMS:
      self->params = g_value_get_variant (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
_ostree_lzma_compressor_get_property (GObject *object, guint prop_id, GValue *value,
                                      GParamSpec *pspec)
{
  OstreeLzmaCompressor *self = OSTREE_LZMA_COMPRESSOR (object);

  switch (prop_id)
    {
    case PROP_PARAMS:
      g_value_set_variant (value, self->params);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
_ostree_lzma_compressor_init (OstreeLzmaCompressor *self)
{
  lzma_stream tmp = LZMA_STREAM_INIT;
  self->lstream = tmp;
}

static void
_ostree_lzma_compressor_class_init (OstreeLzmaCompressorClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = _ostree_lzma_compressor_finalize;
  gobject_class->get_property = _ostree_lzma_compressor_get_property;
  gobject_class->set_property = _ostree_lzma_compressor_set_property;

  g_object_class_install_property (
      gobject_class, PROP_PARAMS,
      g_param_spec_variant ("params", "", "", G_VARIANT_TYPE ("a{sv}"), NULL,
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}

OstreeLzmaCompressor *
_ostree_lzma_compressor_new (GVariant *params)
{
  return g_object_new (OSTREE_TYPE_LZMA_COMPRESSOR, "params", params, NULL);
}

static void
_ostree_lzma_compressor_reset (GConverter *converter)
{
  OstreeLzmaCompressor *self = OSTREE_LZMA_COMPRESSOR (converter);

  if (self->initialized)
    {
      lzma_stream tmp = LZMA_STREAM_INIT;
      lzma_end (&self->lstream);
      self->lstream = tmp;
      self->initialized = FALSE;
    }
}

static GConverterResult
_ostree_lzma_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)
{
  OstreeLzmaCompressor *self = OSTREE_LZMA_COMPRESSOR (converter);
  int res;
  lzma_action action;

  if (inbuf_size != 0 && outbuf_size == 0)
    {
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE, "Output buffer too small");
      return G_CONVERTER_ERROR;
    }

  if (!self->initialized)
    {
      res = lzma_easy_encoder (&self->lstream, 8, LZMA_CHECK_CRC64);
      if (res != LZMA_OK)
        goto out;
      self->initialized = TRUE;
    }

  self->lstream.next_in = (void *)inbuf;
  self->lstream.avail_in = inbuf_size;

  self->lstream.next_out = outbuf;
  self->lstream.avail_out = outbuf_size;

  action = LZMA_RUN;
  if (flags & G_CONVERTER_INPUT_AT_END)
    action = LZMA_FINISH;
  else if (flags & G_CONVERTER_FLUSH)
    action = LZMA_SYNC_FLUSH;

  res = lzma_code (&self->lstream, action);
  if (res != LZMA_OK && res != LZMA_STREAM_END)
    goto out;

  *bytes_read = inbuf_size - self->lstream.avail_in;
  *bytes_written = outbuf_size - self->lstream.avail_out;

out:
  return _ostree_lzma_return (res, error);
}

static void
_ostree_lzma_compressor_iface_init (GConverterIface *iface)
{
  iface->convert = _ostree_lzma_compressor_convert;
  iface->reset = _ostree_lzma_compressor_reset;
}