From 4093b38f678b4d253b610f3439adb9e950e37c92 Mon Sep 17 00:00:00 2001
From: Jan Willamowius <jan@willamowius.de>
Date: 周一, 22 4月 2019 23:56:28 +0800
Subject: [PATCH] fix regex to check format of client signature

---
 src/tools/license-generator/license-generator.cpp |   44 ++++++++++++++++++++++----------------------
 1 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/src/tools/license-generator/license-generator.cpp b/src/tools/license-generator/license-generator.cpp
index ce456cc..0eae1f5 100644
--- a/src/tools/license-generator/license-generator.cpp
+++ b/src/tools/license-generator/license-generator.cpp
@@ -7,6 +7,7 @@
 #include <iostream>
 #include <string.h>
 #include <iostream>
+#include <string.h>
 #include <boost/date_time.hpp>
 #include <boost/program_options.hpp>
 #include <boost/algorithm/string.hpp>
@@ -70,13 +71,13 @@
 }
 
 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")) {
 		const std::string dt_end = vm["expire_date"].as<string>();
 		try {
-			end_date = normalize_date(dt_end.c_str());
+			end_date = normalize_date(dt_end);
 			char curdate[20];
 			time_t curtime = time(NULL);
 			strftime(curdate, 20, "%Y-%m-%d", localtime(&curtime));
@@ -90,7 +91,7 @@
 	if (vm.count("begin_date")) {
 		const std::string begin_date_str = vm["begin_date"].as<string>();
 		try {
-			begin_date = normalize_date(begin_date_str.c_str());
+			begin_date = normalize_date(begin_date_str);
 		} catch (invalid_argument &e) {
 			cerr << endl << "Begin date not recognized: " << begin_date_str
 					<< " Please enter a valid date in format YYYYMMDD" << endl;
@@ -101,15 +102,14 @@
 	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);
-		 }*/
+		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);
+		}
 	}
 	string extra_data = "";
 	if (vm.count("extra_data")) {
@@ -152,7 +152,7 @@
 int LicenseGenerator::generateLicense(int argc, const char **argv) {
 
 	po::options_description visibleOptions = configureProgramOptions();
-	//positional options must be addedd to standard options
+	//positional options must be added to standard options
 	po::options_description allOptions;
 	allOptions.add(visibleOptions).add_options()("product",
 			po::value<vector<string>>(), "product names");
@@ -190,22 +190,22 @@
 const std::string formats[] = { "%4u-%2u-%2u", "%4u/%2u/%2u", "%4u%2u%2u" };
 const size_t formats_n = 3;
 
-string LicenseGenerator::normalize_date(const char * s) {
+string LicenseGenerator::normalize_date(const std::string& sDate) {
+	if(sDate.size()<8)
+		throw invalid_argument("Date string too small for known formats");
 	unsigned int year, month, day;
-	int chread;
 	bool found = false;
 	for (size_t i = 0; i < formats_n && !found; ++i) {
-		chread = sscanf(s, formats[i].c_str(), &year, &month, &day);
-		if (chread == 3) {
+		const int chread = sscanf(sDate.c_str(),formats[i].c_str(),&year,&month,&day);
+		if(chread==3) {
 			found = true;
+			break;
 		}
 	}
-	if (!found) {
-		throw invalid_argument("Date not recognized.");
-	}
+	if(!found)
+		throw invalid_argument("Date string did not match a known format");
 	ostringstream oss;
-	oss << year << "-" << setfill('0') << std::setw(2) << month << "-" << day;
-	//delete (facet);
+	oss << year << "-" << setfill('0') << std::setw(2) << month << "-" << setfill('0') << std::setw(2) << day;
 	return oss.str();
 }
 }

--
Gitblit v1.9.1