ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

pe.c
Go to the documentation of this file.
00001 #include <precomp.h>
00002 
00003 #define NDEBUG
00004 #include <debug.h>
00005 
00006 PeSect *pesection(Pe *pe, const char *name) 
00007 {
00008     int i;
00009     ANSI_STRING WantName;
00010     RtlInitAnsiString(&WantName, name);
00011     werrstr("Searching for section %s\n", name);
00012     for (i = 0; i < pe->nsections; i++) {
00013         PANSI_STRING AnsiString = ANSI_NAME_STRING(&pe->sect[i]);
00014         if (WantName.Length == AnsiString->Length &&
00015             !memcmp(AnsiString->Buffer, name, WantName.Length)) {
00016             werrstr("Found %s (%d) @ %x (%x)\n", name, i, 
00017                    ((PCHAR)pe->imagebase)+pe->sect[i].VirtualAddress,
00018                    pe->sect[i].SizeOfRawData);
00019             return &pe->sect[i];
00020         }
00021     }
00022     werrstr("%s not found\n", name);
00023     return nil;
00024 }
00025 
00026 u16int peget2(const unsigned char *ptr) {
00027     return *((u16int*)ptr);
00028 }
00029 
00030 u32int peget4(const unsigned char *ptr) {
00031     return *((u32int*)ptr);
00032 }
00033 
00034 u64int peget8(const unsigned char *ptr) {
00035     return *((u64int*)ptr);
00036 }
00037 
00038 int readn(void *filectx, char *buffer, ulong size) {
00039     return RosSymReadFile(filectx, buffer, size);
00040 }
00041 
00042 int seek(void *filectx, ulong position, int origin) {
00043     assert(origin == 0);
00044     return RosSymSeekFile(filectx, position);
00045 }
00046 
00047 static int
00048 readblock(void *fd, DwarfBlock *b, ulong off, ulong len)
00049 {
00050     b->data = malloc(len);
00051     if(b->data == nil)
00052         return -1;
00053     if(!seek(fd, off, 0) || !readn(fd, (char *)b->data, len)){
00054         free(b->data);
00055         b->data = nil;
00056         return -1;
00057     }
00058     b->len = len;
00059     return 0;
00060 }
00061 
00062 int
00063 loaddisksection(Pe *pe, char *name, DwarfBlock *b)
00064 {
00065     PeSect *s;
00066     if((s = pesection(pe, name)) == nil)
00067         return -1;
00068     return readblock(pe->fd, b, s->PointerToRawData, s->SizeOfRawData);
00069 }
00070 
00071 int
00072 loadmemsection(Pe *pe, char *name, DwarfBlock *b)
00073 {
00074     PeSect *s;
00075 
00076     if((s = pesection(pe, name)) == nil)
00077         return -1;
00078     werrstr("Loading section %s (ImageBase %x RVA %x)\n", name, pe->fd, s->VirtualAddress);
00079     b->data = RosSymAllocMem(s->SizeOfRawData);
00080     b->len = s->SizeOfRawData;
00081     PCHAR DataSource = ((char *)pe->fd) + s->VirtualAddress;
00082     werrstr("Copying to %x from %x (%x)\n", DataSource, b->data, b->len);
00083     RtlCopyMemory(b->data, DataSource, s->SizeOfRawData);
00084     
00085     return s->SizeOfRawData;
00086 }
00087 
00088 void *RosSymAllocMemZero(ulong size, ulong count) {
00089     void *res = RosSymAllocMem(size * count);
00090     if (res) memset(res, 0, size * count);
00091     return res;
00092 }
00093 
00094 int GetStrnlen(const char *string, int maxlen) {
00095     int i;
00096     for (i = 0; i < maxlen && string[i]; i++);
00097     return i;
00098 }
00099 
00100 void pefree(Pe *pe) {
00101     int i;
00102     for (i = 0; i < pe->nsections; i++) {
00103         RtlFreeAnsiString(ANSI_NAME_STRING(&pe->sect[i]));
00104     }
00105     free(pe->sect);
00106     free(pe);
00107 }
00108 
00109 void xfree(void *v) {
00110     if (v) RosSymFreeMem(v);
00111 }
00112 
00113 ulong pefindrva(struct _IMAGE_SECTION_HEADER *SectionHeaders, int NumberOfSections, ulong TargetPhysical) {
00114     int i;
00115     werrstr("Finding RVA for Physical %x\n", TargetPhysical);
00116     for (i = 0; i < NumberOfSections; i++) {
00117         werrstr("Section %d name %s Raw %x Virt %x\n",
00118                i, 
00119                ANSI_NAME_STRING(&SectionHeaders[i])->Buffer, 
00120                SectionHeaders[i].PointerToRawData,
00121                SectionHeaders[i].VirtualAddress);
00122         if (TargetPhysical >= SectionHeaders[i].PointerToRawData && 
00123             TargetPhysical < SectionHeaders[i].PointerToRawData + SectionHeaders[i].SizeOfRawData) {
00124             werrstr("RVA %x\n", TargetPhysical - SectionHeaders[i].PointerToRawData + SectionHeaders[i].VirtualAddress);
00125             return TargetPhysical - SectionHeaders[i].PointerToRawData + SectionHeaders[i].VirtualAddress;
00126         }
00127     }
00128     return nil;
00129 }

Generated on Sat May 26 2012 04:35:19 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.