ReactOS  0.4.13-dev-100-gc8611ae
compat.c
Go to the documentation of this file.
1 #include "dbghelp_private.h"
2 
3 void* __HeapAlloc(int heap, int flags, size_t size)
4 {
5  void * ret = malloc(size);
7  memset(ret, 0, size);
8  return ret;
9 }
10 
11 void* __HeapReAlloc(int heap, DWORD d2, void *slab, SIZE_T newsize)
12 {
13  return realloc(slab, newsize);
14 }
15 
16 WCHAR* lstrcpynW(WCHAR* lpString1, const WCHAR* lpString2, int iMaxLength)
17 {
18  LPWSTR d = lpString1;
19  const WCHAR* s = lpString2;
20  UINT count = iMaxLength;
21 
22  while ((count > 1) && *s)
23  {
24  count--;
25  *d++ = *s++;
26  }
27 
28  if (count)
29  *d = 0;
30 
31  return lpString1;
32 }
33 
35 {
37  PIMAGE_NT_HEADERS NtHeaders;
38  PCHAR NtHeaderPtr;
39  if (DosHeader->e_magic != IMAGE_DOS_SIGNATURE)
40  return NULL;
41  NtHeaderPtr = ((PCHAR)data) + DosHeader->e_lfanew;
42  NtHeaders = (PIMAGE_NT_HEADERS)NtHeaderPtr;
43  if (NtHeaders->Signature != IMAGE_NT_SIGNATURE)
44  return NULL;
45  return NtHeaders;
46 }
47 
50  const IMAGE_NT_HEADERS* NtHeader,
52  ULONG Rva)
53 {
54  PIMAGE_SECTION_HEADER Section;
55  ULONG Va;
56  ULONG Count;
57 
59  Section = IMAGE_FIRST_SECTION(NtHeader);
60 
61  while (Count--)
62  {
63  Va = SWAPD(Section->VirtualAddress);
64  if ((Va <= Rva) && (Rva < Va + SWAPD(Section->SizeOfRawData)))
65  return Section;
66  Section++;
67  }
68 
69  return NULL;
70 }
71 
72 PVOID
74 (const IMAGE_NT_HEADERS* NtHeader,
76  ULONG Rva,
77  PIMAGE_SECTION_HEADER *SectionHeader)
78 {
79  PIMAGE_SECTION_HEADER Section = NULL;
80 
81  if (SectionHeader)
82  Section = *SectionHeader;
83 
84  if ((Section == NULL) ||
85  (Rva < SWAPD(Section->VirtualAddress)) ||
86  (Rva >= SWAPD(Section->VirtualAddress) + SWAPD(Section->SizeOfRawData)))
87  {
88  Section = RtlImageRvaToSection(NtHeader, BaseAddress, Rva);
89  if (Section == NULL)
90  return NULL;
91 
92  if (SectionHeader)
93  *SectionHeader = Section;
94  }
95 
96  return (PVOID)((ULONG_PTR)BaseAddress + Rva +
97  (ULONG_PTR)SWAPD(Section->PointerToRawData) -
98  (ULONG_PTR)SWAPD(Section->VirtualAddress));
99 }
100 
101 PVOID
106  PULONG Size)
107 {
108  PIMAGE_NT_HEADERS NtHeader;
109  ULONG Va;
110 
111  /* Magic flag for non-mapped images. */
112  if ((ULONG_PTR)BaseAddress & 1)
113  {
116  }
117 
118  NtHeader = RtlImageNtHeader(BaseAddress);
119  if (NtHeader == NULL)
120  return NULL;
121 
123  return NULL;
124 
126  if (Va == 0)
127  return NULL;
128 
130 
131  if (MappedAsImage || Va < SWAPD(NtHeader->OptionalHeader.SizeOfHeaders))
132  return (PVOID)((ULONG_PTR)BaseAddress + Va);
133 
134  /* Image mapped as ordinary file, we must find raw pointer */
135  return RtlImageRvaToVa(NtHeader, BaseAddress, Va, NULL);
136 }
137 
139 {
140  if (fseek((FILE*)file, 0, 2) == -1)
141  return FALSE;
142  fsize->QuadPart = ftell((FILE*)file);
143  return TRUE;
144 }
145 
147 {
148  fclose(handle);
149  return TRUE;
150 }
151 
154  DWORD dwDesiredAccess,
155  DWORD dwShareMode,
156  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
157  DWORD dwCreationDisposition,
158  DWORD dwFlagsAndAttributes,
159  HANDLE hTemplateFile)
160 {
161  char buf[MAX_PATH];
162  HANDLE res;
163 
165  res = CreateFileA(buf, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
166  return res;
167 }
168 
170 {
171  FILE *f = (FILE*)file;
173  char *result;
174 
175  if (file == INVALID_HANDLE_VALUE)
176  return NULL;
177 
178  if (!GetFileSizeEx(file, &size))
179  return NULL;
180 
181  if (fseek(f, 0, 0) == -1)
182  return NULL;
183 
184  result = malloc(size.LowPart);
185  if (fread(result, 1, size.LowPart, f) != size.LowPart)
186  {
187  free(result);
188  return NULL;
189  }
190 
191  return result;
192 }
193 
195 {
196  free((void *)data);
197  return TRUE;
198 }
199 
201 {
202  LPSTR r = d;
203  while(*s && c)
204  {
205  *d++ = *s++;
206  c--;
207  }
208  return r;
209 }
210 
211 /* From Wine implementation over their unicode library */
212 INT
214  LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
215 {
216  int i;
217 
218  if (!src || !srclen || (!dst && dstlen))
219  {
221  return 0;
222  }
223 
224  if (srclen < 0) srclen = strlenW(src) + 1;
225 
226  if(!dstlen)
227  return srclen;
228 
229  for(i=0; i<srclen && i<dstlen; i++)
230  dst[i] = src[i] & 0xFF;
231 
232  if (used) *used = FALSE;
233 
234  return i;
235 }
236 
237 INT
239  LPWSTR dst, INT dstlen )
240 {
241  int i;
242 
243  if (!src || !srclen || (!dst && dstlen))
244  {
246  return 0;
247  }
248 
249  if (srclen < 0) srclen = strlen(src) + 1;
250 
251  if(!dstlen)
252  return srclen;
253 
254  for(i=0; i<srclen && i<dstlen; i++)
255  dst[i] = src[i];
256 
257  return i;
258 }
259 
260 /* In our case, the provided file path is the one we are looking for */
262 {
263  HANDLE ret = CreateFileW(file, 0, 0, NULL, 0, 0, NULL);
264  if(ret)
265  memcpy(out_buffer, file, (strlenW(file) + 1)*sizeof(WCHAR));
266 
267  return ret;
268 }
269 
270 /* printf with temp buffer allocation */
271 const char *wine_dbg_sprintf( const char *format, ... )
272 {
273  static const int max_size = 200;
274  static char buffer[256];
275  char *ret;
276  int len;
277  va_list valist;
278 
280  ret = buffer;
282  if (len == -1 || len >= max_size) ret[max_size-1] = 0;
283  va_end(valist);
284  return ret;
285 }
286 
287 /* default implementation of wine_dbgstr_an */
288 const char *wine_dbgstr_an( const char *str, int n )
289 {
290  static const char hex[16] = "0123456789abcdef";
291  char *dst, *res;
292  size_t size;
293  static char buffer[256];
294 
295  if (!((ULONG_PTR)str >> 16))
296  {
297  if (!str) return "(null)";
298  res = buffer;
299  sprintf( res, "#%04x", LOWORD(str) );
300  return res;
301  }
302  if (n == -1) n = strlen(str);
303  if (n < 0) n = 0;
304  size = 10 + min( 300, n * 4 );
305  dst = res = buffer;
306  *dst++ = '"';
307  while (n-- > 0 && dst <= res + size - 9)
308  {
309  unsigned char c = *str++;
310  switch (c)
311  {
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;
317  default:
318  if (c >= ' ' && c <= 126)
319  *dst++ = c;
320  else
321  {
322  *dst++ = '\\';
323  *dst++ = 'x';
324  *dst++ = hex[(c >> 4) & 0x0f];
325  *dst++ = hex[c & 0x0f];
326  }
327  }
328  }
329  *dst++ = '"';
330  if (n > 0)
331  {
332  *dst++ = '.';
333  *dst++ = '.';
334  *dst++ = '.';
335  }
336  *dst++ = 0;
337  return res;
338 }
339 
340 
341 /* default implementation of wine_dbgstr_wn */
342 const char *wine_dbgstr_wn( const WCHAR *str, int n )
343 {
344  char *dst, *res;
345  size_t size;
346  static char buffer[256];
347 
348  if (!((ULONG_PTR)str >> 16))
349  {
350  if (!str) return "(null)";
351  res = buffer;
352  sprintf( res, "#%04x", LOWORD(str) );
353  return res;
354  }
355  if (n == -1)
356  {
357  const WCHAR *end = str;
358  while (*end) end++;
359  n = end - str;
360  }
361  if (n < 0) n = 0;
362  size = 12 + min( 300, n * 5 );
363  dst = res = buffer;
364  *dst++ = 'L';
365  *dst++ = '"';
366  while (n-- > 0 && dst <= res + size - 10)
367  {
368  WCHAR c = *str++;
369  switch (c)
370  {
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;
376  default:
377  if (c >= ' ' && c <= 126)
378  *dst++ = c;
379  else
380  {
381  *dst++ = '\\';
382  sprintf(dst,"%04x",c);
383  dst+=4;
384  }
385  }
386  }
387  *dst++ = '"';
388  if (n > 0)
389  {
390  *dst++ = '.';
391  *dst++ = '.';
392  *dst++ = '.';
393  }
394  *dst++ = 0;
395  return res;
396 }
#define realloc
Definition: debug_ros.c:6
signed char * PCHAR
Definition: retypes.h:7
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define vsnprintf
Definition: tif_win32.c:406
const uint16_t * PCWSTR
Definition: typedefs.h:55
WCHAR * lstrcpynW(WCHAR *lpString1, const WCHAR *lpString2, int iMaxLength)
Definition: compat.c:16
#define TRUE
Definition: types.h:120
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]
Definition: ntddk_ex.h:178
WINE_UNICODE_INLINE unsigned int strlenW(const WCHAR *str)
Definition: unicode.h:212
#define SWAPW(x)
Definition: bytesex.h:8
#define WideCharToMultiByte
Definition: compat.h:101
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
uint16_t * PWSTR
Definition: typedefs.h:54
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
#define CP_ACP
Definition: compat.h:99
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define free
Definition: debug_ros.c:5
#define RtlImageRvaToVa
Definition: compat.h:458
GLdouble n
Definition: glext.h:7729
#define SWAPD(x)
Definition: bytesex.h:7
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
GLuint buffer
Definition: glext.h:5915
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS
Definition: ntddk_ex.h:187
GLuint GLuint end
Definition: gl.h:1545
BOOL(CALLBACK * PFIND_EXE_FILE_CALLBACKW)(HANDLE, PCWSTR, PVOID)
Definition: compat.h:781
PVOID __RtlImageDirectoryEntryToData(PVOID BaseAddress, BOOLEAN MappedAsImage, USHORT Directory, PULONG Size)
Definition: compat.c:102
DWORD PointerToRawData
Definition: pedump.c:290
char * LPSTR
Definition: xmlstorage.h:182
static const char hex[16]
Definition: profile.c:123
int32_t INT
Definition: typedefs.h:56
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
static HANDLE PIO_APC_ROUTINE PVOID PIO_STATUS_BLOCK ULONG PVOID ULONG PVOID out_buffer
Definition: file.c:100
uint32_t ULONG_PTR
Definition: typedefs.h:63
const char * wine_dbg_sprintf(const char *format,...)
Definition: compat.c:271
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89
#define sprintf(buf, format,...)
Definition: sprintf.c:55
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
INT __MultiByteToWideChar(UINT page, DWORD flags, LPCSTR src, INT srclen, LPWSTR dst, INT dstlen)
Definition: compat.c:238
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
#define va_end(ap)
Definition: acmsvcex.h:90
#define IMAGE_FIRST_SECTION(NtHeader)
Definition: ntimage.h:427
unsigned int BOOL
Definition: ntddk_ex.h:94
void * __MapViewOfFile(HANDLE file, DWORD d1, DWORD d2, DWORD d3, SIZE_T s)
Definition: compat.c:169
static INT max_size
Definition: history.c:51
_In_ BOOLEAN MappedAsImage
Definition: rtlfuncs.h:3718
const WCHAR * str
unsigned char BOOLEAN
void * __HeapAlloc(int heap, int flags, size_t size)
Definition: compat.c:3
smooth NULL
Definition: ftsmooth.c:416
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
INT __WideCharToMultiByte(UINT page, DWORD flags, LPCWSTR src, INT srclen, LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used)
Definition: compat.c:213
struct _IMAGE_DOS_HEADER * PIMAGE_DOS_HEADER
HANDLE __CreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
Definition: compat.c:152
char * va_list
Definition: acmsvcex.h:78
Definition: module.h:566
const char * LPCSTR
Definition: xmlstorage.h:183
void * PVOID
Definition: retypes.h:9
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
PIMAGE_NT_HEADERS __RtlImageNtHeader(void *data)
Definition: compat.c:34
#define PCHAR
Definition: match.c:90
#define IMAGE_NT_SIGNATURE
Definition: pedump.c:93
c used
Definition: write.c:2885
BOOL __UnmapViewOfFile(const void *data)
Definition: compat.c:194
GLfloat f
Definition: glext.h:7540
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
GLsizeiptr size
Definition: glext.h:5919
#define d
Definition: ke_i.h:81
BOOL __CloseHandle(HANDLE handle)
Definition: compat.c:146
const char * wine_dbgstr_an(const char *str, int n)
Definition: compat.c:288
__wchar_t WCHAR
Definition: xmlstorage.h:180
static DWORD LPDWORD LPCSTR DWORD srclen
Definition: directory.c:51
#define MAX_PATH
Definition: compat.h:26
const GLubyte * c
Definition: glext.h:8905
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLbitfield flags
Definition: glext.h:7161
#define GetFileSizeEx
Definition: compat.h:414
int ret
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
GLdouble s
Definition: gl.h:2039
const char * wine_dbgstr_wn(const WCHAR *str, int n)
Definition: compat.c:342
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
GLenum src
Definition: glext.h:6340
LPSTR __lstrcpynA(LPSTR d, LPCSTR s, int c)
Definition: compat.c:200
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
ULONG_PTR SIZE_T
Definition: typedefs.h:78
static DWORD dstlen
Definition: directory.c:51
unsigned short USHORT
Definition: pedump.c:61
void * __HeapReAlloc(int heap, DWORD d2, void *slab, SIZE_T newsize)
Definition: compat.c:11
PVOID __RtlImageRvaToVa(const IMAGE_NT_HEADERS *NtHeader, PVOID BaseAddress, ULONG Rva, PIMAGE_SECTION_HEADER *SectionHeader)
Definition: compat.c:74
Definition: services.c:325
GLenum GLenum dst
Definition: glext.h:6340
#define RtlImageRvaToSection
Definition: compat.h:459
unsigned int * PULONG
Definition: retypes.h:1
#define min(a, b)
Definition: monoChain.cc:55
#define va_start(ap, A)
Definition: acmsvcex.h:91
unsigned int UINT
Definition: ndis.h:50
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
#define RtlImageNtHeader
Definition: compat.h:457
#define CreateFileW
Definition: compat.h:400
GLuint res
Definition: glext.h:9613
#define c
Definition: ke_i.h:80
_Check_return_ _CRTIMP long __cdecl ftell(_Inout_ FILE *_File)
unsigned int ULONG
Definition: retypes.h:1
base for all directory entries
Definition: entries.h:138
#define ULONG_PTR
Definition: config.h:101
HANDLE __FindExecutableImageExW(PCWSTR file, PCWSTR path, PWSTR out_buffer, PFIND_EXE_FILE_CALLBACKW x, PVOID y)
Definition: compat.c:261
#define malloc
Definition: debug_ros.c:4
static __ms_va_list valist
Definition: printf.c:59
#define CreateFileA(a, b, c, d, e, f, g)
Definition: compat.h:399
WCHAR * LPWSTR
Definition: xmlstorage.h:184
PIMAGE_SECTION_HEADER __RtlImageRvaToSection(const IMAGE_NT_HEADERS *NtHeader, PVOID BaseAddress, ULONG Rva)
Definition: compat.c:49
BOOL __GetFileSizeEx(HANDLE file, PLARGE_INTEGER fsize)
Definition: compat.c:138
GLuint64EXT * result
Definition: glext.h:11304
#define memset(x, y, z)
Definition: compat.h:39
#define LOWORD(l)
Definition: pedump.c:82
_In_ LPCSTR lpFileName
Definition: winbase.h:3011
Definition: fci.c:126
GLint fsize
Definition: glext.h:9408