]> pd.if.org Git - zpackage/blob - compress.c
fixup
[zpackage] / compress.c
1 /*
2  * add a file/files to an sqlite db
3  * in the 'files' table.
4  */
5
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11
12 #include <sys/mman.h>
13
14 #include "lzma.h"
15
16 #include "sha256.h"
17
18 void *compresslzma(void *buf, size_t bufsize, size_t *len) {
19         /* xz compress it */
20         size_t outsize;
21         void *outbuf;
22         size_t outlen = 0;
23
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,
28                         buf, bufsize,
29                         outbuf, &outlen, outsize
30                         );
31         *len = outlen;
32         return outbuf;
33 }
34
35 int main(int ac, char **av){
36         int i, j;
37
38         /* insert each file */
39         for (i = 1; i < ac; i++) {
40                 int fd;
41                 void *content;
42                 struct stat sbuf;
43                 unsigned char tmp[32];
44                 char hash[65]; /* hash in hex */
45                 hash_state md;
46
47                 fd = open(av[i], O_RDONLY);
48                 if (fd == -1) {
49                         exit(1);
50                 }
51                 if (fstat(fd, &sbuf) == -1) {
52                         exit(1);
53                 }
54                 /* not a regular file? */
55                 if (!S_ISREG(sbuf.st_mode)) {
56                         exit(1);
57                 }
58
59                 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
60                 if (!content) {
61                         exit(2);
62                 }
63
64                 sha256_init(&md);
65                 sha256_process(&md, content, sbuf.st_size);
66                 sha256_done(&md, tmp);
67                 for (j=0;j<32;j++) {
68                         sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
69                 }
70                 hash[64] = 0;
71                 fprintf(stderr, "file %s: %s\n", av[i], hash);
72
73                 /* xz compress it */
74                 size_t outlen = 0;
75                 FILE *save;
76                 void *outbuf;
77
78                 outbuf = compresslzma(content, sbuf.st_size, &outlen);
79                 save = fopen("foo.xz", "w");
80                 fwrite(outbuf, outlen, 1, save);
81                 fclose(save);
82                 free(outbuf);
83
84                 //Find out the maximum size of the compressed output for single-call compression:
85 //                      zlib: compressBound()
86 //                      liblzma: lzma_stream_buffer_bound()
87
88 //                      Compressing:
89 //zlib: compress() or compress2()
90  //     liblzma: lzma_easy_buffer_encode() or lzma_stream_buffer_encode()
91
92   //    Decompressing:
93 //zlib: uncompress()
94  //     liblzma: lzma_stream_buffer_decode()
95
96   //    See container.h (src/liblzma/api/lzma/container.h in the source tree) for details how to use these functions.
97
98                 /* either way we're done with this now */
99                 munmap(content, sbuf.st_size);
100         }
101         return 0;
102 }