author | hgs |
Mon, 18 Oct 2010 15:31:10 +0100 | |
changeset 291 | 206a6eaaeb71 |
parent 176 | af6ec97d9189 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). |
2 |
// All rights reserved. |
|
3 |
// This component and the accompanying materials are made available |
|
4 |
// under the terms of the License "Eclipse Public License v1.0" |
|
5 |
// which accompanies this distribution, and is available |
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// |
|
15 |
||
16 |
#define _CRTIMP // we want to use the static runtime library |
|
17 |
#include <e32cmn.h> |
|
18 |
#include <e32cmn_private.h> |
|
19 |
#include <string.h> |
|
20 |
#include <emulator.h> |
|
21 |
#include <e32ldr.h> |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
22 |
#include <e32ldr_private.h> |
0 | 23 |
#include <e32uid.h> |
24 |
||
25 |
#pragma data_seg(".data2") |
|
26 |
#ifdef __VC32__ |
|
27 |
#pragma bss_seg(".data2") |
|
28 |
#endif |
|
29 |
static TBool UnicodeHost; |
|
30 |
static Emulator::SInit Data; |
|
31 |
#pragma data_seg() |
|
32 |
#ifdef __VC32__ |
|
33 |
#pragma bss_seg() |
|
34 |
#endif |
|
35 |
||
36 |
/** |
|
37 |
Initializes a module and prepares it to handle requests. |
|
38 |
||
39 |
@param aInit An object of the structure SInit. |
|
40 |
||
41 |
@see SInit. |
|
42 |
*/ |
|
43 |
EXPORT_C void Emulator::Init(const Emulator::SInit& aInit) |
|
44 |
{ |
|
45 |
UnicodeHost = (GetVersion() & 0x80000000) ? FALSE : TRUE; |
|
46 |
Data = aInit; |
|
47 |
if (Data.iCodePage == 0) |
|
48 |
Data.iCodePage = CP_ACP; |
|
49 |
} |
|
50 |
||
51 |
LOCAL_C DWORD UStringLength(LPCSTR lpMultiByteStr) |
|
52 |
// |
|
53 |
// returns length of unicode string generated - assumes null terminated string |
|
54 |
// |
|
55 |
{ |
|
56 |
return MultiByteToWideChar(Data.iCodePage,0,lpMultiByteStr,-1,0,0); |
|
57 |
} |
|
58 |
||
59 |
||
60 |
LOCAL_C DWORD ConvertToUnicode(LPCSTR aNarrow,LPWSTR aUnicode,DWORD aLength,BOOL aCharsRequired) |
|
61 |
// |
|
62 |
// Converts narrow string to unicode string |
|
63 |
// |
|
64 |
{ |
|
65 |
DWORD uniLength=UStringLength(aNarrow); |
|
66 |
if(uniLength>aLength || uniLength==0) |
|
67 |
return aCharsRequired ? uniLength : 0; |
|
68 |
uniLength=MultiByteToWideChar(Data.iCodePage,0,aNarrow,-1,aUnicode,aLength); |
|
69 |
// return number of characters excluding the null terminator |
|
70 |
return uniLength ? uniLength-1 : 0; |
|
71 |
} |
|
72 |
||
73 |
||
74 |
LOCAL_C DWORD ConvertToNarrow(LPCWSTR aUnicode,LPSTR aNarrow,DWORD aLength) |
|
75 |
// |
|
76 |
// Converts unicode string to narrow string |
|
77 |
// |
|
78 |
{ |
|
79 |
return WideCharToMultiByte(Data.iCodePage,0,aUnicode,-1,aNarrow,aLength,"@",NULL); |
|
80 |
} |
|
81 |
||
176 | 82 |
LOCAL_C TInt GetSectionsSize(const IMAGE_NT_HEADERS32* aBase) |
83 |
// |
|
84 |
// Iterates through the section headers at the start of an image and determines the minimum amount of the |
|
85 |
// PE file that must be read in in order to examine the sections themselves |
|
86 |
// |
|
87 |
{ |
|
88 |
// List of sections are are interested in examining |
|
89 |
const BYTE* sectionNames[] = |
|
90 |
{ |
|
91 |
KWin32SectionName_Symbian, KWin32SectionName_Import, KWin32SectionName_EpocData, KWin32SectionName_EpocBss, |
|
92 |
KWin32SectionName_Text, KWin32SectionName_RData, KWin32SectionName_NmdExpData |
|
93 |
}; |
|
94 |
||
95 |
// Get a ptr to the first section header in preparation for examining all the section headers |
|
96 |
DWORD maxOffset = 0; |
|
97 |
const IMAGE_NT_HEADERS32* ntHead = aBase; |
|
98 |
const IMAGE_SECTION_HEADER* imgHead = (const IMAGE_SECTION_HEADER*) ((TUint8*) &ntHead->OptionalHeader + ntHead->FileHeader.SizeOfOptionalHeader); |
|
99 |
const IMAGE_SECTION_HEADER* end = imgHead + ntHead->FileHeader.NumberOfSections; |
|
100 |
||
101 |
for (; imgHead < end; ++imgHead) |
|
102 |
{ |
|
103 |
TBool SectionUsed = EFalse; |
|
104 |
||
105 |
// Go through each of the sections that we need to examine and see if the current section is in the list |
|
106 |
for (TInt index = 0; index < (sizeof(sectionNames) / sizeof(BYTE*)); ++index) |
|
107 |
{ |
|
108 |
if (memcmp(imgHead->Name, sectionNames[index], IMAGE_SIZEOF_SHORT_NAME)==0) |
|
109 |
SectionUsed = ETrue; |
|
110 |
} |
|
111 |
||
112 |
// If the current section is one we are interested in, calculate its offset in the raw file and add its size; |
|
113 |
// this gives us the minimum amount of the file to be mapped in order to examine this section and all those |
|
114 |
// preceding it |
|
115 |
if (SectionUsed) |
|
116 |
{ |
|
117 |
if ((imgHead->PointerToRawData + imgHead->SizeOfRawData) > maxOffset) |
|
118 |
{ |
|
119 |
maxOffset = (imgHead->PointerToRawData + imgHead->SizeOfRawData); |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
return(maxOffset); |
|
125 |
} |
|
126 |
||
0 | 127 |
template <TUint S> |
128 |
struct Buf8 |
|
129 |
{ |
|
130 |
public: |
|
131 |
Buf8(LPCWSTR aUnicode); |
|
132 |
inline operator LPCSTR() const |
|
133 |
{return iPtr;} |
|
134 |
private: |
|
135 |
const char* iPtr; |
|
136 |
char iBuf[S]; |
|
137 |
}; |
|
138 |
||
139 |
template <TUint S> |
|
140 |
Buf8<S>::Buf8(LPCWSTR aUnicode) |
|
141 |
{ |
|
142 |
if (aUnicode) |
|
143 |
{ |
|
144 |
iPtr = iBuf; |
|
145 |
ConvertToNarrow(aUnicode,iBuf,S); |
|
146 |
} |
|
147 |
else |
|
148 |
{ |
|
149 |
iPtr = NULL; |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
||
154 |
||
155 |
/** |
|
156 |
Acquires the global lock for host interaction. |
|
157 |
*/ |
|
158 |
EXPORT_C void Emulator::Lock() |
|
159 |
{ |
|
160 |
Data.iLock(); |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
Releases the global lock for host interaction. |
|
165 |
This may overwrite the error code for this thread and that is not the behaviour expected, |
|
166 |
so save and restore it. |
|
167 |
*/ |
|
168 |
EXPORT_C void Emulator::Unlock() |
|
169 |
{ |
|
170 |
DWORD error=GetLastError(); |
|
171 |
Data.iUnlock(); |
|
172 |
SetLastError(error); |
|
173 |
} |
|
174 |
||
175 |
/** |
|
176 |
Takes the current thread out of the emulator scheduling model. |
|
177 |
*/ |
|
178 |
EXPORT_C void Emulator::Escape() |
|
179 |
{ |
|
180 |
Data.iEscape(); |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
Returns the calling thread into the emulator scheduling model. |
|
185 |
This may overwrite the error code for this thread and that is not the behaviour expected, |
|
186 |
so save and restore it. |
|
187 |
*/ |
|
188 |
EXPORT_C void Emulator::Reenter() |
|
189 |
{ |
|
190 |
DWORD error=GetLastError(); |
|
191 |
Data.iReenter(); |
|
192 |
SetLastError(error); |
|
193 |
} |
|
194 |
||
195 |
||
196 |
/** |
|
197 |
Jumps to the NThread Win32 SEH exception handler. |
|
198 |
||
199 |
@param aException Describes an exception. |
|
200 |
@param aContext Describes the context. |
|
201 |
||
202 |
@return A handler to handle the exception occurred |
|
203 |
*/ |
|
204 |
EXPORT_C DWORD Emulator::Win32SEHException(EXCEPTION_RECORD* aException, CONTEXT* aContext) |
|
205 |
{ |
|
206 |
return Data.iException(aException, aContext); |
|
207 |
} |
|
208 |
||
209 |
||
210 |
/** |
|
211 |
This function wraps the Win32 CreateDirectory API (see http://msdn2.microsoft.com/en-us/library/aa363855.aspx). |
|
212 |
*/ |
|
213 |
EXPORT_C BOOL Emulator::CreateDirectory(LPCWSTR lpPathName,LPSECURITY_ATTRIBUTES lpSecurityAttributes) |
|
214 |
{ |
|
215 |
__LOCK_HOST; |
|
216 |
||
217 |
if (UnicodeHost) |
|
218 |
return ::CreateDirectoryW(lpPathName, lpSecurityAttributes); |
|
219 |
||
220 |
return ::CreateDirectoryA(Buf8<MAX_PATH>(lpPathName),lpSecurityAttributes); |
|
221 |
} |
|
222 |
||
223 |
#ifdef __VC32__ |
|
224 |
//disable unreachable code warning in VC++ |
|
225 |
#pragma warning (disable : 4702) |
|
226 |
#endif |
|
227 |
||
228 |
||
229 |
/** |
|
230 |
Recursively ensures that the full directory exists. |
|
231 |
||
232 |
@param aPathName Provides the path name. |
|
233 |
||
234 |
@return TRUE, if the function succeeds |
|
235 |
FALSE, if the function fails |
|
236 |
*/ |
|
237 |
EXPORT_C BOOL Emulator::CreateAllDirectories(LPCSTR aPathName) |
|
238 |
{ |
|
239 |
__LOCK_HOST; |
|
240 |
||
241 |
char path[MAX_PATH]; |
|
242 |
strcpy(path, aPathName); |
|
243 |
char* p = path; |
|
244 |
for (;;) |
|
245 |
{ |
|
246 |
p = strchr(p, '\\'); |
|
247 |
char temp=0; |
|
248 |
if (p) |
|
249 |
{ |
|
250 |
temp = *++p; |
|
251 |
*p = '\0'; |
|
252 |
} |
|
253 |
DWORD att = ::GetFileAttributesA(path); |
|
254 |
if ((att == -1 || !(att&FILE_ATTRIBUTE_DIRECTORY)) && !::CreateDirectoryA(path, NULL)) |
|
255 |
return EFalse; |
|
256 |
if (!p) |
|
257 |
return ETrue; |
|
258 |
*p = temp; |
|
259 |
} |
|
260 |
} |
|
261 |
#ifdef __VC32__ |
|
262 |
//enable unreachable code warning in VC++ |
|
263 |
#pragma warning (default : 4702) |
|
264 |
#endif |
|
265 |
||
266 |
#ifndef INVALID_FILE_ATTRIBUTES |
|
267 |
#define INVALID_FILE_ATTRIBUTES ((DWORD)-1) |
|
268 |
#endif |
|
269 |
||
270 |
||
271 |
/** |
|
272 |
This function wraps the Win32 CreateFile API (see http://msdn2.microsoft.com/en-us/library/aa363858.aspx). |
|
273 |
It also modifies file attributes depending on whether the file is hidden or not. |
|
274 |
*/ |
|
275 |
EXPORT_C HANDLE Emulator::CreateFile(LPCWSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode, |
|
276 |
LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDistribution, |
|
277 |
DWORD dwFlagsAndAttributes,HANDLE hTemplateFile) |
|
278 |
{ |
|
279 |
__LOCK_HOST; |
|
280 |
HANDLE h; |
|
281 |
BOOL hidden = 0; |
|
282 |
DWORD att = INVALID_FILE_ATTRIBUTES; |
|
283 |
if(dwCreationDistribution==CREATE_ALWAYS) |
|
284 |
{ |
|
285 |
att = ::GetFileAttributes(lpFileName); |
|
286 |
if(att!=INVALID_FILE_ATTRIBUTES && (att&FILE_ATTRIBUTE_HIDDEN)) |
|
287 |
{ |
|
288 |
hidden = ::SetFileAttributes(lpFileName, (att&~FILE_ATTRIBUTE_HIDDEN)); |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
if (UnicodeHost) |
|
293 |
h = ::CreateFileW(lpFileName,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDistribution, |
|
294 |
dwFlagsAndAttributes,hTemplateFile); |
|
295 |
else |
|
296 |
h = ::CreateFileA(Buf8<MAX_PATH>(lpFileName),dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDistribution, |
|
297 |
dwFlagsAndAttributes,hTemplateFile); |
|
298 |
||
299 |
if(hidden && h!=INVALID_HANDLE_VALUE) |
|
300 |
{ |
|
301 |
::SetFileAttributes(lpFileName, att); |
|
302 |
} |
|
303 |
return h; |
|
304 |
} |
|
305 |
||
306 |
||
307 |
/** |
|
308 |
This function wraps the Win32 DeleteFile API (see http://msdn2.microsoft.com/en-us/library/aa363915.aspx). |
|
309 |
*/ |
|
310 |
EXPORT_C BOOL Emulator::DeleteFile(LPCWSTR lpFileName) |
|
311 |
{ |
|
312 |
__LOCK_HOST; |
|
313 |
||
314 |
if (UnicodeHost) |
|
315 |
return ::DeleteFileW(lpFileName); |
|
316 |
||
317 |
return ::DeleteFileA(Buf8<MAX_PATH>(lpFileName)); |
|
318 |
} |
|
319 |
||
320 |
LOCAL_C TBool MapFindFileData(LPWIN32_FIND_DATAW lpFindFileData, LPWIN32_FIND_DATAA lpFindNarrow) |
|
321 |
{ |
|
322 |
lpFindFileData->dwFileAttributes=lpFindNarrow->dwFileAttributes; |
|
323 |
lpFindFileData->ftCreationTime=lpFindNarrow->ftCreationTime; |
|
324 |
lpFindFileData->ftLastAccessTime=lpFindNarrow->ftLastAccessTime; |
|
325 |
lpFindFileData->ftLastWriteTime=lpFindNarrow->ftLastWriteTime; |
|
326 |
lpFindFileData->nFileSizeHigh=lpFindNarrow->nFileSizeHigh; |
|
327 |
lpFindFileData->nFileSizeLow=lpFindNarrow->nFileSizeLow; |
|
328 |
||
329 |
if(!MultiByteToWideChar(Data.iCodePage,0,lpFindNarrow->cFileName,-1,lpFindFileData->cFileName,MAX_PATH)) |
|
330 |
return FALSE; |
|
331 |
||
332 |
if(lpFindNarrow->cAlternateFileName!=NULL) |
|
333 |
{ |
|
334 |
// magic number 14 comes from MS documentation |
|
335 |
if(!MultiByteToWideChar(Data.iCodePage,0,lpFindNarrow->cAlternateFileName,-1,lpFindFileData->cAlternateFileName,14)) |
|
336 |
return FALSE; |
|
337 |
} |
|
338 |
return TRUE; |
|
339 |
} |
|
340 |
||
341 |
||
342 |
/** |
|
343 |
This function wraps the Win32 FindFirstFile API (see http://msdn2.microsoft.com/en-us/library/aa364418.aspx). |
|
344 |
*/ |
|
345 |
EXPORT_C HANDLE Emulator::FindFirstFile(LPCWSTR lpFileName,LPWIN32_FIND_DATAW lpFindFileData) |
|
346 |
{ |
|
347 |
__LOCK_HOST; |
|
348 |
||
349 |
if (UnicodeHost) |
|
350 |
return ::FindFirstFileW(lpFileName, lpFindFileData); |
|
351 |
||
352 |
WIN32_FIND_DATAA lpFindNarrow; |
|
353 |
HANDLE h=::FindFirstFileA(Buf8<MAX_PATH>(lpFileName),&lpFindNarrow); |
|
354 |
if(h==INVALID_HANDLE_VALUE) |
|
355 |
return h; |
|
356 |
||
357 |
if (!MapFindFileData(lpFindFileData, &lpFindNarrow)) |
|
358 |
{ |
|
359 |
FindClose(h); |
|
360 |
return INVALID_HANDLE_VALUE; |
|
361 |
} |
|
362 |
||
363 |
return h; |
|
364 |
} |
|
365 |
||
366 |
||
367 |
/** |
|
368 |
This function wraps the Win32 FindNextFile API (see http://msdn2.microsoft.com/en-us/library/aa364428.aspx). |
|
369 |
*/ |
|
370 |
EXPORT_C BOOL Emulator::FindNextFile(HANDLE hFindFile,LPWIN32_FIND_DATAW lpFindFileData) |
|
371 |
{ |
|
372 |
__LOCK_HOST; |
|
373 |
||
374 |
if (UnicodeHost) |
|
375 |
return ::FindNextFileW(hFindFile, lpFindFileData); |
|
376 |
||
377 |
WIN32_FIND_DATAA lpFindNarrow; |
|
378 |
if(!::FindNextFileA(hFindFile,&lpFindNarrow)) |
|
379 |
return FALSE; |
|
380 |
||
381 |
return MapFindFileData(lpFindFileData, &lpFindNarrow); |
|
382 |
} |
|
383 |
||
384 |
/** |
|
385 |
This function wraps the Win32 GetDiskFreeSpace API (see http://msdn2.microsoft.com/en-us/library/aa364935.aspx). |
|
386 |
*/ |
|
387 |
EXPORT_C BOOL Emulator::GetDiskFreeSpace(LPCWSTR lpRootPathName,LPDWORD lpSectorsPerCluster,\ |
|
388 |
LPDWORD lpBytesPerSector,LPDWORD lpNumberOfFreeClusters,\ |
|
389 |
LPDWORD lpTotalNumberOfClusters) |
|
390 |
{ |
|
391 |
__LOCK_HOST; |
|
392 |
||
393 |
if (UnicodeHost) |
|
394 |
return ::GetDiskFreeSpaceW(lpRootPathName,lpSectorsPerCluster,lpBytesPerSector,lpNumberOfFreeClusters,lpTotalNumberOfClusters); |
|
395 |
||
396 |
return ::GetDiskFreeSpaceA(Buf8<MAX_PATH>(lpRootPathName),lpSectorsPerCluster,lpBytesPerSector,lpNumberOfFreeClusters,lpTotalNumberOfClusters); |
|
397 |
||
398 |
} |
|
399 |
||
400 |
||
401 |
/** |
|
402 |
This function wraps the Win32 GetFileAttributes API (see http://msdn2.microsoft.com/en-us/library/aa364944.aspx). |
|
403 |
*/ |
|
404 |
EXPORT_C DWORD Emulator::GetFileAttributes(LPCWSTR lpFileName) |
|
405 |
{ |
|
406 |
__LOCK_HOST; |
|
407 |
||
408 |
if (UnicodeHost) |
|
409 |
return ::GetFileAttributesW(lpFileName); |
|
410 |
||
411 |
return ::GetFileAttributesA(Buf8<MAX_PATH>(lpFileName)); |
|
412 |
} |
|
413 |
||
414 |
||
415 |
/** |
|
416 |
This function wraps the Win32 GetModuleHandle API (see http://msdn2.microsoft.com/en-us/library/ms683199.aspx). |
|
417 |
*/ |
|
418 |
EXPORT_C HMODULE Emulator::GetModuleHandle(LPCWSTR lpModuleName) |
|
419 |
{ |
|
420 |
__LOCK_HOST; |
|
421 |
||
422 |
if (UnicodeHost) |
|
423 |
return ::GetModuleHandleW(lpModuleName); |
|
424 |
||
425 |
return ::GetModuleHandleA(Buf8<MAX_PATH>(lpModuleName)); |
|
426 |
} |
|
427 |
||
428 |
||
429 |
/** |
|
430 |
This function wraps the Win32 GetModuleFileName API (see http://msdn2.microsoft.com/en-us/library/ms683197.aspx). |
|
431 |
*/ |
|
432 |
EXPORT_C DWORD Emulator::GetModuleFileName(HMODULE hModule, LPWSTR lpFilename) |
|
433 |
{ |
|
434 |
__LOCK_HOST; |
|
435 |
if (UnicodeHost) |
|
436 |
return ::GetModuleFileNameW(hModule, lpFilename, MAX_PATH); |
|
437 |
char fn[MAX_PATH]; |
|
438 |
DWORD r=::GetModuleFileNameA(hModule, fn, MAX_PATH); |
|
439 |
if (r>MAX_PATH||r==0) |
|
440 |
return 0; |
|
441 |
return ConvertToUnicode(fn, lpFilename, MAX_PATH, TRUE); |
|
442 |
} |
|
443 |
||
444 |
||
445 |
/** |
|
446 |
This function wraps the Win32 GetTempPath API (see http://msdn2.microsoft.com/en-us/library/Aa364992.aspx). |
|
447 |
*/ |
|
448 |
EXPORT_C DWORD Emulator::GetTempPath(DWORD nBufferLength,LPWSTR lpBuff) |
|
449 |
{ |
|
450 |
__LOCK_HOST; |
|
451 |
||
452 |
if (UnicodeHost) |
|
453 |
return ::GetTempPathW(nBufferLength,lpBuff); |
|
454 |
||
455 |
char path[MAX_PATH]; |
|
456 |
DWORD r=::GetTempPathA(MAX_PATH,path); |
|
457 |
if(r>MAX_PATH||r==0) |
|
458 |
return 0; |
|
459 |
return ConvertToUnicode(path,lpBuff,nBufferLength,TRUE); |
|
460 |
} |
|
461 |
||
462 |
||
463 |
/** |
|
464 |
This function wraps the Win32 GetCurrentDirectory API (see http://msdn2.microsoft.com/en-us/library/aa364934.aspx). |
|
465 |
*/ |
|
466 |
EXPORT_C DWORD Emulator::GetCurrentDirectory(DWORD nBufferLength,LPWSTR lpBuff) |
|
467 |
{ |
|
468 |
__LOCK_HOST; |
|
469 |
||
470 |
if (UnicodeHost) |
|
471 |
return ::GetCurrentDirectoryW(nBufferLength,lpBuff); |
|
472 |
||
473 |
char path[MAX_PATH]; |
|
474 |
DWORD r=::GetCurrentDirectoryA(MAX_PATH,path); |
|
475 |
if(r>MAX_PATH||r==0) |
|
476 |
return 0; |
|
477 |
return ConvertToUnicode(path,lpBuff,nBufferLength,TRUE); |
|
478 |
} |
|
479 |
||
480 |
||
481 |
/** |
|
482 |
This function wraps the Win32 GetVolumeInformation API (see http://msdn2.microsoft.com/en-us/library/aa364993.aspx). |
|
483 |
*/ |
|
484 |
EXPORT_C BOOL Emulator::GetVolumeInformation(LPCWSTR lpRootPathName,LPWSTR lpVolumeNameBuffer,DWORD nVolumeNameSize, |
|
485 |
LPDWORD lpVolumeSerialNumber,LPDWORD lpMaximumComponentLength, |
|
486 |
LPDWORD lpFileSystemFlags,LPWSTR,DWORD) |
|
487 |
{ |
|
488 |
__LOCK_HOST; |
|
489 |
||
490 |
// lpfileSystemNameBuffer always NULL so no need to convert |
|
491 |
if (UnicodeHost) |
|
492 |
return ::GetVolumeInformationW(lpRootPathName,lpVolumeNameBuffer,nVolumeNameSize,lpVolumeSerialNumber, |
|
493 |
lpMaximumComponentLength,lpFileSystemFlags,NULL,0); |
|
494 |
||
495 |
char volName[MAX_PATH]; |
|
496 |
BOOL res=::GetVolumeInformationA(Buf8<MAX_PATH>(lpRootPathName),volName,MAX_PATH,lpVolumeSerialNumber, |
|
497 |
lpMaximumComponentLength,lpFileSystemFlags,NULL,0); |
|
498 |
||
499 |
if(res && lpVolumeNameBuffer) |
|
500 |
ConvertToUnicode(volName,lpVolumeNameBuffer,nVolumeNameSize,FALSE); |
|
501 |
return res; |
|
502 |
} |
|
503 |
||
504 |
||
505 |
/** |
|
506 |
This function wraps the Win32 LoadLibrary API (see http://msdn2.microsoft.com/en-us/library/ms684175.aspx). |
|
507 |
*/ |
|
508 |
EXPORT_C HMODULE Emulator::LoadLibrary(LPCWSTR lpLibFileName) |
|
509 |
{ |
|
510 |
__LOCK_HOST; |
|
511 |
||
512 |
if (UnicodeHost) |
|
513 |
return ::LoadLibraryW(lpLibFileName); |
|
514 |
||
515 |
return ::LoadLibraryA(Buf8<MAX_PATH>(lpLibFileName)); |
|
516 |
} |
|
517 |
||
518 |
||
519 |
/** |
|
520 |
This function wraps the Win32 FreeLibrary API (see http://msdn2.microsoft.com/en-us/library/ms683152.aspx). |
|
521 |
*/ |
|
522 |
EXPORT_C BOOL Emulator::FreeLibrary(HMODULE hLibModule) |
|
523 |
{ |
|
524 |
__LOCK_HOST; |
|
525 |
return ::FreeLibrary(hLibModule); |
|
526 |
} |
|
527 |
||
528 |
||
529 |
||
530 |
/** |
|
531 |
This function wraps the Win32 MoveFile API (see http://msdn2.microsoft.com/en-us/library/aa365239.aspx). |
|
532 |
*/ |
|
533 |
EXPORT_C BOOL Emulator::MoveFile(LPCWSTR lpExistingFileName,LPCWSTR lpNewFileName) |
|
534 |
{ |
|
535 |
__LOCK_HOST; |
|
536 |
||
537 |
if (UnicodeHost) |
|
538 |
return ::MoveFileW(lpExistingFileName,lpNewFileName); |
|
539 |
||
540 |
return ::MoveFileA(Buf8<MAX_PATH>(lpExistingFileName),Buf8<MAX_PATH>(lpNewFileName)); |
|
541 |
} |
|
542 |
||
543 |
||
544 |
/** |
|
545 |
This function wraps the Win32 CopyFile API (see http://msdn2.microsoft.com/en-us/library/aa363851.aspx). |
|
546 |
*/ |
|
547 |
EXPORT_C BOOL Emulator::CopyFile(LPCWSTR lpExistingFileName,LPCWSTR lpNewFileName,BOOL aFailIfExists) |
|
548 |
{ |
|
549 |
__LOCK_HOST; |
|
550 |
||
551 |
if (UnicodeHost) |
|
552 |
return ::CopyFileW(lpExistingFileName,lpNewFileName,aFailIfExists); |
|
553 |
||
554 |
return ::CopyFileA(Buf8<MAX_PATH>(lpExistingFileName),Buf8<MAX_PATH>(lpNewFileName),aFailIfExists); |
|
555 |
} |
|
556 |
||
557 |
||
558 |
/** |
|
559 |
This function wraps the Win32 OutputDebugString API (see http://msdn2.microsoft.com/en-us/library/aa363362.aspx). |
|
560 |
*/ |
|
561 |
EXPORT_C VOID Emulator::OutputDebugString(LPCWSTR lpOutputString) |
|
562 |
{ |
|
563 |
if (UnicodeHost) |
|
564 |
::OutputDebugStringW(lpOutputString); |
|
565 |
else |
|
566 |
::OutputDebugStringA(Buf8<1024>(lpOutputString)); |
|
567 |
} |
|
568 |
||
569 |
||
570 |
||
571 |
/** |
|
572 |
This function wraps the Win32 RemoveDirectory API (see http://msdn2.microsoft.com/en-us/library/aa365488.aspx). |
|
573 |
*/ |
|
574 |
EXPORT_C BOOL Emulator::RemoveDirectory(LPCWSTR lpPathName) |
|
575 |
{ |
|
576 |
__LOCK_HOST; |
|
577 |
||
578 |
if (UnicodeHost) |
|
579 |
return ::RemoveDirectoryW(lpPathName); |
|
580 |
||
581 |
return ::RemoveDirectoryA(Buf8<MAX_PATH>(lpPathName)); |
|
582 |
} |
|
583 |
||
584 |
||
585 |
/** |
|
586 |
This function wraps the Win32 SetFileAttributes API (see http://msdn2.microsoft.com/en-us/library/aa365535.aspx). |
|
587 |
*/ |
|
588 |
||
589 |
EXPORT_C BOOL Emulator::SetFileAttributes(LPCWSTR lpFileName,DWORD dwFileAttributes) |
|
590 |
{ |
|
591 |
__LOCK_HOST; |
|
592 |
||
593 |
if (UnicodeHost) |
|
594 |
return ::SetFileAttributesW(lpFileName,dwFileAttributes); |
|
595 |
||
596 |
return ::SetFileAttributesA(Buf8<MAX_PATH>(lpFileName),dwFileAttributes); |
|
597 |
} |
|
598 |
||
599 |
||
600 |
||
601 |
/** |
|
602 |
This function wraps the Win32 SetVolumeLabel API (see http://msdn2.microsoft.com/en-us/library/aa365560.aspx). |
|
603 |
*/ |
|
604 |
EXPORT_C BOOL Emulator::SetVolumeLabel(LPCWSTR lpRootPathName,LPCWSTR lpVolumeName) |
|
605 |
{ |
|
606 |
__LOCK_HOST; |
|
607 |
||
608 |
if (UnicodeHost) |
|
609 |
return ::SetVolumeLabelW(lpRootPathName,lpVolumeName); |
|
610 |
||
611 |
return ::SetVolumeLabelA(Buf8<MAX_PATH>(lpRootPathName),Buf8<MAX_PATH>(lpVolumeName)); |
|
612 |
} |
|
613 |
||
614 |
||
615 |
/** |
|
616 |
Maps an NT error to an Epoc32 error. |
|
617 |
||
618 |
@return Last-error code of the calling thread. |
|
619 |
*/ |
|
620 |
EXPORT_C TInt Emulator::LastError() |
|
621 |
||
622 |
// For other error codes look at MSDN "Numerical List of Error Codes" |
|
623 |
{ |
|
624 |
switch (GetLastError()) |
|
625 |
{ |
|
626 |
case ERROR_SUCCESS: return KErrNone; |
|
627 |
case ERROR_INVALID_DRIVE: return KErrNotReady; |
|
628 |
case ERROR_INVALID_NAME: |
|
629 |
case ERROR_FILENAME_EXCED_RANGE: |
|
630 |
case ERROR_OPEN_FAILED: return KErrBadName; |
|
631 |
case ERROR_INVALID_HANDLE: return KErrBadHandle; |
|
632 |
case ERROR_NOT_SUPPORTED: |
|
633 |
case ERROR_INVALID_FUNCTION: return KErrNotSupported; |
|
634 |
case ERROR_SHARING_VIOLATION: |
|
635 |
case ERROR_ACCESS_DENIED: |
|
636 |
case ERROR_WRITE_PROTECT: return KErrAccessDenied; |
|
637 |
case ERROR_LOCK_VIOLATION: return KErrLocked; |
|
638 |
case ERROR_FILE_NOT_FOUND: |
|
639 |
case ERROR_MOD_NOT_FOUND: return KErrNotFound; |
|
640 |
case ERROR_DIRECTORY: |
|
641 |
case ERROR_BAD_PATHNAME: |
|
642 |
case ERROR_PATH_NOT_FOUND: return KErrPathNotFound; |
|
643 |
case ERROR_ALREADY_EXISTS: |
|
644 |
case ERROR_FILE_EXISTS: return KErrAlreadyExists; |
|
645 |
case ERROR_NOT_READY: return KErrNotReady; |
|
646 |
case ERROR_UNRECOGNIZED_VOLUME: |
|
647 |
case ERROR_NOT_DOS_DISK: return KErrUnknown; |
|
648 |
case ERROR_UNRECOGNIZED_MEDIA: |
|
649 |
case ERROR_BAD_EXE_FORMAT: return KErrCorrupt; |
|
650 |
case ERROR_NO_MORE_FILES: return KErrEof; |
|
651 |
case ERROR_DIR_NOT_EMPTY: return KErrInUse; |
|
652 |
case ERROR_INVALID_USER_BUFFER: |
|
653 |
case ERROR_NOT_ENOUGH_MEMORY: |
|
654 |
case ERROR_INSUFFICIENT_BUFFER: |
|
655 |
case ERROR_OUTOFMEMORY: return KErrNoMemory; |
|
656 |
case ERROR_DISK_FULL: return KErrDiskFull; |
|
657 |
case ERROR_INVALID_DATA: |
|
658 |
case ERROR_INVALID_PARAMETER: return KErrArgument; |
|
659 |
case ERROR_OPERATION_ABORTED: return KErrCancel; |
|
660 |
||
661 |
default: return KErrGeneral; |
|
662 |
} |
|
663 |
} |
|
664 |
||
665 |
/** |
|
666 |
This function wraps the Win32 GetProcAddress API (see http://msdn2.microsoft.com/en-us/library/ms683212.aspx). |
|
667 |
*/ |
|
668 |
FARPROC Emulator::GetProcAddress(HMODULE hModule, LPCSTR lpProcName) |
|
669 |
{ |
|
670 |
__LOCK_HOST; |
|
671 |
return ::GetProcAddress(hModule, lpProcName); |
|
672 |
} |
|
673 |
||
674 |
||
675 |
||
676 |
// image file support |
|
677 |
||
678 |
// loaded modules |
|
679 |
||
680 |
||
681 |
/** |
|
682 |
Gets the header format of the file system. |
|
683 |
||
684 |
@return Returns the header format of the file system. |
|
685 |
*/ |
|
686 |
EXPORT_C const IMAGE_NT_HEADERS32* Emulator::TModule::NtHeader() const |
|
687 |
{ |
|
688 |
if (!IsValid()) |
|
689 |
return 0; |
|
690 |
const IMAGE_DOS_HEADER* dhead = (const IMAGE_DOS_HEADER*)Translate(0); |
|
691 |
if ( IMAGE_DOS_SIGNATURE != dhead->e_magic ) |
|
692 |
return 0; |
|
693 |
if (dhead->e_lfarlc < sizeof(IMAGE_DOS_HEADER)) |
|
694 |
return 0; |
|
695 |
const IMAGE_NT_HEADERS32* ntHead = (const IMAGE_NT_HEADERS32*)Translate(dhead->e_lfanew); |
|
696 |
if ( ntHead->Signature != IMAGE_NT_SIGNATURE ) |
|
697 |
return 0; |
|
698 |
return ntHead; |
|
699 |
} |
|
700 |
||
701 |
||
702 |
/** |
|
703 |
Constructor which sets the handles of loaded module to the specified module. |
|
704 |
||
705 |
@param aModuleName Holds the name of the module to be loaded. |
|
706 |
*/ |
|
707 |
EXPORT_C Emulator::TModule::TModule(LPCSTR aModuleName) |
|
708 |
: iModule(GetModuleHandleA(aModuleName)), iBase(iModule) |
|
709 |
{ |
|
710 |
if (!NtHeader()) |
|
711 |
{ |
|
712 |
iModule = 0; |
|
713 |
iBase = 0; |
|
714 |
} |
|
715 |
} |
|
716 |
||
717 |
||
718 |
/** |
|
719 |
Gets the pointer of the section header if the header name matches with the buffer aSection[]. |
|
720 |
||
721 |
@param aSection[] A buffer of type BYTE. |
|
722 |
||
723 |
@return Returns the pointer to section header of the file system. |
|
724 |
*/ |
|
725 |
EXPORT_C const IMAGE_SECTION_HEADER* Emulator::TModule::SectionHeader(const BYTE aSection[]) const |
|
726 |
{ |
|
727 |
const IMAGE_NT_HEADERS32* ntHead = NtHeader(); |
|
728 |
if (!ntHead) |
|
729 |
return 0; |
|
730 |
||
731 |
const IMAGE_SECTION_HEADER* imgHead = (const IMAGE_SECTION_HEADER*)((TUint8*)&ntHead->OptionalHeader + ntHead->FileHeader.SizeOfOptionalHeader); |
|
732 |
const IMAGE_SECTION_HEADER* end = imgHead + ntHead->FileHeader.NumberOfSections; |
|
733 |
for (; imgHead < end; ++imgHead) |
|
734 |
{ |
|
735 |
if (memcmp(imgHead->Name, aSection, IMAGE_SIZEOF_SHORT_NAME)==0) |
|
736 |
return imgHead; |
|
737 |
} |
|
738 |
return 0; |
|
739 |
} |
|
740 |
||
741 |
||
742 |
/** |
|
743 |
Points to the first byte or first page of the loaded module or mapped file image. |
|
744 |
||
745 |
@param aSection[] A buffer of type BYTE. |
|
746 |
||
747 |
@return TAny Returns first byte of the loaded module or first page of the mapped file image. |
|
748 |
*/ |
|
749 |
EXPORT_C const TAny* Emulator::TModule::Section(const BYTE aSection[]) const |
|
750 |
{ |
|
751 |
const IMAGE_SECTION_HEADER* imgHead = SectionHeader(aSection); |
|
752 |
if (imgHead) |
|
753 |
return Translate(IsLoaded() ? imgHead->VirtualAddress : imgHead->PointerToRawData); |
|
754 |
return 0; |
|
755 |
} |
|
756 |
||
757 |
||
758 |
/** |
|
759 |
Go through the section headers looking for UID section. |
|
760 |
||
761 |
@param aType Contains a UID type. |
|
762 |
*/ |
|
763 |
EXPORT_C void Emulator::TModule::GetUids(TUidType& aType) const |
|
764 |
{ |
|
765 |
const TEmulatorImageHeader* hdr = (const TEmulatorImageHeader*)Section(KWin32SectionName_Symbian); |
|
766 |
if (hdr) |
|
767 |
aType = *(const TUidType*)&hdr->iUids[0]; |
|
768 |
} |
|
769 |
||
770 |
||
771 |
/** |
|
772 |
Goes through section headers and gets the information of file system. |
|
773 |
||
774 |
@param aInfo Contains the information of the file system. |
|
775 |
*/ |
|
776 |
EXPORT_C void Emulator::TModule::GetInfo(TProcessCreateInfo& aInfo) const |
|
777 |
{ |
|
778 |
aInfo.iExceptionDescriptor = 0; |
|
779 |
const IMAGE_NT_HEADERS32* ntHead = NtHeader(); |
|
780 |
if (ntHead) |
|
781 |
{ |
|
782 |
const TEmulatorImageHeader* hdr = (const TEmulatorImageHeader*)Section(KWin32SectionName_Symbian); |
|
783 |
if (hdr) |
|
784 |
{ |
|
785 |
aInfo.iUids = *(const TUidType*)&hdr->iUids[0]; |
|
786 |
TBool isExe = (hdr->iUids[0].iUid==KExecutableImageUidValue); |
|
787 |
TBool data_section_present = (Section(KWin32SectionName_EpocData)!=NULL); |
|
788 |
TBool data = hdr->iFlags & KEmulatorImageFlagAllowDllData; |
|
789 |
if (data_section_present && isExe) |
|
790 |
data = ETrue; |
|
791 |
aInfo.iBssSize=data?1:0; |
|
792 |
aInfo.iDataSize=0; |
|
793 |
aInfo.iTotalDataSize=aInfo.iBssSize; |
|
794 |
aInfo.iDepCount = 0; |
|
795 |
aInfo.iHeapSizeMin = ntHead->OptionalHeader.SizeOfHeapCommit; |
|
796 |
aInfo.iHeapSizeMax = ntHead->OptionalHeader.SizeOfHeapReserve; |
|
797 |
aInfo.iStackSize = 0x1000; |
|
798 |
aInfo.iPriority = hdr->iPriority; |
|
799 |
aInfo.iHandle = NULL; |
|
800 |
aInfo.iS = hdr->iS; |
|
801 |
aInfo.iModuleVersion = hdr->iModuleVersion; |
|
802 |
if (ntHead->FileHeader.Characteristics & IMAGE_FILE_DLL) |
|
803 |
aInfo.iAttr |= ECodeSegAttHDll; |
|
804 |
} |
|
805 |
} |
|
806 |
GetUids(aInfo.iUids); |
|
807 |
} |
|
808 |
||
809 |
||
810 |
/** |
|
811 |
Finds the import section from the data directory. This relies on the module being loaded. |
|
812 |
||
813 |
@return Returns the imported executable. |
|
814 |
*/ |
|
815 |
EXPORT_C const IMAGE_IMPORT_DESCRIPTOR* Emulator::TModule::Imports() const |
|
816 |
{ |
|
817 |
if (!IsLoaded()) |
|
818 |
return NULL; |
|
819 |
const IMAGE_NT_HEADERS32* ntHead = NtHeader(); |
|
820 |
if (!ntHead) |
|
821 |
return NULL; |
|
822 |
DWORD va = ntHead->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress; |
|
823 |
if (!va) |
|
824 |
return NULL; |
|
825 |
return (const IMAGE_IMPORT_DESCRIPTOR*)Translate(va); |
|
826 |
} |
|
827 |
||
828 |
// modules in the file system |
|
829 |
EXPORT_C TInt Emulator::RImageFile::Open(LPCTSTR aImageFile) |
|
830 |
{ |
|
831 |
Buf8<MAX_PATH> nameBuf(aImageFile); |
|
832 |
char *pName = strrchr(LPCSTR(nameBuf), '\\'); |
|
833 |
pName ? ++pName : pName = (char *)LPCSTR(nameBuf); |
|
834 |
||
835 |
__LOCK_HOST; |
|
836 |
iMapping = OpenFileMapping(FILE_MAP_READ, FALSE, (LPCTSTR)pName); |
|
837 |
if (!iMapping) |
|
838 |
{ |
|
839 |
if (pName == (char *)LPCSTR(nameBuf)) |
|
840 |
iModule = Emulator::GetModuleHandle(aImageFile); |
|
841 |
if (iModule) |
|
842 |
iBase = iModule; |
|
843 |
else |
|
844 |
{ |
|
845 |
// need to map the file instead |
|
846 |
HANDLE file = Emulator::CreateFile(aImageFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); |
|
847 |
if (file == INVALID_HANDLE_VALUE) |
|
848 |
return LastError(); |
|
849 |
iMapping = CreateFileMappingA(file, NULL, PAGE_READONLY, 0, 0, pName); |
|
850 |
CloseHandle(file); |
|
851 |
} |
|
852 |
} |
|
853 |
if (!iModule) |
|
854 |
{ |
|
855 |
if (!iMapping) |
|
856 |
return LastError(); |
|
857 |
||
176 | 858 |
// First we need to read in the PE file's image headers, which consist of a 4 byte signature, the file header |
859 |
// containing general information and up to 96 section headers. Map this amount of the file into memory so |
|
860 |
// that we can examine it and calculate the minimum size of the sections themselves that need to be mapped into |
|
861 |
// memory in order to examine the actual sections |
|
862 |
SIZE_T headersSize = (sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + (96 * sizeof(IMAGE_OPTIONAL_HEADER))); |
|
863 |
iBase = MapViewOfFile(iMapping, FILE_MAP_READ, 0, 0, headersSize); |
|
864 |
||
865 |
if (iBase) |
|
866 |
{ |
|
867 |
// Scan through the section headers and determine the minimum amount of the file to be mapped into memory |
|
868 |
// in order for us to safely examine the sections, and map the file into memory. We do this rather than |
|
869 |
// map the entire PE file into memory because with full debug information, DLLs can be anything up to 100 MB!!! |
|
870 |
TInt sectionsSize = GetSectionsSize(NtHeader()); |
|
871 |
||
872 |
// Before remapping the file with its new size, unmap it. While one would think that it is safe to |
|
873 |
// increase the size of the file's mapping it is not, and doing so triggers behaviour in Windows that |
|
874 |
// results in quickly using up all available virtual memory address space! |
|
875 |
if (UnmapViewOfFile(iBase)) |
|
876 |
{ |
|
877 |
iBase = MapViewOfFile(iMapping, FILE_MAP_READ, 0, 0, sectionsSize); |
|
878 |
} |
|
879 |
else |
|
880 |
{ |
|
881 |
iBase = NULL; |
|
882 |
} |
|
883 |
} |
|
0 | 884 |
|
885 |
if (!iBase) |
|
886 |
{ |
|
887 |
CloseHandle(iMapping); |
|
888 |
iMapping = 0; |
|
889 |
return LastError(); |
|
890 |
} |
|
891 |
} |
|
892 |
||
893 |
if (!NtHeader()) |
|
894 |
{ |
|
895 |
Close(); |
|
896 |
return KErrNotSupported; |
|
897 |
} |
|
898 |
return KErrNone; |
|
899 |
} |
|
900 |
||
901 |
EXPORT_C void Emulator::RImageFile::Close() |
|
902 |
{ |
|
903 |
if (iMapping) |
|
904 |
{ |
|
905 |
UnmapViewOfFile(iBase); |
|
906 |
CloseHandle(iMapping); |
|
907 |
iMapping = 0; |
|
908 |
} |
|
909 |
iBase = 0; |
|
910 |
iModule = 0; |
|
911 |
} |