<commit_before>/*
 * LicenseReader.cpp
 *
 *  Created on: Mar 30, 2014
 *      
 */

#ifdef _WIN32
# pragma warning(disable: 4786)
#else
# include <unistd.h>
#endif
#include <cstring>
#include <ctime>
#include <vector>
#include <iostream>
#include <iterator>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include "pc-identifiers.h"
#include "LicenseReader.h"
#include "base/StringUtils.h"
#include "base/logger.h"
#include "public-key.h"
#include <build_properties.h>

namespace license {

const char *FullLicenseInfo::UNUSED_TIME = "0000-00-00";

FullLicenseInfo::FullLicenseInfo(const string& source, const string& product,
		const string& license_signature, int licenseVersion, string from_date,
		string to_date, const string& client_signature,
		unsigned int from_sw_version, unsigned int to_sw_version,
		const string& extra_data) :
		source(source), product(product), //
		license_signature(license_signature), license_version(licenseVersion), //
		from_date(from_date), to_date(to_date), //
		has_expiry(to_date != UNUSED_TIME), //
		from_sw_version(from_sw_version), to_sw_version(to_sw_version), //
		has_versions(
				from_sw_version != UNUSED_SOFTWARE_VERSION
						|| to_sw_version != UNUSED_SOFTWARE_VERSION), //
		client_signature(client_signature), has_client_sig(
				client_signature.length() > 0), //
		extra_data(extra_data) {
}

EventRegistry FullLicenseInfo::validate(int sw_version) {
	EventRegistry er;
	os_initialize();
	FUNCTION_RETURN sigVer = verifySignature(printForSign().c_str(),
			license_signature.c_str());
	bool sigVerified = sigVer == FUNC_RET_OK;
	if (sigVerified) {
		er.addEvent(LICENSE_VERIFIED, SVRT_INFO);
	} else {
		er.addEvent(LICENSE_CORRUPTED, SVRT_ERROR);
	}
	if (has_expiry) {
		time_t now = time(NULL);
		if (expires_on() < now) {
			er.addEvent(PRODUCT_EXPIRED, SVRT_ERROR, "");
		}
		if (valid_from() > now) {
			er.addEvent(PRODUCT_EXPIRED, SVRT_ERROR);
		}
	}
	if (has_client_sig) {
		PcSignature str_code;
		strncpy(str_code, client_signature.c_str(), sizeof(str_code));
		EVENT_TYPE event = validate_pc_signature(str_code);
		if (event != LICENSE_OK) {
			er.addEvent(event, SVRT_ERROR);
		}
	}
	return er;
}

void FullLicenseInfo::toLicenseInfo(LicenseInfo* license) const {
	if (license != NULL) {
		strncpy(license->proprietary_data, extra_data.c_str(),
		PROPRIETARY_DATA_SIZE);

		license->linked_to_pc = has_client_sig;
		license->has_expiry = has_expiry;
		if (!has_expiry) {
			license->expiry_date[0] = '\0';
			license->days_left = 999999;
		} else {
			strncpy(license->expiry_date, to_date.c_str(), 11);
			double secs = difftime(
				seconds_from_epoch(to_date.c_str()),
				time(NULL));
			license->days_left = (int) secs / 60 * 60 * 24;
		}
	}
}
LicenseReader::LicenseReader(const LicenseLocation& licenseLocation) :
		licenseLocation(licenseLocation) {

}

EventRegistry LicenseReader::readLicenses(const string &product,
		vector<FullLicenseInfo>& licenseInfoOut) {
	vector<string> diskFiles;
	EventRegistry result = getLicenseDiskFiles(diskFiles);
	if (!result.isGood()) {
		return result;
	}
	bool loadAtLeastOneFile = false;
	bool atLeastOneProductLicensed = false;
	bool atLeastOneLicenseComplete = false;
	CSimpleIniA ini;
	for (auto it = diskFiles.begin(); it != diskFiles.end(); it++) {
		ini.Reset();
		SI_Error rc = ini.LoadFile((*it).c_str());
		if (rc < 0) {<commit_msg>leave room for terminating nul byte<commit_after>/*
 * LicenseReader.cpp
 *
 *  Created on: Mar 30, 2014
 *      
 */

#ifdef _WIN32
# pragma warning(disable: 4786)
#else
# include <unistd.h>
#endif
#include <cstring>
#include <ctime>
#include <vector>
#include <iostream>
#include <iterator>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include "pc-identifiers.h"
#include "LicenseReader.h"
#include "base/StringUtils.h"
#include "base/logger.h"
#include "public-key.h"
#include <build_properties.h>

namespace license {

const char *FullLicenseInfo::UNUSED_TIME = "0000-00-00";

FullLicenseInfo::FullLicenseInfo(const string& source, const string& product,
		const string& license_signature, int licenseVersion, string from_date,
		string to_date, const string& client_signature,
		unsigned int from_sw_version, unsigned int to_sw_version,
		const string& extra_data) :
		source(source), product(product), //
		license_signature(license_signature), license_version(licenseVersion), //
		from_date(from_date), to_date(to_date), //
		has_expiry(to_date != UNUSED_TIME), //
		from_sw_version(from_sw_version), to_sw_version(to_sw_version), //
		has_versions(
				from_sw_version != UNUSED_SOFTWARE_VERSION
						|| to_sw_version != UNUSED_SOFTWARE_VERSION), //
		client_signature(client_signature), has_client_sig(
				client_signature.length() > 0), //
		extra_data(extra_data) {
}

EventRegistry FullLicenseInfo::validate(int sw_version) {
	EventRegistry er;
	os_initialize();
	FUNCTION_RETURN sigVer = verifySignature(printForSign().c_str(),
			license_signature.c_str());
	bool sigVerified = sigVer == FUNC_RET_OK;
	if (sigVerified) {
		er.addEvent(LICENSE_VERIFIED, SVRT_INFO);
	} else {
		er.addEvent(LICENSE_CORRUPTED, SVRT_ERROR);
	}
	if (has_expiry) {
		time_t now = time(NULL);
		if (expires_on() < now) {
			er.addEvent(PRODUCT_EXPIRED, SVRT_ERROR, "");
		}
		if (valid_from() > now) {
			er.addEvent(PRODUCT_EXPIRED, SVRT_ERROR);
		}
	}
	if (has_client_sig) {
		PcSignature str_code;
		strncpy(str_code, client_signature.c_str(), sizeof(str_code)-1);
		EVENT_TYPE event = validate_pc_signature(str_code);
		if (event != LICENSE_OK) {
			er.addEvent(event, SVRT_ERROR);
		}
	}
	return er;
}

void FullLicenseInfo::toLicenseInfo(LicenseInfo* license) const {
	if (license != NULL) {
		strncpy(license->proprietary_data, extra_data.c_str(),
		PROPRIETARY_DATA_SIZE);

		license->linked_to_pc = has_client_sig;
		license->has_expiry = has_expiry;
		if (!has_expiry) {
			license->expiry_date[0] = '\0';
			license->days_left = 999999;
		} else {
			strncpy(license->expiry_date, to_date.c_str(), 11-1);
			double secs = difftime(
				seconds_from_epoch(to_date.c_str()),
				time(NULL));
			license->days_left = (int) secs / 60 * 60 * 24;
		}
	}
}
LicenseReader::LicenseReader(const LicenseLocation& licenseLocation) :
		licenseLocation(licenseLocation) {

}

EventRegistry LicenseReader::readLicenses(const string &product,
		vector<FullLicenseInfo>& licenseInfoOut) {
	vector<string> diskFiles;
	EventRegistry result = getLicenseDiskFiles(diskFiles);
	if (!result.isGood()) {
		return result;
	}
	bool loadAtLeastOneFile = false;
	bool atLeastOneProductLicensed = false;
	bool atLeastOneLicenseComplete = false;
	CSimpleIniA ini;
	for (auto it = diskFiles.begin(); it != diskFiles.end(); it++) {
		ini.Reset();
		SI_Error rc = ini.LoadFile((*it).c_str());
		if (rc < 0) {
{'fraction_non_alphanumeric': 0.13102632357290742, 'fraction_numerical': 0.011535048802129548, 'mean_word_length': 5.262037037037037, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1337257', 'n_tokens_mistral': 2640, 'n_tokens_neox': 2396, 'n_words': 463}