2 * add a file/files to an sqlite db
3 * in the 'files' table.
18 void *compresslzma(void *buf, size_t bufsize, size_t *len) {
24 outsize = lzma_stream_buffer_bound(bufsize);
25 outbuf = malloc(outsize);
26 /* TODO adjust encoding level for size */
27 lzma_easy_buffer_encode(6, LZMA_CHECK_CRC64, NULL,
29 outbuf, &outlen, outsize
35 int main(int ac, char **av){
38 /* insert each file */
39 for (i = 1; i < ac; i++) {
43 unsigned char tmp[32];
44 char hash[65]; /* hash in hex */
47 fd = open(av[i], O_RDONLY);
51 if (fstat(fd, &sbuf) == -1) {
54 /* not a regular file? */
55 if (!S_ISREG(sbuf.st_mode)) {
59 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
65 sha256_process(&md, content, sbuf.st_size);
66 sha256_done(&md, tmp);
68 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
71 fprintf(stderr, "file %s: %s\n", av[i], hash);
78 outbuf = compresslzma(content, sbuf.st_size, &outlen);
79 save = fopen("foo.xz", "w");
80 fwrite(outbuf, outlen, 1, save);
84 //Find out the maximum size of the compressed output for single-call compression:
85 // zlib: compressBound()
86 // liblzma: lzma_stream_buffer_bound()
89 //zlib: compress() or compress2()
90 // liblzma: lzma_easy_buffer_encode() or lzma_stream_buffer_encode()
94 // liblzma: lzma_stream_buffer_decode()
96 // See container.h (src/liblzma/api/lzma/container.h in the source tree) for details how to use these functions.
98 /* either way we're done with this now */
99 munmap(content, sbuf.st_size);