|
1 /* |
|
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Misc utility functions |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "la.hpp" |
|
20 |
|
21 // ---------------------------------------------------------------------------------------------------------- |
|
22 |
|
23 void MakeSureTrailingDirectoryMarkerExists(string& path) |
|
24 { |
|
25 if (!path.empty() && path.at(path.length()-1) != DIR_SEPARATOR2) |
|
26 { |
|
27 path.insert(path.length(), DIR_SEPARATOR); |
|
28 } |
|
29 } |
|
30 |
|
31 // ---------------------------------------------------------------------------------------------------------- |
|
32 |
|
33 bool FileExists(const string& path) |
|
34 { |
|
35 if (!path.empty()) |
|
36 { |
|
37 struct stat stat_p; |
|
38 if (stat(path.c_str(), &stat_p) == 0) |
|
39 return !S_ISDIR(stat_p.st_mode); // return true if not a directory |
|
40 else |
|
41 return false; // cannot find entry |
|
42 } |
|
43 else |
|
44 return false; |
|
45 } |
|
46 |
|
47 // ---------------------------------------------------------------------------------------------------------- |
|
48 |
|
49 bool DirectoryExists(const string& path) |
|
50 { |
|
51 if (!path.empty()) |
|
52 { |
|
53 string temp_path = path; |
|
54 |
|
55 // remove trailing directory marker if exists |
|
56 if (path.at(path.length()-1) == DIR_SEPARATOR2) |
|
57 temp_path = path.substr(0, path.length()-1); |
|
58 |
|
59 struct stat stat_p; |
|
60 if (stat(temp_path.c_str(), &stat_p) == 0) |
|
61 return S_ISDIR(stat_p.st_mode); // return true if a directory |
|
62 else |
|
63 return false; // cannot find entry |
|
64 } |
|
65 else |
|
66 return false; |
|
67 } |
|
68 |
|
69 // ---------------------------------------------------------------------------------------------------------- |
|
70 |
|
71 bool RemoveFile(const string& path) |
|
72 { |
|
73 return _unlink(path.c_str()) == 0; |
|
74 } |
|
75 |
|
76 // ---------------------------------------------------------------------------------------------------------- |
|
77 |
|
78 string LowerCase(const string& s) |
|
79 { |
|
80 char* buf = new char[s.length()]; |
|
81 s.copy(buf, s.length()); |
|
82 |
|
83 for(unsigned int i = 0; i < s.length(); i++) |
|
84 buf[i] = tolower(buf[i]); |
|
85 |
|
86 string r(buf, s.length()); |
|
87 delete buf; |
|
88 return r; |
|
89 } |
|
90 |
|
91 // ---------------------------------------------------------------------------------------------------------- |
|
92 |
|
93 void MkDirAll(const string& path) |
|
94 { |
|
95 if (!path.empty() && !DirectoryExists(path)) |
|
96 { |
|
97 string target_path = path; |
|
98 |
|
99 // make sure that the directory has a trailing directory marker |
|
100 MakeSureTrailingDirectoryMarkerExists(target_path); |
|
101 |
|
102 // loop through each character in the string and try to find directory delimeters |
|
103 for (unsigned int i=0; i<target_path.length(); i++) |
|
104 { |
|
105 string::size_type pos = target_path.find(DIR_SEPARATOR, i); |
|
106 |
|
107 if (pos != string::npos) |
|
108 { |
|
109 // construct the base directory name |
|
110 string base_directory = target_path.substr(0, pos+1); |
|
111 //cerr << "MkDirAll: base_directory: " << base_directory << endl; |
|
112 |
|
113 if (!DirectoryExists(base_directory)) |
|
114 { |
|
115 //cerr << "MkDirAll: trying to create: " << base_directory << endl; |
|
116 _mkdir(base_directory.c_str()); |
|
117 } |
|
118 |
|
119 i=pos; |
|
120 } |
|
121 } |
|
122 } |
|
123 } |
|
124 |
|
125 // ---------------------------------------------------------------------------------------------------------- |
|
126 |
|
127 bool ExecuteCommand(const string& command, vector<string>& resultset) |
|
128 { |
|
129 // note, cannot use compiler parameters "-std=c++98" because of popen/pclose |
|
130 // also cannot compile this code is MSVC because usage of popen/pclose |
|
131 |
|
132 FILE* fp; |
|
133 char buffer[1024]; |
|
134 string tempstr; |
|
135 |
|
136 resultset.clear(); |
|
137 |
|
138 if ((fp = _popen(command.c_str(), "r")) == NULL) |
|
139 { |
|
140 return false; |
|
141 } |
|
142 |
|
143 while (fgets(buffer, sizeof(buffer), fp)) |
|
144 { |
|
145 tempstr = buffer; |
|
146 resultset.push_back(tempstr.substr(0, tempstr.size()-1)); |
|
147 } |
|
148 |
|
149 _pclose(fp); |
|
150 |
|
151 return true; |
|
152 } |
|
153 |
|
154 // ---------------------------------------------------------------------------------------------------------- |
|
155 |
|
156 string& TrimRight(string& s) |
|
157 { |
|
158 int pos(s.size()); |
|
159 for (; pos && (s[pos-1]==' ' || s[pos-1]=='\t' || s[pos-1]=='\r'); --pos); |
|
160 s.erase(pos, s.size()-pos); |
|
161 return s; |
|
162 } |
|
163 |
|
164 // ---------------------------------------------------------------------------------------------------------- |
|
165 |
|
166 string& TrimLeft(string& s) |
|
167 { |
|
168 int pos(0); |
|
169 for (; s[pos]==' ' || s[pos]=='\t' || s[pos]=='\r'; ++pos); |
|
170 s.erase(0, pos); |
|
171 return s; |
|
172 } |
|
173 |
|
174 // ---------------------------------------------------------------------------------------------------------- |
|
175 |
|
176 string& TrimAll(string& s) |
|
177 { |
|
178 return TrimLeft(TrimRight(s)); |
|
179 } |
|
180 |
|
181 // ---------------------------------------------------------------------------------------------------------- |
|
182 |
|
183 int StringICmp(const string& s1, const string& s2) |
|
184 { |
|
185 string ss1 = LowerCase(s1); |
|
186 string ss2 = LowerCase(s2); |
|
187 |
|
188 return ss1.compare(ss2); |
|
189 } |
|
190 |
|
191 // ---------------------------------------------------------------------------------------------------------- |
|
192 |
|
193 int StringICmpFileNamesWithoutExtension(const string& s1, const string& s2) |
|
194 { |
|
195 // remove extension and then compare |
|
196 string ss1; |
|
197 string ss2; |
|
198 |
|
199 string::size_type dot_pos1 = s1.find_last_of('.'); |
|
200 if (dot_pos1 == string::npos) |
|
201 ss1 = s1; |
|
202 else |
|
203 ss1 = s1.substr(0, dot_pos1); |
|
204 |
|
205 string::size_type dot_pos2 = s2.find_last_of('.'); |
|
206 if (dot_pos2 == string::npos) |
|
207 ss2 = s2; |
|
208 else |
|
209 ss2 = s2.substr(0, dot_pos2); |
|
210 |
|
211 //cerr << endl << ss1 << endl << ss2 << endl; |
|
212 |
|
213 return StringICmp(ss1, ss2); |
|
214 } |
|
215 |
|
216 // ---------------------------------------------------------------------------------------------------------- |
|
217 |
|
218 string Int2Str(int value) |
|
219 { |
|
220 ostringstream os; |
|
221 if (os << value) |
|
222 return os.str(); |
|
223 else |
|
224 return ""; |
|
225 } |
|
226 |
|
227 // ---------------------------------------------------------------------------------------------------------- |
|
228 |
|
229 int Str2Int(const string& s) |
|
230 { |
|
231 int res(0); |
|
232 |
|
233 // return 0 for empty string |
|
234 if (s.empty()) |
|
235 { |
|
236 } |
|
237 |
|
238 // hex conversion if the string begings with 0x... |
|
239 else if (s.length() >= 3 && s.at(0) == '0' && s.at(1) == 'x') |
|
240 { |
|
241 //sscanf(s.c_str(), "%x", &res); |
|
242 istringstream is(s); |
|
243 is >> hex >> res; |
|
244 if(!is || !is.eof()) |
|
245 res = 0; |
|
246 } |
|
247 |
|
248 // normal integer |
|
249 else |
|
250 { |
|
251 //sscanf(s.c_str(), "%d", &res); |
|
252 istringstream is(s); |
|
253 is >> res; |
|
254 if(!is || !is.eof()) |
|
255 res = 0; |
|
256 } |
|
257 |
|
258 return res; |
|
259 } |
|
260 |
|
261 // ---------------------------------------------------------------------------------------------------------- |
|
262 |
|
263 void InsertQuotesToFilePath(string& s) |
|
264 { |
|
265 // example C:\Program Files\do something.exe -> C:\"Program Files"\"do something.exe" |
|
266 |
|
267 bool firstBacklashFound = false; |
|
268 bool anyQuoteInserted = false; |
|
269 |
|
270 if (!s.empty()) |
|
271 { |
|
272 int s_length = s.length(); |
|
273 |
|
274 for (int i=0; i<s_length; i++) |
|
275 { |
|
276 string::size_type pos = s.find(DIR_SEPARATOR, i); |
|
277 |
|
278 if (pos != string::npos) |
|
279 { |
|
280 if (!firstBacklashFound) |
|
281 { |
|
282 // replace \ -> \" |
|
283 s.insert(pos+1, "\""); |
|
284 |
|
285 anyQuoteInserted = true; |
|
286 firstBacklashFound = true; |
|
287 s_length++; |
|
288 i = pos+1; |
|
289 } |
|
290 else |
|
291 { |
|
292 // replace \ -> "\" |
|
293 s.insert(pos, "\""); |
|
294 s.insert(pos+2, "\""); |
|
295 |
|
296 anyQuoteInserted = true; |
|
297 s_length += 2; |
|
298 i = pos+2; |
|
299 } |
|
300 } |
|
301 |
|
302 if (i>255) |
|
303 return; // something went wrong.. |
|
304 } |
|
305 |
|
306 // append extra quote to the end if needed |
|
307 if (anyQuoteInserted) |
|
308 s.insert(s.length(), "\""); |
|
309 } |
|
310 } |
|
311 |
|
312 // ---------------------------------------------------------------------------------------------------------- |
|
313 vector<string> splitString(const string& str, char separator) |
|
314 { |
|
315 vector<string> ret; |
|
316 string::size_type pos = str.find(separator); |
|
317 unsigned int lastpos = 0; |
|
318 while(pos != string::npos) |
|
319 { |
|
320 ret.push_back(str.substr(lastpos, pos - lastpos)); |
|
321 lastpos = (unsigned int)pos + 1; |
|
322 pos = str.find(separator, lastpos); |
|
323 } |
|
324 if (!str.empty()) |
|
325 { |
|
326 ret.push_back(str.substr(lastpos, pos)); |
|
327 } |
|
328 return ret; |
|
329 } |
|
330 // ---------------------------------------------------------------------------------------------------------- |
|
331 const string getFilename( const string& path) |
|
332 { |
|
333 string::size_type pos = path.find_last_of(DIR_SEPARATOR); |
|
334 unsigned int newpos = pos+1; |
|
335 if(pos != string::npos) |
|
336 { |
|
337 return path.substr( newpos, path.size()-newpos); |
|
338 } |
|
339 return path; |
|
340 } |
|
341 // ---------------------------------------------------------------------------------------------------------- |
|
342 const string getPlatform( const string& path) |
|
343 { |
|
344 string platform(path); |
|
345 string pattern; |
|
346 string::size_type pos1; |
|
347 string::size_type pos2; |
|
348 pattern.append("epoc32").append(DIR_SEPARATOR).append("release").append(DIR_SEPARATOR); |
|
349 |
|
350 if ((pos1 = platform.find(pattern)) != string::npos) |
|
351 { |
|
352 pos2=platform.find(DIR_SEPARATOR,pos1+pattern.length()); |
|
353 if(pos2!=string::npos) |
|
354 platform = platform.substr(pos1+pattern.length(),pos2-(pos1+pattern.length())); |
|
355 else |
|
356 platform=""; |
|
357 } |
|
358 else |
|
359 platform = ""; |
|
360 |
|
361 return platform; |
|
362 } |
|
363 // ---------------------------------------------------------------------------------------------------------- |
|
364 void getSeverityString(TypeOfSeverity severity, string& bc_severity, string& sc_severity) |
|
365 { |
|
366 switch (severity) |
|
367 { |
|
368 case CONFIRMED_BC_BREAK: |
|
369 { |
|
370 bc_severity = "BBC Break"; |
|
371 sc_severity = "None"; |
|
372 } |
|
373 break; |
|
374 case POSSIBLE_BC_BREAK: |
|
375 bc_severity = "Possible BBC Break"; |
|
376 sc_severity = "None"; |
|
377 break; |
|
378 case CONFIRMED_SC_BREAK: |
|
379 bc_severity = "None"; |
|
380 sc_severity = "SC Break"; |
|
381 break; |
|
382 case POSSIBLE_SC_BREAK: |
|
383 bc_severity = "None"; |
|
384 sc_severity = "Possible SC Break"; |
|
385 break; |
|
386 case CONFIRMED_BC_AND_SC_BREAK: |
|
387 { |
|
388 bc_severity = "BBC Break"; |
|
389 sc_severity = "SC Break"; |
|
390 } |
|
391 break; |
|
392 case POSSIBLE_BC_SC_BREAK: |
|
393 { |
|
394 bc_severity = "Possible BBC Break"; |
|
395 sc_severity = "Possible SC Break"; |
|
396 } |
|
397 break; |
|
398 case CONFIRMED_BC_POSSIBLE_SC_BREAK: |
|
399 { |
|
400 bc_severity = "BBC Break"; |
|
401 sc_severity = "Possible SC Break"; |
|
402 } |
|
403 break; |
|
404 case POSSIBLE_BC_CONFIRMED_SC_BREAK: |
|
405 { |
|
406 bc_severity = "Possible BBC Break"; |
|
407 sc_severity = "SC Break"; |
|
408 } |
|
409 break; |
|
410 case BC_INFORMATIVE: |
|
411 { |
|
412 bc_severity = "Informative"; |
|
413 sc_severity = "None"; |
|
414 } |
|
415 break; |
|
416 case SC_INFORMATIVE: |
|
417 { |
|
418 bc_severity = "None"; |
|
419 sc_severity = "Informative"; |
|
420 } |
|
421 break; |
|
422 case BC_SC_INFORMATIVE: |
|
423 { |
|
424 bc_severity = "Informative"; |
|
425 sc_severity = "Informative"; |
|
426 } |
|
427 break; |
|
428 default: |
|
429 { |
|
430 bc_severity = "No Break"; |
|
431 sc_severity = "No Break"; |
|
432 } |
|
433 break; |
|
434 } |
|
435 } |
|
436 |
|
437 // ---------------------------------------------------------------------------------------------------------- |
|
438 string CharToHex(char* buf) |
|
439 { |
|
440 string ret; |
|
441 static char finalhash[4]; |
|
442 char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
|
443 int k=0; |
|
444 |
|
445 for(int j = 3; j >=0; j--) |
|
446 { |
|
447 finalhash[k*2] = hex[((buf[j] >> 4) & 0xF)]; |
|
448 finalhash[(k*2) + 1] = hex[((buf[j]) & 0xF)]; |
|
449 k++; |
|
450 } |
|
451 |
|
452 ret=finalhash; |
|
453 return ret; |
|
454 } |
|
455 |
|
456 // ---------------------------------------------------------------------------------------------------------- |
|
457 string HexValueAt(fstream& f, const int offset) |
|
458 { |
|
459 string ret; |
|
460 char* buf; |
|
461 buf = (char*)malloc(DLL_ENTRY_SIZE); |
|
462 f.seekg(offset); |
|
463 memset (buf , 0 , DLL_ENTRY_SIZE); |
|
464 f.read(buf, DLL_ENTRY_SIZE); |
|
465 ret=CharToHex(buf); |
|
466 |
|
467 // free buf memory |
|
468 if(buf != NULL) |
|
469 { |
|
470 free((void*)buf); |
|
471 buf = NULL; |
|
472 } |
|
473 return ret; |
|
474 } |
|
475 // ---------------------------------------------------------------------------------------------------------- |
|
476 void intializeDllIssue(dll_issue& aIssue) |
|
477 { |
|
478 aIssue.l_dllname = ""; |
|
479 aIssue.i_list_typeid.clear(); |
|
480 } |
|
481 // ---------------------------------------------------------------------------------------------------------- |
|
482 void fillDllIssue ( char* dllMember , unsigned int& typeId) |
|
483 { |
|
484 if (strcmp (dllMember,"uid1") == 0 ) |
|
485 typeId = DLL_TARGET_TYPE_CHANGED; |
|
486 if (strcmp (dllMember,"uid2") == 0 ) |
|
487 typeId = DLL_UID2_CHANGED; |
|
488 if (strcmp (dllMember,"uid3") == 0 ) |
|
489 typeId = DLL_UID3_CHANGED; |
|
490 if (strcmp (dllMember,"sid") == 0 ) |
|
491 typeId = DLL_SID_CHANGED; |
|
492 if (strcmp (dllMember,"capability") == 0 ) |
|
493 typeId = DLL_CAPABILITY_CHANGED; |
|
494 } |
|
495 // ---------------------------------------------------------------------------------------------------------- |
|
496 int parseHex(const char* hex) |
|
497 { |
|
498 int res; |
|
499 int val; |
|
500 const char* ptr; |
|
501 |
|
502 res = 0; |
|
503 ptr = hex; |
|
504 |
|
505 while((*ptr)=='0') ptr++; |
|
506 |
|
507 while(*ptr) |
|
508 { |
|
509 if(!((*ptr<'0')||(*ptr>'9'))) |
|
510 { |
|
511 val = *ptr - '0'; |
|
512 } |
|
513 else if(!((*ptr<'a')||(*ptr>'f'))) |
|
514 { |
|
515 val = *ptr - 'a' + 10; |
|
516 } |
|
517 else if(!((*ptr<'A')||(*ptr>'F'))) |
|
518 { |
|
519 val = *ptr - 'A' + 10; |
|
520 } |
|
521 else |
|
522 { |
|
523 return -1; |
|
524 } |
|
525 |
|
526 res = res*16+val; |
|
527 |
|
528 ptr++; |
|
529 } |
|
530 |
|
531 return res; |
|
532 } |
|
533 string GetDllBreakTypeInfoString (int typeId) |
|
534 { |
|
535 string typeinfo = ""; |
|
536 switch(typeId) |
|
537 { |
|
538 case DLL_TARGET_TYPE_CHANGED: |
|
539 typeinfo = "Target type/UID1 has been changed"; |
|
540 break; |
|
541 case DLL_UID2_CHANGED: |
|
542 typeinfo = "UID2 has been changed"; |
|
543 break; |
|
544 case DLL_UID3_CHANGED: |
|
545 typeinfo = "UID3 has been changed"; |
|
546 break; |
|
547 case DLL_SID_CHANGED: |
|
548 typeinfo = "Secure ID has been changed"; |
|
549 break; |
|
550 case DLL_CAPABILITY_CHANGED: |
|
551 typeinfo = "Capability has been changed"; |
|
552 break; |
|
553 case DLL_CURRENT_MISSING: |
|
554 typeinfo = "DLL is missing in current SDK"; |
|
555 break; |
|
556 case DLL_BASELINE_MISSING: |
|
557 typeinfo = "Baseline DLL is not available for analysis"; |
|
558 break; |
|
559 default: |
|
560 break; |
|
561 } |
|
562 return typeinfo; |
|
563 } |