16 void uncompresslzma(void *buf, size_t bufsize, FILE *out) {
17 lzma_stream s = LZMA_STREAM_INIT;
20 uint8_t outbuf[BUFSIZ];
26 ret = lzma_stream_decoder(strm, UINT64_MAX, 0);
27 /* The only reasonable error here is LZMA_MEM_ERROR. */
29 fprintf(stderr, "%s", ret == LZMA_MEM_ERROR ? strerror(ENOMEM)
30 : "Internal error (bug)");
34 strm->avail_in = bufsize;
36 strm->avail_out = BUFSIZ;
37 strm->next_out = outbuf;
39 lzma_action action = LZMA_RUN;
42 ret = lzma_code(strm, action);
44 // Write and check write error before checking decoder error.
45 // This way as much data as possible gets written to output
46 // even if decoder detected an error.
47 if (strm->avail_out == 0 || ret != LZMA_OK) {
48 const size_t write_size = BUFSIZ - strm->avail_out;
50 if (fwrite(outbuf, 1, write_size, out) != write_size) {
51 // Wouldn't be a surprise if writing to stderr
52 // would fail too but at least try to show an
54 fprintf(stderr, "Cannot write to output file stream: "
55 "%s", strerror(errno));
59 strm->next_out = outbuf;
60 strm->avail_out = BUFSIZ;
64 if (ret == LZMA_STREAM_END) {
65 // lzma_stream_decoder() already guarantees
66 // that there's no trailing garbage.
67 assert(strm->avail_in == 0);
68 //assert(action == LZMA_FINISH);
75 msg = strerror(ENOMEM);
78 case LZMA_FORMAT_ERROR:
79 msg = "File format not recognized";
82 case LZMA_OPTIONS_ERROR:
83 // FIXME: Better message?
84 msg = "Unsupported compression options";
88 msg = "File is corrupt";
92 msg = "Unexpected end of input";
96 msg = "Internal error (bug)";
100 fprintf(stderr, "xz: %s\n", msg);
106 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
108 int main(int ac, char **av){
123 fprintf(stderr, "usage: db hash file\n");
127 rc = sqlite3_open(av[1], &db);
129 SQLERROR(sqlite3_errmsg(db));
134 rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
135 if (rc != SQLITE_OK) {
136 SQLERROR(sqlite3_errmsg(db));
144 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
146 rc = sqlite3_step(ifile);
148 if (rc == SQLITE_DONE) {
149 /* didn't find a row */
150 sqlite3_finalize(ifile);
152 fprintf(stderr, "no such hash\n");
155 /* either way we're done with this now */
157 if (rc != SQLITE_ROW) {
158 SQLERROR(sqlite3_errmsg(db));
159 sqlite3_finalize(ifile);
164 type = sqlite3_column_type(ifile, 0);
165 if (type == SQLITE_NULL) {
166 fprintf(stderr, "no file size\n");
167 sqlite3_finalize(ifile);
171 type = sqlite3_column_type(ifile, 1);
172 if (type == SQLITE_NULL) {
173 fprintf(stderr, "no file data\n");
174 sqlite3_finalize(ifile);
178 size = sqlite3_column_int64(ifile, 0);
179 xzdata = (void *)sqlite3_column_blob(ifile, 1);
180 blobsize = sqlite3_column_bytes(ifile, 1);
182 out = fopen(filename, "w");
184 fprintf(stderr, "can't open output file %s\n", filename);
185 sqlite3_finalize(ifile);
189 //fwrite(xzdata, blobsize, 1, stdout);
191 fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
192 uncompresslzma(xzdata, blobsize, out);
195 sqlite3_finalize(ifile);