modify.c 8.42 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 18
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 20
 */

21 22 23
#include <stdarg.h>

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

31
WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
32

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


36
/***********************************************************************
37
 *		BindImage (IMAGEHLP.@)
38
 */
39
BOOL WINAPI BindImage(
40 41
  LPSTR ImageName, LPSTR DllPath, LPSTR SymbolPath)
{
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("(%ld, %s, %s, %s, %p): stub\n",
53 54 55 56 57 58 59
    Flags, debugstr_a(ImageName), debugstr_a(DllPath),
    debugstr_a(SymbolPath), StatusRoutine
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

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 85 86

/***********************************************************************
 *		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));
}


87
/***********************************************************************
88
 *		CheckSumMappedFile (IMAGEHLP.@)
89
 */
90
PIMAGE_NT_HEADERS WINAPI CheckSumMappedFile(
91
  LPVOID BaseAddress, DWORD FileLength,
92 93
  LPDWORD HeaderSum, LPDWORD CheckSum)
{
94 95 96 97
  PIMAGE_NT_HEADERS Header;
  DWORD CalcSum;
  DWORD HdrSum;

98
  FIXME("(%p, %ld, %p, %p): stub\n",
99 100
    BaseAddress, FileLength, HeaderSum, CheckSum
  );
101 102 103 104 105

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

106
  Header = RtlImageNtHeader(BaseAddress);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  HdrSum = Header->OptionalHeader.CheckSum;

  /* 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;
  *HeaderSum = Header->OptionalHeader.CheckSum;

  return Header;
137 138 139
}

/***********************************************************************
140
 *		MapFileAndCheckSumA (IMAGEHLP.@)
141
 */
142
DWORD WINAPI MapFileAndCheckSumA(
143 144
  LPSTR Filename, LPDWORD HeaderSum, LPDWORD CheckSum)
{
145 146 147 148 149 150
  HANDLE hFile;
  HANDLE hMapping;
  LPVOID BaseAddress;
  DWORD FileLength;

  TRACE("(%s, %p, %p): stub\n",
151 152
    debugstr_a(Filename), HeaderSum, CheckSum
  );
153 154 155 156 157 158 159 160 161 162

  hFile = CreateFileA(Filename,
		      GENERIC_READ,
		      FILE_SHARE_READ | FILE_SHARE_WRITE,
		      NULL,
		      OPEN_EXISTING,
		      FILE_ATTRIBUTE_NORMAL,
		      0);
  if (hFile == INVALID_HANDLE_VALUE)
  {
163 164
    return CHECKSUM_OPEN_FAILURE;
  }
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  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);
  if (hMapping == 0)
  {
    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;
}

205
/***********************************************************************
206
 *		MapFileAndCheckSumW (IMAGEHLP.@)
207
 */
208
DWORD WINAPI MapFileAndCheckSumW(
209 210
  LPWSTR Filename, LPDWORD HeaderSum, LPDWORD CheckSum)
{
211 212 213 214 215 216
  HANDLE hFile;
  HANDLE hMapping;
  LPVOID BaseAddress;
  DWORD FileLength;

  TRACE("(%s, %p, %p): stub\n",
217 218
    debugstr_w(Filename), HeaderSum, CheckSum
  );
219 220 221 222 223 224 225 226 227 228

  hFile = CreateFileW(Filename,
		      GENERIC_READ,
		      FILE_SHARE_READ | FILE_SHARE_WRITE,
		      NULL,
		      OPEN_EXISTING,
		      FILE_ATTRIBUTE_NORMAL,
		      0);
  if (hFile == INVALID_HANDLE_VALUE)
  {
229
  return CHECKSUM_OPEN_FAILURE;
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  }

  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);
  if (hMapping == 0)
  {
    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;
269 270 271
}

/***********************************************************************
272
 *		ReBaseImage (IMAGEHLP.@)
273
 */
274 275 276
BOOL WINAPI ReBaseImage(
  LPSTR CurrentImageName, LPSTR SymbolPath, BOOL fReBase,
  BOOL fRebaseSysfileOk, BOOL fGoingDown, ULONG CheckImageSize,
277 278 279
  ULONG *OldImageSize, ULONG *OldImageBase, ULONG *NewImageSize,
  ULONG *NewImageBase, ULONG TimeStamp)
{
280
  FIXME(
281
    "(%s, %s, %d, %d, %d, %ld, %p, %p, %p, %p, %ld): stub\n",
282 283
      debugstr_a(CurrentImageName),debugstr_a(SymbolPath), fReBase,
      fRebaseSysfileOk, fGoingDown, CheckImageSize, OldImageSize,
284 285 286 287 288 289 290
      OldImageBase, NewImageSize, NewImageBase, TimeStamp
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
291
 *		RemovePrivateCvSymbolic (IMAGEHLP.@)
292
 */
293
BOOL WINAPI RemovePrivateCvSymbolic(
294 295
  PCHAR DebugData, PCHAR *NewDebugData, ULONG *NewDebugSize)
{
296
  FIXME("(%p, %p, %p): stub\n",
297 298 299 300 301 302 303
    DebugData, NewDebugData, NewDebugSize
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
304
 *		RemoveRelocations (IMAGEHLP.@)
305
 */
306
VOID WINAPI RemoveRelocations(PCHAR ImageName)
307
{
308
  FIXME("(%p): stub\n", ImageName);
309 310 311 312
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
}

/***********************************************************************
313
 *		SplitSymbols (IMAGEHLP.@)
314
 */
315
BOOL WINAPI SplitSymbols(
316
  LPSTR ImageName, LPSTR SymbolsPath,
317 318
  LPSTR SymbolFilePath, DWORD Flags)
{
319
  FIXME("(%s, %s, %s, %ld): stub\n",
320 321 322 323 324 325 326 327
    debugstr_a(ImageName), debugstr_a(SymbolsPath),
    debugstr_a(SymbolFilePath), Flags
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
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 336 337 338 339 340 341 342
    debugstr_a(ImageFileName), debugstr_a(SymbolPath),
    debugstr_a(DebugFilePath), NtHeaders
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
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, %ld): stub\n",
350 351 352 353 354 355
    debugstr_a(ImageFileName), debugstr_a(SymbolPath),
    debugstr_a(DebugFilePath), NtHeaders, OldChecksum
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}