47#if LZMA_VERSION <= UINT32_C(49990030)
48# define LZMA_EASY_ENCODER(a, b) lzma_easy_encoder_single(a, b)
49#elif LZMA_VERSION <= UINT32_C(49990050)
50# define LZMA_EASY_ENCODER(a, b) lzma_easy_encoder(a, b)
52# define LZMA_EASY_ENCODER(a, b) lzma_easy_encoder(a, b, LZMA_CHECK_CRC32)
57static const lzma_stream lzma_stream_init = LZMA_STREAM_INIT;
63 unsigned char rdbuf[BUF_SIZE];
70void *lzma_open(
const char *path,
const char *mode) {
75 lf->fp = fopen(path, mode);
76 lf->str = lzma_stream_init;
79#if LZMA_VERSION <= UINT32_C(49990030)
80 ret = lzma_auto_decoder(&lf->str, NULL, NULL);
82 ret = lzma_auto_decoder(&lf->str, -1, 0);
88 ret = LZMA_EASY_ENCODER(&lf->str, 2);
91 fprintf(stderr,
"lzma_open error: %d\n", ret);
100int lzma_close(
struct lzmafile *file) {
102 unsigned char buf[BUF_SIZE];
106 if (file->mode ==
'w') {
109 file->str.next_out = buf;
110 file->str.avail_out = BUF_SIZE;
111 ret = lzma_code(&file->str, LZMA_FINISH);
112 if (ret != LZMA_STREAM_END && ret != LZMA_OK) {
113 fprintf(stderr,
"lzma_close error: encoding failed: %d\n", ret);
114 lzma_end(&file->str);
119 outsize = BUF_SIZE - file->str.avail_out;
120 if (fwrite(buf, 1, outsize, file->fp) != outsize) {
121 lzma_end(&file->str);
126 if (ret == LZMA_STREAM_END)
130 lzma_end(&file->str);
131 ret = fclose(file->fp);
140long lzma_read(
struct lzmafile *file,
void *buf,
size_t count) {
143 if (file->mode !=
'r')
147 lstr->next_out = buf;
148 lstr->avail_out = count;
151 while (lstr->avail_out) {
152 if (lstr->avail_in == 0) {
154 ret = fread(file->rdbuf, 1, BUF_SIZE, file->fp);
158 lstr->next_in = file->rdbuf;
159 lstr->avail_in = ret;
161 ret = lzma_code(lstr, LZMA_RUN);
164 if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
165 fprintf(stderr,
"lzma_read error: decoding failed: %d\n", ret);
168 if (ret == LZMA_STREAM_END) {
172 return count - lstr->avail_out;
181char *lzma_gets(
char *s,
int size,
struct lzmafile *file) {
185 if (file->mode !=
'r')
187 if (s == NULL || size < 1)
191 lstr->next_out = (
void *)s;
195 if (lstr->avail_in == 0) {
197 ret = fread(file->rdbuf, 1, BUF_SIZE, file->fp);
201 lstr->next_in = file->rdbuf;
202 lstr->avail_in = ret;
209 ret = lzma_code(lstr, LZMA_RUN);
212 if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
213 fprintf(stderr,
"lzma_gets error: decoding failed: %d\n", ret);
216 if (ret == LZMA_STREAM_END) {
222 if ((i == 1) && (s[0] == 32)) {
239long lzma_write(
struct lzmafile *file,
const void *buf,
size_t count) {
241 lzma_stream *lstr = &file->str;
242 unsigned char *bufout;
243 bufout = malloc(
sizeof(
char) * count);
245 if (file->mode !=
'w') {
246 fprintf(stderr,
"lzma_write error: file was not opened for writting\n");
251 lstr->avail_in = count;
252 while (lstr->avail_in) {
253 lstr->next_out = bufout;
254 lstr->avail_out = count;
255 ret = lzma_code(lstr, LZMA_RUN);
256 if (ret != LZMA_OK) {
257 fprintf(stderr,
"lzma_write error: encoding failed: %d\n", ret);
261 ret = fwrite(bufout, 1, count - lstr->avail_out, file->fp);
262 if (ret != count - lstr->avail_out) {
263 fprintf(stderr,
"lzma_write error\n");
275int lzma_puts(
const char *s,
struct lzmafile *file) {
277 lzma_stream *lstr = &file->str;
279 unsigned char *bufout;
282 if (file->mode !=
'w') {
283 fprintf(stderr,
"lzma_puts error: file was not opened for writting\n");
287 bufout = malloc(
sizeof(
unsigned char) * count);
288 buf = malloc(
sizeof(
char) * count);
289 strncpy(buf, s, count);
291 lstr->next_in = (
void *)buf;
292 lstr->avail_in = count;
293 while (lstr->avail_in) {
294 lstr->next_out = bufout;
295 lstr->avail_out = count;
296 ret = lzma_code(lstr, LZMA_RUN);
297 if (ret != LZMA_OK) {
298 fprintf(stderr,
"lzma_puts error: encoding failed: %d\n", ret);
303 ret = fwrite(bufout, 1, count - lstr->avail_out, file->fp);
304 if (ret != count - lstr->avail_out) {
305 fprintf(stderr,
"lzma_puts error\n");
319int lzma_putc(
int c,
struct lzmafile *file) {
321 lzma_stream *lstr = &file->str;
323 unsigned char bufout[1];
326 if (file->mode !=
'w') {
327 fprintf(stderr,
"lzma_putc error: file was not opened for writting\n");
332 lstr->next_in = (
void *)buf;
334 while (lstr->avail_in) {
335 lstr->next_out = bufout;
337 ret = lzma_code(lstr, LZMA_RUN);
338 if (ret != LZMA_OK) {
339 fprintf(stderr,
"lzma_putc error: encoding failed: %d\n", ret);
342 ret = fwrite(bufout, 1, 1 - lstr->avail_out, file->fp);
343 if (ret != 1 - lstr->avail_out) {
344 fprintf(stderr,
"lzma_putc error\n");
348 return (
unsigned char)c;
355int lzma_printf(
struct lzmafile *file,
const char *format, ...) {
358 unsigned char in[32768];
360 va_start(va, format);
363 (void)vsnprintf((
char *)in, size, format, va);
365 len = strlen((
char *)in);
368 if (len <= 0 || len >= (
int)size || in[size - 1] != 0) {
369 fprintf(stderr,
"lzma_printf error: the printf results do not fit in the buffer\n");
373 len = lzma_write(file, in, len);
378int lzma_eof(
struct lzmafile *file) {
381 if (lstr->avail_in == 0) {
382 return feof(file->fp);
393long lzma_tell(
struct lzmafile *file) {
394 return ftell(file->fp);
397int lzma_seek(
struct lzmafile *file,
long offset,
int whence) {
398 return fseek(file->fp, offset, whence);
401void *UnpackLZMAOpen(
char *filename) {
404 return lzma_open(filename,
"rb");