Handle mailcap's copiousoutput without an external pager.
[elinks.git] / src / encoding / bzip2.c
blob8e7139a6281cad5742fc071195d013fc23c6ccd6
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;
41 /* A buffer for data that has been read from the file but not
42 * yet decompressed. fbz_stream.next_in and fbz_stream.avail_in
43 * refer to this buffer. */
44 unsigned char buf[ELINKS_BZ_BUFFER_LENGTH];
47 static int
48 bzip2_open(struct stream_encoded *stream, int fd)
50 /* A zero-initialized bz_stream. The compiler ensures that all
51 * pointer members in it are null. (Can't do this with memset
52 * because C99 does not require all-bits-zero to be a null
53 * pointer.) */
54 static const bz_stream null_bz_stream = {0};
56 struct bz2_enc_data *data = mem_alloc(sizeof(*data));
57 int err;
59 stream->data = NULL;
60 if (!data) {
61 return -1;
64 /* Initialize all members of *data, except data->buf[], which
65 * will be initialized on demand by bzip2_read. */
66 copy_struct(&data->fbz_stream, &null_bz_stream);
67 data->fdread = fd;
68 data->last_read = 0;
70 err = BZ2_bzDecompressInit(&data->fbz_stream, 0, 0);
71 if (err != BZ_OK) {
72 mem_free(data);
73 return -1;
76 stream->data = data;
78 return 0;
81 static int
82 bzip2_read(struct stream_encoded *stream, unsigned char *buf, int len)
84 struct bz2_enc_data *data = (struct bz2_enc_data *) stream->data;
85 int err = 0;
87 if (!data) return -1;
89 assert(len > 0);
91 if (data->last_read) return 0;
93 data->fbz_stream.avail_out = len;
94 data->fbz_stream.next_out = buf;
96 do {
97 if (data->fbz_stream.avail_in == 0) {
98 int l = safe_read(data->fdread, data->buf,
99 ELINKS_BZ_BUFFER_LENGTH);
101 if (l == -1) {
102 if (errno == EAGAIN)
103 break;
104 else
105 return -1; /* I/O error */
106 } else if (l == 0) {
107 /* EOF. It is error: we wait for more bytes */
108 return -1;
111 data->fbz_stream.next_in = data->buf;
112 data->fbz_stream.avail_in = l;
115 err = BZ2_bzDecompress(&data->fbz_stream);
116 if (err == BZ_STREAM_END) {
117 data->last_read = 1;
118 break;
119 } else if (err != BZ_OK) {
120 return -1;
122 } while (data->fbz_stream.avail_out > 0);
124 assert(len - data->fbz_stream.avail_out == data->fbz_stream.next_out - (char *) buf);
125 return len - data->fbz_stream.avail_out;
128 #ifdef CONFIG_SMALL
129 #define BZIP2_SMALL 1
130 #else
131 #define BZIP2_SMALL 0
132 #endif
134 static unsigned char *
135 bzip2_decode_buffer(unsigned char *data, int len, int *new_len)
137 bz_stream stream;
138 unsigned char *buffer = NULL;
139 int error;
141 *new_len = 0; /* default, left there if an error occurs */
143 memset(&stream, 0, sizeof(bz_stream));
144 stream.next_in = data;
145 stream.avail_in = len;
147 if (BZ2_bzDecompressInit(&stream, 0, BZIP2_SMALL) != BZ_OK)
148 return NULL;
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 error = BZ_OK;
174 break;
177 /* Apparently BZ_STREAM_END is not forced when the end of input
178 * is reached. At least lindi- reported that it caused a
179 * reproducable infinite loop. Maybe it has to do with decoding
180 * an incomplete file. */
181 } while (error == BZ_OK && stream.avail_in > 0);
183 BZ2_bzDecompressEnd(&stream);
185 if (error == BZ_OK) {
186 *new_len = stream.total_out_lo32;
187 return buffer;
188 } else {
189 if (buffer) mem_free(buffer);
190 return NULL;
194 static void
195 bzip2_close(struct stream_encoded *stream)
197 struct bz2_enc_data *data = (struct bz2_enc_data *) stream->data;
199 if (data) {
200 BZ2_bzDecompressEnd(&data->fbz_stream);
201 close(data->fdread);
202 mem_free(data);
203 stream->data = 0;
207 static const unsigned char *const bzip2_extensions[] = { ".bz2", ".tbz", NULL };
209 const struct decoding_backend bzip2_decoding_backend = {
210 "bzip2",
211 bzip2_extensions,
212 bzip2_open,
213 bzip2_read,
214 bzip2_decode_buffer,
215 bzip2_close,