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