Some code formating

This commit is contained in:
2026-01-18 15:44:20 +01:00
parent 8061681705
commit 8f0341d0bd
26 changed files with 3989 additions and 3988 deletions

View File

@@ -4,16 +4,16 @@
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -60,297 +60,297 @@ struct Loan
std::string operatorURL;
std::string validity;
std::string bookName;
std::string path;
};
class LoanMGT
{
public:
~LoanMGT()
{
for (const auto& kv : loanedBooks)
delete kv.second;
for (const auto& kv : loanedBooks)
delete kv.second;
}
int run()
{
int ret = 0;
try
{
DRMProcessorClientImpl client;
gourou::DRMProcessor processor(&client, deviceFile, activationFile, devicekeyFile);
int ret = 0;
try
{
DRMProcessorClientImpl client;
gourou::DRMProcessor processor(&client, deviceFile, activationFile, devicekeyFile);
loadLoanedBooks();
loadLoanedBooks();
if (list)
displayLoanList();
else if (returnID)
returnBook(processor);
else if (deleteID)
deleteLoan();
} catch(std::exception& e)
{
std::cout << e.what() << std::endl;
ret = 1;
}
if (list)
displayLoanList();
else if (returnID)
returnBook(processor);
else if (deleteID)
deleteLoan();
} catch(std::exception& e)
{
std::cout << e.what() << std::endl;
ret = 1;
}
return ret;
return ret;
}
private:
void loadLoanedBooks()
{
DIR *dp;
struct dirent *ep;
int entryLen;
struct Loan* loan;
char * res;
std::string loanDir = std::string(adeptDir) + std::string("/") + LOANS_DIR;
DIR *dp;
struct dirent *ep;
int entryLen;
struct Loan* loan;
char * res;
std::string loanDir = std::string(adeptDir) + std::string("/") + LOANS_DIR;
if (!fileExists(loanDir.c_str()))
return;
dp = opendir (loanDir.c_str());
if (!fileExists(loanDir.c_str()))
return;
dp = opendir (loanDir.c_str());
if(!dp)
EXCEPTION(gourou::USER_INVALID_INPUT, "Cannot read directory " << loanDir);
if(!dp)
EXCEPTION(gourou::USER_INVALID_INPUT, "Cannot read directory " << loanDir);
while ((ep = readdir (dp)))
{
if (ep->d_type != DT_LNK &&
ep->d_type != DT_REG)
continue;
while ((ep = readdir (dp)))
{
if (ep->d_type != DT_LNK &&
ep->d_type != DT_REG)
continue;
entryLen = strlen(ep->d_name);
entryLen = strlen(ep->d_name);
if (entryLen <= 4 ||
ep->d_name[entryLen-4] != '.' ||
ep->d_name[entryLen-3] != 'x' ||
ep->d_name[entryLen-2] != 'm' ||
ep->d_name[entryLen-1] != 'l')
continue;
if (entryLen <= 4 ||
ep->d_name[entryLen-4] != '.' ||
ep->d_name[entryLen-3] != 'x' ||
ep->d_name[entryLen-2] != 'm' ||
ep->d_name[entryLen-1] != 'l')
continue;
std::string id = std::string(ep->d_name, entryLen-4);
loan = new Loan;
loan->path = loanDir + std::string("/") + ep->d_name;
std::string id = std::string(ep->d_name, entryLen-4);
loan = new Loan;
loan->path = loanDir + std::string("/") + ep->d_name;
pugi::xml_document xmlDoc;
pugi::xml_node node;
pugi::xml_document xmlDoc;
pugi::xml_node node;
if (!xmlDoc.load_file(loan->path.c_str(), pugi::parse_ws_pcdata_single|pugi::parse_escapes, pugi::encoding_utf8))
{
std::cout << "Invalid loan entry " << loan->path << std::endl;
goto error;
}
if (!xmlDoc.load_file(loan->path.c_str(), pugi::parse_ws_pcdata_single|pugi::parse_escapes, pugi::encoding_utf8))
{
std::cout << "Invalid loan entry " << loan->path << std::endl;
goto error;
}
// id
node = xmlDoc.select_node("//id").node();
if (!node)
{
std::cout << "Invalid loan entry " << ep->d_name << ", no id element" << std::endl;
goto error;
}
loan->id = node.first_child().value();
// id
node = xmlDoc.select_node("//id").node();
if (!node)
{
std::cout << "Invalid loan entry " << ep->d_name << ", no id element" << std::endl;
goto error;
}
loan->id = node.first_child().value();
// operatorURL
node = xmlDoc.select_node("//operatorURL").node();
if (!node)
{
std::cout << "Invalid loan entry " << ep->d_name << ", no operatorURL element" << std::endl;
goto error;
}
loan->operatorURL = node.first_child().value();
// operatorURL
node = xmlDoc.select_node("//operatorURL").node();
if (!node)
{
std::cout << "Invalid loan entry " << ep->d_name << ", no operatorURL element" << std::endl;
goto error;
}
loan->operatorURL = node.first_child().value();
// validity
node = xmlDoc.select_node("//validity").node();
if (!node)
{
std::cout << "Invalid loan entry " << ep->d_name << ", no validity element" << std::endl;
goto error;
}
loan->validity = node.first_child().value();
// validity
node = xmlDoc.select_node("//validity").node();
if (!node)
{
std::cout << "Invalid loan entry " << ep->d_name << ", no validity element" << std::endl;
goto error;
}
loan->validity = node.first_child().value();
// bookName
node = xmlDoc.select_node("//name").node();
if (!node)
{
std::cout << "Invalid loan entry " << ep->d_name << ", no name element" << std::endl;
goto error;
}
loan->bookName = node.first_child().value();
// bookName
node = xmlDoc.select_node("//name").node();
if (!node)
{
std::cout << "Invalid loan entry " << ep->d_name << ", no name element" << std::endl;
goto error;
}
loan->bookName = node.first_child().value();
struct tm tm;
struct tm tm;
#ifdef __ANDROID__
res = strptime(loan->validity.c_str(), "%Y-%m-%dT%H:%M:%S%z", &tm);
res = strptime(loan->validity.c_str(), "%Y-%m-%dT%H:%M:%S%z", &tm);
#else
res = strptime(loan->validity.c_str(), "%Y-%m-%dT%H:%M:%S%Z", &tm);
res = strptime(loan->validity.c_str(), "%Y-%m-%dT%H:%M:%S%Z", &tm);
#endif
if (res != NULL && *res == 0)
{
if (mktime(&tm) <= time(NULL))
loan->validity = " (Expired)";
}
else
{
std::cout << "Unable to parse validity timestamp :" << loan->validity << std::endl;
loan->validity = " (Unknown)";
}
loanedBooks[id] = loan;
continue;
if (res != NULL && *res == 0)
{
if (mktime(&tm) <= time(NULL))
loan->validity = " (Expired)";
}
else
{
std::cout << "Unable to parse validity timestamp :" << loan->validity << std::endl;
loan->validity = " (Unknown)";
}
loanedBooks[id] = loan;
continue;
error:
if (loan)
delete loan;
}
error:
if (loan)
delete loan;
}
closedir (dp);
closedir (dp);
}
void displayLoanList()
{
if (!loanedBooks.size())
{
std::cout << "No books loaned" << std::endl;
return;
}
if (!loanedBooks.size())
{
std::cout << "No books loaned" << std::endl;
return;
}
struct Loan* loan;
unsigned int maxSizeBookName=0;
// Compute max size
for (const auto& kv : loanedBooks)
{
loan = kv.second;
if (loan->bookName.size() > maxSizeBookName)
maxSizeBookName = loan->bookName.size();
}
struct Loan* loan;
unsigned int maxSizeBookName=0;
// Compute max size
for (const auto& kv : loanedBooks)
{
loan = kv.second;
if (loan->bookName.size() > maxSizeBookName)
maxSizeBookName = loan->bookName.size();
}
/* Manage empty names */
if (maxSizeBookName == 0)
maxSizeBookName = sizeof("No name ")-1;
else if (maxSizeBookName < 4)
maxSizeBookName = 4;
else if (maxSizeBookName > MAX_SIZE_BOOK_NAME)
maxSizeBookName = MAX_SIZE_BOOK_NAME;
else if ((maxSizeBookName % 2))
maxSizeBookName++;
/* Manage empty names */
if (maxSizeBookName == 0)
maxSizeBookName = sizeof("No name ")-1;
else if (maxSizeBookName < 4)
maxSizeBookName = 4;
else if (maxSizeBookName > MAX_SIZE_BOOK_NAME)
maxSizeBookName = MAX_SIZE_BOOK_NAME;
else if ((maxSizeBookName % 2))
maxSizeBookName++;
// std::cout << " ID Book Expiration" << std::endl;
// std::cout << "------------------------------" << std::endl;
// std::cout << " ID Book Expiration" << std::endl;
// std::cout << "------------------------------" << std::endl;
int fillID, fillBookName, fillExpiration=(20 - 10)/2;
fillID = (ID_HASH_SIZE - 2) / 2;
fillBookName = (maxSizeBookName - 4) / 2;
int fillID, fillBookName, fillExpiration=(20 - 10)/2;
fillID = (ID_HASH_SIZE - 2) / 2;
fillBookName = (maxSizeBookName - 4) / 2;
std::cout.width (fillID);
std::cout << "";
std::cout << "ID" ;
std::cout.width (fillID);
std::cout << "";
std::cout << " " ;
std::cout.width (fillBookName);
std::cout << "";
std::cout << "Book" ;
std::cout.width (fillBookName);
std::cout << "";
std::cout << " " ;
std::cout.width (fillID);
std::cout << "";
std::cout << "ID" ;
std::cout.width (fillID);
std::cout << "";
std::cout << " " ;
std::cout.width (fillBookName);
std::cout << "";
std::cout << "Book" ;
std::cout.width (fillBookName);
std::cout << "";
std::cout << " " ;
std::cout.width (fillExpiration);
std::cout << "";
std::cout << "Expiration";
std::cout.width (fillExpiration);
std::cout << "" << std::endl;
std::cout.width (fillExpiration);
std::cout << "";
std::cout << "Expiration";
std::cout.width (fillExpiration);
std::cout << "" << std::endl;
std::cout.fill ('-');
std::cout.width (ID_HASH_SIZE + 4 + maxSizeBookName + 4 + 20);
std::cout << "" << std::endl;
std::cout.fill (' ');
std::cout.fill ('-');
std::cout.width (ID_HASH_SIZE + 4 + maxSizeBookName + 4 + 20);
std::cout << "" << std::endl;
std::cout.fill (' ');
std::string bookName;
std::string bookName;
for (const auto& kv : loanedBooks)
{
loan = kv.second;
for (const auto& kv : loanedBooks)
{
loan = kv.second;
std::cout << kv.first;
std::cout << " ";
std::cout << kv.first;
std::cout << " ";
if (loan->bookName.size() == 0)
bookName = std::string("No name ");
else if (loan->bookName.size() > MAX_SIZE_BOOK_NAME)
bookName = std::string(loan->bookName.c_str(), MAX_SIZE_BOOK_NAME);
else
bookName = loan->bookName;
if (loan->bookName.size() == 0)
bookName = std::string("No name ");
else if (loan->bookName.size() > MAX_SIZE_BOOK_NAME)
bookName = std::string(loan->bookName.c_str(), MAX_SIZE_BOOK_NAME);
else
bookName = loan->bookName;
std::cout << bookName;
std::cout.width (maxSizeBookName - bookName.size());
std::cout << "";
std::cout << " ";
std::cout << loan->validity << std::endl;
}
std::cout << bookName;
std::cout.width (maxSizeBookName - bookName.size());
std::cout << "";
std::cout << " ";
std::cout << loan->validity << std::endl;
}
std::cout << std::endl;
std::cout << std::endl;
}
void returnBook(gourou::DRMProcessor& processor)
{
struct Loan* loan = loanedBooks[std::string(returnID)];
struct Loan* loan = loanedBooks[std::string(returnID)];
if (!loan)
{
std::cout << "Error : Loan " << returnID << " doesn't exists" << std::endl;
return;
}
if (!loan)
{
std::cout << "Error : Loan " << returnID << " doesn't exists" << std::endl;
return;
}
processor.returnLoan(loan->id, loan->operatorURL, notify);
deleteID = returnID;
if (deleteLoan(false))
{
std::cout << "Loan " << returnID << " successfully returned" << std::endl;
}
}
processor.returnLoan(loan->id, loan->operatorURL, notify);
deleteID = returnID;
if (deleteLoan(false))
{
std::cout << "Loan " << returnID << " successfully returned" << std::endl;
}
}
bool deleteLoan(bool displayResult=true)
{
struct Loan* loan = loanedBooks[std::string(deleteID)];
struct Loan* loan = loanedBooks[std::string(deleteID)];
if (!loan)
{
std::cout << "Error : Loan " << deleteID << " doesn't exists" << std::endl;
return false;
}
if (!loan)
{
std::cout << "Error : Loan " << deleteID << " doesn't exists" << std::endl;
return false;
}
if (unlink(loan->path.c_str()))
{
std::cout << "Error : Cannot delete " << loan->path << std::endl;
return false;
}
else if (displayResult)
{
std::cout << "Loan " << deleteID << " deleted" << std::endl;
}
return true;
if (unlink(loan->path.c_str()))
{
std::cout << "Error : Cannot delete " << loan->path << std::endl;
return false;
}
else if (displayResult)
{
std::cout << "Loan " << deleteID << " deleted" << std::endl;
}
return true;
}
std::map<std::string, struct Loan*> loanedBooks;
};
};
static void usage(const char* cmd)
{
std::cout << basename((char*)cmd) << " manage loaned books" << std::endl << std::endl;
std::cout << "Usage: " << basename((char*)cmd) << " [OPTIONS]" << std::endl << std::endl;
std::cout << "Global Options:" << std::endl;
std::cout << " " << "-l|--list" << "\t\t" << "List all loaned books" << std::endl;
std::cout << " " << "-r|--return" << "\t\t" << "Return a loaned book" << std::endl;
@@ -381,69 +381,69 @@ int main(int argc, char** argv)
const char** files[] = {&devicekeyFile, &deviceFile, &activationFile};
int verbose = gourou::DRMProcessor::getLogLevel();
int actions = 0;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{"adept-directory", required_argument, 0, 'D' },
{"list", no_argument, 0, 'l' },
{"return", no_argument, 0, 'r' },
{"delete", no_argument, 0, 'd' },
{"no-notify", no_argument, 0, 'N' },
{"verbose", no_argument, 0, 'v' },
{"version", no_argument, 0, 'V' },
{"help", no_argument, 0, 'h' },
{0, 0, 0, 0 }
};
int option_index = 0;
static struct option long_options[] = {
{"adept-directory", required_argument, 0, 'D' },
{"list", no_argument, 0, 'l' },
{"return", no_argument, 0, 'r' },
{"delete", no_argument, 0, 'd' },
{"no-notify", no_argument, 0, 'N' },
{"verbose", no_argument, 0, 'v' },
{"version", no_argument, 0, 'V' },
{"help", no_argument, 0, 'h' },
{0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "d:lr:D:vVh",
c = getopt_long(argc, argv, "d:lr:D:vVh",
long_options, &option_index);
if (c == -1)
break;
if (c == -1)
break;
switch (c) {
case 'D':
adeptDir = optarg;
break;
case 'l':
list = true;
actions++;
break;
case 'r':
returnID = optarg;
actions++;
break;
case 'd':
deleteID = optarg;
actions++;
break;
case 'N':
notify = false;
break;
case 'v':
verbose++;
break;
case 'V':
version();
return 0;
case 'h':
usage(argv[0]);
return 0;
default:
usage(argv[0]);
return -1;
}
switch (c) {
case 'D':
adeptDir = optarg;
break;
case 'l':
list = true;
actions++;
break;
case 'r':
returnID = optarg;
actions++;
break;
case 'd':
deleteID = optarg;
actions++;
break;
case 'N':
notify = false;
break;
case 'v':
verbose++;
break;
case 'V':
version();
return 0;
case 'h':
usage(argv[0]);
return 0;
default:
usage(argv[0]);
return -1;
}
}
gourou::DRMProcessor::setLogLevel(verbose);
// By default, simply list books loaned
if (actions == 0)
list = true;
list = true;
else if (actions != 1)
{
usage(argv[0]);
return -1;
usage(argv[0]);
return -1;
}
LoanMGT loanMGT;
@@ -454,50 +454,50 @@ int main(int argc, char** argv)
char *filename;
for (i=0; i<(int)ARRAY_SIZE(files); i++)
{
orig = *files[i];
if (adeptDir)
{
std::string path = std::string(adeptDir) + std::string("/") + orig;
filename = strdup(path.c_str());
}
else
filename = strdup(orig);
*files[i] = findFile(filename);
free(filename);
if (!*files[i])
{
std::cout << "Error : " << orig << " doesn't exists, did you activate your device ?" << std::endl;
hasErrors = true;
}
orig = *files[i];
if (adeptDir)
{
std::string path = std::string(adeptDir) + std::string("/") + orig;
filename = strdup(path.c_str());
}
else
filename = strdup(orig);
*files[i] = findFile(filename);
free(filename);
if (!*files[i])
{
std::cout << "Error : " << orig << " doesn't exists, did you activate your device ?" << std::endl;
hasErrors = true;
}
}
if (hasErrors)
{
// In case of adept dir was provided by user
adeptDir = 0;
goto end;
// In case of adept dir was provided by user
adeptDir = 0;
goto end;
}
if (adeptDir)
adeptDir = strdup(adeptDir); // For below free
adeptDir = strdup(adeptDir); // For below free
else
{
adeptDir = strdup(deviceFile);
adeptDir = dirname(adeptDir);
adeptDir = strdup(deviceFile);
adeptDir = dirname(adeptDir);
}
ret = loanMGT.run();
end:
for (i=0; i<(int)ARRAY_SIZE(files); i++)
{
if (*files[i])
free((void*)*files[i]);
if (*files[i])
free((void*)*files[i]);
}
if (adeptDir)
free(adeptDir);
free(adeptDir);
return ret;
}