From f20a6bf2f62d79f6e40609d80ad9fdcc1df8299c Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Sun, 26 Jan 2020 00:30:13 +0100 Subject: [PATCH] zlib used for (de)compression --- Makefile.simple | 6 +++--- configure.ac | 1 + mymake.cpp | 2 +- sysconfig.h | 2 ++ util.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/Makefile.simple b/Makefile.simple index 0cfe4f07..9092040a 100644 --- a/Makefile.simple +++ b/Makefile.simple @@ -52,7 +52,7 @@ ifeq (${OS},linux) LDFLAGS_GL := -lGL LDFLAGS_GLEW := -lGLEW LDFLAGS_PNG := -lpng - LDFLAGS_SDL := -lSDL -lSDL_gfx -lSDL_mixer -lSDL_ttf -lpthread + LDFLAGS_SDL := -lSDL -lSDL_gfx -lSDL_mixer -lSDL_ttf -lpthread -lz OBJ_EXTENSION := .o hyper_RES := endif @@ -63,7 +63,7 @@ ifeq (${OS},mingw) LDFLAGS_GL := -lopengl32 LDFLAGS_GLEW := -lglew32 LDFLAGS_PNG := -lpng - LDFLAGS_SDL := -lSDL -lSDL_gfx -lSDL_mixer -lSDL_ttf + LDFLAGS_SDL := -lSDL -lSDL_gfx -lSDL_mixer -lSDL_ttf -lz -lphtread OBJ_EXTENSION := .o hyper_RES := hyper.res ifeq (${HYPERROGUE_USE_GLEW},) @@ -78,7 +78,7 @@ ifeq (${OS},osx) LDFLAGS_GL := -framework AppKit -framework OpenGL LDFLAGS_GLEW := -lGLEW LDFLAGS_PNG := -lpng - LDFLAGS_SDL := -lSDL -lSDLMain -lSDL_gfx -lSDL_mixer -lSDL_ttf + LDFLAGS_SDL := -lSDL -lSDLMain -lSDL_gfx -lSDL_mixer -lSDL_ttf -lz -lpthread OBJ_EXTENSION := .o hyper_RES := endif diff --git a/configure.ac b/configure.ac index 66c1d9c6..4c8e8b56 100644 --- a/configure.ac +++ b/configure.ac @@ -30,6 +30,7 @@ AC_SEARCH_LIBS([aacircleColor], [SDL_gfx], [], AC_MSG_RESULT([SDL_gfx library wa AC_SEARCH_LIBS([Mix_LoadMUS], [SDL_mixer], [], AC_MSG_ERROR([SDL_mixer library was not found])) AC_SEARCH_LIBS([TTF_OpenFont], [SDL_ttf], [], AC_MSG_RESULT([SDL_ttf library was not found])) AC_SEARCH_LIBS(pthread_create, [pthread], ,AC_MSG_ERROR([pthread library was not found])) +AC_SEARCH_LIBS(deflateInit, [z], ,AC_MSG_ERROR([zlib was not found])) AC_CONFIG_FILES([Makefile]) AC_OUTPUT diff --git a/mymake.cpp b/mymake.cpp index 48b14a2a..3abc5a8d 100644 --- a/mymake.cpp +++ b/mymake.cpp @@ -30,7 +30,7 @@ string compiler = string linker = "g++ -rdynamic -o hyper"; -string libs = " savepng-loc.o -lSDL -lSDL_ttf -lSDL_mixer -lSDL_gfx -lGLEW -lGL -lpng -rdynamic -lpthread"; +string libs = " savepng-loc.o -lSDL -lSDL_ttf -lSDL_mixer -lSDL_gfx -lGLEW -lGL -lpng -rdynamic -lpthread -lz"; vector modules; diff --git a/sysconfig.h b/sysconfig.h index 29e3160b..0208dcd1 100644 --- a/sysconfig.h +++ b/sysconfig.h @@ -426,6 +426,8 @@ extern "C" { #include #include +#include + #if CAP_THREAD #if WINDOWS #include "mingw.thread.h" diff --git a/util.cpp b/util.cpp index c31c7f21..7983f16d 100644 --- a/util.cpp +++ b/util.cpp @@ -523,4 +523,45 @@ bignum::bignum(ld d) { while(n >= 0) { digits[n] = int(d); d -= digits[n]; d *= BASE; n--; } } +/* compression/decompression */ + +EX string compress_string(string s) { + z_stream strm; + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + println(hlog, "pre init"); + auto ret = deflateInit(&strm, 9); + if(ret != Z_OK) throw "z-error"; + println(hlog, "init ok"); + strm.avail_in = isize(s); + strm.next_in = (Bytef*) &s[0]; + vector buf(1000000, 0); + strm.avail_out = 1000000; + strm.next_out = (Bytef*) &buf[0]; + if(deflate(&strm, Z_FINISH) != Z_STREAM_END) throw "z-error-2"; + println(hlog, "deflate ok"); + string out(&buf[0], (char*)(strm.next_out) - &buf[0]); + println(hlog, isize(s), " -> ", isize(out)); + return out; + } + +EX string decompress_string(string s) { + z_stream strm; + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + auto ret = inflateInit(&strm); + if(ret != Z_OK) throw "z-error"; + strm.avail_in = isize(s); + strm.next_in = (Bytef*) &s[0]; + vector buf(1000000, 0); + strm.avail_out = 1000000; + strm.next_out = (Bytef*) &buf[0]; + if(inflate(&strm, Z_FINISH) != Z_STREAM_END) throw "z-error-2"; + string out(&buf[0], (char*)(strm.next_out) - &buf[0]); + println(hlog, isize(s), " -> ", isize(out)); + return out; + } + }