dsound: Some cosmetic changes.
[wine] / dlls / imagehlp / access.c
1 /*
2  *      IMAGEHLP library
3  *
4  *      Copyright 1998  Patrik Stridvall
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnt.h"
26 #include "winternl.h"
27 #include "winerror.h"
28 #include "wine/debug.h"
29 #include "imagehlp.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
32
33 /***********************************************************************
34  *           Data
35  */
36
37 static PLOADED_IMAGE IMAGEHLP_pFirstLoadedImage=NULL;
38 static PLOADED_IMAGE IMAGEHLP_pLastLoadedImage=NULL;
39
40 static LOADED_IMAGE IMAGEHLP_EmptyLoadedImage = {
41   NULL,       /* ModuleName */
42   0,          /* hFile */
43   NULL,       /* MappedAddress */
44   NULL,       /* FileHeader */
45   NULL,       /* LastRvaSection */
46   0,          /* NumberOfSections */
47   NULL,       /* Sections */
48   1,          /* Characteristics */
49   FALSE,      /* fSystemImage */
50   FALSE,      /* fDOSImage */
51   { &IMAGEHLP_EmptyLoadedImage.Links, &IMAGEHLP_EmptyLoadedImage.Links }, /* Links */
52   148,        /* SizeOfImage; */
53 };
54
55 extern HANDLE IMAGEHLP_hHeap;
56
57 /***********************************************************************
58  *              GetImageConfigInformation (IMAGEHLP.@)
59  */
60 BOOL WINAPI GetImageConfigInformation(
61   PLOADED_IMAGE LoadedImage,
62   PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
63 {
64   FIXME("(%p, %p): stub\n",
65     LoadedImage, ImageConfigInformation
66   );
67   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
68   return FALSE;
69 }
70
71 /***********************************************************************
72  *              GetImageUnusedHeaderBytes (IMAGEHLP.@)
73  */
74 DWORD WINAPI GetImageUnusedHeaderBytes(
75   PLOADED_IMAGE LoadedImage,
76   LPDWORD SizeUnusedHeaderBytes)
77 {
78   FIXME("(%p, %p): stub\n",
79     LoadedImage, SizeUnusedHeaderBytes
80   );
81   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
82   return 0;
83 }
84
85 /***********************************************************************
86  *              ImageLoad (IMAGEHLP.@)
87  */
88 PLOADED_IMAGE WINAPI ImageLoad(LPSTR DllName, LPSTR DllPath)
89 {
90   PLOADED_IMAGE pLoadedImage;
91
92   FIXME("(%s, %s): stub\n", DllName, DllPath);
93           
94   pLoadedImage = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(LOADED_IMAGE));
95   if (pLoadedImage)
96     pLoadedImage->FileHeader = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(IMAGE_NT_HEADERS));
97   
98   return pLoadedImage;
99 }
100
101 /***********************************************************************
102  *              ImageUnload (IMAGEHLP.@)
103  */
104 BOOL WINAPI ImageUnload(PLOADED_IMAGE pLoadedImage)
105 {
106   LIST_ENTRY *pCurrent, *pFind;
107
108   TRACE("(%p)\n", pLoadedImage);
109   
110   if(!IMAGEHLP_pFirstLoadedImage || !pLoadedImage)
111     {
112       /* No image loaded or null pointer */
113       SetLastError(ERROR_INVALID_PARAMETER);
114       return FALSE;
115     }
116
117   pFind=&pLoadedImage->Links;
118   pCurrent=&IMAGEHLP_pFirstLoadedImage->Links;
119   while((pCurrent != pFind) &&
120     (pCurrent != NULL))
121       pCurrent = pCurrent->Flink;
122   if(!pCurrent)
123     {
124       /* Not found */
125       SetLastError(ERROR_INVALID_PARAMETER);
126       return FALSE;
127     }
128
129   if(pCurrent->Blink)
130     pCurrent->Blink->Flink = pCurrent->Flink;
131   else
132     IMAGEHLP_pFirstLoadedImage = pCurrent->Flink?CONTAINING_RECORD(
133       pCurrent->Flink, LOADED_IMAGE, Links):NULL;
134
135   if(pCurrent->Flink)
136     pCurrent->Flink->Blink = pCurrent->Blink;
137   else
138     IMAGEHLP_pLastLoadedImage = pCurrent->Blink?CONTAINING_RECORD(
139       pCurrent->Blink, LOADED_IMAGE, Links):NULL;
140
141   return FALSE;
142 }
143
144 /***********************************************************************
145  *              MapAndLoad (IMAGEHLP.@)
146  */
147 BOOL WINAPI MapAndLoad(LPSTR pszImageName, LPSTR pszDllPath, PLOADED_IMAGE pLoadedImage,
148                        BOOL bDotDll, BOOL bReadOnly)
149 {
150     CHAR szFileName[MAX_PATH];
151     HANDLE hFile = INVALID_HANDLE_VALUE;
152     HANDLE hFileMapping = NULL;
153     PVOID mapping = NULL;
154     PIMAGE_NT_HEADERS pNtHeader = NULL;
155
156     TRACE("(%s, %s, %p, %d, %d)\n",
157           pszImageName, pszDllPath, pLoadedImage, bDotDll, bReadOnly);
158
159     if (!SearchPathA(pszDllPath, pszImageName, bDotDll ? ".DLL" : ".EXE",
160                      sizeof(szFileName), szFileName, NULL))
161     {
162         SetLastError(ERROR_FILE_NOT_FOUND);
163         goto Error;
164     }
165
166     hFile = CreateFileA(szFileName,
167                         GENERIC_READ | (bReadOnly ? 0 : GENERIC_WRITE),
168                         FILE_SHARE_READ,
169                         NULL, OPEN_EXISTING, 0, NULL);
170     if (hFile == INVALID_HANDLE_VALUE)
171     {
172         WARN("CreateFile: Error = %d\n", GetLastError());
173         goto Error;
174     }
175
176     hFileMapping = CreateFileMappingA(hFile, NULL, 
177                                       (bReadOnly ? PAGE_READONLY : PAGE_READWRITE) | SEC_COMMIT,
178                                       0, 0, NULL);
179     if (!hFileMapping)
180     {
181         WARN("CreateFileMapping: Error = %d\n", GetLastError());
182         goto Error;
183     }
184
185     mapping = MapViewOfFile(hFileMapping, bReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0);
186     CloseHandle(hFileMapping);
187     if (!mapping)
188     {
189         WARN("MapViewOfFile: Error = %d\n", GetLastError());
190         goto Error;
191     }
192
193     if (!(pNtHeader = RtlImageNtHeader(mapping)))
194     {
195         WARN("Not an NT header\n");
196         UnmapViewOfFile(mapping);
197         goto Error;
198     }
199
200     pLoadedImage->ModuleName       = HeapAlloc(GetProcessHeap(), 0,
201                                                strlen(szFileName) + 1);
202     if (pLoadedImage->ModuleName) strcpy(pLoadedImage->ModuleName, szFileName);
203     pLoadedImage->hFile            = hFile;
204     pLoadedImage->MappedAddress    = mapping;
205     pLoadedImage->FileHeader       = pNtHeader;
206     pLoadedImage->Sections         = (PIMAGE_SECTION_HEADER)
207         ((LPBYTE) &pNtHeader->OptionalHeader +
208          pNtHeader->FileHeader.SizeOfOptionalHeader);
209     pLoadedImage->NumberOfSections = pNtHeader->FileHeader.NumberOfSections;
210     pLoadedImage->SizeOfImage      = GetFileSize(hFile, NULL);
211     pLoadedImage->Characteristics  = pNtHeader->FileHeader.Characteristics;
212     pLoadedImage->LastRvaSection   = pLoadedImage->Sections;
213
214     pLoadedImage->fSystemImage     = FALSE; /* FIXME */
215     pLoadedImage->fDOSImage        = FALSE; /* FIXME */
216
217     pLoadedImage->Links.Flink      = &pLoadedImage->Links;
218     pLoadedImage->Links.Blink      = &pLoadedImage->Links;
219
220     return TRUE;
221
222 Error:
223     if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
224     return FALSE;
225 }
226
227 /***********************************************************************
228  *              SetImageConfigInformation (IMAGEHLP.@)
229  */
230 BOOL WINAPI SetImageConfigInformation(
231   PLOADED_IMAGE LoadedImage,
232   PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
233 {
234   FIXME("(%p, %p): stub\n",
235     LoadedImage, ImageConfigInformation
236   );
237   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
238   return FALSE;
239 }
240
241 /***********************************************************************
242  *              UnMapAndLoad (IMAGEHLP.@)
243  */
244 BOOL WINAPI UnMapAndLoad(PLOADED_IMAGE pLoadedImage)
245 {
246     HeapFree(GetProcessHeap(), 0, pLoadedImage->ModuleName);
247     /* FIXME: MSDN states that a new checksum is computed and stored into the file */
248     if (pLoadedImage->MappedAddress) UnmapViewOfFile(pLoadedImage->MappedAddress);
249     if (pLoadedImage->hFile != INVALID_HANDLE_VALUE) CloseHandle(pLoadedImage->hFile);
250     return TRUE;
251 }