diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/compression.c | 77 | ||||
-rw-r--r-- | src/compression.h | 27 | ||||
-rw-r--r-- | src/local.mk | 2 |
3 files changed, 106 insertions, 0 deletions
diff --git a/src/compression.c b/src/compression.c new file mode 100644 index 0000000..5e7b07a --- /dev/null +++ b/src/compression.c @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2013 P. J. McDermott + * + * This file is part of Dodge Balls + * + * Dodge Balls is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Dodge Balls is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Dodge Balls. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stddef.h> +#include <zlib.h> +#include "compression.h" +#include "output.h" + +static void +_db_zlib_err(int status) +{ + switch (status) { + case Z_OK: + case Z_STREAM_END: + break; + case Z_MEM_ERROR: + db_err("zlib error: out of memory"); + break; + case Z_VERSION_ERROR: + db_err("zlib error: incompatible library version"); + break; + case Z_STREAM_ERROR: + db_err("zlib error: invalid stream"); + break; + case Z_NEED_DICT: + db_err("zlib error: need dictionary"); + break; + case Z_DATA_ERROR: + db_err("zlib error: corrupted data"); + break; + case Z_BUF_ERROR: + db_err("zlib error: buffer error"); + break; + default: + db_err("zlib error: unknown"); + break; + } +} + +void +db_decompress(char *src, size_t src_len, char *dest, size_t dest_len) +{ + z_stream d_stream; + + d_stream.zalloc = Z_NULL; + d_stream.zfree = Z_NULL; + d_stream.opaque = Z_NULL; + d_stream.next_in = (Bytef *) src; + d_stream.avail_in = src_len; + d_stream.next_out = (Bytef *) dest; + d_stream.avail_out = dest_len; + + db_dbg("Initializing inflation stream..."); + _db_zlib_err(inflateInit2(&d_stream, 15 + 32)); + + db_dbg("Inflating %zu bytes into up to %zu bytes...", src_len, dest_len); + _db_zlib_err(inflate(&d_stream, Z_NO_FLUSH)); + + db_dbg("Ending inflation stream..."); + _db_zlib_err(inflateEnd(&d_stream)); +} diff --git a/src/compression.h b/src/compression.h new file mode 100644 index 0000000..894a3fd --- /dev/null +++ b/src/compression.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2013 P. J. McDermott + * + * This file is part of Dodge Balls + * + * Dodge Balls is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Dodge Balls is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Dodge Balls. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef DB_COMPRESSION_H_ +#define DB_COMPRESSION_H_ + +#include <stddef.h> + +void db_decompress(char *src, size_t src_len, char *dest, size_t dest_len); + +#endif /* DB_COMPRESSION_H_ */ diff --git a/src/local.mk b/src/local.mk index 4b6f55d..c0c83dd 100644 --- a/src/local.mk +++ b/src/local.mk @@ -3,6 +3,8 @@ dodge_balls_SOURCES += \ %reldir%/base64.h \ %reldir%/collision.c \ %reldir%/collision.h \ + %reldir%/compression.c \ + %reldir%/compression.h \ %reldir%/defs.h \ %reldir%/dirs.c \ %reldir%/dirs.h \ |