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