15 ssize_t zpm_uncompress_cb(void *buf, size_t bufsize, void *cbdata,
16 int (*cb)(void *ud, void *buf, size_t bufsize)) {
17 lzma_stream s = LZMA_STREAM_INIT;
21 uint8_t outbuf[BUFSIZ];
27 ret = lzma_stream_decoder(strm, UINT64_MAX, 0);
28 /* The only reasonable error here is LZMA_MEM_ERROR. */
30 fprintf(stderr, "%s", ret == LZMA_MEM_ERROR ? strerror(ENOMEM)
31 : "Internal error (bug)");
35 strm->avail_in = bufsize;
37 strm->avail_out = BUFSIZ;
38 strm->next_out = outbuf;
40 lzma_action action = LZMA_RUN;
43 ret = lzma_code(strm, action);
45 // Write and check write error before checking decoder error.
46 // This way as much data as possible gets written to output
47 // even if decoder detected an error.
48 if (strm->avail_out == 0 || ret != LZMA_OK) {
49 size_t avail = BUFSIZ - strm->avail_out;
56 written = cb(cbdata, outbuf, avail);
58 /* Wouldn't be a surprise if writing to
59 * stderr would fail too but at least
60 * try to show an error message.
69 strm->next_out = outbuf;
70 strm->avail_out = BUFSIZ;
74 if (ret == LZMA_STREAM_END) {
75 // lzma_stream_decoder() already guarantees
76 // that there's no trailing garbage.
77 assert(strm->avail_in == 0);
78 //assert(action == LZMA_FINISH);
87 msg = strerror(ENOMEM);
90 case LZMA_FORMAT_ERROR:
91 msg = "File format not recognized";
94 case LZMA_OPTIONS_ERROR:
95 // FIXME: Better message?
96 msg = "Unsupported compression options";
100 msg = "File is corrupt";
104 msg = "Unexpected end of input";
108 msg = "Internal error (bug)";
112 fprintf(stderr, "zpmuncompress: %s\n", msg);
119 ssize_t uncompresslzma(void *buf, size_t bufsize, int out) {
120 lzma_stream s = LZMA_STREAM_INIT;
124 uint8_t outbuf[BUFSIZ];
130 ret = lzma_stream_decoder(strm, UINT64_MAX, 0);
131 /* The only reasonable error here is LZMA_MEM_ERROR. */
132 if (ret != LZMA_OK) {
133 fprintf(stderr, "%s", ret == LZMA_MEM_ERROR ? strerror(ENOMEM)
134 : "Internal error (bug)");
138 strm->avail_in = bufsize;
140 strm->avail_out = BUFSIZ;
141 strm->next_out = outbuf;
143 lzma_action action = LZMA_RUN;
146 ret = lzma_code(strm, action);
148 // Write and check write error before checking decoder error.
149 // This way as much data as possible gets written to output
150 // even if decoder detected an error.
151 if (strm->avail_out == 0 || ret != LZMA_OK) {
152 size_t avail = BUFSIZ - strm->avail_out;
159 written = write(out, outbuf, avail);
161 /* Wouldn't be a surprise if writing to
162 * stderr would fail too but at least
163 * try to show an error message.
165 fprintf(stderr, "Cannot write to output"
175 strm->next_out = outbuf;
176 strm->avail_out = BUFSIZ;
179 if (ret != LZMA_OK) {
180 if (ret == LZMA_STREAM_END) {
181 // lzma_stream_decoder() already guarantees
182 // that there's no trailing garbage.
183 assert(strm->avail_in == 0);
184 //assert(action == LZMA_FINISH);
193 msg = strerror(ENOMEM);
196 case LZMA_FORMAT_ERROR:
197 msg = "File format not recognized";
200 case LZMA_OPTIONS_ERROR:
201 // FIXME: Better message?
202 msg = "Unsupported compression options";
205 case LZMA_DATA_ERROR:
206 msg = "File is corrupt";
210 msg = "Unexpected end of input";
214 msg = "Internal error (bug)";
218 fprintf(stderr, "zpmuncompress: %s\n", msg);