summaryrefslogtreecommitdiff
path: root/src/modules/loaders/loader_lzma.c
blob: f74c39ac390c4166d0f361799d1ada5ada787679 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "loader_common.h"

#include <lzma.h>

#define OUTBUF_SIZE 16484

static int
uncompress_file(const void *fdata, unsigned int fsize, int dest)
{
   int                 ok;
   lzma_stream         strm = LZMA_STREAM_INIT;
   lzma_ret            ret;
   uint8_t             outbuf[OUTBUF_SIZE];
   unsigned int        bytes;

   ok = 0;

   ret = lzma_auto_decoder(&strm, UINT64_MAX, 0);
   if (ret != LZMA_OK)
      return ok;

   strm.next_in = fdata;
   strm.avail_in = fsize;

   for (;;)
     {
        strm.next_out = outbuf;
        strm.avail_out = sizeof(outbuf);

        ret = lzma_code(&strm, 0);

        if (ret != LZMA_OK && ret != LZMA_STREAM_END)
           goto quit;

        bytes = sizeof(outbuf) - strm.avail_out;
        if (write(dest, outbuf, bytes) != bytes)
           goto quit;

        if (ret == LZMA_STREAM_END)
           break;
     }

   ok = 1;

 quit:
   lzma_end(&strm);

   return ok;
}

static const char  *const list_formats[] = { "xz", "lzma" };

int
load2(ImlibImage * im, int load_data)
{

   return decompress_load(im, load_data, list_formats, ARRAY_SIZE(list_formats),
                          uncompress_file);
}

void
formats(ImlibLoader * l)
{
   __imlib_LoaderSetFormats(l, list_formats, ARRAY_SIZE(list_formats));
}