modify.c 8.94 KB
Newer Older
1 2 3 4
/*
 *	IMAGEHLP library
 *
 *	Copyright 1998	Patrik Stridvall
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23
#include <stdarg.h>

#include "windef.h"
24
#include "winbase.h"
25
#include "winternl.h"
26
#include "winerror.h"
27
#include "wine/debug.h"
28 29
#include "imagehlp.h"

30
WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
31

32 33 34
static WORD CalcCheckSum(DWORD StartValue, LPVOID BaseAddress, DWORD WordCount);


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 53 54
    Flags, debugstr_a(ImageName), debugstr_a(DllPath),
    debugstr_a(SymbolPath), StatusRoutine
  );
55
  return TRUE;
56 57
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

/***********************************************************************
 *		CheckSum (internal)
 */
static WORD CalcCheckSum(
  DWORD StartValue, LPVOID BaseAddress, DWORD WordCount)
{
   LPWORD Ptr;
   DWORD Sum;
   DWORD i;

   Sum = StartValue;
   Ptr = (LPWORD)BaseAddress;
   for (i = 0; i < WordCount; i++)
     {
	Sum += *Ptr;
	if (HIWORD(Sum) != 0)
	  {
	     Sum = LOWORD(Sum) + HIWORD(Sum);
	  }
	Ptr++;
     }

   return (WORD)(LOWORD(Sum) + HIWORD(Sum));
}


85
/***********************************************************************
86
 *		CheckSumMappedFile (IMAGEHLP.@)
87
 */
88
PIMAGE_NT_HEADERS WINAPI CheckSumMappedFile(
89
  LPVOID BaseAddress, DWORD FileLength,
90 91
  LPDWORD HeaderSum, LPDWORD CheckSum)
{
92 93 94 95
  IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *) BaseAddress;
  PIMAGE_NT_HEADERS32 Header32;
  PIMAGE_NT_HEADERS64 Header64;
  DWORD *ChecksumFile;
96 97 98
  DWORD CalcSum;
  DWORD HdrSum;

99
  TRACE("(%p, %d, %p, %p)\n",
100 101
    BaseAddress, FileLength, HeaderSum, CheckSum
  );
102 103 104 105 106

  CalcSum = (DWORD)CalcCheckSum(0,
				BaseAddress,
				(FileLength + 1) / sizeof(WORD));

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  if (dos->e_magic != IMAGE_DOS_SIGNATURE)
    return NULL;

  Header32 = (IMAGE_NT_HEADERS32 *)((char *)dos + dos->e_lfanew);

  if (Header32->Signature != IMAGE_NT_SIGNATURE)
    return NULL;

  if (Header32->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
    ChecksumFile = &Header32->OptionalHeader.CheckSum;
  else if (Header32->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
  {
    Header64 = (IMAGE_NT_HEADERS64 *)Header32;
    ChecksumFile = &Header64->OptionalHeader.CheckSum;
  }
  else
    return NULL;

  HdrSum = *ChecksumFile;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

  /* Subtract image checksum from calculated checksum. */
  /* fix low word of checksum */
  if (LOWORD(CalcSum) >= LOWORD(HdrSum))
  {
    CalcSum -= LOWORD(HdrSum);
  }
  else
  {
    CalcSum = ((LOWORD(CalcSum) - LOWORD(HdrSum)) & 0xFFFF) - 1;
  }

   /* fix high word of checksum */
  if (LOWORD(CalcSum) >= HIWORD(HdrSum))
  {
    CalcSum -= HIWORD(HdrSum);
  }
  else
  {
    CalcSum = ((LOWORD(CalcSum) - HIWORD(HdrSum)) & 0xFFFF) - 1;
  }

  /* add file length */
  CalcSum += FileLength;

  *CheckSum = CalcSum;
152
  *HeaderSum = *ChecksumFile;
153

154
  return (PIMAGE_NT_HEADERS) Header32;
155 156 157
}

/***********************************************************************
158
 *		MapFileAndCheckSumA (IMAGEHLP.@)
159
 */
160
DWORD WINAPI MapFileAndCheckSumA(
161
  PCSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
162
{
163 164 165 166 167 168
  HANDLE hFile;
  HANDLE hMapping;
  LPVOID BaseAddress;
  DWORD FileLength;

  TRACE("(%s, %p, %p): stub\n",
169 170
    debugstr_a(Filename), HeaderSum, CheckSum
  );
171 172 173 174 175 176 177 178 179 180

  hFile = CreateFileA(Filename,
		      GENERIC_READ,
		      FILE_SHARE_READ | FILE_SHARE_WRITE,
		      NULL,
		      OPEN_EXISTING,
		      FILE_ATTRIBUTE_NORMAL,
		      0);
  if (hFile == INVALID_HANDLE_VALUE)
  {
181 182
    return CHECKSUM_OPEN_FAILURE;
  }
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  hMapping = CreateFileMappingW(hFile,
			       NULL,
			       PAGE_READONLY,
			       0,
			       0,
			       NULL);
  if (hMapping == 0)
  {
    CloseHandle(hFile);
    return CHECKSUM_MAP_FAILURE;
  }

  BaseAddress = MapViewOfFile(hMapping,
			      FILE_MAP_READ,
			      0,
			      0,
			      0);
201
  if (BaseAddress == 0)
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  {
    CloseHandle(hMapping);
    CloseHandle(hFile);
    return CHECKSUM_MAPVIEW_FAILURE;
  }

  FileLength = GetFileSize(hFile,
			   NULL);

  CheckSumMappedFile(BaseAddress,
		     FileLength,
		     HeaderSum,
		     CheckSum);

  UnmapViewOfFile(BaseAddress);
  CloseHandle(hMapping);
  CloseHandle(hFile);

  return 0;
}

223
/***********************************************************************
224
 *		MapFileAndCheckSumW (IMAGEHLP.@)
225
 */
226
DWORD WINAPI MapFileAndCheckSumW(
227
  PCWSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
228
{
229 230 231 232 233 234
  HANDLE hFile;
  HANDLE hMapping;
  LPVOID BaseAddress;
  DWORD FileLength;

  TRACE("(%s, %p, %p): stub\n",
235 236
    debugstr_w(Filename), HeaderSum, CheckSum
  );
237 238 239 240 241 242 243 244 245 246

  hFile = CreateFileW(Filename,
		      GENERIC_READ,
		      FILE_SHARE_READ | FILE_SHARE_WRITE,
		      NULL,
		      OPEN_EXISTING,
		      FILE_ATTRIBUTE_NORMAL,
		      0);
  if (hFile == INVALID_HANDLE_VALUE)
  {
247
  return CHECKSUM_OPEN_FAILURE;
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
  }

  hMapping = CreateFileMappingW(hFile,
			       NULL,
			       PAGE_READONLY,
			       0,
			       0,
			       NULL);
  if (hMapping == 0)
  {
    CloseHandle(hFile);
    return CHECKSUM_MAP_FAILURE;
  }

  BaseAddress = MapViewOfFile(hMapping,
			      FILE_MAP_READ,
			      0,
			      0,
			      0);
267
  if (BaseAddress == 0)
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
  {
    CloseHandle(hMapping);
    CloseHandle(hFile);
    return CHECKSUM_MAPVIEW_FAILURE;
  }

  FileLength = GetFileSize(hFile,
			   NULL);

  CheckSumMappedFile(BaseAddress,
		     FileLength,
		     HeaderSum,
		     CheckSum);

  UnmapViewOfFile(BaseAddress);
  CloseHandle(hMapping);
  CloseHandle(hFile);

  return 0;
287 288 289
}

/***********************************************************************
290
 *		ReBaseImage (IMAGEHLP.@)
291
 */
292
BOOL WINAPI ReBaseImage(
293
  PCSTR CurrentImageName, PCSTR SymbolPath, BOOL fReBase,
294
  BOOL fRebaseSysfileOk, BOOL fGoingDown, ULONG CheckImageSize,
295 296
  ULONG *OldImageSize, ULONG_PTR *OldImageBase, ULONG *NewImageSize,
  ULONG_PTR *NewImageBase, ULONG TimeStamp)
297
{
298
  FIXME(
299
    "(%s, %s, %d, %d, %d, %d, %p, %p, %p, %p, %d): stub\n",
300 301
      debugstr_a(CurrentImageName),debugstr_a(SymbolPath), fReBase,
      fRebaseSysfileOk, fGoingDown, CheckImageSize, OldImageSize,
302 303 304 305 306 307 308
      OldImageBase, NewImageSize, NewImageBase, TimeStamp
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
309
 *		RemovePrivateCvSymbolic (IMAGEHLP.@)
310
 */
311
BOOL WINAPI RemovePrivateCvSymbolic(
312 313
  PCHAR DebugData, PCHAR *NewDebugData, ULONG *NewDebugSize)
{
314
  FIXME("(%p, %p, %p): stub\n",
315 316 317 318 319 320 321
    DebugData, NewDebugData, NewDebugSize
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
322
 *		RemoveRelocations (IMAGEHLP.@)
323
 */
324
VOID WINAPI RemoveRelocations(PCHAR ImageName)
325
{
326
  FIXME("(%p): stub\n", ImageName);
327 328 329 330
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
}

/***********************************************************************
331
 *		SplitSymbols (IMAGEHLP.@)
332
 */
333
BOOL WINAPI SplitSymbols(
334 335
  PSTR ImageName, PCSTR SymbolsPath,
  PSTR SymbolFilePath, ULONG Flags)
336
{
337
  FIXME("(%s, %s, %s, %d): stub\n",
338 339 340 341 342 343 344 345
    debugstr_a(ImageName), debugstr_a(SymbolsPath),
    debugstr_a(SymbolFilePath), Flags
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
346
 *		UpdateDebugInfoFile (IMAGEHLP.@)
347
 */
348
BOOL WINAPI UpdateDebugInfoFile(
349
  PCSTR ImageFileName, PCSTR SymbolPath,
350
  PSTR DebugFilePath, PIMAGE_NT_HEADERS32 NtHeaders)
351
{
352
  FIXME("(%s, %s, %s, %p): stub\n",
353 354 355 356 357 358 359 360
    debugstr_a(ImageFileName), debugstr_a(SymbolPath),
    debugstr_a(DebugFilePath), NtHeaders
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
361
 *		UpdateDebugInfoFileEx (IMAGEHLP.@)
362
 */
363
BOOL WINAPI UpdateDebugInfoFileEx(
364 365
  PCSTR ImageFileName, PCSTR SymbolPath, PSTR DebugFilePath,
  PIMAGE_NT_HEADERS32 NtHeaders, DWORD OldChecksum)
366
{
367
  FIXME("(%s, %s, %s, %p, %d): stub\n",
368 369 370 371 372 373
    debugstr_a(ImageFileName), debugstr_a(SymbolPath),
    debugstr_a(DebugFilePath), NtHeaders, OldChecksum
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}