data/domains: Drop unused msgs.xsl
[yelp.git] / libyelp / yelp-bz2-decompressor.c
blob98802cee13c815ba4f7a7cbc8a1698e84db5bf2a
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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Shaun McCance <shaunm@gnome.org>
24 #include "config.h"
26 #include <errno.h>
27 #include <string.h>
29 #include <glib/gi18n.h>
31 #include "yelp-bz2-decompressor.h"
33 static void yelp_bz2_decompressor_iface_init (GConverterIface *iface);
35 struct _YelpBz2Decompressor
37 GObject parent_instance;
39 bz_stream bzstream;
42 G_DEFINE_TYPE_WITH_CODE (YelpBz2Decompressor, yelp_bz2_decompressor, G_TYPE_OBJECT,
43 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
44 yelp_bz2_decompressor_iface_init))
46 static void
47 yelp_bz2_decompressor_finalize (GObject *object)
49 YelpBz2Decompressor *decompressor;
51 decompressor = YELP_BZ2_DECOMPRESSOR (object);
53 BZ2_bzDecompressEnd (&decompressor->bzstream);
55 G_OBJECT_CLASS (yelp_bz2_decompressor_parent_class)->finalize (object);
58 static void
59 yelp_bz2_decompressor_init (YelpBz2Decompressor *decompressor)
63 static void
64 yelp_bz2_decompressor_constructed (GObject *object)
66 YelpBz2Decompressor *decompressor;
67 int res;
69 decompressor = YELP_BZ2_DECOMPRESSOR (object);
71 res = BZ2_bzDecompressInit (&decompressor->bzstream, 0, FALSE);
73 if (res == BZ_MEM_ERROR )
74 g_error ("YelpBz2Decompressor: Not enough memory for bzip2 use");
76 if (res != BZ_OK)
77 g_error ("YelpBz2Decompressor: Unexpected bzip2 error");
80 static void
81 yelp_bz2_decompressor_class_init (YelpBz2DecompressorClass *klass)
83 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
85 gobject_class->finalize = yelp_bz2_decompressor_finalize;
86 gobject_class->constructed = yelp_bz2_decompressor_constructed;
89 YelpBz2Decompressor *
90 yelp_bz2_decompressor_new (void)
92 YelpBz2Decompressor *decompressor;
94 decompressor = g_object_new (YELP_TYPE_BZ2_DECOMPRESSOR, NULL);
96 return decompressor;
99 static void
100 yelp_bz2_decompressor_reset (GConverter *converter)
102 YelpBz2Decompressor *decompressor = YELP_BZ2_DECOMPRESSOR (converter);
103 int res;
105 /* libbzip2 doesn't have a reset function. Ending and reiniting
106 * might do the trick. But this is untested. If reset matters
107 * to you, test this.
109 BZ2_bzDecompressEnd (&decompressor->bzstream);
110 res = BZ2_bzDecompressInit (&decompressor->bzstream, 0, FALSE);
112 if (res == BZ_MEM_ERROR )
113 g_error ("YelpBz2Decompressor: Not enough memory for bzip2 use");
115 if (res != BZ_OK)
116 g_error ("YelpBz2Decompressor: Unexpected bzip2 error");
119 static GConverterResult
120 yelp_bz2_decompressor_convert (GConverter *converter,
121 const void *inbuf,
122 gsize inbuf_size,
123 void *outbuf,
124 gsize outbuf_size,
125 GConverterFlags flags,
126 gsize *bytes_read,
127 gsize *bytes_written,
128 GError **error)
130 YelpBz2Decompressor *decompressor;
131 gsize header_size;
132 int res;
134 decompressor = YELP_BZ2_DECOMPRESSOR (converter);
136 decompressor->bzstream.next_in = (void *)inbuf;
137 decompressor->bzstream.avail_in = inbuf_size;
139 decompressor->bzstream.next_out = outbuf;
140 decompressor->bzstream.avail_out = outbuf_size;
142 res = BZ2_bzDecompress (&decompressor->bzstream);
144 if (res == BZ_DATA_ERROR || res == BZ_DATA_ERROR_MAGIC) {
145 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
146 _("Invalid compressed data"));
147 return G_CONVERTER_ERROR;
150 if (res == BZ_MEM_ERROR) {
151 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
152 _("Not enough memory"));
153 return G_CONVERTER_ERROR;
156 if (res == BZ_OK || res == BZ_STREAM_END) {
157 *bytes_read = inbuf_size - decompressor->bzstream.avail_in;
158 *bytes_written = outbuf_size - decompressor->bzstream.avail_out;
160 if (res == BZ_STREAM_END)
161 return G_CONVERTER_FINISHED;
162 return G_CONVERTER_CONVERTED;
165 g_assert_not_reached ();
168 static void
169 yelp_bz2_decompressor_iface_init (GConverterIface *iface)
171 iface->convert = yelp_bz2_decompressor_convert;
172 iface->reset = yelp_bz2_decompressor_reset;