X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=compress.c;fp=compress.c;h=648a6d9ed3def94841fdd3aee7db3b5fb03a13c0;hb=ecdd57da7df775c039aee834e1f74172004f352b;hp=0000000000000000000000000000000000000000;hpb=fa23ca4b77cd45ed4996eafc1fa714e12a72439e;p=zpackage diff --git a/compress.c b/compress.c new file mode 100644 index 0000000..648a6d9 --- /dev/null +++ b/compress.c @@ -0,0 +1,102 @@ +/* + * add a file/files to an sqlite db + * in the 'files' table. + */ + +#include +#include +#include +#include +#include + +#include + +#include "lzma.h" + +#include "sha256.h" + +void *compresslzma(void *buf, size_t bufsize, size_t *len) { + /* xz compress it */ + size_t outsize; + void *outbuf; + size_t outlen = 0; + + outsize = lzma_stream_buffer_bound(bufsize); + outbuf = malloc(outsize); + /* TODO adjust encoding level for size */ + lzma_easy_buffer_encode(6, LZMA_CHECK_CRC64, NULL, + buf, bufsize, + outbuf, &outlen, outsize + ); + *len = outlen; + return outbuf; +} + +int main(int ac, char **av){ + int i, j; + + /* insert each file */ + for (i = 1; i < ac; i++) { + int fd; + void *content; + struct stat sbuf; + unsigned char tmp[32]; + char hash[65]; /* hash in hex */ + hash_state md; + + fd = open(av[i], O_RDONLY); + if (fd == -1) { + exit(1); + } + if (fstat(fd, &sbuf) == -1) { + exit(1); + } + /* not a regular file? */ + if (!S_ISREG(sbuf.st_mode)) { + exit(1); + } + + content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0); + if (!content) { + exit(2); + } + + sha256_init(&md); + sha256_process(&md, content, sbuf.st_size); + sha256_done(&md, tmp); + for (j=0;j<32;j++) { + sprintf(hash+j*2, "%02x", (unsigned)tmp[j]); + } + hash[64] = 0; + fprintf(stderr, "file %s: %s\n", av[i], hash); + + /* xz compress it */ + size_t outlen = 0; + FILE *save; + void *outbuf; + + outbuf = compresslzma(content, sbuf.st_size, &outlen); + save = fopen("foo.xz", "w"); + fwrite(outbuf, outlen, 1, save); + fclose(save); + free(outbuf); + + //Find out the maximum size of the compressed output for single-call compression: +// zlib: compressBound() +// liblzma: lzma_stream_buffer_bound() + +// Compressing: +//zlib: compress() or compress2() + // liblzma: lzma_easy_buffer_encode() or lzma_stream_buffer_encode() + + // Decompressing: +//zlib: uncompress() + // liblzma: lzma_stream_buffer_decode() + + // See container.h (src/liblzma/api/lzma/container.h in the source tree) for details how to use these functions. + + /* either way we're done with this now */ + munmap(content, sbuf.st_size); + } + return 0; +}