ReactOS  0.4.14-dev-49-gfb4591c
data.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: Support functions for dbghelp api test
5  * COPYRIGHT: Copyright 2017-2019 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include <windows.h>
9 #include <stdio.h>
10 #include <zlib.h>
11 
12 #include "wine/test.h"
13 
15 
16 static char szTempPath[MAX_PATH];
17 
18 static const char* tmpdir()
19 {
20  if (szTempPath[0] == '\0')
21  {
23  lstrcatA(szTempPath, "dbghelp_tst");
24  }
25  return szTempPath;
26 }
27 
28 static int extract_one(const char* filename, const char* resid)
29 {
31  HGLOBAL glob;
32  PVOID data, decompressed;
33  uLongf size, dstsize;
34  DWORD gccSize, dwErr;
35  HANDLE file;
36  int ret;
37  HRSRC rsrc = FindResourceA(mod, resid, MAKEINTRESOURCEA(RT_RCDATA));
38  ok(rsrc != 0, "Failed finding '%s' res\n", resid);
39  if (!rsrc)
40  return 0;
41 
42  size = SizeofResource(mod, rsrc);
43  glob = LoadResource(mod, rsrc);
44  ok(glob != NULL, "Failed loading '%s' res\n", resid);
45  if (!glob)
46  return 0;
47 
49 
50  dstsize = 1024 * 256;
51  decompressed = malloc(dstsize);
52 
53  if (uncompress(decompressed, &dstsize, data, size) != Z_OK)
54  {
55  ok(0, "uncompress failed for %s\n", resid);
56  free(decompressed);
57  return 0;
58  }
59 
60 
62  gccSize = size;
63  ret = WriteFile(file, decompressed, dstsize, &gccSize, NULL);
64  dwErr = GetLastError();
66  free(decompressed);
67  ok(ret, "WriteFile failed (%d)\n", dwErr);
68  return ret && dstsize == gccSize;
69 }
70 
71 
72 int extract_msvc_dll(char szFile[MAX_PATH], char szPath[MAX_PATH])
73 {
74  const char* dir = tmpdir();
76  ok(ret, "CreateDirectoryA failed(%d)\n", GetLastError());
77 
78  sprintf(szFile, "%s\\uffs.pdb", dir);
79  if (!extract_one(szFile, "msvc_uffs.pdb"))
80  return 0;
81 
82  sprintf(szFile, "%s\\uffs.dll", dir);
83  if (!extract_one(szFile, "msvc_uffs.dll"))
84  return 0;
85 
86  strcpy(szPath, dir);
87  return 1;
88 }
89 
91 {
92  char szFile[MAX_PATH];
93  BOOL ret;
94  const char* dir = tmpdir();
95 
96  sprintf(szFile, "%s\\uffs.pdb", dir);
97  ret = DeleteFileA(szFile);
98  ok(ret, "DeleteFileA failed(%d)\n", GetLastError());
99 
100  sprintf(szFile, "%s\\uffs.dll", dir);
101  ret = DeleteFileA(szFile);
102  ok(ret, "DeleteFileA failed(%d)\n", GetLastError());
104  ok(ret, "RemoveDirectoryA failed(%d)\n", GetLastError());
105 }
106 
107 int extract_gcc_dll(char szFile[MAX_PATH])
108 {
109  const char* dir = tmpdir();
111  ok(ret, "CreateDirectoryA failed(%d)\n", GetLastError());
112 
113  sprintf(szFile, "%s\\uffs.dll", dir);
114  if (!extract_one(szFile, "gcc_uffs.dll"))
115  return 0;
116 
117  return 1;
118 }
119 
121 {
122  char szFile[MAX_PATH];
123  BOOL ret;
124  const char* dir = tmpdir();
125 
126  sprintf(szFile, "%s\\uffs.dll", dir);
127  ret = DeleteFileA(szFile);
128  ok(ret, "DeleteFileA failed(%d)\n", GetLastError());
130  ok(ret, "RemoveDirectoryA failed(%d)\n", GetLastError());
131 }
132 
133 
134 #if 0
135 static int compress_one(const char* src, const char* dest)
136 {
137  DWORD size, size2, res;
138  FILE* file = fopen(src, "rb");
139  fseek(file, 0, SEEK_END);
140  size = ftell(file);
141  fseek(file, 0, SEEK_SET);
142 
143  Bytef* buffer, *buffer2;
145 
146  buffer = malloc(size);
147  res = fread(buffer, 1, size, file);
148 
149  fclose(file);
150 
151  if (res != size)
152  {
153  printf("Could not read file: 0x%x\n", dwErr);
154  free(buffer);
155  CloseHandle(file);
156  return 0;
157  }
158  size2 = size *2;
159  buffer2 = malloc(size2);
160  res = compress(buffer2, &size2, buffer, size);
161 
162  free(buffer);
163 
164  if (Z_OK != res)
165  {
166  free(buffer2);
167  return 0;
168  }
169 
170  file = fopen(dest, "wb");
171  res = fwrite(buffer2, 1, size2, file);
172  fclose(file);
173 
174  free(buffer2);
175 
176  return size2 == res;
177 }
178 
180 {
181  SetCurrentDirectoryA("R:/src/trunk/reactos/modules/rostests/apitests/dbghelp");
182  if (!compress_one("testdata/msvc_uffs.dll", "testdata/msvc_uffs.dll.compr"))
183  printf("msvc_uffs.dll failed\n");
184  if (!compress_one("testdata/msvc_uffs.pdb", "testdata/msvc_uffs.pdb.compr"))
185  printf("msvc_uffs.pdb failed\n");
186  if (!compress_one("testdata/gcc_uffs.dll", "testdata/gcc_uffs.dll.compr"))
187  printf("gcc_uffs.dll failed\n");
188 }
189 #endif
190 
191 #if 0
192 typedef struct _SYMBOLFILE_HEADER {
198 
199 typedef struct _ROSSYM_ENTRY {
200  ULONG Address;
205 
206 
207 static int is_metadata(const char* name)
208 {
209  size_t len = name ? strlen(name) : 0;
210  return len > 3 && name[0] == '_' && name[1] != '_' && name[len-1] == '_' && name[len-2] == '_';
211 };
212 
213 static void dump_rsym_internal(void* data)
214 {
216  PROSSYM_ENTRY Entries = (PROSSYM_ENTRY)((char *)data + RosSymHeader->SymbolsOffset);
217  size_t symbols = RosSymHeader->SymbolsLength / sizeof(ROSSYM_ENTRY);
218  size_t i;
219  char *Strings = (char *)data + RosSymHeader->StringsOffset;
220 
221  for (i = 0; i < symbols; i++)
222  {
224  if (!Entry->FileOffset)
225  {
226  if (Entry->SourceLine)
227  printf("ERR: SOURCELINE (%D) ", Entry->SourceLine);
228  if (is_metadata(Strings + Entry->FunctionOffset))
229  printf("metadata: %s: 0x%x\n", Strings + Entry->FunctionOffset, Entry->Address);
230  else
231  printf("0x%x: %s\n", Entry->Address, Strings + Entry->FunctionOffset);
232  }
233  else
234  {
235  printf("0x%x: %s (%s:%u)\n", Entry->Address,
236  Strings + Entry->FunctionOffset,
237  Strings + Entry->FileOffset,
238  Entry->SourceLine);
239  }
240  }
241 
242 }
243 
244 void dump_rsym(const char* filename)
245 {
246  char* data;
247  long size, res;
248  PIMAGE_FILE_HEADER PEFileHeader;
249  PIMAGE_OPTIONAL_HEADER PEOptHeader;
250  PIMAGE_SECTION_HEADER PESectionHeaders;
251  WORD i;
252 
253  FILE* f = fopen(filename, "rb");
254 
255  fseek(f, 0, SEEK_END);
256  size = ftell(f);
257  fseek(f, 0, SEEK_SET);
258 
259  data = malloc(size);
260  res = fread(data, 1, size, f);
261  fclose(f);
262 
263  PEFileHeader = (PIMAGE_FILE_HEADER)((char *)data + ((PIMAGE_DOS_HEADER)data)->e_lfanew + sizeof(ULONG));
264  PEOptHeader = (PIMAGE_OPTIONAL_HEADER)(PEFileHeader + 1);
265  PESectionHeaders = (PIMAGE_SECTION_HEADER)((char *)PEOptHeader + PEFileHeader->SizeOfOptionalHeader);
266 
267  for (i = 0; i < PEFileHeader->NumberOfSections; i++)
268  {
269  if (!strcmp((char *)PESectionHeaders[i].Name, ".rossym"))
270  {
271  dump_rsym_internal(data + PESectionHeaders[i].PointerToRawData);
272  break;
273  }
274  }
275  free(data);
276 }
277 
278 #endif
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
ULONG SourceLine
Definition: rossym.h:30
#define CloseHandle
Definition: compat.h:398
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: uncompr.c:86
IMAGE_DOS_HEADER __ImageBase
Definition: mstscax.cpp:17
void create_compressed_files()
static int extract_one(const char *filename, const char *resid)
Definition: data.c:28
#define free
Definition: debug_ros.c:5
HRSRC WINAPI FindResourceA(HMODULE hModule, LPCSTR name, LPCSTR type)
Definition: res.c:155
static const char * tmpdir()
Definition: data.c:18
ULONG StringsOffset
Definition: rsym.h:34
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
LPVOID WINAPI LockResource(HGLOBAL handle)
Definition: res.c:550
GLuint buffer
Definition: glext.h:5915
#define MAKEINTRESOURCEA(i)
Definition: winuser.h:581
Definition: rossym.h:26
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
const char * filename
Definition: ioapi.h:135
struct _SYMBOLFILE_HEADER * PSYMBOLFILE_HEADER
struct _ROSSYM_ENTRY * PROSSYM_ENTRY
#define sprintf(buf, format,...)
Definition: sprintf.c:55
Byte FAR Bytef
Definition: zconf.h:400
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
ULONG FileOffset
Definition: rossym.h:29
_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)
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GENERIC_WRITE
Definition: nt_native.h:90
#define Z_OK
Definition: zlib.h:177
HGLOBAL WINAPI LoadResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:532
BOOL WINAPI CreateDirectoryA(IN LPCSTR lpPathName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: dir.c:37
smooth NULL
Definition: ftsmooth.c:416
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
struct _IMAGE_DOS_HEADER * PIMAGE_DOS_HEADER
struct _IMAGE_OPTIONAL_HEADER * PIMAGE_OPTIONAL_HEADER
BOOL WINAPI DeleteFileA(IN LPCSTR lpFileName)
Definition: delete.c:24
WORD SizeOfOptionalHeader
Definition: ntddk_ex.h:127
unsigned int dir
Definition: maze.c:112
struct _SYMBOLFILE_HEADER SYMBOLFILE_HEADER
DWORD dwErr
Definition: service.c:36
#define SEEK_SET
Definition: jmemansi.c:26
LPSTR WINAPI lstrcatA(LPSTR lpString1, LPCSTR lpString2)
Definition: lstring.c:123
struct _ROSSYM_ENTRY ROSSYM_ENTRY
GLfloat f
Definition: glext.h:7540
GLsizeiptr size
Definition: glext.h:5919
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:68
struct _IMAGE_FILE_HEADER * PIMAGE_FILE_HEADER
#define MAX_PATH
Definition: compat.h:26
const char file[]
Definition: icontest.c:11
static char szTempPath[MAX_PATH]
Definition: data.c:16
unsigned short WORD
Definition: ntddk_ex.h:93
#define for
Definition: utility.h:88
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR Strings[]
Definition: reg.c:35
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
void dump_rsym(const char *filename)
#define RT_RCDATA
Definition: pedump.c:372
int ret
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
BOOL WINAPI SetCurrentDirectoryA(IN LPCSTR lpPathName)
Definition: path.c:2205
GLenum GLsizei len
Definition: glext.h:6722
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
GLenum src
Definition: glext.h:6340
void cleanup_msvc_dll()
Definition: data.c:90
HANDLE HMODULE
Definition: typedefs.h:75
uLong FAR uLongf
Definition: zconf.h:405
ULONG SymbolsOffset
Definition: rsym.h:32
struct _IMAGE_SECTION_HEADER * PIMAGE_SECTION_HEADER
#define CREATE_ALWAYS
Definition: disk.h:72
ULONG StringsLength
Definition: rsym.h:35
DWORD WINAPI GetTempPathA(IN DWORD nBufferLength, OUT LPSTR lpBuffer)
Definition: path.c:2053
LIST_ENTRY Entries[5]
Definition: ExDoubleList.c:8
LPCWSTR szPath
Definition: env.c:35
#define ok(value,...)
Definition: atltest.h:57
BOOL WINAPI RemoveDirectoryA(IN LPCSTR lpPathName)
Definition: dir.c:714
char ** glob(const char *v)
Definition: fake.c:36
int extract_gcc_dll(char szFile[MAX_PATH])
Definition: data.c:107
DWORD WINAPI SizeofResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:568
BYTE Name[IMAGE_SIZEOF_SHORT_NAME]
Definition: pedump.c:281
ULONG SymbolsLength
Definition: rsym.h:33
int extract_msvc_dll(char szFile[MAX_PATH], char szPath[MAX_PATH])
Definition: data.c:72
Definition: name.c:36
void cleanup_gcc_dll()
Definition: data.c:120
GLuint res
Definition: glext.h:9613
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
_Check_return_ _CRTIMP long __cdecl ftell(_Inout_ FILE *_File)
unsigned int ULONG
Definition: retypes.h:1
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define SEEK_END
Definition: cabinet.c:27
#define malloc
Definition: debug_ros.c:4
static char * dest
Definition: rtl.c:135
#define CreateFileA(a, b, c, d, e, f, g)
Definition: compat.h:399
ULONG FunctionOffset
Definition: rossym.h:28
ULONG_PTR Address
Definition: rossym.h:27
base of all file and directory entries
Definition: entries.h:82
#define printf
Definition: config.h:203
static int mod
Definition: i386-dis.c:1273
Definition: fci.c:126