bzip2: buf must be the last.
[elinks.git] / src / encoding / bzip2.c
blob475c7488fdaf476010bf17f621c0eff36bc6cfa6
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 #define ELINKS_BZ_BUFFER_LENGTH BZ_MAX_UNUSED
25 struct bz2_enc_data {
26 bz_stream fbz_stream;
27 int fdread;
28 int last_read; /* If err after last bzDecompress was BZ_STREAM_END.. */
29 unsigned char buf[ELINKS_BZ_BUFFER_LENGTH]; /* must be the last */
32 static int
33 bzip2_open(struct stream_encoded *stream, int fd)
35 struct bz2_enc_data *data = mem_alloc(sizeof(*data));
36 int err;
38 stream->data = 0;
39 if (!data) {
40 return -1;
42 memset(data, 0, sizeof(struct bz2_enc_data) - ELINKS_BZ_BUFFER_LENGTH);
44 data->last_read = 0;
45 data->fdread = fd;
47 err = BZ2_bzDecompressInit(&data->fbz_stream, 0, 0);
48 if (err != BZ_OK) {
49 mem_free(data);
50 return -1;
53 stream->data = data;
55 return 0;
58 static int
59 bzip2_read(struct stream_encoded *stream, unsigned char *buf, int len)
61 struct bz2_enc_data *data = (struct bz2_enc_data *) stream->data;
62 int err = 0;
64 if (!data) return -1;
66 assert(len > 0);
68 if (data->last_read) return 0;
70 data->fbz_stream.avail_out = len;
71 data->fbz_stream.next_out = buf;
73 do {
74 if (data->fbz_stream.avail_in == 0) {
75 int l = safe_read(data->fdread, data->buf,
76 ELINKS_BZ_BUFFER_LENGTH);
78 if (l == -1) {
79 if (errno == EAGAIN)
80 break;
81 else
82 return -1; /* I/O error */
83 } else if (l == 0) {
84 /* EOF. It is error: we wait for more bytes */
85 return -1;
88 data->fbz_stream.next_in = data->buf;
89 data->fbz_stream.avail_in = l;
92 err = BZ2_bzDecompress(&data->fbz_stream);
93 if (err == BZ_STREAM_END) {
94 data->last_read = 1;
95 break;
96 } else if (err != BZ_OK) {
97 return -1;
99 } while (data->fbz_stream.avail_out > 0);
101 return len - data->fbz_stream.avail_out;
104 static unsigned char *
105 bzip2_decode(struct stream_encoded *stream, unsigned char *data, int len,
106 int *new_len)
108 *new_len = len;
109 return data;
112 #ifdef CONFIG_SMALL
113 #define BZIP2_SMALL 1
114 #else
115 #define BZIP2_SMALL 0
116 #endif
118 static unsigned char *
119 bzip2_decode_buffer(unsigned char *data, int len, int *new_len)
121 bz_stream stream;
122 unsigned char *buffer = NULL;
123 int error;
125 memset(&stream, 0, sizeof(bz_stream));
126 stream.next_in = data;
127 stream.avail_in = len;
129 if (BZ2_bzDecompressInit(&stream, 0, BZIP2_SMALL) != BZ_OK)
130 return NULL;
132 do {
133 unsigned char *new_buffer;
134 size_t size = stream.total_out_lo32 + MAX_STR_LEN;
136 /* FIXME: support for 64 bit. real size is
138 * (total_in_hi32 << * 32) + total_in_lo32
140 * --jonas */
141 assertm(!stream.total_out_hi32, "64 bzip2 decoding not supported");
143 new_buffer = mem_realloc(buffer, size);
144 if (!new_buffer) {
145 error = BZ_MEM_ERROR;
146 break;
149 buffer = new_buffer;
150 stream.next_out = buffer + stream.total_out_lo32;
151 stream.avail_out = MAX_STR_LEN;
153 error = BZ2_bzDecompress(&stream);
154 if (error == BZ_STREAM_END) {
155 *new_len = stream.total_out_lo32;
156 error = BZ_OK;
157 break;
160 /* Apparently BZ_STREAM_END is not forced when the end of input
161 * is reached. At least lindi- reported that it caused a
162 * reproducable infinite loop. Maybe it has to do with decoding
163 * an incomplete file. */
164 } while (error == BZ_OK && stream.avail_in > 0);
166 BZ2_bzDecompressEnd(&stream);
168 if (error != BZ_OK) {
169 if (buffer) mem_free(buffer);
170 *new_len = 0;
171 return NULL;
174 return buffer;
177 static void
178 bzip2_close(struct stream_encoded *stream)
180 struct bz2_enc_data *data = (struct bz2_enc_data *) stream->data;
182 if (data) {
183 BZ2_bzDecompressEnd(&data->fbz_stream);
184 close(data->fdread);
185 mem_free(data);
186 stream->data = 0;
190 static unsigned char *bzip2_extensions[] = { ".bz2", ".tbz", NULL };
192 struct decoding_backend bzip2_decoding_backend = {
193 "bzip2",
194 bzip2_extensions,
195 bzip2_open,
196 bzip2_read,
197 bzip2_decode,
198 bzip2_decode_buffer,
199 bzip2_close,