1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
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
))
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
);
59 yelp_bz2_decompressor_init (YelpBz2Decompressor
*decompressor
)
64 yelp_bz2_decompressor_constructed (GObject
*object
)
66 YelpBz2Decompressor
*decompressor
;
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");
77 g_error ("YelpBz2Decompressor: Unexpected bzip2 error");
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
;
90 yelp_bz2_decompressor_new (void)
92 YelpBz2Decompressor
*decompressor
;
94 decompressor
= g_object_new (YELP_TYPE_BZ2_DECOMPRESSOR
, NULL
);
100 yelp_bz2_decompressor_reset (GConverter
*converter
)
102 YelpBz2Decompressor
*decompressor
= YELP_BZ2_DECOMPRESSOR (converter
);
105 /* libbzip2 doesn't have a reset function. Ending and reiniting
106 * might do the trick. But this is untested. If reset matters
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");
116 g_error ("YelpBz2Decompressor: Unexpected bzip2 error");
119 static GConverterResult
120 yelp_bz2_decompressor_convert (GConverter
*converter
,
125 GConverterFlags flags
,
127 gsize
*bytes_written
,
130 YelpBz2Decompressor
*decompressor
;
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 ();
169 yelp_bz2_decompressor_iface_init (GConverterIface
*iface
)
171 iface
->convert
= yelp_bz2_decompressor_convert
;
172 iface
->reset
= yelp_bz2_decompressor_reset
;