|
1 /* |
|
2 * Copyright (c) 2008-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 the License "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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 /** |
|
20 @file |
|
21 @internalComponent |
|
22 @released |
|
23 */ |
|
24 |
|
25 #include "utility_interface.h" |
|
26 |
|
27 #include <stdio.h> |
|
28 #include "utils.h" |
|
29 #include <sys/types.h> |
|
30 #include <sys/stat.h> |
|
31 #include <unistd.h> |
|
32 #include <fstream> |
|
33 #include <stdint.h> |
|
34 #include <fcntl.h> |
|
35 #include <limits.h> |
|
36 #include <unistd.h> |
|
37 #include <setjmp.h> |
|
38 #include <signal.h> |
|
39 #include <locale.h> |
|
40 #include <algorithm> |
|
41 #include <memory> |
|
42 |
|
43 |
|
44 // globals |
|
45 unsigned char g_ucCalledByTestRoutine = 0; |
|
46 jmp_buf g_pBuf; |
|
47 |
|
48 void MemTestHandler ( int nSig) |
|
49 { |
|
50 if ( g_ucCalledByTestRoutine ) longjmp ( g_pBuf, 1); |
|
51 } |
|
52 |
|
53 void* GetStdHandle(unsigned long) |
|
54 { |
|
55 return; |
|
56 } |
|
57 |
|
58 wchar_t** CommandLineArgs(int &argc , char *argv[]) |
|
59 { |
|
60 /* |
|
61 This is an entry point of program.It is observed that program doesn't take |
|
62 LC_ALL and other TYPES from environment so setlocate is the safest way. This |
|
63 setting makes it work for japanese characters also. |
|
64 */ |
|
65 setlocale(LC_ALL,"en_US.utf8"); |
|
66 wchar_t **argv1 = new wchar_t*[argc]; |
|
67 int size=0 ; |
|
68 |
|
69 // Convert the char* parameter to wchar_t* parameter. |
|
70 for(int i=0 ; i<argc ; i++) |
|
71 { |
|
72 size = ConvertMultiByteToWideChar(argv[i] , strlen(argv[i])+1 , NULL , 0); |
|
73 argv1[i] = new wchar_t [size+1]; |
|
74 ConvertMultiByteToWideChar(argv[i], strlen(argv[i])+1, argv1[i],size+1); |
|
75 } |
|
76 return argv1; |
|
77 } |
|
78 |
|
79 void cleanup(int argc, wchar_t **argv) |
|
80 { |
|
81 for(int i = argc-1 ; i>=0 ; i--) |
|
82 { |
|
83 delete argv[i]; |
|
84 } |
|
85 |
|
86 delete argv; |
|
87 } |
|
88 |
|
89 /* |
|
90 Windows uses 0xFEFF for unicode marker when it tries to create a file with WriteFile() |
|
91 Eventhough file is in utf-8 format.As Linux is having utf-8 encoding scheme by default |
|
92 it tries to write 0xEF,0xBB, 0xBF in the begining of file, if the file is utf-8. so to |
|
93 make it compatible for the tool we insert 0xFF,0xFE,0x00 in the begining of file. |
|
94 */ |
|
95 |
|
96 bool UnicodeMarker(void *hfile) |
|
97 { |
|
98 unsigned long dwNumBytes; |
|
99 ::SetFilePointer(hfile, 0L, NULL, FILE_BEGIN); |
|
100 const BYTE pBuf[] = { 0xFF,0xFE,0x00 }; |
|
101 return WriteFile(hfile, (LPVOID)pBuf, sizeof(pBuf), &dwNumBytes, NULL); |
|
102 } |
|
103 |
|
104 int CompareTwoString(wchar_t* string ,wchar_t* option) |
|
105 { |
|
106 return wcscasecmp(string,option); |
|
107 } |
|
108 |
|
109 int CompareNString(wchar_t* string ,wchar_t* option , int len) |
|
110 { |
|
111 return wcsncasecmp(string,option,len); |
|
112 } |
|
113 |
|
114 TUint64 GetSizeOfFile(void* hFile) |
|
115 { |
|
116 DWORD dwHigh = 0; |
|
117 DWORD dwLow = GetFileSize(hFile, &dwHigh); |
|
118 TInt64 size = (dwHigh << 32) | dwLow; |
|
119 CloseHandle(hFile); |
|
120 return size; |
|
121 } |
|
122 |
|
123 /* |
|
124 |
|
125 The below function works in windows because windows largely uses |
|
126 segments to manage memory These functions are not really useful in |
|
127 linux because linux relies on paging to manage memory a running linux |
|
128 system only defines four segment, which are shared by all running |
|
129 processes (a code and data segment for kernel-space, and a code and |
|
130 data segment for userland) and just swaps pages of memory in and out |
|
131 of those segments as needed. So testing the segment limits under Linux |
|
132 tells you nothing. |
|
133 |
|
134 ** IsBadReadPtr and IsBadWritePtr is deprecated in windows too ** |
|
135 |
|
136 */ |
|
137 |
|
138 bool IsBadReadPtr(const void* pv, unsigned long ulSize) |
|
139 { |
|
140 /* char* pc; |
|
141 void(*pPrev) ( int sig); |
|
142 g_ucCalledByTestRoutine = 1; |
|
143 if(setjmp ( g_pBuf)) |
|
144 { |
|
145 return true; |
|
146 } |
|
147 pPrev = signal (SIGSEGV, MemTestHandler); |
|
148 pc = (char*) malloc ( ulSize); |
|
149 memcpy ( pc,const_cast<void*>(pv), ulSize); |
|
150 free(pc); |
|
151 g_ucCalledByTestRoutine = 0; |
|
152 signal ( SIGSEGV, pPrev);*/ |
|
153 return false; |
|
154 } |
|
155 |
|
156 bool IsBadWritePtr(const void* pv, unsigned long ulSize) |
|
157 { |
|
158 /* char* pc; |
|
159 void(*pPrev) ( int sig); |
|
160 g_ucCalledByTestRoutine = 1; |
|
161 if(setjmp ( g_pBuf)) |
|
162 { |
|
163 return true; |
|
164 } |
|
165 pPrev = signal (SIGSEGV, MemTestHandler); |
|
166 pc = (char*) malloc ( ulSize); |
|
167 strcpy(pc,"Security Developement"); |
|
168 memcpy (const_cast<void*>(pv),pc, ulSize); |
|
169 free(pc); |
|
170 g_ucCalledByTestRoutine = 0; |
|
171 signal ( SIGSEGV, pPrev); */ |
|
172 return false; |
|
173 } |
|
174 |
|
175 int _wunlink(const wchar_t* wc) |
|
176 { |
|
177 int ret = 0; |
|
178 int len = wcstombs(0,wc,-1); |
|
179 char *tmp = new char[len]; |
|
180 ret = wcstombs(tmp, wc, len); |
|
181 if(ret == -1) { |
|
182 printf("wunlink: wcstombs error\n"); |
|
183 delete[] tmp; |
|
184 return ret; |
|
185 } |
|
186 tmp[ret] = '\0'; |
|
187 ret = unlink(tmp); |
|
188 if(ret != 0) |
|
189 printf("wunlink: %s: %s\n", tmp, strerror(ret)); |
|
190 return ret; |
|
191 } |
|
192 |
|
193 int GetTempPathW(unsigned int maxlen, const wchar_t *ptr) |
|
194 { |
|
195 wcsncpy(const_cast<wchar_t*>(ptr), L"/tmp/", maxlen); |
|
196 return 0; |
|
197 } |
|
198 |
|
199 |
|
200 unsigned long SetFilePointer(void *hFile, |
|
201 long distance, |
|
202 long *highword, |
|
203 unsigned long method) |
|
204 { |
|
205 (void)highword; |
|
206 |
|
207 int fd = (int)hFile; |
|
208 long off = 0; |
|
209 |
|
210 if(method == 0) |
|
211 off = lseek(fd, (long)distance, 0); |
|
212 else if(method == 1) |
|
213 off = lseek(fd, (long)distance, 1); |
|
214 else if(method == 2) |
|
215 off = lseek(fd, (long)distance, 2); |
|
216 else { |
|
217 printf(" NOT IMPLEMENTED \n"); |
|
218 } |
|
219 |
|
220 return off; |
|
221 } |
|
222 |
|
223 int CloseHandle(void *fd) |
|
224 { |
|
225 return close((int)fd) == 0; |
|
226 } |
|
227 |
|
228 |
|
229 long int GetFileSize(void *h, unsigned long*) |
|
230 { |
|
231 int fd = (int)h; |
|
232 int ret = 0; |
|
233 |
|
234 struct stat s; |
|
235 |
|
236 ret = fstat(fd, &s); |
|
237 |
|
238 if(ret != 0) { |
|
239 printf("GetFileSize: %s\n", strerror(ret)); |
|
240 return 0; |
|
241 } |
|
242 return s.st_size; |
|
243 } |
|
244 |
|
245 int RemoveDirectoryA(const char *a) |
|
246 { |
|
247 const int ret = rmdir(a); |
|
248 return(ret == 0); |
|
249 } |
|
250 |
|
251 |
|
252 int CreateDirectoryA(const char *pathname, void*) |
|
253 { |
|
254 const int ret = mkdir(pathname, 0664); |
|
255 if(ret == -1) { |
|
256 return 0; |
|
257 } |
|
258 return 1; |
|
259 } |
|
260 |
|
261 |
|
262 int WriteFile(void *hFile,const void *buffer,unsigned long bytesToWrite,unsigned long *bytesWritten,void*) |
|
263 { |
|
264 size_t ret = write((int)hFile, buffer, bytesToWrite); |
|
265 if(ret == -1) { |
|
266 printf("WriteFile: %s", strerror(ret)); |
|
267 return 0; |
|
268 } |
|
269 if(bytesWritten) |
|
270 *bytesWritten = ret; |
|
271 |
|
272 return 1; |
|
273 } |
|
274 |
|
275 |
|
276 |
|
277 void* CreateFileA(const char *filename, unsigned long access, unsigned long sharing,void*, unsigned long creation,\ |
|
278 unsigned long attributes, void*) |
|
279 { |
|
280 //(void)attributes; |
|
281 int fd = -1; |
|
282 if(creation == OPEN_EXISTING) |
|
283 { |
|
284 if(access == (GENERIC_WRITE|GENERIC_READ)) |
|
285 fd = open(filename, O_RDWR); // APB: was flags = 0666 |
|
286 else if(access == GENERIC_WRITE) |
|
287 fd = open(filename, O_WRONLY); // APB: was flags = 0222 |
|
288 else if(access == GENERIC_READ) |
|
289 fd = open(filename,O_RDONLY); // APB: was flags = 0444 |
|
290 } |
|
291 else if(creation == CREATE_NEW) |
|
292 { |
|
293 fd = creat(filename, 0664); |
|
294 } |
|
295 else if(creation == CREATE_ALWAYS) |
|
296 { |
|
297 fd = creat(filename, 0664); |
|
298 } |
|
299 else |
|
300 { |
|
301 printf("CreateFile: unknown creation flag %lu\n", creation); |
|
302 } |
|
303 |
|
304 // check the handle |
|
305 if(fd < 0) |
|
306 { |
|
307 return INVALID_HANDLE_VALUE; |
|
308 } |
|
309 else |
|
310 { |
|
311 return (void *)fd; |
|
312 } |
|
313 } |
|
314 |
|
315 |
|
316 int ReadFile(void *hFile, void *buffer, unsigned long bytesToRead,unsigned long *bytesRead, void *) |
|
317 { |
|
318 const int size = read((int)hFile, buffer, bytesToRead); |
|
319 if(size < 0) |
|
320 return 0; |
|
321 |
|
322 if(bytesRead) |
|
323 { |
|
324 *bytesRead = size; |
|
325 } |
|
326 return 1; |
|
327 } |
|
328 |
|
329 int GetTempFileNameW(const wchar_t* tmpPath, const wchar_t* prefix, int unique, const wchar_t *filename ) |
|
330 { |
|
331 wcsncpy(const_cast<wchar_t*>(filename), L"tmpXXXXXX", wcslen(L"tmpXXXXXX")); |
|
332 int ret = 0; |
|
333 char *tmp1 = new char[PATH_MAX]; |
|
334 wcscat(const_cast<wchar_t*>(tmpPath),filename); |
|
335 ret = wcstombs(tmp1, tmpPath, PATH_MAX); |
|
336 if(ret == -1) |
|
337 { |
|
338 printf("GetTempFileName: wcstombs error\n"); |
|
339 delete[] tmp1; |
|
340 return ret; |
|
341 } |
|
342 mkstemp(tmp1); |
|
343 |
|
344 wchar_t *pwc=(wchar_t *)malloc((PATH_MAX)*sizeof(wchar_t)); |
|
345 |
|
346 ret = mbstowcs(const_cast<wchar_t*>(tmpPath), tmp1,PATH_MAX); |
|
347 |
|
348 if(ret == -1) |
|
349 { |
|
350 printf("GetTempFileName: mbstowcs error\n"); |
|
351 delete[] tmp1; |
|
352 return ret; |
|
353 } |
|
354 |
|
355 wcscpy(const_cast<wchar_t*>(filename), tmpPath); |
|
356 |
|
357 delete[] tmp1; |
|
358 free (pwc); |
|
359 } |
|
360 |
|
361 |
|
362 void WriteConsole(void* handle, const void * buffer, unsigned long len, unsigned long *nosOfByteWritten, void* reserved) |
|
363 { |
|
364 wchar_t *buf= (wchar_t *)buffer; |
|
365 for (int i = 0;i <len;i++) |
|
366 std::wcout<<buf[i]; |
|
367 } |
|
368 |
|
369 |
|
370 void WriteConsoleW(void* handle, const void * buffer, unsigned long len, unsigned long *nosOfByteWritten, void* reserved) |
|
371 { |
|
372 wchar_t *buf= (wchar_t *)buffer; |
|
373 for (int i = 0;i <len;i++) |
|
374 std::wcout<<buf[i]; |
|
375 } |
|
376 |
|
377 |
|
378 int _wchmod(const wchar_t *filename, unsigned long mode ) |
|
379 { |
|
380 int ret = 0; |
|
381 int len = wcstombs(0,filename,-1); |
|
382 char *tmp = new char[len]; |
|
383 ret = wcstombs(tmp, filename, len); |
|
384 if(ret == -1) |
|
385 { |
|
386 printf("wchmod: wcstombs error\n"); |
|
387 delete[] tmp; |
|
388 return ret; |
|
389 } |
|
390 if(mode == _S_IWRITE || mode == _S_IWRITE | _S_IREAD) |
|
391 chmod(tmp,S_IWUSR); |
|
392 else |
|
393 if(mode == _S_IREAD) |
|
394 chmod(tmp,S_IRUSR); |
|
395 |
|
396 ret = mbstowcs(const_cast<wchar_t*>(filename),tmp,sizeof(tmp)); |
|
397 |
|
398 if(ret == -1) |
|
399 { |
|
400 printf("wchmod: mbstowcs error\n"); |
|
401 delete[] tmp; |
|
402 return ret; |
|
403 } |
|
404 |
|
405 delete []tmp; |
|
406 } |
|
407 |
|
408 |
|
409 int _wgetcwd(const wchar_t *directory , unsigned long length) |
|
410 { |
|
411 int ret = 0; |
|
412 int len = wcstombs(0,directory,-1); |
|
413 char *tmp = new char[len]; |
|
414 ret = wcstombs(tmp, directory, len); |
|
415 if(ret == -1) { |
|
416 printf("wgetwd: wcstombs error\n"); |
|
417 delete[] tmp; |
|
418 return ret; |
|
419 } |
|
420 getcwd(tmp,length); |
|
421 len = mbstowcs(0,tmp,-1); |
|
422 ret = mbstowcs(directory, tmp, len); |
|
423 if(ret == -1) { |
|
424 printf("wgetwd: wcstombs error\n"); |
|
425 delete[] tmp; |
|
426 return ret; |
|
427 } |
|
428 delete[] tmp; |
|
429 } |
|
430 |
|
431 char* itoa( int value, char* result, int base ) |
|
432 { |
|
433 if (base < 2 || base > 16) { *result = 0; return result; } |
|
434 char* out = result; |
|
435 int quotient = value; |
|
436 do { |
|
437 |
|
438 *out = "0123456789abcdef"[ std::abs( quotient % base ) ]; |
|
439 ++out; |
|
440 quotient /= base; |
|
441 } while ( quotient ); |
|
442 if ( value < 0 && base == 10) *out++ = '-'; |
|
443 std::reverse( result, out ); |
|
444 *out = 0; |
|
445 return result; |
|
446 } |
|
447 |
|
448 unsigned long GetFileAttributesW(const wchar_t *filename) |
|
449 { |
|
450 int ret = 0; |
|
451 int len = wcstombs(0,filename,-1); |
|
452 char *tmp = new char[len]; |
|
453 ret = wcstombs(tmp, filename, len); |
|
454 if(ret == -1) { |
|
455 printf("GetFileAttributesW: wcstombs error\n"); |
|
456 delete[] tmp; |
|
457 return ret; |
|
458 } |
|
459 |
|
460 struct stat s; |
|
461 ret = fstat(tmp, &s); |
|
462 return ret; |
|
463 } |
|
464 |
|
465 char* GetTempFile() |
|
466 { |
|
467 char *templateFile = new char[PATHMAX]; |
|
468 strcpy(templateFile,"tmpmakesisXXXXXX"); |
|
469 int fd = mkstemp(templateFile); |
|
470 return templateFile; |
|
471 } |
|
472 |
|
473 void TransferFileData(const wchar_t *Fromfile , char* Tofile) |
|
474 { |
|
475 BYTE buffer[512]; |
|
476 FILE *fp = NULL, *tp = NULL; |
|
477 int numread=0; |
|
478 int ret = 0; |
|
479 int len = wcstombs(0,Fromfile,-1); |
|
480 char *tmp = new char[len]; |
|
481 ret = wcstombs(tmp, Fromfile, len); |
|
482 if(ret == -1) { |
|
483 printf("wchmod: wcstombs error\n"); |
|
484 delete[] tmp; |
|
485 return ret; |
|
486 } |
|
487 if((fp=fopen(tmp, "rb")) != NULL) |
|
488 { |
|
489 if((tp = fopen(Tofile, "wb")) != NULL) |
|
490 { |
|
491 while(!feof(fp)) |
|
492 { |
|
493 if((numread = fread(buffer, sizeof(unsigned char), 255, fp))>0) |
|
494 { |
|
495 fwrite(buffer, sizeof(unsigned char), numread, tp); |
|
496 } |
|
497 } |
|
498 fclose(fp); |
|
499 fclose(tp); |
|
500 delete[] tmp; |
|
501 } |
|
502 } |
|
503 else |
|
504 { |
|
505 delete[] tmp; |
|
506 } |
|
507 } |
|
508 |
|
509 void* MakeSISOpenFile(const wchar_t *pszFilename, unsigned long dwAccessMode, unsigned long dwCreateFlags) |
|
510 // Open file with Unicode filename correctly under Win95 and WinNT |
|
511 { |
|
512 char pszMultiByte[PATHMAX] = "\0"; |
|
513 wchar_t *p=pszFilename; |
|
514 |
|
515 if (!wcsncmp(pszFilename,L"./",2)) p+=2; |
|
516 ConvertWideCharToMultiByte( |
|
517 p, // address of wide-character string |
|
518 -1, // number of characters in string |
|
519 pszMultiByte, // address of buffer for new string |
|
520 PATHMAX); // size of buffer |
|
521 |
|
522 return CreateFileA(pszMultiByte, dwAccessMode, 0, NULL, dwCreateFlags, FILE_ATTRIBUTE_NORMAL, NULL); |
|
523 } |
|
524 |
|
525 int MakeSISDeleteFile(const wchar_t *pszFilename) |
|
526 { |
|
527 char pszMultiByte[PATHMAX] = "\0"; |
|
528 wchar_t* p=(wchar_t*)pszFilename; |
|
529 |
|
530 if (!wcsncmp(pszFilename,L"./",2)) p+=2; |
|
531 ConvertWideCharToMultiByte( |
|
532 p, // address of wide-character string |
|
533 -1, // number of characters in string |
|
534 pszMultiByte, // address of buffer for new string |
|
535 PATHMAX); // size of buffer |
|
536 return unlink(pszMultiByte); |
|
537 } |
|
538 |
|
539 int DeleteFileA(const char* filename) |
|
540 { |
|
541 return unlink(filename); |
|
542 } |
|
543 |
|
544 int FullPath(wchar_t *pszAbsolutePath, const wchar_t *pszRelativePath, unsigned int maxLength) |
|
545 { |
|
546 // Not implemented because this function is not used in the tool |
|
547 return 0; |
|
548 } |
|
549 |
|
550 bool CreateDir(const wchar_t* aPathName) |
|
551 { |
|
552 int size = wcslen(aPathName) * 2; |
|
553 char* fileName = new char[size]; |
|
554 int ret = wcstombs(fileName, aPathName, size); |
|
555 |
|
556 int dirMode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
|
557 |
|
558 int retVal = mkdir(fileName, dirMode); |
|
559 |
|
560 |
|
561 return (retVal == 0)? true: false; |
|
562 } |
|
563 |
|
564 int FileAttributes(const wchar_t* aPathName) |
|
565 { |
|
566 int size = wcslen(aPathName) * 2; |
|
567 char* fileName = new char[size]; |
|
568 int ret = wcstombs(fileName, aPathName, size); |
|
569 |
|
570 struct stat attrStruct; |
|
571 |
|
572 stat(fileName, &attrStruct); |
|
573 int attr = 0; |
|
574 |
|
575 if(S_ISDIR(attrStruct.st_mode)) |
|
576 { |
|
577 attr |= FILE_ATTRIBUTE_DIRECTORY; |
|
578 } |
|
579 if(attrStruct.st_mode & S_IROTH == 0) |
|
580 { |
|
581 attr |= FILE_ATTRIBUTE_READONLY; |
|
582 } |
|
583 |
|
584 return attr; |
|
585 } |
|
586 |
|
587 void WriteToFile(const std::wstring& aFileName, const TUint8* aFileData, int aFileLength) |
|
588 { |
|
589 int size = wcslen(aFileName.c_str()) * 2; |
|
590 std::auto_ptr<char> fileName(new char[size]); |
|
591 int ret = wcstombs(fileName.get(), aFileName.c_str(), size); |
|
592 std::ofstream stream(fileName.get(), std::ios::out); |
|
593 stream.write(aFileData, aFileLength); |
|
594 stream.close(); |
|
595 } |
|
596 |
|
597 int GetErrorValue() |
|
598 { |
|
599 return errno; |
|
600 } |
|
601 |
|
602 |
|
603 int ConvertWideCharToMultiByte(const wchar_t* aSource, int /*aSourceLen*/, char* aTarget, int aTargetLen, TUint32 /*aCodePage*/) |
|
604 { |
|
605 int retValue = wcstombs(aTarget, aSource, aTargetLen); |
|
606 if (-1 == retValue) |
|
607 { |
|
608 return 0; |
|
609 } |
|
610 return retValue; |
|
611 } |
|
612 |
|
613 int ConvertMultiByteToWideChar(const char* aSource, int /*aSourceLen*/, wchar_t* aTarget, int aTargetLen, TUint32 /*aCodePage*/) |
|
614 { |
|
615 int retValue = mbstowcs(aTarget, aSource, aTargetLen); |
|
616 if (-1 == retValue) |
|
617 { |
|
618 return 0; |
|
619 } |
|
620 return retValue; |
|
621 } |
|
622 |
|
623 |
|
624 |
|
625 |