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

This commit is contained in:
2017-08-09 20:12:36 +00:00
parent ae6d397e0e
commit 5dcc867689
5 changed files with 71 additions and 36 deletions

View File

@@ -204,6 +204,12 @@ endif()
if(APPLE)
add_definitions(-D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE)
endif()
if ((NOT DEFINED LIBPROJECT) AND (DEFINED ANDROID_PLATFORM))
include_directories(${ANDROID_SYSTEM_LIBRARY_PATH}/usr/include)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${ANDROID_NDK}/sysroot/usr/include")
#message("${ANDROID_SYSTEM_LIBRARY_PATH}/usr/include")
#message("${ANDROID_NDK}/sysroot/usr/include")
endif()
if(WIN32)
list(APPEND LIBS_MAIN ws2_32 iphlpapi psapi)
else()
@@ -340,6 +346,11 @@ if(LIB)
install(FILES ${HDRS} DESTINATION ${MINGW_INCLUDE}/pip)
install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION ${MINGW_LIB})
install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION ${MINGW_BIN})
find_library(STDLIB "stdc++-6")
#message("${STDLIB}")
if (STDLIB)
file(COPY "${STDLIB}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/utils/code_model_generator")
endif()
endif()
else()
if(APPLE)

View File

@@ -19,7 +19,7 @@
#include "picrypt.h"
#ifdef PIP_CRYPT
# include "sodium.h"
# include <sodium.h>
#endif
@@ -28,7 +28,7 @@ const char hash_def_key[] = "_picrypt_";
PICrypt::PICrypt() {
#ifdef PIP_CRYPT
if (!sodium_init()) piCout << "[PICrypt]" << "Error while initialize sodium!";
if (!init()) piCout << "[PICrypt]" << "Error while initialize sodium!";
nonce_.resize(crypto_secretbox_NONCEBYTES);
key_.resize(crypto_secretbox_KEYBYTES);
randombytes_buf(key_.data(), key_.size());
@@ -77,7 +77,7 @@ PIByteArray PICrypt::crypt(const PIByteArray & data, PIByteArray key) {
if (key.size() != crypto_secretbox_KEYBYTES)
key.resize(crypto_secretbox_KEYBYTES, ' ');
//return PIByteArray();
if (!sodium_init()) return retba;
if (!init()) return retba;
PIByteArray n;
retba.resize(data.size() + crypto_secretbox_MACBYTES);
n.resize(crypto_secretbox_NONCEBYTES);
@@ -123,7 +123,7 @@ PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, PIByteArray key, bo
if (ok) *ok = false;
return PIByteArray();
}
if (!sodium_init()) return retba;
if (!init()) return retba;
PIByteArray n;
n.resize(crypto_secretbox_NONCEBYTES);
retba.resize(crypt_data.size() - n.size() - crypto_secretbox_MACBYTES);
@@ -144,7 +144,7 @@ PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, PIByteArray key, bo
PIByteArray PICrypt::hash(const PIString & secret) {
PIByteArray hash;
#ifdef PIP_CRYPT
if (!sodium_init()) return hash;
if (!init()) return hash;
hash.resize(crypto_generichash_BYTES);
PIByteArray s(secret.data(), secret.size());
crypto_generichash(hash.data(), hash.size(), s.data(), s.size(), (const uchar*)hash_def_key, sizeof(hash_def_key) - 1);
@@ -159,7 +159,7 @@ ullong PICrypt::shorthash(const PIString& s, PIByteArray key) {
ullong hash = 0;
#ifdef PIP_CRYPT
if (crypto_shorthash_BYTES != sizeof(hash)) piCout << "[PICrypt]" << "internal error: bad hash size";
if (!sodium_init()) return hash;
if (!init()) return hash;
if (key.size() != crypto_shorthash_KEYBYTES) {
piCout << "[PICrypt]" << "invalid key size" << key.size() << ", shoud be" << crypto_shorthash_KEYBYTES << ", filled zeros";
key.resize(crypto_shorthash_KEYBYTES, 0);
@@ -176,7 +176,7 @@ ullong PICrypt::shorthash(const PIString& s, PIByteArray key) {
PIByteArray PICrypt::generateKey() {
PIByteArray hash;
#ifdef PIP_CRYPT
if (!sodium_init()) return hash;
if (!init()) return hash;
hash.resize(crypto_secretbox_KEYBYTES);
randombytes_buf(hash.data(), hash.size());
#else
@@ -206,4 +206,19 @@ size_t PICrypt::sizeCrypt() {
}
bool PICrypt::init() {
#ifdef PIP_CRYPT
static bool inited = false;
if (inited) return true;
//piCout << "[PICrypt]" << "init ...";
inited = sodium_init();
//piCout << "[PICrypt]" << "init" << inited;
return inited;
#else
piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install sodium library and build pip with -DCRYPT=";
#endif
return false;
}

View File

@@ -32,6 +32,9 @@ char * __sysoemname__ = 0;
#ifdef BLACKBERRY
# include <ctype.h>
#endif
//#ifdef ANDROID
# include <wchar.h>
//#endif
/*! \class PIChar
* \brief Unicode char
@@ -110,7 +113,14 @@ PIChar PIChar::fromUTF8(const char * c) {
return ret;
}
#endif
ret.ch = *(ushort*)c;
//#ifdef ANDROID
wchar_t wc(0);
mbstate_t s;
mbrtowc(&wc, c, 4, &s);
ret.ch = wc;
return ret;
//#endif
// ret.ch = *(ushort*)c;
return ret;
}

View File

@@ -21,6 +21,9 @@
#ifdef PIP_ICU
# include "unicode/ucnv.h"
#endif
//#ifdef ANDROID
# include <wchar.h>
//#endif
/*! \class PIString
* \brief String class
@@ -217,25 +220,7 @@ void PIString::appendFromChars(const char * c, int s, const char * cp) {
continue;
}
sz = mbtowc(&wc, &(c[i]), 4);
//cout << sz << endl;
switch (sz) {
case 4:
push_back(PIChar(*(int*)&(c[i])));
i += 3;
continue;
case 3:
push_back(PIChar(*(int*)&(c[i])));
back().ch &= 0xFFFFFF;
i += 2;
continue;
case 2:
push_back(PIChar(*(short * )&(c[i])));
++i;
continue;
default:
push_back(PIChar(c[i]));
break;
}
push_back(PIChar(int(wc)));
}
}
@@ -332,19 +317,28 @@ void PIString::buildData(const char * cp) const {
return;
}
#endif
uint wc;
uchar tc;
wchar_t wc;
char tc[4];
mbstate_t s;
int sz(0);
//printf("PIString::data %d\n", size_s());
for (int i = 0, j = 0; i < size_s(); ++i) {
wc = uint(at(i).unicode16Code());
for (int i = 0; i < size_s(); ++i) {
if (at(i).isAscii()) {
data_.push_back(uchar(at(i).toAscii()));
continue;
}
wc = at(i).toWChar();
sz = piClampi(wcrtomb(tc, wc, &s), 0, 4);
for (int b = 0; b < sz; ++b)
data_.push_back(uchar(tc[b]));
//printf("__%d_%d\n", i, wc);
tc = wc & 0xFF;
/*tc = wc & 0xFF;
while (tc) {
data_.push_back(uchar(tc)); ++j;
data_.push_back(uchar(tc));
wc >>= 8;
tc = wc & 0xFF;
//printf("____%d\n", wc);
}
}*/
/*if (at(i).isAscii())
data_.push_back(uchar(at(i).toAscii()));
else {
@@ -429,7 +423,7 @@ PIString & PIString::operator +=(const char * str) {
PIString & PIString::operator +=(const wchar_t * str) {
//cout << "wc" << endl;
int l = 0, sz;
/*int l = 0, sz;
char * c = new char[MB_CUR_MAX];
while (str[l] != 0) ++l;
for (int i = 0; i < l; ++i) {
@@ -450,7 +444,10 @@ PIString & PIString::operator +=(const wchar_t * str) {
break;
}
}
delete[] c;
delete[] c;*/
int i = -1;
while (str[++i])
push_back(PIChar(int(str[i])));
return *this;
}

View File

@@ -67,6 +67,8 @@ public:
static size_t sizeCrypt();
private:
static bool init();
PIByteArray nonce_, key_;
};