77 lines
2.3 KiB
C++
77 lines
2.3 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
Cryptographic class using lib Sodium
|
|
Copyright (C) 2020 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
|
|
# ifdef FREERTOS
|
|
# include "rom/miniz.h"
|
|
# define compress2 mz_compress2
|
|
# define Z_OK MZ_OK
|
|
# define uncompress mz_uncompress
|
|
# else
|
|
# include <zlib.h>
|
|
# endif
|
|
#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;
|
|
}
|