From 82275ca11059ba9697c752a2e0fcda26c1974a7d Mon Sep 17 00:00:00 2001 From: zhongjiafeng Date: Wed, 1 Jul 2020 14:18:12 +0800 Subject: [PATCH] change linux -> __linux__ & WIN32 -> _WIN32 & use existed key encrypt files in dir --- deploy/encryption/build.sh | 2 +- .../encryption/include/paddle_model_decrypt.h | 4 +- .../encryption/include/paddle_model_encrypt.h | 4 +- .../encryption/sample/paddle_encrypt_tool.cpp | 58 ++++++++++++++----- deploy/encryption/src/util/io_utils.cpp | 12 ++-- 5 files changed, 55 insertions(+), 25 deletions(-) diff --git a/deploy/encryption/build.sh b/deploy/encryption/build.sh index 1a0f7d7..108edc0 100644 --- a/deploy/encryption/build.sh +++ b/deploy/encryption/build.sh @@ -1,4 +1,4 @@ -PADDLE_DIR=/home/parallels/developers/paddleX-PR/paddle +PADDLE_DIR=/path/to/paddle if [ ! -d "3rd" ]; then mkdir 3rd diff --git a/deploy/encryption/include/paddle_model_decrypt.h b/deploy/encryption/include/paddle_model_decrypt.h index c722d73..cf9cda5 100644 --- a/deploy/encryption/include/paddle_model_decrypt.h +++ b/deploy/encryption/include/paddle_model_decrypt.h @@ -6,14 +6,14 @@ #ifndef PADDLE_MODEL_PROTECT_API_PADDLE_MODEL_DECRYPT_H #define PADDLE_MODEL_PROTECT_API_PADDLE_MODEL_DECRYPT_H -#ifdef WIN32 +#ifdef _WIN32 #ifdef PM_EXPORTS #define PDD_MODEL_API __declspec(dllexport) #else #define PDD_MODEL_API __declspec(dllimport) #endif #endif -#ifdef linux +#ifdef __linux__ #define PDD_MODEL_API __attribute__((visibility("default"))) #endif diff --git a/deploy/encryption/include/paddle_model_encrypt.h b/deploy/encryption/include/paddle_model_encrypt.h index adc55da..345114c 100644 --- a/deploy/encryption/include/paddle_model_encrypt.h +++ b/deploy/encryption/include/paddle_model_encrypt.h @@ -5,7 +5,7 @@ #ifndef PADDLE_MODEL_PROTECT_API_PADDLE_MODEL_ENCRYPT_H #define PADDLE_MODEL_PROTECT_API_PADDLE_MODEL_ENCRYPT_H -#ifdef WIN32 +#ifdef _WIN32 #ifdef PM_EXPORTS #define PDE_MODEL_API __declspec(dllexport) #else @@ -13,7 +13,7 @@ #endif #endif -#ifdef linux +#ifdef __linux__ #define PDE_MODEL_API __attribute__((visibility("default"))) #endif diff --git a/deploy/encryption/sample/paddle_encrypt_tool.cpp b/deploy/encryption/sample/paddle_encrypt_tool.cpp index ea4ce2c..9c5f6bf 100644 --- a/deploy/encryption/sample/paddle_encrypt_tool.cpp +++ b/deploy/encryption/sample/paddle_encrypt_tool.cpp @@ -8,7 +8,7 @@ #define RESET "\033[0m" #define BOLD "\033[1m" #define BOLDGREEN "\033[1m\033[32m" -#elif WIN32 +#elif _WIN32 #define RESET "" #define BOLD "" #define BOLDGREEN "" @@ -21,9 +21,11 @@ void help() { std::cout << "\t-h" << std::endl; std::cout << "[2]Generate random key and encrypt dir files" << std::endl; std::cout << "\t-model_dir\tmodel_dir_ori\t-save_dir\tencrypted_models" << std::endl; - std::cout << "[3]Generate random key for encrypt file" << std::endl; + std::cout << "[3]Encrypt dir files with key" << std::endl; + std::cout << "\t-key\tkeydata \t-model_dir\tmodel_dir_ori\t-save_dir\tencrypted_models" << std::endl; + std::cout << "[4]Generate random key for encrypt file" << std::endl; std::cout << "\t-g" << std::endl; - std::cout << "[4]Encrypt file:" << std::endl; + std::cout << "[5]Encrypt file:" << std::endl; std::cout << "\t-e\t-key\tkeydata\t-infile\tinfile\t-outfile\toutfile" << std::endl; } @@ -46,27 +48,22 @@ int main(int argc, char** argv) { int ret = paddle_encrypt_dir(key_random.c_str(), argv[2], argv[4]); switch (ret) { case CODE_OK: - std::cout << "Success, Encrypt __model__, __params__ to " << argv[4] << "(dir) success!" - << std::endl; + std::cout << "Success, Encrypt __model__, __params__ to " << argv[4] << "(dir) success!" << std::endl; break; case CODE_MODEL_FILE_NOT_EXIST: - std::cout << "Failed, errorcode = " << ret << ", could't find __model__(file) in " << argv[2] - << std::endl; + std::cout << "Failed, errorcode = " << ret << ", could't find __model__(file) in " << argv[2] << std::endl; break; case CODE_MODEL_YML_FILE_NOT_EXIST: - std::cout << "Failed, errorcode = " << ret << ", could't find model.yml(file) in " << argv[2] - << std::endl; + std::cout << "Failed, errorcode = " << ret << ", could't find model.yml(file) in " << argv[2] << std::endl; break; case CODE_PARAMS_FILE_NOT_EXIST: - std::cout << "Failed, errorcode = " << ret << ", could't find __params__(file) in " << argv[2] - << std::endl; + std::cout << "Failed, errorcode = " << ret << ", could't find __params__(file) in " << argv[2] << std::endl; break; case CODE_NOT_EXIST_DIR: std::cout << "Failed, errorcode = " << ret << ", " << argv[2] << "(dir) not exist" << std::endl; break; case CODE_FILES_EMPTY_WITH_DIR: - std::cout << "Failed, errorcode = " << ret << ", could't find any files in " << argv[2] - << std::endl; + std::cout << "Failed, errorcode = " << ret << ", could't find any files in " << argv[2] << std::endl; break; default:std::cout << "Failed, errorcode = " << ret << ", others" << std::endl; break; @@ -75,6 +72,39 @@ int main(int argc, char** argv) { help(); } break; + case 7: + if (strcmp(argv[1], "-key") == 0 + && strcmp(argv[3], "-model_dir") == 0 && strcmp(argv[5], "-save_dir") == 0) { + int ret_e = paddle_encrypt_dir(argv[2], argv[4], argv[6]); + switch (ret_e) { + case CODE_OK: + std::cout << "Success, Encrypt __model__, __params__ to " << argv[6] << "(dir) success!" << std::endl; + break; + case CODE_KEY_LENGTH_ABNORMAL: + std::cout << "Failed, errorcode = " << ret_e << ", key length normal" << argv[2] << std::endl; + break; + case CODE_MODEL_FILE_NOT_EXIST: + std::cout << "Failed, errorcode = " << ret_e << ", could't find __model__(file) in " << argv[4] << std::endl; + break; + case CODE_MODEL_YML_FILE_NOT_EXIST: + std::cout << "Failed, errorcode = " << ret_e << ", could't find model.yml(file) in " << argv[4] << std::endl; + break; + case CODE_PARAMS_FILE_NOT_EXIST: + std::cout << "Failed, errorcode = " << ret_e << ", could't find __params__(file) in " << argv[4] << std::endl; + break; + case CODE_NOT_EXIST_DIR: + std::cout << "Failed, errorcode = " << ret_e << ", " << argv[4] << "(dir) not exist" << std::endl; + break; + case CODE_FILES_EMPTY_WITH_DIR: + std::cout << "Failed, errorcode = " << ret_e << ", could't find any files in " << argv[4] << std::endl; + break; + default:std::cout << "Failed, errorcode = " << ret_e << ", others" << std::endl; + break; + } + } else { + help(); + } + break; case 8: if (strcmp(argv[1], "-e") == 0 && strcmp(argv[2], "-key") == 0 && strcmp(argv[4], "-infile") == 0 && strcmp(argv[6], "-outfile") == 0) { @@ -91,7 +121,7 @@ int main(int argc, char** argv) { default:help(); } -#ifdef WIN32 +#ifdef _WIN32 system("pause"); #endif diff --git a/deploy/encryption/src/util/io_utils.cpp b/deploy/encryption/src/util/io_utils.cpp index 8cae296..dd3c297 100644 --- a/deploy/encryption/src/util/io_utils.cpp +++ b/deploy/encryption/src/util/io_utils.cpp @@ -1,8 +1,8 @@ -#ifdef linux +#ifdef __linux__ #include #include #endif -#ifdef WIN32 +#ifdef _WIN32 #include #include #endif @@ -141,7 +141,7 @@ int read_file_to_file(const char* src_path, const char* dst_path) { } int read_dir_files(const char* dir_path, std::vector& files) { -#ifdef linux +#ifdef __linux__ struct dirent* ptr; DIR* dir = NULL; dir = opendir(dir_path); @@ -155,7 +155,7 @@ int read_dir_files(const char* dir_path, std::vector& files) { } closedir(dir); #endif -#ifdef WIN32 +#ifdef _WIN32 intptr_t handle; struct _finddata_t fileinfo; @@ -190,7 +190,7 @@ std::cout << files.size() << std::endl; } int dir_exist_or_mkdir(const char* dir) { -#ifdef WIN32 +#ifdef _WIN32 if (CreateDirectory(dir, NULL)) { // return CODE_OK; } else { @@ -198,7 +198,7 @@ int dir_exist_or_mkdir(const char* dir) { } #endif -#ifdef linux +#ifdef __linux__ if (access(dir, 0) != 0) { mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO); } -- GitLab