Modify VarDiv() and VarSub() to use the existing VarDecSub() and
[wine/wine64.git] / dlls / imagehlp / access.c
blob6a17238164a7028a37f5daa333268f9d5d27b701
1 /*
2 * IMAGEHLP library
4 * Copyright 1998 Patrik Stridvall
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
22 #include <string.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnt.h"
26 #include "winreg.h"
27 #include "winternl.h"
28 #include "winerror.h"
29 #include "wine/debug.h"
30 #include "imagehlp.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
34 /***********************************************************************
35 * Data
38 static PLOADED_IMAGE IMAGEHLP_pFirstLoadedImage=NULL;
39 static PLOADED_IMAGE IMAGEHLP_pLastLoadedImage=NULL;
41 static LOADED_IMAGE IMAGEHLP_EmptyLoadedImage = {
42 NULL, /* ModuleName */
43 0, /* hFile */
44 NULL, /* MappedAddress */
45 NULL, /* FileHeader */
46 NULL, /* LastRvaSection */
47 0, /* NumberOfSections */
48 NULL, /* Sections */
49 1, /* Characteristics */
50 FALSE, /* fSystemImage */
51 FALSE, /* fDOSImage */
52 { &IMAGEHLP_EmptyLoadedImage.Links, &IMAGEHLP_EmptyLoadedImage.Links }, /* Links */
53 148, /* SizeOfImage; */
56 extern HANDLE IMAGEHLP_hHeap;
58 /***********************************************************************
59 * GetImageConfigInformation (IMAGEHLP.@)
61 BOOL WINAPI GetImageConfigInformation(
62 PLOADED_IMAGE LoadedImage,
63 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
65 FIXME("(%p, %p): stub\n",
66 LoadedImage, ImageConfigInformation
68 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
69 return FALSE;
72 /***********************************************************************
73 * GetImageUnusedHeaderBytes (IMAGEHLP.@)
75 DWORD WINAPI GetImageUnusedHeaderBytes(
76 PLOADED_IMAGE LoadedImage,
77 LPDWORD SizeUnusedHeaderBytes)
79 FIXME("(%p, %p): stub\n",
80 LoadedImage, SizeUnusedHeaderBytes
82 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
83 return 0;
86 /***********************************************************************
87 * ImageLoad (IMAGEHLP.@)
89 PLOADED_IMAGE WINAPI ImageLoad(LPSTR DllName, LPSTR DllPath)
91 PLOADED_IMAGE pLoadedImage;
93 FIXME("(%s, %s): stub\n", DllName, DllPath);
95 pLoadedImage = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(LOADED_IMAGE));
96 if (pLoadedImage)
97 pLoadedImage->FileHeader = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(IMAGE_NT_HEADERS));
99 return pLoadedImage;
102 /***********************************************************************
103 * ImageUnload (IMAGEHLP.@)
105 BOOL WINAPI ImageUnload(PLOADED_IMAGE pLoadedImage)
107 LIST_ENTRY *pCurrent, *pFind;
109 TRACE("(%p)\n", pLoadedImage);
111 if(!IMAGEHLP_pFirstLoadedImage || !pLoadedImage)
113 /* No image loaded or null pointer */
114 SetLastError(ERROR_INVALID_PARAMETER);
115 return FALSE;
118 pFind=&pLoadedImage->Links;
119 pCurrent=&IMAGEHLP_pFirstLoadedImage->Links;
120 while((pCurrent != pFind) &&
121 (pCurrent != NULL))
122 pCurrent = pCurrent->Flink;
123 if(!pCurrent)
125 /* Not found */
126 SetLastError(ERROR_INVALID_PARAMETER);
127 return FALSE;
130 if(pCurrent->Blink)
131 pCurrent->Blink->Flink = pCurrent->Flink;
132 else
133 IMAGEHLP_pFirstLoadedImage = pCurrent->Flink?CONTAINING_RECORD(
134 pCurrent->Flink, LOADED_IMAGE, Links):NULL;
136 if(pCurrent->Flink)
137 pCurrent->Flink->Blink = pCurrent->Blink;
138 else
139 IMAGEHLP_pLastLoadedImage = pCurrent->Blink?CONTAINING_RECORD(
140 pCurrent->Blink, LOADED_IMAGE, Links):NULL;
142 return FALSE;
145 /***********************************************************************
146 * MapAndLoad (IMAGEHLP.@)
148 BOOL WINAPI MapAndLoad(
149 LPSTR pszImageName, LPSTR pszDllPath, PLOADED_IMAGE pLoadedImage,
150 BOOL bDotDll, BOOL bReadOnly)
152 CHAR szFileName[MAX_PATH];
153 HANDLE hFile = NULL;
154 HANDLE hFileMapping = NULL;
155 HMODULE hModule = NULL;
156 PIMAGE_NT_HEADERS pNtHeader = NULL;
158 TRACE("(%s, %s, %p, %d, %d)\n", pszImageName, pszDllPath, pLoadedImage,
159 bDotDll, bReadOnly);
161 /* PathCombine(&szFileName, pszDllPath, pszImageName); */
162 /* PathRenameExtension(&szFileName, bDotDll?:"dll":"exe"); */
164 /* FIXME: Check if the file already loaded (use IMAGEHLP_pFirstLoadedImage) */
165 if(!(hFile = CreateFileA(
166 szFileName, GENERIC_READ, 1, /* FIXME: FILE_SHARE_READ not defined */
167 NULL, OPEN_EXISTING, 0, NULL)))
169 SetLastError(ERROR_FILE_NOT_FOUND);
170 goto Error;
173 if(!(hFileMapping = CreateFileMappingA(
174 hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL)))
176 DWORD dwLastError = GetLastError();
177 WARN("CreateFileMapping: Error = %ld\n", dwLastError);
178 SetLastError(dwLastError);
179 goto Error;
181 CloseHandle(hFile);
182 hFile = NULL;
184 if(!(hModule = (HMODULE) MapViewOfFile(
185 hFileMapping, FILE_MAP_READ, 0, 0, 0)))
187 DWORD dwLastError = GetLastError();
188 WARN("MapViewOfFile: Error = %ld\n", dwLastError);
189 SetLastError(dwLastError);
190 goto Error;
193 CloseHandle(hFileMapping);
194 hFileMapping=NULL;
196 pLoadedImage = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(LOADED_IMAGE));
198 pNtHeader = RtlImageNtHeader(hModule);
200 pLoadedImage->ModuleName = HeapAlloc(IMAGEHLP_hHeap, 0, strlen(pszDllPath)+1); /* FIXME: Correct? */
201 strcpy( pLoadedImage->ModuleName, pszDllPath );
202 pLoadedImage->hFile = hFile;
203 pLoadedImage->MappedAddress = (PUCHAR) hModule;
204 pLoadedImage->FileHeader = pNtHeader;
205 pLoadedImage->Sections = (PIMAGE_SECTION_HEADER)
206 ((LPBYTE) &pNtHeader->OptionalHeader +
207 pNtHeader->FileHeader.SizeOfOptionalHeader);
208 pLoadedImage->NumberOfSections =
209 pNtHeader->FileHeader.NumberOfSections;
210 pLoadedImage->SizeOfImage =
211 pNtHeader->OptionalHeader.SizeOfImage;
212 pLoadedImage->Characteristics =
213 pNtHeader->FileHeader.Characteristics;
214 pLoadedImage->LastRvaSection = pLoadedImage->Sections;
216 pLoadedImage->fSystemImage = FALSE; /* FIXME */
217 pLoadedImage->fDOSImage = FALSE; /* FIXME */
219 /* FIXME: Make thread safe */
220 pLoadedImage->Links.Flink = NULL;
221 pLoadedImage->Links.Blink = &IMAGEHLP_pLastLoadedImage->Links;
222 if(IMAGEHLP_pLastLoadedImage)
223 IMAGEHLP_pLastLoadedImage->Links.Flink = &pLoadedImage->Links;
224 IMAGEHLP_pLastLoadedImage = pLoadedImage;
225 if(!IMAGEHLP_pFirstLoadedImage)
226 IMAGEHLP_pFirstLoadedImage = pLoadedImage;
228 return TRUE;
230 Error:
231 if(hModule)
232 UnmapViewOfFile((PVOID) hModule);
233 if(hFileMapping)
234 CloseHandle(hFileMapping);
235 if(hFile)
236 CloseHandle(hFile);
237 return FALSE;
240 /***********************************************************************
241 * SetImageConfigInformation (IMAGEHLP.@)
243 BOOL WINAPI SetImageConfigInformation(
244 PLOADED_IMAGE LoadedImage,
245 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
247 FIXME("(%p, %p): stub\n",
248 LoadedImage, ImageConfigInformation
250 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
251 return FALSE;
254 /***********************************************************************
255 * UnMapAndLoad (IMAGEHLP.@)
257 BOOL WINAPI UnMapAndLoad(PLOADED_IMAGE LoadedImage)
259 FIXME("(%p): stub\n", LoadedImage);
260 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
261 return FALSE;