From a1b95e6e66f648f3c03eb75a67be6f8034fa6319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=8B=D1=87=D0=BA=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9?= Date: Mon, 20 Nov 2017 12:26:39 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/pip@554 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- CMakeLists.txt | 20 ++++++++++- main.cpp | 16 ++++++--- src_compress/picompress.cpp | 69 +++++++++++++++++++++++++++++++++++++ src_crypt/picrypt.cpp | 2 +- src_main/math/picompress.h | 10 ++++++ src_main/piversion.h | 2 +- 6 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 src_compress/picompress.cpp create mode 100644 src_main/math/picompress.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b0a555b..08deeeb2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ endmacro() set(PIP_SRC_MAIN "src_main") set(PIP_SRC_CRYPT "src_crypt") +set(PIP_SRC_COMPRESS "src_compress") set(PIP_SRC_USB "src_usb") set(PIP_SRC_FFTW "src_fftw") set(PIP_LIBS_TARGETS pip) @@ -94,6 +95,9 @@ endforeach(F) # Crypt lib gather_src("${PIP_SRC_CRYPT}" CPP_LIB_CRYPT HDRS PHDRS) +# Compress lib +gather_src("${PIP_SRC_COMPRESS}" CPP_LIB_COMPRESS HDRS PHDRS) + # USB lib gather_src("${PIP_SRC_USB}" CPP_LIB_USB HDRS PHDRS) @@ -291,6 +295,20 @@ else() endif() +# Check if PIP support compress/decompress using zlib library +find_library(zlib_FOUND NAMES zlib z) +if(zlib_FOUND) + message(STATUS "Building with zlib compress support") + add_definitions(-DPIP_COMPRESS) + add_library(pip_compress SHARED ${CPP_LIB_COMPRESS}) + target_link_libraries(pip_compress pip ${zlib_FOUND}) + list(APPEND LIBS_STATUS zlib) + list(APPEND PIP_LIBS_TARGETS pip_compress) +else() + message(STATUS "Building without compress support") +endif() + + # Check if PIP support fftw3 for PIFFT using in math module set(FFTW_LIB_NAME fftw3) set(FFTW_LIB_SUFFIXES "" "f" "l" "q") @@ -341,7 +359,7 @@ endif() # Test program add_executable(pip_test "main.cpp") -target_link_libraries(pip_test pip) +target_link_libraries(pip_test pip pip_compress) # Install diff --git a/main.cpp b/main.cpp index 0e1de889..1d1072cd 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,19 @@ #include "pip.h" -#include "picodeparser.h" -#include "pivector2d.h" +#include "picompress.h" int main(int argc, char *argv[]) { - PICodeParser cp; - cp.parseFile("cp.h"); + PIString s = PIString::fromUTF8("hello, привет привет привет привет привет привет"); + PIByteArray ba = s.toUTF8(); + piCout << "original " << ba.toHex(); + ba = piCompress(ba); + piCout << "compress " << ba.toHex(); + ba = piDecompress(ba); + piCout << "decompress" << ba.toHex(); + piCout << PIString::fromUTF8(ba); + ba = PIByteArray(16); + piCout << ba.toHex() << ba.size(); + piCout << piCompress(ba).toHex() << piCompress(ba).size(); /*PIString s = PIString::fromUTF8("hello, привет"); piCout << s; PIByteArray ba = s.toUTF8(); diff --git a/src_compress/picompress.cpp b/src_compress/picompress.cpp new file mode 100644 index 00000000..3236ae15 --- /dev/null +++ b/src_compress/picompress.cpp @@ -0,0 +1,69 @@ +/* + PIP - Platform Independent Primitives + Cryptographic class using lib Sodium + Copyright (C) 2017 Andrey Bychkov work.a.b@yandex.ru + + This program 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. + + This program 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 this program. If not, see . +*/ + +#include "picompress.h" +#ifdef PIP_COMPRESS +# include +#endif + + +PIByteArray piCompress(const PIByteArray & ba, int level) { +#ifdef PIP_COMPRESS + PIByteArray zba; + zba.resize(ba.size() + 128); + int ret = 0; + ulong sz = zba.size(); + ret = compress2(zba.data(), &sz, ba.data(), ba.size(), level); + if (ret != Z_OK) { + piCout << "[PICompress]" << "Error: invalid input or not enought memory"; + return ba; + } + zba.resize(sz); + zba << ullong(ba.size()); + return zba; +#else + piCout << "[PICompress]" << "Warning: PICompress is disabled, to enable install zlib library and build pip_compress library"; +#endif + return ba; +} + + +PIByteArray piDecompress(const PIByteArray & zba) { +#ifdef PIP_COMPRESS + ullong sz; + if (zba.size() < sizeof(ullong)) { + piCout << "[PICompress]" << "Error: invalid input"; + return zba; + } + PIByteArray ba(zba.data(zba.size() - sizeof(ullong)), sizeof(ullong)); + ba >> sz; + ba.resize(sz); + int ret = 0; + ulong s = sz; + ret = uncompress(ba.data(), &s, zba.data(), zba.size()); + if (ret != Z_OK) { + piCout << "[PICompress]" << "Error: invalid input or not enought memory"; + return zba; + } + return ba; +#else + piCout << "[PICompress]" << "Warning: PICompress is disabled, to enable install zlib library and build pip_compress library"; +#endif + return zba; +} diff --git a/src_crypt/picrypt.cpp b/src_crypt/picrypt.cpp index f061de11..36a74624 100644 --- a/src_crypt/picrypt.cpp +++ b/src_crypt/picrypt.cpp @@ -34,7 +34,7 @@ PICrypt::PICrypt() { randombytes_buf(key_.data(), key_.size()); randombytes_buf(nonce_.data(), nonce_.size()); #else - piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install libsodium-dev library and build pip with -DCRYPT=1"; + piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install libsodium-dev library and build pip_crypt library"; #endif } diff --git a/src_main/math/picompress.h b/src_main/math/picompress.h new file mode 100644 index 00000000..f2f883fd --- /dev/null +++ b/src_main/math/picompress.h @@ -0,0 +1,10 @@ +#ifndef PICOMPRESS_H +#define PICOMPRESS_H + +#include "pibytearray.h" + +PIByteArray piCompress(const PIByteArray & ba, int level = 6); + +PIByteArray piDecompress(const PIByteArray & zba); + +#endif // PICOMPRESS_H diff --git a/src_main/piversion.h b/src_main/piversion.h index dddd3807..81022238 100644 --- a/src_main/piversion.h +++ b/src_main/piversion.h @@ -3,7 +3,7 @@ #define PIVERSION_H #define PIP_VERSION_MAJOR 1 -#define PIP_VERSION_MINOR 0 +#define PIP_VERSION_MINOR 1 #define PIP_VERSION_REVISION 0 #define PIP_VERSION_SUFFIX ""