From 0ec4d68600b83d41d2e48ad35d18e2a00f234619 Mon Sep 17 00:00:00 2001
From: gcontini <1121667+gcontini@users.noreply.github.com>
Date: 周六, 21 9月 2019 01:34:22 +0800
Subject: [PATCH] Merge branch 'develop' of https://github.com/open-license-manager/open-license-manager into develop

---
 src/tools/license-generator/license-generator.cpp |   74 ++++++++++++++++++++-----------------
 1 files changed, 40 insertions(+), 34 deletions(-)

diff --git a/src/tools/license-generator/license-generator.cpp b/src/tools/license-generator/license-generator.cpp
index 3b331ef..dcffd7e 100644
--- a/src/tools/license-generator/license-generator.cpp
+++ b/src/tools/license-generator/license-generator.cpp
@@ -40,38 +40,43 @@
 
 po::options_description LicenseGenerator::configureProgramOptions() {
 	po::options_description common("General options");
-	common.add_options()("help,h", "print help message and exit.") //
-	("verbose,v", "print more information.") //
-	("output,o", po::value<string>(), "Output file name. If not specified the "
-			"license will be printed in standard output"); //
+	common.add_options()
+		("help,h", "print help message and exit.")
+		("verbose,v", "print more information.")
+		("output,o", po::value<string>(), "Output file name. If not specified the "
+			"license will be printed in standard output")
+		;
 	po::options_description licenseGeneration("License Generation");
-	licenseGeneration.add_options()("private_key,p", po::value<string>(),
+	licenseGeneration.add_options()
+		("private_key,p", po::value<string>(),
 			"Specify an alternate file for the primary key to be used. "
-					"If not specified the internal primary key will be used.") //
-	("begin_date,b", po::value<string>(),
+					"If not specified the internal primary key will be used.")
+		("begin_date,b", po::value<string>(),
 			"Specify the start of the validity for this license. "
-					" Format YYYYMMDD. If not specified defaults to today") //
-	("expire_date,e", po::value<string>(),
+					" Format YYYYMMDD. If not specified defaults to today")
+		("expire_date,e", po::value<string>(),
 			"Specify the expire date for this license. "
-					" Format YYYYMMDD. If not specified the license won't expire") //
-	("client_signature,s", po::value<string>(),
+					" Format YYYYMMDD. If not specified the license won't expire")
+		("client_signature,s", po::value<string>(),
 			"The signature of the pc that requires the license. "
 					"It should be in the format XXXX-XXXX-XXXX-XXXX."
 					" If not specified the license "
-					"won't be linked to a specific pc.") //
-	("start_version,t", po::value<unsigned int>()->default_value(0
-	/*FullLicenseInfo.UNUSED_SOFTWARE_VERSION*/, "All Versions"),
-			"Specify the first version of the software this license apply to.") //
-	("end_version,n", po::value<unsigned int>()->default_value(0
-	/*FullLicenseInfo.UNUSED_SOFTWARE_VERSION*/, "All Versions"),
-			"Specify the last version of the software this license apply to."); //
+					"won't be linked to a specific pc.")
+		("start_version,t", po::value<unsigned int>()->default_value(0
+			/*FullLicenseInfo.UNUSED_SOFTWARE_VERSION*/, "All Versions"),
+			"Specify the first version of the software this license apply to.")
+		("end_version,n", po::value<unsigned int>()->default_value(0
+			/*FullLicenseInfo.UNUSED_SOFTWARE_VERSION*/, "All Versions"),
+			"Specify the last version of the software this license apply to.")
+		("extra_data,x", po::value<string>(), "Specify extra data to be included into the license")
+		;
 	po::options_description visibleOptions;
 	visibleOptions.add(common).add(licenseGeneration);
 	return visibleOptions;
 }
 
 vector<FullLicenseInfo> LicenseGenerator::parseLicenseInfo(
-		po::variables_map vm) {
+		const po::variables_map& vm) {
 	string begin_date = FullLicenseInfo::UNUSED_TIME;
 	string end_date = FullLicenseInfo::UNUSED_TIME;
 	if (vm.count("expire_date")) {
@@ -79,7 +84,7 @@
 		try {
 			end_date = normalize_date(dt_end);
 			char curdate[20];
-			time_t curtime = time(NULL);
+			time_t curtime = time(nullptr);
 			strftime(curdate, 20, "%Y-%m-%d", localtime(&curtime));
 			begin_date.assign(curdate);
 		} catch (const invalid_argument &e) {
@@ -102,15 +107,16 @@
 	string client_signature = "";
 	if (vm.count("client_signature")) {
 		client_signature = vm["client_signature"].as<string>();
-		//fixme match + and /
-		/*regex e("(A-Za-z0-9){4}-(A-Za-z0-9){4}-(A-Za-z0-9){4}-(A-Za-z0-9){4}");
-		 if (!regex_match(client_signature, e)) {
-		 cerr << endl << "Client signature not recognized: "
-		 << client_signature
-		 << " Please enter a valid signature in format XXXX-XXXX-XXXX-XXXX"
-		 << endl;
-		 exit(2);
-		 }*/
+		cout << "cli sig:" << client_signature;
+		regex e("[A-Za-z0-9\\+/]{4}-[A-Za-z0-9\\+/]{4}-[A-Za-z0-9\\+/]{4}-[A-Za-z0-9\\+/]{4}");
+		cout << "\nregex:";
+		if (!regex_match(client_signature, e)) {
+			cerr << endl << "Client signature not recognized: "
+				<< client_signature
+				<< " Please enter a valid signature in format XXXX-XXXX-XXXX-XXXX"
+				<< endl;
+			exit(2);
+		}
 	}
 	string extra_data = "";
 	if (vm.count("extra_data")) {
@@ -139,12 +145,12 @@
 void LicenseGenerator::generateAndOutputLicenses(const po::variables_map& vm,
 		ostream& outputFile) {
 	vector<FullLicenseInfo> licenseInfo = parseLicenseInfo(vm);
-	unique_ptr<CryptoHelper> helper = CryptoHelper::getInstance();
-	const char pkey[] = PRIVATE_KEY;
-	size_t len = sizeof(pkey);
+	const unique_ptr<CryptoHelper> helper = CryptoHelper::getInstance();
+	const unsigned char pkey[] = PRIVATE_KEY;
+	const size_t len = sizeof(pkey);
 	for (auto it = licenseInfo.begin(); it != licenseInfo.end(); ++it) {
 		const string license = it->printForSign();
-		string signature = helper->signString((const void *)pkey,len,license);
+		const string signature = helper->signString((const void *)pkey,len,license);
 		it->license_signature = signature;
 		it->printAsIni(outputFile);
 	}
@@ -174,7 +180,7 @@
 	if (vm.count("output")) {
 		const std::string fname = vm["output"].as<string>();
 
-		fstream ofstream(fname, std::ios::out | std::ios::trunc);
+		fstream ofstream(fname, std::ios::out | std::ios::app);
 		if (!ofstream.is_open()) {
 			cerr << "can't open file [" << fname << "] for output." << endl
 					<< " error: " << strerror( errno);

--
Gitblit v1.9.1