]> pd.if.org Git - zpackage/blobdiff - lib/compress.c
reorg lib
[zpackage] / lib / compress.c
diff --git a/lib/compress.c b/lib/compress.c
new file mode 100644 (file)
index 0000000..a7427cd
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * add a file/files to an sqlite db
+ * in the 'files' table.
+ */
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/mman.h>
+
+#include <sqlite3.h>
+#include "sha256.h"
+
+#include "lzma.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);
+       if (!outbuf) {
+               *len = 0;
+               return NULL;
+       }
+       /* TODO adjust encoding level for size */
+       lzma_easy_buffer_encode(6, LZMA_CHECK_CRC64, NULL,
+                       buf, bufsize,
+                       outbuf, &outlen, outsize
+                       );
+       *len = outlen;
+       return outbuf;
+}
+
+static int callback(void *NotUsed, int argc, char **argv, char **azColName){
+       int i;
+       for(i=0; i<argc; i++){
+               printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
+       }
+       printf("\n");
+       return 0;
+}
+
+static char *create_table = "create table if not exists files (hash text primary key, size integer, compression text, content blob)";
+
+struct dbh {
+       sqlite3 *db;
+       char *errmsg;
+       int rc;
+};