1 #include "dbghelp_private.h"
3 void* __HeapAlloc(int heap
, int flags
, size_t size
)
5 void * ret
= malloc(size
);
6 if(flags
& HEAP_ZERO_MEMORY
)
11 void* __HeapReAlloc(int heap
, DWORD d2
, void *slab
, SIZE_T newsize
)
13 return realloc(slab
, newsize
);
16 WCHAR
* lstrcpynW(WCHAR
* lpString1
, const WCHAR
* lpString2
, int iMaxLength
)
19 const WCHAR
* s
= lpString2
;
20 UINT count
= iMaxLength
;
22 while ((count
> 1) && *s
)
34 PIMAGE_NT_HEADERS
__RtlImageNtHeader(void *data
)
36 PIMAGE_DOS_HEADER DosHeader
= (PIMAGE_DOS_HEADER
)data
;
37 PIMAGE_NT_HEADERS NtHeaders
;
39 if (DosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
41 NtHeaderPtr
= ((PCHAR
)data
) + DosHeader
->e_lfanew
;
42 NtHeaders
= (PIMAGE_NT_HEADERS
)NtHeaderPtr
;
43 if (NtHeaders
->Signature
!= IMAGE_NT_SIGNATURE
)
49 __RtlImageRvaToSection(
50 const IMAGE_NT_HEADERS
* NtHeader
,
54 PIMAGE_SECTION_HEADER Section
;
58 Count
= SWAPW(NtHeader
->FileHeader
.NumberOfSections
);
59 Section
= IMAGE_FIRST_SECTION(NtHeader
);
63 Va
= SWAPD(Section
->VirtualAddress
);
64 if ((Va
<= Rva
) && (Rva
< Va
+ SWAPD(Section
->SizeOfRawData
)))
74 (const IMAGE_NT_HEADERS
* NtHeader
,
77 PIMAGE_SECTION_HEADER
*SectionHeader
)
79 PIMAGE_SECTION_HEADER Section
= NULL
;
82 Section
= *SectionHeader
;
84 if ((Section
== NULL
) ||
85 (Rva
< SWAPD(Section
->VirtualAddress
)) ||
86 (Rva
>= SWAPD(Section
->VirtualAddress
) + SWAPD(Section
->SizeOfRawData
)))
88 Section
= RtlImageRvaToSection(NtHeader
, BaseAddress
, Rva
);
93 *SectionHeader
= Section
;
96 return (PVOID
)((ULONG_PTR
)BaseAddress
+ Rva
+
97 (ULONG_PTR
)SWAPD(Section
->PointerToRawData
) -
98 (ULONG_PTR
)SWAPD(Section
->VirtualAddress
));
102 __RtlImageDirectoryEntryToData(
104 BOOLEAN MappedAsImage
,
108 PIMAGE_NT_HEADERS NtHeader
;
111 /* Magic flag for non-mapped images. */
112 if ((ULONG_PTR
)BaseAddress
& 1)
114 BaseAddress
= (PVOID
)((ULONG_PTR
)BaseAddress
& ~1);
115 MappedAsImage
= FALSE
;
118 NtHeader
= RtlImageNtHeader(BaseAddress
);
119 if (NtHeader
== NULL
)
122 if (Directory
>= SWAPD(NtHeader
->OptionalHeader
.NumberOfRvaAndSizes
))
125 Va
= SWAPD(NtHeader
->OptionalHeader
.DataDirectory
[Directory
].VirtualAddress
);
129 *Size
= SWAPD(NtHeader
->OptionalHeader
.DataDirectory
[Directory
].Size
);
131 if (MappedAsImage
|| Va
< SWAPD(NtHeader
->OptionalHeader
.SizeOfHeaders
))
132 return (PVOID
)((ULONG_PTR
)BaseAddress
+ Va
);
134 /* Image mapped as ordinary file, we must find raw pointer */
135 return RtlImageRvaToVa(NtHeader
, BaseAddress
, Va
, NULL
);
138 BOOL
__GetFileSizeEx(HANDLE file
, PLARGE_INTEGER fsize
)
140 if (fseek((FILE*)file
, 0, 2) == -1)
142 fsize
->QuadPart
= ftell((FILE*)file
);
146 BOOL
__CloseHandle(HANDLE handle
)
152 HANDLE
__CreateFileW(
154 DWORD dwDesiredAccess
,
156 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
157 DWORD dwCreationDisposition
,
158 DWORD dwFlagsAndAttributes
,
159 HANDLE hTemplateFile
)
164 WideCharToMultiByte(CP_ACP
, 0, lpFileName
, -1, buf
, MAX_PATH
, NULL
, NULL
);
165 res
= CreateFileA(buf
, dwDesiredAccess
, dwShareMode
, lpSecurityAttributes
, dwCreationDisposition
, dwFlagsAndAttributes
, hTemplateFile
);
169 void* __MapViewOfFile(HANDLE file
,DWORD d1
,DWORD d2
,DWORD d3
,SIZE_T s
)
171 FILE *f
= (FILE*)file
;
175 if (file
== INVALID_HANDLE_VALUE
)
178 if (!GetFileSizeEx(file
, &size
))
181 if (fseek(f
, 0, 0) == -1)
184 result
= malloc(size
.LowPart
);
185 if (fread(result
, 1, size
.LowPart
, f
) != size
.LowPart
)
194 BOOL
__UnmapViewOfFile(const void* data
)
200 LPSTR
__lstrcpynA(LPSTR d
,LPCSTR s
,int c
)
211 /* From Wine implementation over their unicode library */
213 __WideCharToMultiByte(UINT page
, DWORD flags
, LPCWSTR src
, INT srclen
,
214 LPSTR dst
, INT dstlen
, LPCSTR defchar
, BOOL
*used
)
218 if (!src
|| !srclen
|| (!dst
&& dstlen
))
220 SetLastError( ERROR_INVALID_PARAMETER
);
224 if (srclen
< 0) srclen
= strlenW(src
) + 1;
229 for(i
=0; i
<srclen
&& i
<dstlen
; i
++)
230 dst
[i
] = src
[i
] & 0xFF;
232 if (used
) *used
= FALSE
;
238 __MultiByteToWideChar(UINT page
, DWORD flags
, LPCSTR src
, INT srclen
,
239 LPWSTR dst
, INT dstlen
)
243 if (!src
|| !srclen
|| (!dst
&& dstlen
))
245 SetLastError( ERROR_INVALID_PARAMETER
);
249 if (srclen
< 0) srclen
= strlen(src
) + 1;
254 for(i
=0; i
<srclen
&& i
<dstlen
; i
++)
260 /* In our case, the provided file path is the one we are looking for */
261 HANDLE
__FindExecutableImageExW(PCWSTR file
, PCWSTR path
, PWSTR out_buffer
, PFIND_EXE_FILE_CALLBACKW x
, PVOID y
)
263 HANDLE ret
= CreateFileW(file
, 0, 0, NULL
, 0, 0, NULL
);
265 memcpy(out_buffer
, file
, (strlenW(file
) + 1)*sizeof(WCHAR
));
270 /* printf with temp buffer allocation */
271 const char *wine_dbg_sprintf( const char *format
, ... )
273 static const int max_size
= 200;
274 static char buffer
[256];
279 va_start(valist
, format
);
281 len
= vsnprintf( ret
, max_size
, format
, valist
);
282 if (len
== -1 || len
>= max_size
) ret
[max_size
-1] = 0;
287 /* default implementation of wine_dbgstr_an */
288 const char *wine_dbgstr_an( const char *str
, int n
)
290 static const char hex
[16] = "0123456789abcdef";
293 static char buffer
[256];
295 if (!((ULONG_PTR
)str
>> 16))
297 if (!str
) return "(null)";
299 sprintf( res
, "#%04x", LOWORD(str
) );
302 if (n
== -1) n
= strlen(str
);
304 size
= 10 + min( 300, n
* 4 );
307 while (n
-- > 0 && dst
<= res
+ size
- 9)
309 unsigned char c
= *str
++;
312 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
313 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
314 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
315 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
316 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
318 if (c
>= ' ' && c
<= 126)
324 *dst
++ = hex
[(c
>> 4) & 0x0f];
325 *dst
++ = hex
[c
& 0x0f];
341 /* default implementation of wine_dbgstr_wn */
342 const char *wine_dbgstr_wn( const WCHAR
*str
, int n
)
346 static char buffer
[256];
348 if (!((ULONG_PTR
)str
>> 16))
350 if (!str
) return "(null)";
352 sprintf( res
, "#%04x", LOWORD(str
) );
357 const WCHAR
*end
= str
;
362 size
= 12 + min( 300, n
* 5 );
366 while (n
-- > 0 && dst
<= res
+ size
- 10)
371 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
372 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
373 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
374 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
375 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
377 if (c
>= ' ' && c
<= 126)
382 sprintf(dst
,"%04x",c
);