Check if hb is NULL.
[elinks.git] / src / encoding / bzip2.c
blob5cc21f24623e8b4a354e319be187737d53234827
1 /* Bzip2 encoding (ENCODING_BZIP2) backend */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdio.h>
8 #include <string.h>
9 #ifdef HAVE_UNISTD_H
10 #include <unistd.h>
11 #endif
12 #ifdef HAVE_BZLIB_H
13 #include <bzlib.h> /* Everything needs this after stdio.h */
14 #endif
15 #include <errno.h>
17 #include "elinks.h"
19 #include "encoding/bzip2.h"
20 #include "encoding/encoding.h"
21 #include "util/memory.h"
23 /* How many bytes of compressed data to read before decompressing.
24 * This is currently defined as BZ_MAX_UNUSED to make the behaviour
25 * similar to BZ2_bzRead; but other values would work too. */
26 #define ELINKS_BZ_BUFFER_LENGTH BZ_MAX_UNUSED
28 struct bz2_enc_data {
29 bz_stream fbz_stream;
31 /* The file descriptor from which we read. */
32 int fdread;
34 /* Initially 0; set to 1 when BZ2_bzDecompress indicates
35 * BZ_STREAM_END, which means it has found the bzip2-specific
36 * end-of-stream marker and all data has been decompressed.
37 * Then we neither read from the file nor call BZ2_bzDecompress
38 * any more. */
39 int last_read:1;
40 int after_end:1;
42 /* A buffer for data that has been read from the file but not
43 * yet decompressed. fbz_stream.next_in and fbz_stream.avail_in
44 * refer to this buffer. */
45 unsigned char buf[ELINKS_BZ_BUFFER_LENGTH];
48 static int
49 bzip2_open(struct stream_encoded *stream, int fd)
51 /* A zero-initialized bz_stream. The compiler ensures that all
52 * pointer members in it are null. (Can't do this with memset
53 * because C99 does not require all-bits-zero to be a null
54 * pointer.) */
55 static const bz_stream null_bz_stream = {0};
57 struct bz2_enc_data *data = mem_alloc(sizeof(*data));
58 int err;
60 stream->data = NULL;
61 if (!data) {
62 return -1;
65 /* Initialize all members of *data, except data->buf[], which
66 * will be initialized on demand by bzip2_read. */
67 copy_struct(&data->fbz_stream, &null_bz_stream);
68 data->fdread = fd;
69 data->last_read = 0;
71 err = BZ2_bzDecompressInit(&data->fbz_stream, 0, 0);
72 if (err != BZ_OK) {
73 mem_free(data);
74 return -1;
77 stream->data = data;
79 return 0;
82 static int
83 bzip2_read(struct stream_encoded *stream, unsigned char *buf, int len)
85 struct bz2_enc_data *data = (struct bz2_enc_data *) stream->data;
86 int err = 0;
88 if (!data) return -1;
90 assert(len > 0);
92 if (data->last_read) return 0;
94 data->fbz_stream.avail_out = len;
95 data->fbz_stream.next_out = buf;
97 do {
98 if (data->fbz_stream.avail_in == 0) {
99 int l = safe_read(data->fdread, data->buf,
100 ELINKS_BZ_BUFFER_LENGTH);
102 if (l == -1) {
103 if (errno == EAGAIN)
104 break;
105 else
106 return -1; /* I/O error */
107 } else if (l == 0) {
108 /* EOF. It is error: we wait for more bytes */
109 return -1;
112 data->fbz_stream.next_in = data->buf;
113 data->fbz_stream.avail_in = l;
116 err = BZ2_bzDecompress(&data->fbz_stream);
117 if (err == BZ_STREAM_END) {
118 data->last_read = 1;
119 break;
120 } else if (err != BZ_OK) {
121 return -1;
123 } while (data->fbz_stream.avail_out > 0);
125 assert(len - data->fbz_stream.avail_out == data->fbz_stream.next_out - (char *) buf);
126 return len - data->fbz_stream.avail_out;
129 #ifdef CONFIG_SMALL
130 #define BZIP2_SMALL 1
131 #else
132 #define BZIP2_SMALL 0
133 #endif
135 static unsigned char *
136 bzip2_decode_buffer(struct stream_encoded *st, unsigned char *data, int len, int *new_len)
138 struct bz2_enc_data *enc_data = (struct bz2_enc_data *)st->data;
139 bz_stream *stream = &enc_data->fbz_stream;
140 unsigned char *buffer = NULL;
141 int error;
143 *new_len = 0; /* default, left there if an error occurs */
145 stream->next_in = data;
146 stream->avail_in = len;
147 stream->total_out_lo32 = 0;
148 stream->total_out_hi32 = 0;
150 do {
151 unsigned char *new_buffer;
152 size_t size = stream->total_out_lo32 + MAX_STR_LEN;
154 /* FIXME: support for 64 bit. real size is
156 * (total_in_hi32 << * 32) + total_in_lo32
158 * --jonas */
159 assertm(!stream->total_out_hi32, "64 bzip2 decoding not supported");
161 new_buffer = mem_realloc(buffer, size);
162 if (!new_buffer) {
163 error = BZ_MEM_ERROR;
164 break;
167 buffer = new_buffer;
168 stream->next_out = buffer + stream->total_out_lo32;
169 stream->avail_out = MAX_STR_LEN;
171 error = BZ2_bzDecompress(stream);
172 if (error == BZ_STREAM_END) {
173 break;
176 /* Apparently BZ_STREAM_END is not forced when the end of input
177 * is reached. At least lindi- reported that it caused a
178 * reproducable infinite loop. Maybe it has to do with decoding
179 * an incomplete file. */
180 } while (error == BZ_OK && stream->avail_in > 0);
182 if (error == BZ_STREAM_END) {
183 BZ2_bzDecompressEnd(stream);
184 enc_data->after_end = 1;
185 error = BZ_OK;
188 if (error == BZ_OK) {
189 *new_len = stream->total_out_lo32;
190 return buffer;
191 } else {
192 if (buffer) mem_free(buffer);
193 return NULL;
197 static void
198 bzip2_close(struct stream_encoded *stream)
200 struct bz2_enc_data *data = (struct bz2_enc_data *) stream->data;
202 if (data) {
203 if (!data->after_end) {
204 BZ2_bzDecompressEnd(&data->fbz_stream);
206 if (data->fdread != -1) {
207 close(data->fdread);
209 mem_free(data);
210 stream->data = 0;
214 static const unsigned char *const bzip2_extensions[] = { ".bz2", ".tbz", NULL };
216 const struct decoding_backend bzip2_decoding_backend = {
217 "bzip2",
218 bzip2_extensions,
219 bzip2_open,
220 bzip2_read,
221 bzip2_decode_buffer,
222 bzip2_close,