| 1 |
/* |
| 2 |
genidl - Generate interface defintion language listing from a |
| 3 |
Portable Executable. |
| 4 |
Copyright (C) 2009-2016 mingw-w64 project |
| 5 |
|
| 6 |
This program is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program 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 |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#define _CRT_SECURE_NO_WARNINGS |
| 21 |
#include "genidl_cfg.h" |
| 22 |
|
| 23 |
#include "genidl_readpe.h" |
| 24 |
|
| 25 |
static int32_t readResourceDirectory (FILE *fp, unsigned char **dta, size_t *length, size_t *resRVA); |
| 26 |
|
| 27 |
static void walk_res_dir (sImgResourceDirectory *resDir, unsigned char *base,uint32_t level, uint32_t resourceType, int32_t beTypelib, int32_t *noRes,uint32_t *zOff, uint32_t *zSize); |
| 28 |
|
| 29 |
int32_t |
| 30 |
genidl_pe_typelib_resource_count (FILE *fp) |
| 31 |
{ |
| 32 |
int32_t cnt = 0x2000; |
| 33 |
uint32_t zOff = 0,zSize = 0; |
| 34 |
size_t resRVA = 0; |
| 35 |
unsigned char *dta = NULL; |
| 36 |
size_t length = 0; |
| 37 |
if (!readResourceDirectory (fp, &dta, &length,&resRVA)) |
| 38 |
return 0; |
| 39 |
if (!length || !dta) |
| 40 |
return 0; |
| 41 |
zOff = zSize = 0; |
| 42 |
walk_res_dir ((sImgResourceDirectory *) dta, dta,0, 0, 0, &cnt,&zOff,&zSize); |
| 43 |
free (dta); |
| 44 |
return 0x2000 - cnt; |
| 45 |
} |
| 46 |
|
| 47 |
int32_t |
| 48 |
genidl_pe_typelib_resource_read (FILE *fp, int32_t noRes, unsigned char **pDta, size_t *szDta) |
| 49 |
{ |
| 50 |
uint32_t zOff,zSize; |
| 51 |
unsigned char *dta = NULL; |
| 52 |
size_t length = 0; |
| 53 |
size_t resRVA = 0; |
| 54 |
if (!readResourceDirectory (fp, &dta, &length, &resRVA)) |
| 55 |
return 0; |
| 56 |
if (!length || !dta) |
| 57 |
return 0; |
| 58 |
zOff = zSize = 0; |
| 59 |
walk_res_dir ((sImgResourceDirectory *) dta, dta,0, 0, 0, &noRes,&zOff,&zSize); |
| 60 |
if (zOff != 0) |
| 61 |
{ |
| 62 |
pDta[0] = (unsigned char *) malloc (zSize + 1); |
| 63 |
szDta[0] = zSize; |
| 64 |
|
| 65 |
memcpy (pDta[0], dta + zOff - resRVA , zSize); |
| 66 |
free (dta); |
| 67 |
return 1; |
| 68 |
} |
| 69 |
free (dta); |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
static void get_res_name_by_id (uint32_t id, unsigned char *resourceBase, char *buffer, uint32_t cBytes); |
| 74 |
static void walk_res_entry (sImgResourceDirectoryEntry *resDirEntry, unsigned char *resourceBase, uint32_t level, int32_t beTypelib, int32_t *noRes,uint32_t *zOff,uint32_t *zSize); |
| 75 |
|
| 76 |
static void |
| 77 |
walk_res_dir (sImgResourceDirectory *resDir, unsigned char *base,uint32_t level, uint32_t resourceType, int32_t beTypelib, int32_t *noRes,uint32_t *zOff,uint32_t *zSize) |
| 78 |
{ |
| 79 |
sImgResourceDirectoryEntry *resDirEntry; |
| 80 |
char szType[64]; |
| 81 |
uint32_t i; |
| 82 |
uint32_t mloop = 0; |
| 83 |
|
| 84 |
if (resourceType & GENIDL_IMG_RESNAME_IS_STR) |
| 85 |
{ |
| 86 |
get_res_name_by_id (resourceType, base,szType, sizeof(szType)); |
| 87 |
} |
| 88 |
else |
| 89 |
sprintf(szType, "%X", resourceType); |
| 90 |
if (!strcmp (szType,"TYPELIB")) |
| 91 |
beTypelib = 1; |
| 92 |
|
| 93 |
resDirEntry = (sImgResourceDirectoryEntry *) (resDir + 1); |
| 94 |
|
| 95 |
mloop = resDir->NumberOfNamedEntries + resDir->NumberOfIdEntries; |
| 96 |
|
| 97 |
for (i=0; i < mloop; i++, resDirEntry++) |
| 98 |
{ |
| 99 |
walk_res_entry (resDirEntry, base, level + 1, beTypelib, noRes,zOff,zSize); |
| 100 |
if (zOff[0] != 0) |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
if (!strcmp (szType,"TYPELIB")) |
| 105 |
beTypelib = 0; |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
walk_res_entry (sImgResourceDirectoryEntry *resDirEntry, unsigned char *resourceBase, uint32_t level, int32_t beTypelib,int32_t *noRes,uint32_t *zOff,uint32_t *zSize) |
| 110 |
{ |
| 111 |
sImgResourceDataEntry *pResDataEntry; |
| 112 |
if (resDirEntry->OffsetToData & GENIDL_IMG_RESDATA_IS_DIR) { |
| 113 |
walk_res_dir ((sImgResourceDirectory *) ((resDirEntry->OffsetToData & 0x7FFFFFFF) + resourceBase), |
| 114 |
resourceBase, level, resDirEntry->Name, beTypelib, noRes,zOff,zSize); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
if (!beTypelib) |
| 119 |
return; |
| 120 |
if (noRes[0] != 0) |
| 121 |
{ |
| 122 |
noRes[0] -= 1; |
| 123 |
return; |
| 124 |
} |
| 125 |
pResDataEntry = (sImgResourceDataEntry *) |
| 126 |
(resourceBase + resDirEntry->OffsetToData); |
| 127 |
zOff[0] = pResDataEntry->OffsetToData; /* RVA!!! */ |
| 128 |
zSize[0] = pResDataEntry->Size; |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
get_res_name_by_id (uint32_t id, unsigned char *resourceBase, char *buffer, uint32_t cBytes) |
| 133 |
{ |
| 134 |
sImgResourceDirStringWC *prdsu; |
| 135 |
|
| 136 |
if ( !(id & GENIDL_IMG_RESNAME_IS_STR) ) |
| 137 |
{ |
| 138 |
sprintf (buffer, "%X", id); |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
id &= 0x7FFFFFFF; |
| 143 |
prdsu = (sImgResourceDirStringWC *) (resourceBase + id); |
| 144 |
|
| 145 |
#ifdef _WIN32 |
| 146 |
WideCharToMultiByte(CP_ACP, 0, prdsu->NameString, prdsu->Length, buffer, cBytes, 0, 0); |
| 147 |
buffer[min(cBytes - 1, prdsu->Length)] = 0; /* Null terminate it!!! */ |
| 148 |
#else |
| 149 |
{ |
| 150 |
uint32_t mi = cBytes - 1; |
| 151 |
uint32_t i; |
| 152 |
if (mi > prdsu->Length) |
| 153 |
mi = prdsu->Length; |
| 154 |
for (i = 0; i < mi; i++) |
| 155 |
{ |
| 156 |
uint16_t ch = prdsu->NameString[i]; |
| 157 |
if (!ch) |
| 158 |
break; |
| 159 |
if (ch >= 128) |
| 160 |
ch = '?'; |
| 161 |
buffer[i] = (char) (ch & 0xff); |
| 162 |
} |
| 163 |
buffer[i] = 0; |
| 164 |
} |
| 165 |
#endif |
| 166 |
} |
| 167 |
|
| 168 |
static int32_t |
| 169 |
readResourceDirectory (FILE *fp, unsigned char **dta, size_t *length, size_t *resRVA) |
| 170 |
{ |
| 171 |
long pe_header; |
| 172 |
int32_t be64; |
| 173 |
uImgHeader hdr; |
| 174 |
uint32_t res_size = 0; |
| 175 |
uint32_t sect_count; |
| 176 |
sImgSectionHdr sec; |
| 177 |
|
| 178 |
pe_header = genidl_ispe (fp, &be64); |
| 179 |
*dta = NULL; length[0]=0; |
| 180 |
if (!pe_header) |
| 181 |
return 0; |
| 182 |
fseek (fp, pe_header, SEEK_SET); |
| 183 |
if (be64) |
| 184 |
{ |
| 185 |
fread (&hdr.hdr64, 1, sizeof (sImgNtHeaders64), fp); |
| 186 |
if (hdr.hdr64.OptionalHeader.NumberOfRvaAndSizes > 2) |
| 187 |
res_size = hdr.hdr64.OptionalHeader.DataDirectory[2].size; |
| 188 |
sect_count = hdr.hdr64.FileHeader.NumberOfSections; |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
fread (&hdr.hdr32, 1, sizeof (sImgNtHeaders32), fp); |
| 193 |
if (hdr.hdr32.OptionalHeader.NumberOfRvaAndSizes > 2) |
| 194 |
res_size = hdr.hdr32.OptionalHeader.DataDirectory[2].size; |
| 195 |
sect_count = hdr.hdr32.FileHeader.NumberOfSections; |
| 196 |
} |
| 197 |
if (!res_size) |
| 198 |
return 1; |
| 199 |
while (sect_count > 0) |
| 200 |
{ |
| 201 |
fread (&sec, 1, sizeof (sImgSectionHdr),fp); |
| 202 |
if (!strcmp ((char *) sec.Name, ".rsrc")) |
| 203 |
{ |
| 204 |
fseek (fp, (int32_t) sec.PointerToRawData, SEEK_SET); |
| 205 |
resRVA[0] = sec.VirtualAddress; |
| 206 |
dta[0] = (unsigned char *) malloc (res_size); |
| 207 |
fread (dta[0], 1, res_size, fp); |
| 208 |
length[0] = res_size; |
| 209 |
return 1; |
| 210 |
} |
| 211 |
sect_count--; |
| 212 |
} |
| 213 |
return 1; |
| 214 |
} |
| 215 |
|
| 216 |
int32_t |
| 217 |
genidl_ispe (FILE *fp, int32_t *be64) |
| 218 |
{ |
| 219 |
sDosHeader hdr; |
| 220 |
uImgHeader img; |
| 221 |
|
| 222 |
*be64 = 0; |
| 223 |
fseek(fp, 0, SEEK_SET); |
| 224 |
if (fread (&hdr, 1, sizeof (hdr), fp) != sizeof (hdr)) |
| 225 |
return 0; |
| 226 |
if (hdr.magic != 0x5a4d) |
| 227 |
return 0; |
| 228 |
if (hdr.lfanew == 0) |
| 229 |
return 0; |
| 230 |
fseek (fp, (int32_t) hdr.lfanew, SEEK_SET); |
| 231 |
if (fread (&img, 1, sizeof (img), fp) != sizeof (img)) |
| 232 |
return 0; |
| 233 |
if (img.hdr32.Signature != 0x4550) |
| 234 |
return 0; |
| 235 |
if (img.hdr32.OptionalHeader.Magic == 0x10b) |
| 236 |
*be64 = 0; |
| 237 |
else if (img.hdr64.OptionalHeader.Magic == 0x20b) |
| 238 |
*be64 = 1; |
| 239 |
else |
| 240 |
return 0; |
| 241 |
return (int32_t) hdr.lfanew; |
| 242 |
} |