summaryrefslogtreecommitdiff
path: root/core/compress.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/compress.c')
-rw-r--r--core/compress.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/core/compress.c b/core/compress.c
new file mode 100644
index 0000000..6ca72c6
--- /dev/null
+++ b/core/compress.c
@@ -0,0 +1,77 @@
+// file : core/compress.c
+// project : Salis-VM
+// author : Paul Oliver <contact@pauloliver.dev>
+
+// index:
+// [section] includes
+// [section] definitions
+
+// ----------------------------------------------------------------------------
+// [section] includes
+// ----------------------------------------------------------------------------
+#include <assert.h>
+#include <zlib.h>
+
+#include "compress.h"
+
+// ----------------------------------------------------------------------------
+// [section] definitions
+// ----------------------------------------------------------------------------
+int comp_deflate(struct DeflateParams *params) {
+ assert(params);
+ assert(params->size);
+ assert(params->in);
+ assert(params->out);
+
+ params->strm.zalloc = NULL;
+ params->strm.zfree = NULL;
+ params->strm.opaque = NULL;
+
+ deflateInit(&params->strm, Z_DEFAULT_COMPRESSION);
+
+ params->strm.avail_in = params->size;
+ params->strm.avail_out = params->size;
+ params->strm.next_in = params->in;
+ params->strm.next_out = params->out;
+
+ deflate(&params->strm, Z_FINISH);
+
+ return 0;
+}
+
+int comp_inflate(struct InflateParams *params) {
+ assert(params);
+ assert(params->avail_in);
+ assert(params->size);
+ assert(params->in);
+ assert(params->out);
+
+ params->strm.next_in = params->in;
+ params->strm.avail_in = params->avail_in;
+ params->strm.zalloc = NULL;
+ params->strm.zfree = NULL;
+ params->strm.opaque = NULL;
+
+ inflateInit(&params->strm);
+
+ params->strm.avail_out = params->size;
+ params->strm.next_out = params->out;
+
+#if defined(NDEBUG)
+ inflate(&params->strm, Z_FINISH);
+#else
+ assert(inflate(&params->strm, Z_FINISH));
+#endif
+
+ return 0;
+}
+
+void comp_deflate_end(struct DeflateParams *params) {
+ assert(params);
+ deflateEnd(&params->strm);
+}
+
+void comp_inflate_end(struct InflateParams *params) {
+ assert(params);
+ inflateEnd(&params->strm);
+}