mshtml: Added DIID_DispHTMLDocument to QueryInterface.
[wine/testsucceed.git] / dlls / dbghelp / path.c
blobbd974376ae9e5e247b549bb7db51c7ac71302ec8
1 /*
2 * File path.c - managing path in debugging environments
4 * Copyright (C) 2004, Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include "dbghelp_private.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
34 static inline BOOL is_sep(char ch) {return ch == '/' || ch == '\\';}
35 static inline BOOL is_sepW(WCHAR ch) {return ch == '/' || ch == '\\';}
37 static inline const char* file_name(const char* str)
39 const char* p;
41 for (p = str + strlen(str) - 1; p >= str && !is_sep(*p); p--);
42 return p + 1;
45 static inline const WCHAR* file_nameW(const WCHAR* str)
47 const WCHAR* p;
49 for (p = str + strlenW(str) - 1; p >= str && !is_sepW(*p); p--);
50 return p + 1;
53 /******************************************************************
54 * FindDebugInfoFile (DBGHELP.@)
57 HANDLE WINAPI FindDebugInfoFile(PCSTR FileName, PCSTR SymbolPath, PSTR DebugFilePath)
59 HANDLE h;
61 h = CreateFileA(DebugFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
62 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
63 if (h == INVALID_HANDLE_VALUE)
65 if (!SearchPathA(SymbolPath, file_name(FileName), NULL, MAX_PATH, DebugFilePath, NULL))
66 return NULL;
67 h = CreateFileA(DebugFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
68 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
70 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
73 /******************************************************************
74 * FindDebugInfoFileEx (DBGHELP.@)
77 HANDLE WINAPI FindDebugInfoFileEx(PCSTR FileName, PCSTR SymbolPath,
78 PSTR DebugFilePath,
79 PFIND_DEBUG_FILE_CALLBACK Callback,
80 PVOID CallerData)
82 FIXME("(%s %s %p %p %p): stub\n",
83 FileName, SymbolPath, DebugFilePath, Callback, CallerData);
84 return NULL;
87 /******************************************************************
88 * FindExecutableImageExW (DBGHELP.@)
91 HANDLE WINAPI FindExecutableImageExW(PCWSTR FileName, PCWSTR SymbolPath, PWSTR ImageFilePath,
92 PFIND_EXE_FILE_CALLBACKW Callback, void* user)
94 HANDLE h;
96 if (Callback) FIXME("Unsupported callback yet\n");
97 if (!SearchPathW(SymbolPath, FileName, NULL, MAX_PATH, ImageFilePath, NULL))
98 return NULL;
99 h = CreateFileW(ImageFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
100 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
101 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
104 /******************************************************************
105 * FindExecutableImageEx (DBGHELP.@)
108 HANDLE WINAPI FindExecutableImageEx(PCSTR FileName, PCSTR SymbolPath, PSTR ImageFilePath,
109 PFIND_EXE_FILE_CALLBACK Callback, void* user)
111 HANDLE h;
113 if (Callback) FIXME("Unsupported callback yet\n");
114 if (!SearchPathA(SymbolPath, FileName, NULL, MAX_PATH, ImageFilePath, NULL))
115 return NULL;
116 h = CreateFileA(ImageFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
117 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
118 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
121 /******************************************************************
122 * FindExecutableImage (DBGHELP.@)
125 HANDLE WINAPI FindExecutableImage(PCSTR FileName, PCSTR SymbolPath, PSTR ImageFilePath)
127 return FindExecutableImageEx(FileName, SymbolPath, ImageFilePath, NULL, NULL);
130 /***********************************************************************
131 * MakeSureDirectoryPathExists (DBGHELP.@)
133 BOOL WINAPI MakeSureDirectoryPathExists(LPCSTR DirPath)
135 char path[MAX_PATH];
136 const char *p = DirPath;
137 int n;
139 if (p[0] && p[1] == ':') p += 2;
140 while (*p == '\\') p++; /* skip drive root */
141 while ((p = strchr(p, '\\')) != NULL)
143 n = p - DirPath + 1;
144 memcpy(path, DirPath, n);
145 path[n] = '\0';
146 if( !CreateDirectoryA(path, NULL) &&
147 (GetLastError() != ERROR_ALREADY_EXISTS))
148 return FALSE;
149 p++;
151 if (GetLastError() == ERROR_ALREADY_EXISTS)
152 SetLastError(ERROR_SUCCESS);
154 return TRUE;
157 /******************************************************************
158 * SymMatchFileNameW (DBGHELP.@)
161 BOOL WINAPI SymMatchFileNameW(WCHAR* file, WCHAR* match,
162 WCHAR** filestop, WCHAR** matchstop)
164 WCHAR* fptr;
165 WCHAR* mptr;
167 TRACE("(%s %s %p %p)\n",
168 debugstr_w(file), debugstr_w(match), filestop, matchstop);
170 fptr = file + strlenW(file) - 1;
171 mptr = match + strlenW(match) - 1;
173 while (fptr >= file && mptr >= match)
175 if (toupperW(*fptr) != toupperW(*mptr) && !(is_sepW(*fptr) && is_sepW(*mptr)))
176 break;
177 fptr--; mptr--;
179 if (filestop) *filestop = fptr;
180 if (matchstop) *matchstop = mptr;
182 return mptr == match - 1;
185 /******************************************************************
186 * SymMatchFileName (DBGHELP.@)
189 BOOL WINAPI SymMatchFileName(char* file, char* match,
190 char** filestop, char** matchstop)
192 char* fptr;
193 char* mptr;
195 TRACE("(%s %s %p %p)\n", file, match, filestop, matchstop);
197 fptr = file + strlen(file) - 1;
198 mptr = match + strlen(match) - 1;
200 while (fptr >= file && mptr >= match)
202 if (toupper(*fptr) != toupper(*mptr) && !(is_sep(*fptr) && is_sep(*mptr)))
203 break;
204 fptr--; mptr--;
206 if (filestop) *filestop = fptr;
207 if (matchstop) *matchstop = mptr;
209 return mptr == match - 1;
212 static BOOL do_searchW(const WCHAR* file, WCHAR* buffer, BOOL recurse,
213 PENUMDIRTREE_CALLBACKW cb, void* user)
215 HANDLE h;
216 WIN32_FIND_DATAW fd;
217 unsigned pos;
218 BOOL found = FALSE;
219 static const WCHAR S_AllW[] = {'*','.','*','\0'};
220 static const WCHAR S_DotW[] = {'.','\0'};
221 static const WCHAR S_DotDotW[] = {'.','\0'};
223 pos = strlenW(buffer);
224 if (buffer[pos - 1] != '\\') buffer[pos++] = '\\';
225 strcpyW(buffer + pos, S_AllW);
226 if ((h = FindFirstFileW(buffer, &fd)) == INVALID_HANDLE_VALUE)
227 return FALSE;
228 /* doc doesn't specify how the tree is enumerated...
229 * doing a depth first based on, but may be wrong
233 if (!strcmpW(fd.cFileName, S_DotW) || !strcmpW(fd.cFileName, S_DotDotW)) continue;
235 strcpyW(buffer + pos, fd.cFileName);
236 if (recurse && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
237 found = do_searchW(file, buffer, TRUE, cb, user);
238 else if (SymMatchFileNameW(buffer, (WCHAR*)file, NULL, NULL))
240 if (!cb || cb(buffer, user)) found = TRUE;
242 } while (!found && FindNextFileW(h, &fd));
243 if (!found) buffer[--pos] = '\0';
244 FindClose(h);
246 return found;
249 /***********************************************************************
250 * SearchTreeForFileW (DBGHELP.@)
252 BOOL WINAPI SearchTreeForFileW(PCWSTR root, PCWSTR file, PWSTR buffer)
254 TRACE("(%s, %s, %p)\n",
255 debugstr_w(root), debugstr_w(file), buffer);
256 strcpyW(buffer, root);
257 return do_searchW(file, buffer, TRUE, NULL, NULL);
260 /***********************************************************************
261 * SearchTreeForFile (DBGHELP.@)
263 BOOL WINAPI SearchTreeForFile(PCSTR root, PCSTR file, PSTR buffer)
265 WCHAR rootW[MAX_PATH];
266 WCHAR fileW[MAX_PATH];
267 WCHAR bufferW[MAX_PATH];
268 BOOL ret;
270 MultiByteToWideChar(CP_ACP, 0, root, -1, rootW, MAX_PATH);
271 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, MAX_PATH);
272 ret = SearchTreeForFileW(rootW, fileW, bufferW);
273 if (ret)
274 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
275 return ret;
278 /******************************************************************
279 * EnumDirTreeW (DBGHELP.@)
283 BOOL WINAPI EnumDirTreeW(HANDLE hProcess, PCWSTR root, PCWSTR file,
284 LPWSTR buffer, PENUMDIRTREE_CALLBACKW cb, PVOID user)
286 TRACE("(%p %s %s %p %p %p)\n",
287 hProcess, debugstr_w(root), debugstr_w(file), buffer, cb, user);
289 strcpyW(buffer, root);
290 return do_searchW(file, buffer, TRUE, cb, user);
293 /******************************************************************
294 * EnumDirTree (DBGHELP.@)
298 struct enum_dir_treeWA
300 PENUMDIRTREE_CALLBACK cb;
301 void* user;
302 char name[MAX_PATH];
305 static BOOL CALLBACK enum_dir_treeWA(LPCWSTR name, PVOID user)
307 struct enum_dir_treeWA* edt = user;
309 WideCharToMultiByte(CP_ACP, 0, name, -1, edt->name, MAX_PATH, NULL, NULL);
310 return edt->cb(edt->name, edt->user);
313 BOOL WINAPI EnumDirTree(HANDLE hProcess, PCSTR root, PCSTR file,
314 LPSTR buffer, PENUMDIRTREE_CALLBACK cb, PVOID user)
316 WCHAR rootW[MAX_PATH];
317 WCHAR fileW[MAX_PATH];
318 WCHAR bufferW[MAX_PATH];
319 struct enum_dir_treeWA edt;
320 BOOL ret;
322 edt.cb = cb;
323 edt.user = user;
324 MultiByteToWideChar(CP_ACP, 0, root, -1, rootW, MAX_PATH);
325 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, MAX_PATH);
326 if ((ret = EnumDirTreeW(hProcess, rootW, fileW, bufferW, enum_dir_treeWA, &edt)))
327 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
328 return ret;
331 struct sffip
333 enum module_type kind;
334 /* pe: id -> DWORD:timestamp
335 * two -> size of image (from PE header)
336 * pdb: id -> PDB signature
337 * I think either DWORD:timestamp or GUID:guid depending on PDB version
338 * two -> PDB age ???
339 * elf: id -> DWORD:CRC 32 of ELF image (Wine only)
341 PVOID id;
342 DWORD two;
343 DWORD three;
344 DWORD flags;
345 PFINDFILEINPATHCALLBACKW cb;
346 void* user;
349 /* checks that buffer (as found by matching the name) matches the info
350 * (information is based on file type)
351 * returns TRUE when file is found, FALSE to continue searching
352 * (NB this is the opposite conventions as for SymFindFileInPathProc)
354 static BOOL CALLBACK sffip_cb(LPCWSTR buffer, void* user)
356 struct sffip* s = (struct sffip*)user;
357 DWORD size, checksum;
359 /* FIXME: should check that id/two/three match the file pointed
360 * by buffer
362 switch (s->kind)
364 case DMT_PE:
366 HANDLE hFile, hMap;
367 void* mapping;
368 DWORD timestamp;
370 timestamp = ~(DWORD_PTR)s->id;
371 size = ~s->two;
372 hFile = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ, NULL,
373 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
374 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
375 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
377 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
379 IMAGE_NT_HEADERS* nth = RtlImageNtHeader(mapping);
380 timestamp = nth->FileHeader.TimeDateStamp;
381 size = nth->OptionalHeader.SizeOfImage;
382 UnmapViewOfFile(mapping);
384 CloseHandle(hMap);
386 CloseHandle(hFile);
387 if (timestamp != (DWORD_PTR)s->id || size != s->two)
389 WARN("Found %s, but wrong size or timestamp\n", debugstr_w(buffer));
390 return FALSE;
393 break;
394 case DMT_ELF:
395 if (elf_fetch_file_info(buffer, 0, &size, &checksum))
397 if (checksum != (DWORD_PTR)s->id)
399 WARN("Found %s, but wrong checksums: %08x %08lx\n",
400 debugstr_w(buffer), checksum, (DWORD_PTR)s->id);
401 return FALSE;
404 else
406 WARN("Couldn't read %s\n", debugstr_w(buffer));
407 return FALSE;
409 break;
410 case DMT_PDB:
412 struct pdb_lookup pdb_lookup;
413 char fn[MAX_PATH];
415 WideCharToMultiByte(CP_ACP, 0, buffer, -1, fn, MAX_PATH, NULL, NULL);
416 pdb_lookup.filename = fn;
418 if (!pdb_fetch_file_info(&pdb_lookup)) return FALSE;
419 switch (pdb_lookup.kind)
421 case PDB_JG:
422 if (s->flags & SSRVOPT_GUIDPTR)
424 WARN("Found %s, but wrong PDB version\n", debugstr_w(buffer));
425 return FALSE;
427 if (pdb_lookup.u.jg.timestamp != (DWORD_PTR)s->id)
429 WARN("Found %s, but wrong signature: %08x %08lx\n",
430 debugstr_w(buffer), pdb_lookup.u.jg.timestamp, (DWORD_PTR)s->id);
431 return FALSE;
433 break;
434 case PDB_DS:
435 if (!(s->flags & SSRVOPT_GUIDPTR))
437 WARN("Found %s, but wrong PDB version\n", debugstr_w(buffer));
438 return FALSE;
440 if (memcmp(&pdb_lookup.u.ds.guid, (GUID*)s->id, sizeof(GUID)))
442 WARN("Found %s, but wrong GUID: %s %s\n",
443 debugstr_w(buffer), debugstr_guid(&pdb_lookup.u.ds.guid),
444 debugstr_guid((GUID*)s->id));
445 return FALSE;
447 break;
449 if (pdb_lookup.age != s->two)
451 WARN("Found %s, but wrong age: %08x %08x\n",
452 debugstr_w(buffer), pdb_lookup.age, s->two);
453 return FALSE;
456 break;
457 default:
458 FIXME("What the heck??\n");
459 return FALSE;
461 /* yes, EnumDirTree/do_search and SymFindFileInPath callbacks use the opposite
462 * convention to stop/continue enumeration. sigh.
464 return !(s->cb)((WCHAR*)buffer, s->user);
467 /******************************************************************
468 * SymFindFileInPathW (DBGHELP.@)
471 BOOL WINAPI SymFindFileInPathW(HANDLE hProcess, PCWSTR searchPath, PCWSTR full_path,
472 PVOID id, DWORD two, DWORD three, DWORD flags,
473 LPWSTR buffer, PFINDFILEINPATHCALLBACKW cb,
474 PVOID user)
476 struct sffip s;
477 struct process* pcs = process_find_by_handle(hProcess);
478 WCHAR tmp[MAX_PATH];
479 WCHAR* ptr;
480 const WCHAR* filename;
482 TRACE("(%p %s %s %p %08x %08x %08x %p %p %p)\n",
483 hProcess, debugstr_w(searchPath), debugstr_w(full_path),
484 id, two, three, flags, buffer, cb, user);
486 if (!pcs) return FALSE;
487 if (!searchPath) searchPath = pcs->search_path;
489 s.id = id;
490 s.two = two;
491 s.three = three;
492 s.flags = flags;
493 s.cb = cb;
494 s.user = user;
496 filename = file_nameW(full_path);
497 s.kind = module_get_type_by_name(filename);
499 /* first check full path to file */
500 if (sffip_cb(full_path, &s))
502 strcpyW(buffer, full_path);
503 return TRUE;
506 while (searchPath)
508 ptr = strchrW(searchPath, ';');
509 if (ptr)
511 memcpy(tmp, searchPath, (ptr - searchPath) * sizeof(WCHAR));
512 tmp[ptr - searchPath] = 0;
513 searchPath = ptr + 1;
515 else
517 strcpyW(tmp, searchPath);
518 searchPath = NULL;
520 if (do_searchW(filename, tmp, FALSE, sffip_cb, &s))
522 strcpyW(buffer, tmp);
523 return TRUE;
526 return FALSE;
529 /******************************************************************
530 * SymFindFileInPath (DBGHELP.@)
533 BOOL WINAPI SymFindFileInPath(HANDLE hProcess, PCSTR searchPath, PCSTR full_path,
534 PVOID id, DWORD two, DWORD three, DWORD flags,
535 LPSTR buffer, PFINDFILEINPATHCALLBACK cb,
536 PVOID user)
538 WCHAR searchPathW[MAX_PATH];
539 WCHAR full_pathW[MAX_PATH];
540 WCHAR bufferW[MAX_PATH];
541 struct enum_dir_treeWA edt;
542 BOOL ret;
544 /* a PFINDFILEINPATHCALLBACK and a PENUMDIRTREE_CALLBACK have actually the
545 * same signature & semantics, hence we can reuse the EnumDirTree W->A
546 * conversion helper
548 edt.cb = cb;
549 edt.user = user;
550 if (searchPath)
551 MultiByteToWideChar(CP_ACP, 0, searchPath, -1, searchPathW, MAX_PATH);
552 MultiByteToWideChar(CP_ACP, 0, full_path, -1, full_pathW, MAX_PATH);
553 if ((ret = SymFindFileInPathW(hProcess, searchPath ? searchPathW : NULL, full_pathW,
554 id, two, three, flags,
555 bufferW, enum_dir_treeWA, &edt)))
556 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
557 return ret;