1 /* lzma encoding (ENCODING_LZMA) backend */
19 #include "encoding/encoding.h"
20 #include "encoding/lzma.h"
21 #include "util/memory.h"
23 #define ELINKS_BZ_BUFFER_LENGTH 5000
25 #define ELINKS_LZMA_MEMORY_LIMIT (1024 * 1024 * 128)
27 struct lzma_enc_data
{
28 lzma_stream flzma_stream
;
31 unsigned char buf
[ELINKS_BZ_BUFFER_LENGTH
];
35 lzma_open(struct stream_encoded
*stream
, int fd
)
37 struct lzma_enc_data
*data
= mem_alloc(sizeof(*data
));
45 memset(&data
->flzma_stream
, 0, sizeof(data
->flzma_stream
));
49 err
= lzma_auto_decoder(&data
->flzma_stream
, ELINKS_LZMA_MEMORY_LIMIT
, 0);
61 lzma_read(struct stream_encoded
*stream
, unsigned char *buf
, int len
)
63 struct lzma_enc_data
*data
= (struct lzma_enc_data
*) stream
->data
;
70 if (data
->last_read
) return 0;
72 data
->flzma_stream
.avail_out
= len
;
73 data
->flzma_stream
.next_out
= buf
;
76 if (data
->flzma_stream
.avail_in
== 0) {
77 int l
= safe_read(data
->fdread
, data
->buf
,
78 ELINKS_BZ_BUFFER_LENGTH
);
84 return -1; /* I/O error */
86 /* EOF. It is error: we wait for more bytes */
90 data
->flzma_stream
.next_in
= data
->buf
;
91 data
->flzma_stream
.avail_in
= l
;
94 err
= lzma_code(&data
->flzma_stream
, LZMA_RUN
);
95 if (err
== LZMA_STREAM_END
) {
98 } else if (err
!= LZMA_OK
) {
101 } while (data
->flzma_stream
.avail_out
> 0);
103 assert(len
- data
->flzma_stream
.avail_out
== data
->flzma_stream
.next_out
- buf
);
104 return len
- data
->flzma_stream
.avail_out
;
107 static unsigned char *
108 lzma_decode_buffer(unsigned char *data
, int len
, int *new_len
)
110 lzma_stream stream
= LZMA_STREAM_INIT
;
111 unsigned char *buffer
= NULL
;
114 *new_len
= 0; /* default, left there if an error occurs */
116 stream
.next_in
= data
;
117 stream
.avail_in
= len
;
119 if (lzma_auto_decoder(&stream
, ELINKS_LZMA_MEMORY_LIMIT
, 0) != LZMA_OK
)
123 unsigned char *new_buffer
;
124 size_t size
= stream
.total_out
+ MAX_STR_LEN
;
126 new_buffer
= mem_realloc(buffer
, size
);
128 error
= LZMA_MEM_ERROR
;
133 stream
.next_out
= buffer
+ stream
.total_out
;
134 stream
.avail_out
= MAX_STR_LEN
;
136 error
= lzma_code(&stream
, LZMA_RUN
);
137 if (error
== LZMA_STREAM_END
) {
141 } while (error
== LZMA_OK
&& stream
.avail_in
> 0);
145 if (error
== LZMA_OK
) {
146 *new_len
= stream
.total_out
;
149 if (buffer
) mem_free(buffer
);
155 lzma_close(struct stream_encoded
*stream
)
157 struct lzma_enc_data
*data
= (struct lzma_enc_data
*) stream
->data
;
160 lzma_end(&data
->flzma_stream
);
167 static const unsigned char *const lzma_extensions
[] = { ".lzma", ".xz", NULL
};
169 const struct decoding_backend lzma_decoding_backend
= {