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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include "EntryPoint.h"
#include <string>
#include "TypeFactory.h"
#include "strUtils.h"
#include <sstream>
EntryPoint::EntryPoint()
{
reset();
}
EntryPoint::~EntryPoint()
{
}
void EntryPoint::reset()
{
m_unsupported = false;
m_customDecoder = false;
m_notApi = false;
m_flushOnEncode = false;
m_vars.empty();
}
bool parseTypeField(const std::string & f, std::string *vartype, std::string *varname)
{
size_t pos = 0, last;
bool done = false;
*vartype = "";
if (varname != NULL) *varname = "";
enum { ST_TYPE, ST_NAME, ST_END } state = ST_TYPE;
while(!done) {
std::string str = getNextToken(f, pos, &last, WHITESPACE);
if (str.size() == 0) break;
switch(state) {
case ST_TYPE:
if (str == "const") {
pos = last;
*vartype = "const ";
} else {
// must be a type name;
*vartype += str;
state = ST_NAME;
pos = last;
}
break;
case ST_NAME:
if (str.size() == 0) {
done = true;
} else if (str == "*") {
(*vartype) += "*";
pos = last;
} else if (varname == NULL) {
done = true;
} else {
while (str[0] == '*') {
(*vartype) += "*";
str[0] = ' ';
str = trim(str);
}
*varname = str;
done = true;
}
break;
case ST_END:
break;
}
}
return true;
}
// return true for valid line (need to get into the entry points list)
bool EntryPoint::parse(unsigned int lc, const std::string & str)
{
size_t pos, last;
std::string field;
reset();
std::string linestr = trim(str);
if (linestr.size() == 0) return false;
if (linestr.at(0) == '#') return false;
// skip PREFIX
field = getNextToken(linestr, 0, &last, "(");
pos = last + 1;
// return type
field = getNextToken(linestr, pos, &last, ",)");
std::string retTypeName;
if (!parseTypeField(field, &retTypeName, NULL)) {
fprintf(stderr, "line: %d: Parsing error in field <%s>\n", lc, field.c_str());
return false;
}
pos = last + 1;
const VarType *theType = TypeFactory::instance()->getVarTypeByName(retTypeName);
if (theType->name() == "UNKNOWN") {
fprintf(stderr, "UNKNOWN retval: %s\n", linestr.c_str());
}
m_retval.init(std::string(""), theType, std::string(""), Var::POINTER_OUT, std::string(""), std::string(""));
// function name
m_name = getNextToken(linestr, pos, &last, ",)");
pos = last + 1;
// parameters;
int nvars = 0;
while (pos < linestr.size() - 1) {
field = getNextToken(linestr, pos, &last, ",)");
std::string vartype, varname;
if (!parseTypeField(field, &vartype, &varname)) {
fprintf(stderr, "line: %d: Parsing error in field <%s>\n", lc, field.c_str());
return false;
}
nvars++;
const VarType *v = TypeFactory::instance()->getVarTypeByName(vartype);
if (v->id() == 0) {
fprintf(stderr, "%d: Unknown type: %s\n", lc, vartype.c_str());
} else {
if (varname == "" &&
!(v->name() == "void" && !v->isPointer())) {
std::ostringstream oss;
oss << "var" << nvars;
varname = oss.str();
}
m_vars.push_back(Var(varname, v, std::string(""), Var::POINTER_IN, "", ""));
}
pos = last + 1;
}
return true;
}
void EntryPoint::print(FILE *fp, bool newline,
const std::string & name_suffix,
const std::string & name_prefix,
const std::string & ctx_param ) const
{
fprintf(fp, "%s %s%s%s(",
m_retval.type()->name().c_str(),
name_prefix.c_str(),
m_name.c_str(),
name_suffix.c_str());
if (ctx_param != "") fprintf(fp, "%s ", ctx_param.c_str());
for (size_t i = 0; i < m_vars.size(); i++) {
if (m_vars[i].isVoid()) continue;
if (i != 0 || ctx_param != "") fprintf(fp, ", ");
fprintf(fp, "%s %s", m_vars[i].type()->name().c_str(),
m_vars[i].name().c_str());
}
fprintf(fp, ")%s", newline? "\n" : "");
}
Var * EntryPoint::var(const std::string & name)
{
Var *v = NULL;
for (size_t i = 0; i < m_vars.size(); i++) {
if (m_vars[i].name() == name) {
v = &m_vars[i];
break;
}
}
return v;
}
bool EntryPoint::hasPointers()
{
bool pointers = false;
if (m_retval.isPointer()) pointers = true;
if (!pointers) {
for (size_t i = 0; i < m_vars.size(); i++) {
if (m_vars[i].isPointer()) {
pointers = true;
break;
}
}
}
return pointers;
}
int EntryPoint::setAttribute(const std::string &line, size_t lc)
{
size_t pos = 0;
size_t last;
std::string token = getNextToken(line, 0, &last, WHITESPACE);
if (token == "len") {
pos = last;
std::string varname = getNextToken(line, pos, &last, WHITESPACE);
if (varname.size() == 0) {
fprintf(stderr, "ERROR: %u: Missing variable name in 'len' attribute\n", (unsigned int)lc);
return -1;
}
Var * v = var(varname);
if (v == NULL) {
fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
(unsigned int)lc, varname.c_str(), name().c_str());
return -2;
}
// set the size expression into var
pos = last;
v->setLenExpression(line.substr(pos));
} else if (token == "param_check") {
pos = last;
std::string varname = getNextToken(line, pos, &last, WHITESPACE);
if (varname.size() == 0) {
fprintf(stderr, "ERROR: %u: Missing variable name in 'param_check' attribute\n", (unsigned int)lc);
return -1;
}
Var * v = var(varname);
if (v == NULL) {
fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
(unsigned int)lc, varname.c_str(), name().c_str());
return -2;
}
// set the size expression into var
pos = last;
v->setParamCheckExpression(line.substr(pos));
} else if (token == "dir") {
pos = last;
std::string varname = getNextToken(line, pos, &last, WHITESPACE);
if (varname.size() == 0) {
fprintf(stderr, "ERROR: %u: Missing variable name in 'dir' attribute\n", (unsigned int)lc);
return -1;
}
Var * v = var(varname);
if (v == NULL) {
fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
(unsigned int)lc, varname.c_str(), name().c_str());
return -2;
}
pos = last;
std::string pointerDirStr = getNextToken(line, pos, &last, WHITESPACE);
if (pointerDirStr.size() == 0) {
fprintf(stderr, "ERROR: %u: missing pointer directions\n", (unsigned int)lc);
return -3;
}
if (pointerDirStr == "out") {
v->setPointerDir(Var::POINTER_OUT);
} else if (pointerDirStr == "inout") {
v->setPointerDir(Var::POINTER_INOUT);
} else if (pointerDirStr == "in") {
v->setPointerDir(Var::POINTER_IN);
} else {
fprintf(stderr, "ERROR: %u: unknow pointer direction %s\n", (unsigned int)lc, pointerDirStr.c_str());
}
} else if (token == "var_flag") {
pos = last;
std::string varname = getNextToken(line, pos, &last, WHITESPACE);
if (varname.size() == 0) {
fprintf(stderr, "ERROR: %u: Missing variable name in 'var_flag' attribute\n", (unsigned int)lc);
return -1;
}
Var * v = var(varname);
if (v == NULL) {
fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
(unsigned int)lc, varname.c_str(), name().c_str());
return -2;
}
int count = 0;
for (;;) {
pos = last;
std::string flag = getNextToken(line, pos, &last, WHITESPACE);
if (flag.size() == 0) {
if (count == 0) {
fprintf(stderr, "ERROR: %u: missing flag\n", (unsigned int) lc);
return -3;
}
break;
}
count++;
if (flag == "nullAllowed") {
if (v->isPointer()) {
v->setNullAllowed(true);
} else {
fprintf(stderr, "WARNING: %u: setting nullAllowed for non-pointer variable %s\n",
(unsigned int) lc, v->name().c_str());
}
} else if (flag == "isLarge") {
if (v->isPointer()) {
v->setIsLarge(true);
} else {
fprintf(stderr, "WARNING: %u: setting isLarge flag for a non-pointer variable %s\n",
(unsigned int) lc, v->name().c_str());
}
} else {
fprintf(stderr, "WARNING: %u: unknow flag %s\n", (unsigned int)lc, flag.c_str());
}
}
} else if (token == "custom_pack") {
pos = last;
std::string varname = getNextToken(line, pos, &last, WHITESPACE);
if (varname.size() == 0) {
fprintf(stderr, "ERROR: %u: Missing variable name in 'custom_pack' attribute\n", (unsigned int)lc);
return -1;
}
Var * v = var(varname);
if (v == NULL) {
fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
(unsigned int)lc, varname.c_str(), name().c_str());
return -2;
}
// set the size expression into var
pos = last;
v->setPackExpression(line.substr(pos));
} else if (token == "custom_write") {
pos = last;
std::string varname = getNextToken(line, pos, &last, WHITESPACE);
if (varname.size() == 0) {
fprintf(stderr, "ERROR: %u: Missing variable name in 'custom_write' attribute\n", (unsigned int)lc);
return -1;
}
Var * v = var(varname);
if (v == NULL) {
fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
(unsigned int)lc, varname.c_str(), name().c_str());
return -2;
}
// set the size expression into var
pos = last;
v->setWriteExpression(line.substr(pos));
} else if (token == "flag") {
pos = last;
std::string flag = getNextToken(line, pos, &last, WHITESPACE);
if (flag.size() == 0) {
fprintf(stderr, "ERROR: %u: missing flag\n", (unsigned int) lc);
return -4;
}
if (flag == "unsupported") {
setUnsupported(true);
} else if (flag == "custom_decoder") {
setCustomDecoder(true);
} else if (flag == "not_api") {
setNotApi(true);
} else if (flag == "flushOnEncode") {
setFlushOnEncode(true);
} else {
fprintf(stderr, "WARNING: %u: unknown flag %s\n", (unsigned int)lc, flag.c_str());
}
} else {
fprintf(stderr, "WARNING: %u: unknown attribute %s\n", (unsigned int)lc, token.c_str());
}
return 0;
}
|