Update Icelandic translation
[yelp.git] / libyelp / yelp-bz2-decompressor.c
blobe1053815e16784129d94d62ada4c029f683a0ade
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2009 Red Hat, Inc.
4 * Copyright (C) 2009 Shaun McCance <shaunm@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Shaun McCance <shaunm@gnome.org>
22 #include "config.h"
24 #include <errno.h>
25 #include <string.h>
27 #include <glib/gi18n.h>
29 #include "yelp-bz2-decompressor.h"
31 static void yelp_bz2_decompressor_iface_init (GConverterIface *iface);
33 struct _YelpBz2Decompressor
35 GObject parent_instance;
37 bz_stream bzstream;
40 G_DEFINE_TYPE_WITH_CODE (YelpBz2Decompressor, yelp_bz2_decompressor, G_TYPE_OBJECT,
41 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
42 yelp_bz2_decompressor_iface_init))
44 static void
45 yelp_bz2_decompressor_finalize (GObject *object)
47 YelpBz2Decompressor *decompressor;
49 decompressor = YELP_BZ2_DECOMPRESSOR (object);
51 BZ2_bzDecompressEnd (&decompressor->bzstream);
53 G_OBJECT_CLASS (yelp_bz2_decompressor_parent_class)->finalize (object);
56 static void
57 yelp_bz2_decompressor_init (YelpBz2Decompressor *decompressor)
61 static void
62 yelp_bz2_decompressor_constructed (GObject *object)
64 YelpBz2Decompressor *decompressor;
65 int res;
67 decompressor = YELP_BZ2_DECOMPRESSOR (object);
69 res = BZ2_bzDecompressInit (&decompressor->bzstream, 0, FALSE);
71 if (res == BZ_MEM_ERROR )
72 g_error ("YelpBz2Decompressor: Not enough memory for bzip2 use");
74 if (res != BZ_OK)
75 g_error ("YelpBz2Decompressor: Unexpected bzip2 error");
78 static void
79 yelp_bz2_decompressor_class_init (YelpBz2DecompressorClass *klass)
81 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
83 gobject_class->finalize = yelp_bz2_decompressor_finalize;
84 gobject_class->constructed = yelp_bz2_decompressor_constructed;
87 YelpBz2Decompressor *
88 yelp_bz2_decompressor_new (void)
90 YelpBz2Decompressor *decompressor;
92 decompressor = g_object_new (YELP_TYPE_BZ2_DECOMPRESSOR, NULL);
94 return decompressor;
97 static void
98 yelp_bz2_decompressor_reset (GConverter *converter)
100 YelpBz2Decompressor *decompressor = YELP_BZ2_DECOMPRESSOR (converter);
101 int res;
103 /* libbzip2 doesn't have a reset function. Ending and reiniting
104 * might do the trick. But this is untested. If reset matters
105 * to you, test this.
107 BZ2_bzDecompressEnd (&decompressor->bzstream);
108 res = BZ2_bzDecompressInit (&decompressor->bzstream, 0, FALSE);
110 if (res == BZ_MEM_ERROR )
111 g_error ("YelpBz2Decompressor: Not enough memory for bzip2 use");
113 if (res != BZ_OK)
114 g_error ("YelpBz2Decompressor: Unexpected bzip2 error");
117 static GConverterResult
118 yelp_bz2_decompressor_convert (GConverter *converter,
119 const void *inbuf,
120 gsize inbuf_size,
121 void *outbuf,
122 gsize outbuf_size,
123 GConverterFlags flags,
124 gsize *bytes_read,
125 gsize *bytes_written,
126 GError **error)
128 YelpBz2Decompressor *decompressor;
129 int res;
131 decompressor = YELP_BZ2_DECOMPRESSOR (converter);
133 decompressor->bzstream.next_in = (void *)inbuf;
134 decompressor->bzstream.avail_in = inbuf_size;
136 decompressor->bzstream.next_out = outbuf;
137 decompressor->bzstream.avail_out = outbuf_size;
139 res = BZ2_bzDecompress (&decompressor->bzstream);
141 if (res == BZ_DATA_ERROR || res == BZ_DATA_ERROR_MAGIC) {
142 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
143 _("Invalid compressed data"));
144 return G_CONVERTER_ERROR;
147 if (res == BZ_MEM_ERROR) {
148 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
149 _("Not enough memory"));
150 return G_CONVERTER_ERROR;
153 if (res == BZ_OK || res == BZ_STREAM_END) {
154 *bytes_read = inbuf_size - decompressor->bzstream.avail_in;
155 *bytes_written = outbuf_size - decompressor->bzstream.avail_out;
157 if (res == BZ_STREAM_END)
158 return G_CONVERTER_FINISHED;
159 return G_CONVERTER_CONVERTED;
162 g_assert_not_reached ();
165 static void
166 yelp_bz2_decompressor_iface_init (GConverterIface *iface)
168 iface->convert = yelp_bz2_decompressor_convert;
169 iface->reset = yelp_bz2_decompressor_reset;