]> pd.if.org Git - zpackage/blob - lib/compress.c
cleanup sqlite references
[zpackage] / lib / compress.c
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6
7 #include <sys/mman.h>
8
9 #include "sha256.h"
10
11 #include "lzma.h"
12
13 /* TODO pass in the outbuf ? */
14 /* or wrap lzma_stream_buffer_bound */
15 void *compresslzma(void *buf, size_t bufsize, size_t *len) {
16         /* xz compress it */
17         size_t outsize;
18         void *outbuf;
19         size_t outlen = 0;
20         int err = LZMA_OK;
21
22         outsize = lzma_stream_buffer_bound(bufsize);
23         outbuf = malloc(outsize);
24         if (!outbuf) {
25                 *len = 0;
26                 fprintf(stderr, "failed to malloc compression buffer\n");
27                 return NULL;
28         }
29         /* TODO adjust encoding level for size */
30         switch (err = lzma_easy_buffer_encode(6, LZMA_CHECK_CRC64, NULL,
31                         buf, bufsize,
32                         outbuf, &outlen, outsize
33                         )) {
34                 case LZMA_OK: break;
35                 case LZMA_OPTIONS_ERROR: fprintf(stderr, "lzma options error\n");
36                 default: fprintf(stderr, "lzma error %d\n", err);
37                 return NULL;
38                          break;
39         }
40         *len = outlen;
41         return outbuf;
42 }
43
44 #if 0
45 static int callback(void *NotUsed, int argc, char **argv, char **azColName){
46         int i;
47         for(i=0; i<argc; i++){
48                 printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
49         }
50         printf("\n");
51         return 0;
52 }
53
54 static char *create_table = "create table if not exists files (hash text primary key, size integer, compression text, content blob)";
55 #endif