1 /* Bzip2 encoding (ENCODING_BZIP2) backend */
13 #include <bzlib.h> /* Everything needs this after stdio.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
31 /* The file descriptor from which we read. */
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
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
];
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
54 static const bz_stream null_bz_stream
= {0};
56 struct bz2_enc_data
*data
= mem_alloc(sizeof(*data
));
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
);
70 err
= BZ2_bzDecompressInit(&data
->fbz_stream
, 0, 0);
82 bzip2_read(struct stream_encoded
*stream
, unsigned char *buf
, int len
)
84 struct bz2_enc_data
*data
= (struct bz2_enc_data
*) stream
->data
;
91 if (data
->last_read
) return 0;
93 data
->fbz_stream
.avail_out
= len
;
94 data
->fbz_stream
.next_out
= buf
;
97 if (data
->fbz_stream
.avail_in
== 0) {
98 int l
= safe_read(data
->fdread
, data
->buf
,
99 ELINKS_BZ_BUFFER_LENGTH
);
105 return -1; /* I/O error */
107 /* EOF. It is error: we wait for more bytes */
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
) {
119 } else if (err
!= BZ_OK
) {
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
;
129 #define BZIP2_SMALL 1
131 #define BZIP2_SMALL 0
134 static unsigned char *
135 bzip2_decode_buffer(unsigned char *data
, int len
, int *new_len
)
138 unsigned char *buffer
= NULL
;
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
)
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
159 assertm(!stream
.total_out_hi32
, "64 bzip2 decoding not supported");
161 new_buffer
= mem_realloc(buffer
, size
);
163 error
= BZ_MEM_ERROR
;
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
) {
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
;
189 if (buffer
) mem_free(buffer
);
195 bzip2_close(struct stream_encoded
*stream
)
197 struct bz2_enc_data
*data
= (struct bz2_enc_data
*) stream
->data
;
200 BZ2_bzDecompressEnd(&data
->fbz_stream
);
207 static const unsigned char *const bzip2_extensions
[] = { ".bz2", ".tbz", NULL
};
209 const struct decoding_backend bzip2_decoding_backend
= {