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