- Merge aicom-network-fixes up to r36740
[reactos.git] / reactos / lib / rossym / frommem.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: lib/rossym/frommem.c
5 * PURPOSE: Creating rossym info from an in-memory image
6 *
7 * PROGRAMMERS: Ge van Geldorp (gvg@reactos.com)
8 */
9
10 #define NTOSAPI
11 #include <ntddk.h>
12 #include <reactos/rossym.h>
13 #include "rossympriv.h"
14 #ifdef _MSC_VER
15 #include "ntimage.h"
16 #endif
17
18 #define NDEBUG
19 #include <debug.h>
20
21 BOOLEAN
22 RosSymCreateFromMem(PVOID ImageStart, ULONG_PTR ImageSize, PROSSYM_INFO *RosSymInfo)
23 {
24 PIMAGE_DOS_HEADER DosHeader;
25 PIMAGE_NT_HEADERS NtHeaders;
26 PIMAGE_SECTION_HEADER SectionHeader;
27 ULONG SectionIndex;
28 BOOLEAN RosSymSectionFound = FALSE;
29 CHAR SectionName[IMAGE_SIZEOF_SHORT_NAME];
30
31 /* Check if MZ header is valid */
32 DosHeader = (PIMAGE_DOS_HEADER) ImageStart;
33 if (ImageSize < sizeof(IMAGE_DOS_HEADER)
34 || ! ROSSYM_IS_VALID_DOS_HEADER(DosHeader))
35 {
36 DPRINT1("Image doesn't have a valid DOS header\n");
37 return FALSE;
38 }
39
40 /* Locate NT header */
41 NtHeaders = (PIMAGE_NT_HEADERS)((char *) ImageStart + DosHeader->e_lfanew);
42 if (ImageSize < DosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS)
43 || ! ROSSYM_IS_VALID_NT_HEADERS(NtHeaders))
44 {
45 DPRINT1("Image doesn't have a valid PE header\n");
46 return FALSE;
47 }
48
49 /* Search for the section header */
50 SectionHeader = IMAGE_FIRST_SECTION(NtHeaders);
51 if (ImageSize < (ULONG_PTR)((char *) (SectionHeader + NtHeaders->FileHeader.NumberOfSections)
52 - (char *) ImageStart))
53 {
54 DPRINT1("Image doesn't have valid section headers\n");
55 return FALSE;
56 }
57 strncpy(SectionName, ROSSYM_SECTION_NAME, IMAGE_SIZEOF_SHORT_NAME);
58 for (SectionIndex = 0; SectionIndex < NtHeaders->FileHeader.NumberOfSections; SectionIndex++)
59 {
60 if (0 == memcmp(SectionName, SectionHeader->Name, IMAGE_SIZEOF_SHORT_NAME))
61 {
62 RosSymSectionFound = TRUE;
63 break;
64 }
65 SectionHeader++;
66 }
67
68 if (!RosSymSectionFound)
69 {
70 DPRINT("No %s section found\n", ROSSYM_SECTION_NAME);
71 return FALSE;
72 }
73
74 /* Locate the section itself */
75 if (ImageSize < SectionHeader->PointerToRawData + SectionHeader->SizeOfRawData
76 || SectionHeader->SizeOfRawData < sizeof(ROSSYM_HEADER))
77 {
78 DPRINT("Invalid %s section\n", ROSSYM_SECTION_NAME);
79 return FALSE;
80 }
81
82 if (SectionHeader->VirtualAddress + SectionHeader->Misc.VirtualSize > ImageSize)
83 {
84 DPRINT("Bad %s section virtual size!\n", ROSSYM_SECTION_NAME);
85 return FALSE;
86 }
87
88 /* Load it */
89 return RosSymCreateFromRaw((char *) ImageStart + SectionHeader->VirtualAddress,
90 SectionHeader->SizeOfRawData, RosSymInfo);
91 }
92
93 /* EOF */