git-svn-id: svn://db.shs.com.ru/pip@554 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-11-20 12:26:39 +00:00
parent 6cbe77c5b6
commit a1b95e6e66
6 changed files with 112 additions and 7 deletions

View File

@@ -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

View File

@@ -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();

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "picompress.h"
#ifdef PIP_COMPRESS
# include <zlib.h>
#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;
}

View File

@@ -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
}

View File

@@ -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

View File

@@ -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 ""