From 1922d8c2cf7dcaacafd17394849577794b1f10eb Mon Sep 17 00:00:00 2001
From: open-license-manager <rillf@maildrop.cc>
Date: 周六, 13 9月 2014 06:24:55 +0800
Subject: [PATCH] signature windows

---
 src/license-generator/license-generator.cpp |   77 ++++++++++++++++++++------------------
 1 files changed, 40 insertions(+), 37 deletions(-)

diff --git a/src/license-generator/license-generator.cpp b/src/license-generator/license-generator.cpp
index a967e6f..846d5d9 100644
--- a/src/license-generator/license-generator.cpp
+++ b/src/license-generator/license-generator.cpp
@@ -1,6 +1,7 @@
 #include <build_properties.h>
 #include "LicenseSigner.h"
 #include "license-generator.h"
+#include "../library/base/StringUtils.h"
 #include <stdlib.h>
 #include <stdio.h>
 #include <iostream>
@@ -49,12 +50,12 @@
 	("begin_date,b", po::value<string>(),
 			"Specify the start of the validity for this license. "
 					" Format YYYYMMDD. If not specified defaults to today") //
-	("end_date,e", po::value<string>(),
+	("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>(),
 			"The signature of the pc that requires the license. "
-					"It should be in the format XXX-XXXX-XXXX."
+					"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
@@ -70,14 +71,17 @@
 
 vector<FullLicenseInfo> LicenseGenerator::parseLicenseInfo(
 		po::variables_map vm) {
-	time_t begin_date = FullLicenseInfo::UNUSED_TIME;
-	time_t end_date = FullLicenseInfo::UNUSED_TIME;
-	if (vm.count("end_date")) {
-		const std::string dt_end = vm["end_date"].as<string>();
+	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 = seconds_from_epoch(dt_end);
-			begin_date = time(NULL);
-		} catch (invalid_argument &e) {
+			end_date = normalize_date(dt_end.c_str());
+			char curdate[20];
+			time_t curtime = time(NULL);
+			strftime(curdate, 20, "%Y-%m-%d", localtime(&curtime));
+			begin_date.assign(curdate);
+		} catch (const invalid_argument &e) {
 			cerr << endl << "End date not recognized: " << dt_end
 					<< " Please enter a valid date in format YYYYMMDD" << endl;
 			exit(2);
@@ -86,7 +90,7 @@
 	if (vm.count("begin_date")) {
 		const std::string begin_date_str = vm["begin_date"].as<string>();
 		try {
-			begin_date = seconds_from_epoch(begin_date_str);
+			begin_date = normalize_date(begin_date_str.c_str());
 		} catch (invalid_argument &e) {
 			cerr << endl << "Begin date not recognized: " << begin_date_str
 					<< " Please enter a valid date in format YYYYMMDD" << endl;
@@ -97,22 +101,23 @@
 	string client_signature = "";
 	if (vm.count("client_signature")) {
 		client_signature = vm["client_signature"].as<string>();
-		regex e("\\d{3}-[:alnum:]{4}-[:alnum:]{4}");
+		//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 XXX-XXXX-XXX"
+					<< " Please enter a valid signature in format XXXX-XXXX-XXXX-XXXX"
 					<< endl;
 			exit(2);
-		}
+		}*/
 	}
 	string extra_data = "";
 	if (vm.count("extra_data")) {
 		extra_data = vm["extra_data"].as<string>();
 	}
-	unsigned int from_sw_version = end_date = vm["start_version"].as<
+	unsigned int from_sw_version = vm["start_version"].as<
 			unsigned int>();
-	unsigned int to_sw_version = end_date =
+	unsigned int to_sw_version =
 			vm["end_version"].as<unsigned int>();
 	if (vm.count("product") == 0) {
 		cerr << endl << "Parameter [product] not found. " << endl;
@@ -132,7 +137,7 @@
 	return licInfo;
 }
 
-void LicenseGenerator::generateAndOutptuLicenses(const po::variables_map& vm,
+void LicenseGenerator::generateAndOutputLicenses(const po::variables_map& vm,
 		ostream& outputFile) {
 	vector<FullLicenseInfo> licenseInfo = parseLicenseInfo(vm);
 	license::LicenseSigner licSigner =
@@ -175,36 +180,34 @@
 					<< " error: " << strerror( errno);
 			exit(3);
 		}
-		generateAndOutptuLicenses(vm, ofstream);
+		generateAndOutputLicenses(vm, ofstream);
 		ofstream.close();
 	} else {
-		generateAndOutptuLicenses(vm, cout);
+		generateAndOutputLicenses(vm, cout);
 	}
 	return 0;
 }
 
-const std::locale formats[] = { std::locale(std::locale::classic(),
-		new bt::time_input_facet("%Y-%m-%d")), //
-std::locale(std::locale::classic(), new bt::time_input_facet("%Y/%m/%d")), //
-std::locale(std::locale::classic(), new bt::time_input_facet("%Y%m%d")) };
-const size_t formats_n = sizeof(formats) / sizeof(formats[0]);
 
-time_t LicenseGenerator::seconds_from_epoch(const std::string& s) {
-	bt::ptime pt;
-	for (size_t i = 0; i < formats_n; ++i) {
-		std::istringstream is(s);
-		is.imbue(formats[i]);
-		is >> pt;
-		if (pt != bt::ptime()) {
-			break;
+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) {
+	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){
+			found = true;
 		}
 	}
-	if (pt == bt::ptime()) {
-		throw invalid_argument(string("Date not regognized") + s);
+	if (!found){
+		throw invalid_argument("Date not recognized.");
 	}
-	bt::ptime timet_start(boost::gregorian::date(1970, 1, 1));
-	bt::time_duration diff = pt - timet_start;
-	return diff.ticks() / bt::time_duration::rep_type::ticks_per_second;
+	ostringstream oss;
+	oss << year << "-"<<setfill('0') << std::setw(2)<< month<<"-"<<day;
+	//delete (facet);
+	return oss.str();
 }
-
 }

--
Gitblit v1.9.1