gcontini
2019-10-19 8bbef2865455754425a84b86680a89bff8aa7691
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * EnvironmentVarData.cpp
 *
 *  Created on: Oct 12, 2019
 *     Author: Gabriele Contini
 */
 
#include "EnvironmentVarData.hpp"
 
#include <build_properties.h>
#include <cstdlib>
#include <regex>
#include <string>
#include <vector>
 
#include "../api/datatypes.h"
#include "../base/base64.h"
#include "../base/EventRegistry.h"
#include "../base/StringUtils.h"
 
namespace license {
namespace locate {
 
using namespace std;
 
EnvironmentVarData::EnvironmentVarData() :
        LocatorStrategy("EnvironmentVarData") {
}
 
EnvironmentVarData::~EnvironmentVarData() {
}
 
const vector<string> EnvironmentVarData::license_locations(
        EventRegistry &eventRegistry) {
    vector<string> diskFiles;
    char *env_var_value = getenv(LICENSE_DATA_ENV_VAR);
    if (env_var_value != nullptr && env_var_value[0] != '\0') {
        eventRegistry.addEvent(LICENSE_SPECIFIED, LICENSE_LOCATION_ENV_VAR);
        FILE_FORMAT licenseFormat = identify_format(env_var_value);
        if (licenseFormat == UNKNOWN) {
            eventRegistry.addEvent(LICENSE_MALFORMED, LICENSE_LOCATION_ENV_VAR);
        } else {
            diskFiles.push_back(LICENSE_LOCATION_ENV_VAR);
            isBase64 = (licenseFormat == BASE64);
        }
    } else {
        eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED,
        LICENSE_LOCATION_ENV_VAR);
    }
    return diskFiles;
}
 
const std::string EnvironmentVarData::retrieve_license_content(
        const std::string &licenseLocation) const {
    string tmpVal = getenv(LICENSE_LOCATION_ENV_VAR);
    if (isBase64) {
        int flen = 0;
        unsigned char *raw = unbase64(tmpVal.c_str(), tmpVal.length(), &flen);
        string str = string(reinterpret_cast<char*>(raw));
        free(raw);
        return str;
    }
    return tmpVal;
}
 
}
}