From c07db22abaa767aeb89502d139070ff6489b572e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0alda?= Date: Thu, 17 Aug 2023 14:30:11 +0200 Subject: [PATCH] licence final --- include/utils.h | 3 +++ src/CreateLicence.cpp | 56 +++++++++++++++++++++++++++---------------- src/PlcLicence.cpp | 13 +++++----- src/utils.cpp | 10 ++++++++ 4 files changed, 56 insertions(+), 26 deletions(-) diff --git a/include/utils.h b/include/utils.h index a16edab..afbd507 100644 --- a/include/utils.h +++ b/include/utils.h @@ -65,6 +65,9 @@ map getArguments(int argc, char *argv[]); map mapArguments(int argc, char *argv[]); char* getCharArray(string source); void getCharsFromString1(string source, char *charArray); +void getCharsFromString(string& source, char *charArray, int length); + + diff --git a/src/CreateLicence.cpp b/src/CreateLicence.cpp index 590a640..b612214 100644 --- a/src/CreateLicence.cpp +++ b/src/CreateLicence.cpp @@ -9,11 +9,13 @@ #define FILEBUFFER 300 #define TWO_HOURSE_SECONDS 7200 -void signalHandler(int signum) { - if (signum == SIGSEGV) { - std::cerr << "Caught segmentation fault (SIGSEGV)" << std::endl; - // You can perform some recovery actions here if needed - } +void signalHandler(int signum) +{ + if (signum == SIGSEGV) + { + std::cerr << "Caught segmentation fault (SIGSEGV)" << std::endl; + // You can perform some recovery actions here if needed + } } /// @brief hlavnĂ­ funkce @@ -24,14 +26,13 @@ void signalHandler(int signum) { int main(int argc, char *argv[]) { -std::signal(SIGSEGV, signalHandler); + std::signal(SIGSEGV, signalHandler); -// a segmentation fault -//int* ptr = nullptr; -//*ptr = 42; // This will trigger a segmentation fault + // a segmentation fault + // int* ptr = nullptr; + //*ptr = 42; // This will trigger a segmentation fault - - //map arguments = mapArguments(argc, argv); + // map arguments = mapArguments(argc, argv); map argumentsString = getArguments(argc, argv); // std::cout << "Number of arguments: " << argc << std::endl; @@ -48,6 +49,7 @@ std::signal(SIGSEGV, signalHandler); // for (auto it = argumentsString.begin(); it != argumentsString.end(); ++it) // { // std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl; + // std::cout << it->second.length() << std::endl; // } // generatePause(); @@ -100,21 +102,35 @@ std::signal(SIGSEGV, signalHandler); // cout << "binaryType: " << binaryType; // cout << "CID:" << argumentsString["-cid"]; - const int cidLength = argumentsString["-cid"].length(); - char cidArg[cidLength] = {}; - getCharsFromString(argumentsString["-cid"], cidArg); + // cout << "final-cids: " << argumentsString["-cid"].length() << endl; + // cout << "final-cds: " << argumentsString["-csd"].length() << endl; + // cout << "-----------------------------------\n"; const int csdLength = argumentsString["-csd"].length(); - char csdArg[csdLength] = {}; - getCharsFromString(argumentsString["-csd"], csdArg); + char csdArg[32] = {}; + getCharsFromString(argumentsString["-csd"], csdArg, csdLength); + //cout << "csd delka pred: " << strlen(csdArg) << "\n"; + + //cout << "csd delka po: " << strlen(csdArg) << "\n"; + //cout << "csd:" << csdArg << "\n"; + const int cidLength = argumentsString["-cid"].length(); + char cidArg[32] = {}; + getCharsFromString(argumentsString["-cid"], cidArg, cidLength); + //cout << "cid delka pred: " << strlen(cidArg) << "\n"; + + //for (int i=0;i<32;i++) cidArg[i]=argumentsString["-cid"][i]; + //cout << "cid delka po: " << strlen(cidArg) << "\n"; + //cout << "cid delka sizeof po: " << sizeof(cidArg) << "\n"; + //cout << "cid:" << cidArg; - //char cidTest = getCharArray(argumentsString["-cid"]); + // char cidTest = getCharArray(argumentsString["-cid"]); + + // PlcLicence plcWriteLicence = PlcLicence(argumentsString); + // PlcLicence plcWriteLicence = PlcLicence(cidArg, csdArg, stationName, switchName, licenceType, binaryType, nullptr); + // PlcLicence plcWriteLicence = PlcLicence(arguments["-cid"], arguments["-csd"], stationName, switchName, licenceType, binaryType, nullptr); - //PlcLicence plcWriteLicence = PlcLicence(argumentsString); - //PlcLicence plcWriteLicence = PlcLicence(cidArg, csdArg, stationName, switchName, licenceType, binaryType, nullptr); PlcLicence plcWriteLicence = PlcLicence(cidArg, csdArg, argumentsString["-outputType"], argumentsString["-configFileName"]); - // PlcLicence plcWriteLicence = PlcLicence(arguments["-cid"], arguments["-csd"], stationName, switchName, licenceType, binaryType, nullptr); if (plcWriteLicence.CreateLicence() == false) { cerr << "Licence creation failed"; diff --git a/src/PlcLicence.cpp b/src/PlcLicence.cpp index 09ccea4..9e383a9 100644 --- a/src/PlcLicence.cpp +++ b/src/PlcLicence.cpp @@ -3,6 +3,9 @@ #include "plcLicence.h" #include "utils.h" +#define CID_LENGTH 32 +#define CSD_LENGTH 32 + const std::string cEzLic_p78ou3_sdinfofilepath = "/sys/block/mmcblk0/device/"; const std::string cEzLic_p78ou3_licencfilepath_f21 = "/mnt/mmc/ez_sys/licence/"; const std::string cEzLic_p78ou3_licencfilepath_f17 = "/home/admin/ez/licence/"; @@ -167,9 +170,8 @@ PlcLicence::PlcLicence(string cid, string csd, char *stationName, char *switchNa void PlcLicence::getSDData() { - int length = strlen(cid); // Get the length of the char array - for (int i = 0; i < length; i++) - this->sdData.CID_nibble[i] = (BYTE)cid[i]; + //int length = strlen(cid); // Get the length of the char array + for (int i = 0; i < CID_LENGTH; i++) this->sdData.CID_nibble[i] = (BYTE)cid[i]; for (int i = 0; i < cnibblescount / 2; i++) { @@ -201,9 +203,7 @@ void PlcLicence::getSDData() this->sdData.manufacturerDate[i] = date[i]; // CSD - length = strlen(csd); // Get the length of the char array - for (int i = 0; i < length; i++) - this->sdData.CSD_nibble[i] = (BYTE)csd[i]; + for (int i = 0; i < CSD_LENGTH; i++) this->sdData.CSD_nibble[i] = (BYTE)csd[i]; for (int i = 0; i < cnibblescount / 2; i++) { @@ -738,6 +738,7 @@ bool PlcLicence::CreateLicence() size_t r1 = fwrite(&totalEncryptedArray, sizeof(totalEncryptedArray), 1, fileLicence); //printf("License binary saved.\n"); fclose(fileLicence); + cout << licFileNameToSave << endl; return true; } } diff --git a/src/utils.cpp b/src/utils.cpp index 5bf6f40..b202eeb 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -72,6 +72,16 @@ void getCharsFromString1(string source, char *charArray) } } +void getCharsFromString(string& source, char *charArray, int length) +{ + memset(charArray, 0, length); + // charArray[length]; // Character array to store the string + for (int i = 0; i < length; i++) + { + charArray[i] = source[i]; + } +} + void getCharsFromString(string source, char *charArray) { memset(charArray, 0, sizeof(charArray));