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