ddraw/tests: Fix compilation on systems that don't support nameless unions.
[wine] / dlls / imagehlp / modify.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
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winternl.h"
26 #include "winerror.h"
27 #include "wine/debug.h"
28 #include "imagehlp.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
31
32 static WORD CalcCheckSum(DWORD StartValue, LPVOID BaseAddress, DWORD WordCount);
33
34
35 /***********************************************************************
36  *              BindImage (IMAGEHLP.@)
37  */
38 BOOL WINAPI BindImage(
39   PCSTR ImageName, PCSTR DllPath, PCSTR SymbolPath)
40 {
41   return BindImageEx(0, ImageName, DllPath, SymbolPath, NULL);
42 }
43
44 /***********************************************************************
45  *              BindImageEx (IMAGEHLP.@)
46  */
47 BOOL WINAPI BindImageEx(
48   DWORD Flags, PCSTR ImageName, PCSTR DllPath, PCSTR SymbolPath,
49   PIMAGEHLP_STATUS_ROUTINE StatusRoutine)
50 {
51   FIXME("(%d, %s, %s, %s, %p): stub\n",
52     Flags, debugstr_a(ImageName), debugstr_a(DllPath),
53     debugstr_a(SymbolPath), StatusRoutine
54   );
55   return TRUE;
56 }
57
58
59 /***********************************************************************
60  *              CheckSum (internal)
61  */
62 static WORD CalcCheckSum(
63   DWORD StartValue, LPVOID BaseAddress, DWORD WordCount)
64 {
65    LPWORD Ptr;
66    DWORD Sum;
67    DWORD i;
68
69    Sum = StartValue;
70    Ptr = (LPWORD)BaseAddress;
71    for (i = 0; i < WordCount; i++)
72      {
73         Sum += *Ptr;
74         if (HIWORD(Sum) != 0)
75           {
76              Sum = LOWORD(Sum) + HIWORD(Sum);
77           }
78         Ptr++;
79      }
80
81    return (WORD)(LOWORD(Sum) + HIWORD(Sum));
82 }
83
84
85 /***********************************************************************
86  *              CheckSumMappedFile (IMAGEHLP.@)
87  */
88 PIMAGE_NT_HEADERS WINAPI CheckSumMappedFile(
89   LPVOID BaseAddress, DWORD FileLength,
90   LPDWORD HeaderSum, LPDWORD CheckSum)
91 {
92   PIMAGE_NT_HEADERS Header;
93   DWORD CalcSum;
94   DWORD HdrSum;
95
96   FIXME("(%p, %d, %p, %p): stub\n",
97     BaseAddress, FileLength, HeaderSum, CheckSum
98   );
99
100   CalcSum = (DWORD)CalcCheckSum(0,
101                                 BaseAddress,
102                                 (FileLength + 1) / sizeof(WORD));
103
104   Header = RtlImageNtHeader(BaseAddress);
105   HdrSum = Header->OptionalHeader.CheckSum;
106
107   /* Subtract image checksum from calculated checksum. */
108   /* fix low word of checksum */
109   if (LOWORD(CalcSum) >= LOWORD(HdrSum))
110   {
111     CalcSum -= LOWORD(HdrSum);
112   }
113   else
114   {
115     CalcSum = ((LOWORD(CalcSum) - LOWORD(HdrSum)) & 0xFFFF) - 1;
116   }
117
118    /* fix high word of checksum */
119   if (LOWORD(CalcSum) >= HIWORD(HdrSum))
120   {
121     CalcSum -= HIWORD(HdrSum);
122   }
123   else
124   {
125     CalcSum = ((LOWORD(CalcSum) - HIWORD(HdrSum)) & 0xFFFF) - 1;
126   }
127
128   /* add file length */
129   CalcSum += FileLength;
130
131   *CheckSum = CalcSum;
132   *HeaderSum = Header->OptionalHeader.CheckSum;
133
134   return Header;
135 }
136
137 /***********************************************************************
138  *              MapFileAndCheckSumA (IMAGEHLP.@)
139  */
140 DWORD WINAPI MapFileAndCheckSumA(
141   PCSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
142 {
143   HANDLE hFile;
144   HANDLE hMapping;
145   LPVOID BaseAddress;
146   DWORD FileLength;
147
148   TRACE("(%s, %p, %p): stub\n",
149     debugstr_a(Filename), HeaderSum, CheckSum
150   );
151
152   hFile = CreateFileA(Filename,
153                       GENERIC_READ,
154                       FILE_SHARE_READ | FILE_SHARE_WRITE,
155                       NULL,
156                       OPEN_EXISTING,
157                       FILE_ATTRIBUTE_NORMAL,
158                       0);
159   if (hFile == INVALID_HANDLE_VALUE)
160   {
161     return CHECKSUM_OPEN_FAILURE;
162   }
163
164   hMapping = CreateFileMappingW(hFile,
165                                NULL,
166                                PAGE_READONLY,
167                                0,
168                                0,
169                                NULL);
170   if (hMapping == 0)
171   {
172     CloseHandle(hFile);
173     return CHECKSUM_MAP_FAILURE;
174   }
175
176   BaseAddress = MapViewOfFile(hMapping,
177                               FILE_MAP_READ,
178                               0,
179                               0,
180                               0);
181   if (BaseAddress == 0)
182   {
183     CloseHandle(hMapping);
184     CloseHandle(hFile);
185     return CHECKSUM_MAPVIEW_FAILURE;
186   }
187
188   FileLength = GetFileSize(hFile,
189                            NULL);
190
191   CheckSumMappedFile(BaseAddress,
192                      FileLength,
193                      HeaderSum,
194                      CheckSum);
195
196   UnmapViewOfFile(BaseAddress);
197   CloseHandle(hMapping);
198   CloseHandle(hFile);
199
200   return 0;
201 }
202
203 /***********************************************************************
204  *              MapFileAndCheckSumW (IMAGEHLP.@)
205  */
206 DWORD WINAPI MapFileAndCheckSumW(
207   PCWSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
208 {
209   HANDLE hFile;
210   HANDLE hMapping;
211   LPVOID BaseAddress;
212   DWORD FileLength;
213
214   TRACE("(%s, %p, %p): stub\n",
215     debugstr_w(Filename), HeaderSum, CheckSum
216   );
217
218   hFile = CreateFileW(Filename,
219                       GENERIC_READ,
220                       FILE_SHARE_READ | FILE_SHARE_WRITE,
221                       NULL,
222                       OPEN_EXISTING,
223                       FILE_ATTRIBUTE_NORMAL,
224                       0);
225   if (hFile == INVALID_HANDLE_VALUE)
226   {
227   return CHECKSUM_OPEN_FAILURE;
228   }
229
230   hMapping = CreateFileMappingW(hFile,
231                                NULL,
232                                PAGE_READONLY,
233                                0,
234                                0,
235                                NULL);
236   if (hMapping == 0)
237   {
238     CloseHandle(hFile);
239     return CHECKSUM_MAP_FAILURE;
240   }
241
242   BaseAddress = MapViewOfFile(hMapping,
243                               FILE_MAP_READ,
244                               0,
245                               0,
246                               0);
247   if (BaseAddress == 0)
248   {
249     CloseHandle(hMapping);
250     CloseHandle(hFile);
251     return CHECKSUM_MAPVIEW_FAILURE;
252   }
253
254   FileLength = GetFileSize(hFile,
255                            NULL);
256
257   CheckSumMappedFile(BaseAddress,
258                      FileLength,
259                      HeaderSum,
260                      CheckSum);
261
262   UnmapViewOfFile(BaseAddress);
263   CloseHandle(hMapping);
264   CloseHandle(hFile);
265
266   return 0;
267 }
268
269 /***********************************************************************
270  *              ReBaseImage (IMAGEHLP.@)
271  */
272 BOOL WINAPI ReBaseImage(
273   PCSTR CurrentImageName, PCSTR SymbolPath, BOOL fReBase,
274   BOOL fRebaseSysfileOk, BOOL fGoingDown, ULONG CheckImageSize,
275   ULONG *OldImageSize, ULONG_PTR *OldImageBase, ULONG *NewImageSize,
276   ULONG_PTR *NewImageBase, ULONG TimeStamp)
277 {
278   FIXME(
279     "(%s, %s, %d, %d, %d, %d, %p, %p, %p, %p, %d): stub\n",
280       debugstr_a(CurrentImageName),debugstr_a(SymbolPath), fReBase,
281       fRebaseSysfileOk, fGoingDown, CheckImageSize, OldImageSize,
282       OldImageBase, NewImageSize, NewImageBase, TimeStamp
283   );
284   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
285   return FALSE;
286 }
287
288 /***********************************************************************
289  *              RemovePrivateCvSymbolic (IMAGEHLP.@)
290  */
291 BOOL WINAPI RemovePrivateCvSymbolic(
292   PCHAR DebugData, PCHAR *NewDebugData, ULONG *NewDebugSize)
293 {
294   FIXME("(%p, %p, %p): stub\n",
295     DebugData, NewDebugData, NewDebugSize
296   );
297   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
298   return FALSE;
299 }
300
301 /***********************************************************************
302  *              RemoveRelocations (IMAGEHLP.@)
303  */
304 VOID WINAPI RemoveRelocations(PCHAR ImageName)
305 {
306   FIXME("(%p): stub\n", ImageName);
307   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
308 }
309
310 /***********************************************************************
311  *              SplitSymbols (IMAGEHLP.@)
312  */
313 BOOL WINAPI SplitSymbols(
314   PSTR ImageName, PCSTR SymbolsPath,
315   PSTR SymbolFilePath, ULONG Flags)
316 {
317   FIXME("(%s, %s, %s, %d): stub\n",
318     debugstr_a(ImageName), debugstr_a(SymbolsPath),
319     debugstr_a(SymbolFilePath), Flags
320   );
321   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
322   return FALSE;
323 }
324
325 /***********************************************************************
326  *              UpdateDebugInfoFile (IMAGEHLP.@)
327  */
328 BOOL WINAPI UpdateDebugInfoFile(
329   PCSTR ImageFileName, PCSTR SymbolPath,
330   PSTR DebugFilePath, PIMAGE_NT_HEADERS32 NtHeaders)
331 {
332   FIXME("(%s, %s, %s, %p): stub\n",
333     debugstr_a(ImageFileName), debugstr_a(SymbolPath),
334     debugstr_a(DebugFilePath), NtHeaders
335   );
336   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
337   return FALSE;
338 }
339
340 /***********************************************************************
341  *              UpdateDebugInfoFileEx (IMAGEHLP.@)
342  */
343 BOOL WINAPI UpdateDebugInfoFileEx(
344   PCSTR ImageFileName, PCSTR SymbolPath, PSTR DebugFilePath,
345   PIMAGE_NT_HEADERS32 NtHeaders, DWORD OldChecksum)
346 {
347   FIXME("(%s, %s, %s, %p, %d): stub\n",
348     debugstr_a(ImageFileName), debugstr_a(SymbolPath),
349     debugstr_a(DebugFilePath), NtHeaders, OldChecksum
350   );
351   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
352   return FALSE;
353 }