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
);
65 (Rva
< Va
+ SWAPD(Section
->Misc
.VirtualSize
)))
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
->Misc
.VirtualSize
)))
88 Section
= RtlImageRvaToSection (NtHeader
, BaseAddress
, Rva
);
93 *SectionHeader
= Section
;
96 return (PVOID
)((ULONG_PTR
)BaseAddress
+
98 SWAPD(Section
->PointerToRawData
) -
99 (ULONG_PTR
)SWAPD(Section
->VirtualAddress
));
103 __RtlImageDirectoryEntryToData(
105 BOOLEAN MappedAsImage
,
109 PIMAGE_NT_HEADERS NtHeader
;
112 /* Magic flag for non-mapped images. */
113 if ((ULONG_PTR
)BaseAddress
& 1)
115 BaseAddress
= (PVOID
)((ULONG_PTR
)BaseAddress
& ~1);
116 MappedAsImage
= FALSE
;
119 NtHeader
= RtlImageNtHeader(BaseAddress
);
120 if (NtHeader
== NULL
)
123 if (Directory
>= SWAPD(NtHeader
->OptionalHeader
.NumberOfRvaAndSizes
))
126 Va
= SWAPD(NtHeader
->OptionalHeader
.DataDirectory
[Directory
].VirtualAddress
);
130 *Size
= SWAPD(NtHeader
->OptionalHeader
.DataDirectory
[Directory
].Size
);
132 if (MappedAsImage
|| Va
< SWAPD(NtHeader
->OptionalHeader
.SizeOfHeaders
))
133 return (PVOID
)((ULONG_PTR
)BaseAddress
+ Va
);
135 /* image mapped as ordinary file, we must find raw pointer */
136 return RtlImageRvaToVa(NtHeader
, BaseAddress
, Va
, NULL
);
139 BOOL
__GetFileSizeEx(HANDLE file
, PLARGE_INTEGER fsize
)
141 if (fseek((FILE*)file
, 0, 2) == -1)
143 fsize
->QuadPart
= ftell((FILE*)file
);
147 BOOL
__CloseHandle(HANDLE handle
)
153 HANDLE
__CreateFileW(
155 DWORD dwDesiredAccess
,
157 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
158 DWORD dwCreationDisposition
,
159 DWORD dwFlagsAndAttributes
,
160 HANDLE hTemplateFile
)
165 WideCharToMultiByte(CP_ACP
, 0, lpFileName
, -1, buf
, MAX_PATH
, NULL
, NULL
);
166 res
= CreateFileA(buf
, dwDesiredAccess
, dwShareMode
, lpSecurityAttributes
, dwCreationDisposition
, dwFlagsAndAttributes
, hTemplateFile
);
170 void* __MapViewOfFile(HANDLE file
,DWORD d1
,DWORD d2
,DWORD d3
,SIZE_T s
)
172 FILE *f
= (FILE*)file
;
176 if (file
== INVALID_HANDLE_VALUE
)
179 if (!GetFileSizeEx(file
, &size
))
182 if (fseek(f
, 0, 0) == -1)
185 result
= malloc(size
.LowPart
);
186 if (fread(result
, 1, size
.LowPart
, f
) != size
.LowPart
)
195 BOOL
__UnmapViewOfFile(const void* data
)
201 LPSTR
__lstrcpynA(LPSTR d
,LPCSTR s
,int c
)
212 /* From Wine implementation over their unicode library */
214 __WideCharToMultiByte(UINT page
, DWORD flags
, LPCWSTR src
, INT srclen
,
215 LPSTR dst
, INT dstlen
, LPCSTR defchar
, BOOL
*used
)
219 if (!src
|| !srclen
|| (!dst
&& dstlen
))
221 SetLastError( ERROR_INVALID_PARAMETER
);
225 if (srclen
< 0) srclen
= strlenW(src
) + 1;
230 for(i
=0; i
<srclen
&& i
<dstlen
; i
++)
231 dst
[i
] = src
[i
] & 0xFF;
233 if (used
) *used
= FALSE
;
239 __MultiByteToWideChar(UINT page
, DWORD flags
, LPCSTR src
, INT srclen
,
240 LPWSTR dst
, INT dstlen
)
244 if (!src
|| !srclen
|| (!dst
&& dstlen
))
246 SetLastError( ERROR_INVALID_PARAMETER
);
250 if (srclen
< 0) srclen
= strlen(src
) + 1;
255 for(i
=0; i
<srclen
&& i
<dstlen
; i
++)
261 /* In our case, the provided file path is the one we are looking for */
262 HANDLE
__FindExecutableImageExW(PCWSTR file
, PCWSTR path
, PWSTR out_buffer
, PFIND_EXE_FILE_CALLBACKW x
, PVOID y
)
264 HANDLE ret
= CreateFileW(file
, 0, 0, NULL
, 0, 0, NULL
);
266 memcpy(out_buffer
, file
, (strlenW(file
) + 1)*sizeof(WCHAR
));
271 /* printf with temp buffer allocation */
272 const char *wine_dbg_sprintf( const char *format
, ... )
274 static const int max_size
= 200;
275 static char buffer
[256];
280 va_start(valist
, format
);
282 len
= vsnprintf( ret
, max_size
, format
, valist
);
283 if (len
== -1 || len
>= max_size
) ret
[max_size
-1] = 0;
288 /* default implementation of wine_dbgstr_an */
289 const char *wine_dbgstr_an( const char *str
, int n
)
291 static const char hex
[16] = "0123456789abcdef";
294 static char buffer
[256];
296 if (!((ULONG_PTR
)str
>> 16))
298 if (!str
) return "(null)";
300 sprintf( res
, "#%04x", LOWORD(str
) );
303 if (n
== -1) n
= strlen(str
);
305 size
= 10 + min( 300, n
* 4 );
308 while (n
-- > 0 && dst
<= res
+ size
- 9)
310 unsigned char c
= *str
++;
313 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
314 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
315 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
316 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
317 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
319 if (c
>= ' ' && c
<= 126)
325 *dst
++ = hex
[(c
>> 4) & 0x0f];
326 *dst
++ = hex
[c
& 0x0f];
342 /* default implementation of wine_dbgstr_wn */
343 const char *wine_dbgstr_wn( const WCHAR
*str
, int n
)
347 static char buffer
[256];
349 if (!((ULONG_PTR
)str
>> 16))
351 if (!str
) return "(null)";
353 sprintf( res
, "#%04x", LOWORD(str
) );
358 const WCHAR
*end
= str
;
363 size
= 12 + min( 300, n
* 5 );
367 while (n
-- > 0 && dst
<= res
+ size
- 10)
372 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
373 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
374 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
375 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
376 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
378 if (c
>= ' ' && c
<= 126)
383 sprintf(dst
,"%04x",c
);