aboutsummaryrefslogtreecommitdiffstats
path: root/rilsrc/libsecnativefeature/SecNativeFeatureCppIf.cpp
blob: 0c5bd10109936f110a1e1cc804012439594438b2 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <string>
#include <iostream>
#include "SecNativeFeatureCppIf.h"
#include <expat.h>
#include <stdlib.h>

// feature file location (which should be "/system/csc/feature.xml")
#define FEATURE_FILE "/system/csc/feature.xml"
// feature file location (which should be "/system/csc/others.xml")
#define MPS_FEATURE_FILE "/system/csc/others.xml"

// XML parsing using expat lib - handlers
typedef struct{
	std::string curr_name;
	std::map<std::string,std::string> *pFeatures;
	int depth;
} ParserUserData;

static void XMLCALL
charDataHandler(void *userData, const char *s, int len){
	ParserUserData* pData = (ParserUserData*)userData;
	std::string value(s, len);
	std::string curr_name = pData->curr_name;

	if(!curr_name.empty()){
		std::map<std::string,std::string>::iterator result = (*pData->pFeatures).find(curr_name);

		if (result != (*pData->pFeatures).end()) {
			value = result->second + value;
			(*pData->pFeatures).erase(curr_name);
		}

		std::map<std::string,std::string>::iterator begin;
		begin = (*pData->pFeatures).begin();
		std::pair<std::string,std::string> feature(curr_name,value);
		(*pData->pFeatures).insert(begin, feature);
	}
	// else{
		// // printf("no name\n");
	// }
}

static void XMLCALL
startElement(void *userData, const char *name, const char **atts)
{
	ParserUserData* pData = (ParserUserData*)userData;
	pData->curr_name.assign(name);
	pData->depth += 1;
}

static void XMLCALL
endElement(void *userData, const char *name)
{
	ParserUserData* pData = (ParserUserData*)userData;
	pData->curr_name.clear();
	pData->depth -= 1;
}

// SecNativeFeture class implementation
SecNativeFeature* SecNativeFeature::_instance = NULL;
SecNativeFeature::SecNativeFeature() {
	int load_result = 0;
	_features.clear();
	load_result = _loadFeature();
	if (load_result == -1){
		// todo : handle _loadFeature errors here.
	}
}

SecNativeFeature::~SecNativeFeature() {
	delete _instance;
}
SecNativeFeature* SecNativeFeature::getInstance() {
	if(_instance == NULL) {
		_instance = new SecNativeFeature();
	}
	return _instance;
}

bool SecNativeFeature::getEnableStatus(const char* tag) {
	std::map<std::string,std::string>::iterator found;
	found = _features.find(tag);
	
	if(found == _features.end()) {
		return false;
	}

	if(found->second.compare("true") == 0 || found->second.compare("TRUE") == 0) {
		return true;
	}
	return false;
}

bool SecNativeFeature::getEnableStatus(const char* tag, bool defaultValue) {
	std::map<std::string,std::string>::iterator found;
	found = _features.find(tag);
	
	if(found == _features.end()) {
		return defaultValue;
	}

	if(found->second.compare("true") == 0 || found->second.compare("TRUE") == 0) {
		return true;
	}
	return defaultValue;
}

const char* SecNativeFeature::getString(const char* tag) {
	std::map<std::string,std::string>::iterator found;
	found = _features.find(tag);

	if(found == _features.end()) {
		return "";
	}
	return found->second.c_str();
}

const char* SecNativeFeature::getString(const char* tag, char* defaultValue) {
	std::map<std::string,std::string>::iterator found;
	found = _features.find(tag);

	if(found == _features.end()) {
		return defaultValue;
	}
	return found->second.c_str();
}

int SecNativeFeature::getInteger(const char* tag) {
	std::map<std::string,std::string>::iterator found;
	found = _features.find(tag);
	
	if(found == _features.end()) {
		return -1;
	}
	std::string raw_value = _features.find(tag)->second;
	return atoi(raw_value.c_str());
}

int SecNativeFeature::getInteger(const char* tag, int defaultValue) {
	std::map<std::string,std::string>::iterator found;
	found = _features.find(tag);
	
	if(found == _features.end()) {
		return defaultValue;
	}
	std::string raw_value = _features.find(tag)->second;
	return atoi(raw_value.c_str());
}

int SecNativeFeature::_loadFeature(){
	char buf[BUFSIZ];
	XML_Parser parser = XML_ParserCreate(NULL);
	int done;
	FILE * pFeatureFile = NULL;
	ParserUserData userData;
	userData.curr_name = std::string ();
	userData.pFeatures = &_features;
	userData.depth = 0;
	
	pFeatureFile = fopen(FEATURE_FILE, "r");
	if (pFeatureFile == NULL) {
		pFeatureFile = fopen(MPS_FEATURE_FILE, "r");
		if (pFeatureFile == NULL) {
			return -1;
		}
	}
	XML_SetUserData(parser, &userData);
	XML_SetElementHandler(parser, startElement, endElement);
	XML_SetCharacterDataHandler(parser, charDataHandler);
	do {
		size_t len = fread(buf, 1, sizeof(buf), pFeatureFile);
		if ((len != sizeof(buf)) && (ferror(pFeatureFile))){
			fclose(pFeatureFile);
			return -1;
		}
		done = len < sizeof(buf);
		if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
			if(pFeatureFile) {
				fclose(pFeatureFile);
			}
			return -1;
		}
	} while (!done);
	XML_ParserFree(parser);
	fclose(pFeatureFile);
	return 0;
}