summaryrefslogtreecommitdiff
path: root/libyelp/yelp-lzma-decompressor.c
blob: 8e04cfdf18ba2e15974e0bc6ffaaf595b7711e39 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Copyright (C) 2009 Red Hat, Inc.
 * Copyright (C) 2009 Shaun McCance <shaunm@gnome.org>
 *
 * 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/>.
 *
 * Author: Shaun McCance <shaunm@gnome.org>
 */

#include "config.h"

#include "yelp-lzma-decompressor.h"

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

#include <lzma.h>

#include <glib/gi18n.h>
#include <gio/gio.h>


static void yelp_lzma_decompressor_iface_init          (GConverterIface *iface);

struct _YelpLzmaDecompressor
{
    GObject parent_instance;

    lzma_stream lzmastream;
};

G_DEFINE_TYPE_WITH_CODE (YelpLzmaDecompressor, yelp_lzma_decompressor, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
                                                yelp_lzma_decompressor_iface_init))

static void
yelp_lzma_decompressor_finalize (GObject *object)
{
    YelpLzmaDecompressor *decompressor;

    decompressor = YELP_LZMA_DECOMPRESSOR (object);

    lzma_end (&decompressor->lzmastream);

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

static void
yelp_lzma_decompressor_init (YelpLzmaDecompressor *decompressor)
{
}

static void
yelp_lzma_decompressor_constructed (GObject *object)
{
    YelpLzmaDecompressor *decompressor;
    lzma_stream tmp = LZMA_STREAM_INIT;
    lzma_ret res;

    decompressor = YELP_LZMA_DECOMPRESSOR (object);

    decompressor->lzmastream = tmp;
    res = lzma_auto_decoder (&decompressor->lzmastream, SIZE_MAX, 0);

    if (res == LZMA_MEM_ERROR)
        g_error ("YelpLzmaDecompressor: Not enough memory for lzma use");

    if (res == LZMA_OPTIONS_ERROR)
        g_error ("YelpLzmaDecompressor: Unsupported flags");

    if (res != LZMA_OK)
        g_error ("YelpLzmaDecompressor: Unexpected lzma error");
}

static void
yelp_lzma_decompressor_class_init (YelpLzmaDecompressorClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

    gobject_class->finalize = yelp_lzma_decompressor_finalize;
    gobject_class->constructed = yelp_lzma_decompressor_constructed;
}

YelpLzmaDecompressor *
yelp_lzma_decompressor_new (void)
{
    YelpLzmaDecompressor *decompressor;

    decompressor = g_object_new (YELP_TYPE_LZMA_DECOMPRESSOR, NULL);

    return decompressor;
}

static void
yelp_lzma_decompressor_reset (GConverter *converter)
{
    YelpLzmaDecompressor *decompressor = YELP_LZMA_DECOMPRESSOR (converter);
    lzma_ret res;

    /* lzma doesn't have a reset function.  Ending and reiniting
     * might do the trick.  But this is untested.  If reset matters
     * to you, test this.
     */
    lzma_end (&decompressor->lzmastream);
    res = lzma_code (&decompressor->lzmastream, LZMA_RUN);

    if (res == LZMA_MEM_ERROR )
        g_error ("YelpLzmaDecompressor: Not enough memory for lzma use");

    if (res != LZMA_OK)
        g_error ("YelpLzmaDecompressor: Unexpected lzma error");
}

static GConverterResult
yelp_lzma_decompressor_convert (GConverter *converter,
                                const void *inbuf,
                                gsize       inbuf_size,
                                void       *outbuf,
                                gsize       outbuf_size,
                                GConverterFlags flags,
                                gsize      *bytes_read,
                                gsize      *bytes_written,
                                GError    **error)
{
    YelpLzmaDecompressor *decompressor;
    lzma_ret res;

    decompressor = YELP_LZMA_DECOMPRESSOR (converter);

    decompressor->lzmastream.next_in = (void *)inbuf;
    decompressor->lzmastream.avail_in = inbuf_size;

    decompressor->lzmastream.next_out = outbuf;
    decompressor->lzmastream.avail_out = outbuf_size;

    res = lzma_code (&decompressor->lzmastream, LZMA_RUN);

    if (res == LZMA_DATA_ERROR) {
        g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
                             _("Invalid compressed data"));
        return G_CONVERTER_ERROR;
    }

    if (res == LZMA_MEM_ERROR) {
        g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                             _("Not enough memory"));
        return G_CONVERTER_ERROR;
    }

    if (res == LZMA_OK || res == LZMA_STREAM_END) {
        *bytes_read = inbuf_size - decompressor->lzmastream.avail_in;
        *bytes_written = outbuf_size - decompressor->lzmastream.avail_out;

        if (res == LZMA_STREAM_END)
            return G_CONVERTER_FINISHED;
        return G_CONVERTER_CONVERTED;
    }

    g_assert_not_reached ();
}

static void
yelp_lzma_decompressor_iface_init (GConverterIface *iface)
{
  iface->convert = yelp_lzma_decompressor_convert;
  iface->reset = yelp_lzma_decompressor_reset;
}