| 1 |
/* |
| 2 |
genidl - Generate interface listing from a Portable Executable. |
| 3 |
Copyright (C) 2009-2016 mingw-w64 project |
| 4 |
|
| 5 |
This program is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation, either version 3 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#define _CRT_SECURE_NO_WARNINGS |
| 20 |
#include "genidl_cfg.h" |
| 21 |
#include "genidl_readpe.h" |
| 22 |
#include "genidl_typeinfo.h" |
| 23 |
#include "genidl_typinfo.h" |
| 24 |
#include "fsredir.h" |
| 25 |
|
| 26 |
/* Configure globals. */ |
| 27 |
int show_dump_too = 0; |
| 28 |
int generate_header = 0; |
| 29 |
int is_verbose = 0; |
| 30 |
|
| 31 |
/* Process files. */ |
| 32 |
size_t file_args_cnt = 0; |
| 33 |
char **file_args = NULL; |
| 34 |
const char *basedumpname = ""; |
| 35 |
|
| 36 |
static char *get_idl_basename (const char *file); |
| 37 |
|
| 38 |
#ifdef REDIRECTOR |
| 39 |
static int use_redirector = 0; /* Use/Disable FS redirector */ |
| 40 |
#endif |
| 41 |
|
| 42 |
static void |
| 43 |
show_usage (void) |
| 44 |
{ |
| 45 |
fprintf (stderr, "Usage: genidl [OPTION]... [FILE]...\n"); |
| 46 |
fprintf (stderr, "Dumps IDL information from typelib data found in PE32/PE32+ executables and\n" |
| 47 |
"TLB files.\n"); |
| 48 |
fprintf (stderr, "\n"); |
| 49 |
fprintf (stderr, "Options:\n" |
| 50 |
" -b ARG, --basedumpname=ARG\n" |
| 51 |
" Specify ARG as prefix of generated idl files.\n" |
| 52 |
" -H, --header Generate header\n" |
| 53 |
" -d, --dump Dump additional internal debugging information.\n" |
| 54 |
" -v, --verbose Show additional status prints.\n" |
| 55 |
" -h, --help Show this help.\n" |
| 56 |
#ifdef REDIRECTOR |
| 57 |
" -r, --disable-fs-redirector\n" |
| 58 |
" Disable Win64 FS redirection, for 32-bit\n" |
| 59 |
" gendef on 64-bit Windows\n" |
| 60 |
#endif |
| 61 |
); |
| 62 |
fprintf (stderr, "\nReport bugs to <[email protected]>\n"); |
| 63 |
exit (1); |
| 64 |
} |
| 65 |
|
| 66 |
static int |
| 67 |
scanArgs (int argc, char **argv) |
| 68 |
{ |
| 69 |
int seen_error = 0; |
| 70 |
if (!argc) |
| 71 |
return -1; |
| 72 |
file_args = (char **) malloc (sizeof (char *) * argc); |
| 73 |
while (argc > 0) |
| 74 |
{ |
| 75 |
char *h = *argv; |
| 76 |
if (h[0] == '-') |
| 77 |
{ |
| 78 |
h++; |
| 79 |
switch (*h) { |
| 80 |
case '-': /* Long arguments section */ |
| 81 |
h++; |
| 82 |
switch (*h) { |
| 83 |
case 'H': |
| 84 |
generate_header = 1; |
| 85 |
break; |
| 86 |
case 'd': |
| 87 |
if(! strcmp (h, "dump")) |
| 88 |
{ |
| 89 |
show_dump_too = 1; |
| 90 |
break; |
| 91 |
} |
| 92 |
else |
| 93 |
goto unknown_fail; |
| 94 |
case 'h': |
| 95 |
if (!strcmp (h, "header")) |
| 96 |
{ |
| 97 |
generate_header = 1; |
| 98 |
break; |
| 99 |
} |
| 100 |
if (!strcmp (h, "help")) return -2; |
| 101 |
goto unknown_fail; |
| 102 |
case 'v': |
| 103 |
if (! strcmp (h, "verbose")) |
| 104 |
is_verbose++; |
| 105 |
else |
| 106 |
goto unknown_fail; |
| 107 |
break; |
| 108 |
case 'b': |
| 109 |
if (! strncmp(h, "basedumpname=", 13)) |
| 110 |
{ |
| 111 |
basedumpname = &(h[13]); |
| 112 |
break; |
| 113 |
} |
| 114 |
else |
| 115 |
goto unknown_fail; |
| 116 |
#ifdef REDIRECTOR |
| 117 |
case 'r': |
| 118 |
if(! strcmp (h, "disable-fs-redirector")) |
| 119 |
{ |
| 120 |
use_redirector = 1; |
| 121 |
break; |
| 122 |
} |
| 123 |
else |
| 124 |
goto unknown_fail; |
| 125 |
#endif |
| 126 |
default: goto unknown_fail; |
| 127 |
} |
| 128 |
break; |
| 129 |
/* Short arguments section */ |
| 130 |
case 'd': |
| 131 |
if (h[1] == 0) |
| 132 |
show_dump_too = 1; |
| 133 |
else |
| 134 |
goto unknown_fail; |
| 135 |
break; |
| 136 |
case 'h': |
| 137 |
if (h[1] == 0) |
| 138 |
return -2; |
| 139 |
goto unknown_fail; |
| 140 |
case 'b': |
| 141 |
if (h[1] == 0) |
| 142 |
{ |
| 143 |
basedumpname = *(++argv); |
| 144 |
--argc; |
| 145 |
break; |
| 146 |
} |
| 147 |
goto unknown_fail; |
| 148 |
case 'v': |
| 149 |
if (h[1] != 0) |
| 150 |
goto unknown_fail; |
| 151 |
is_verbose++; |
| 152 |
break; |
| 153 |
#ifdef REDIRECTOR |
| 154 |
case 'r': |
| 155 |
if (h[1] != 0) |
| 156 |
goto unknown_fail; |
| 157 |
use_redirector = 1; |
| 158 |
break; |
| 159 |
#endif |
| 160 |
default: |
| 161 |
unknown_fail: |
| 162 |
fprintf (stderr, "Option %s' is unknown.\n", *argv); |
| 163 |
seen_error = 1; |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
else |
| 168 |
{ |
| 169 |
file_args[file_args_cnt++] = *argv; |
| 170 |
} |
| 171 |
--argc; |
| 172 |
++argv; |
| 173 |
} |
| 174 |
if (seen_error || file_args_cnt == 0) |
| 175 |
return -2; |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
int main(int argc,char **argv) |
| 180 |
{ |
| 181 |
FILE *fp,*gp; |
| 182 |
long p; |
| 183 |
int32_t be64; |
| 184 |
size_t len; |
| 185 |
size_t i; |
| 186 |
unsigned char *dta = NULL; |
| 187 |
genidl_read_config ("./genidl.conf"); |
| 188 |
if (scanArgs (--argc, ++argv) < 0) |
| 189 |
{ |
| 190 |
show_usage (); |
| 191 |
} |
| 192 |
#ifdef REDIRECTOR |
| 193 |
doredirect(use_redirector); |
| 194 |
#endif |
| 195 |
for (i = 0; i < file_args_cnt; i++) |
| 196 |
{ |
| 197 |
char s[1024], *idl_basename,*org_basename; |
| 198 |
int start, end; |
| 199 |
gp = fopen (file_args[i], "rb"); |
| 200 |
if (!gp) |
| 201 |
{ |
| 202 |
fprintf (stderr, "Failed to open file ,%s'.\n", file_args[i]); |
| 203 |
continue; |
| 204 |
} |
| 205 |
p = genidl_ispe (gp, &be64); |
| 206 |
|
| 207 |
if (is_verbose) |
| 208 |
fprintf (stderr, "Found PE at %ld (%s bits)\n", p, !be64 ? "32" : "64"); |
| 209 |
|
| 210 |
end = genidl_pe_typelib_resource_count (gp); |
| 211 |
|
| 212 |
if (is_verbose) |
| 213 |
fprintf (stderr, "Contains %d typelib resource(s)\n", end); |
| 214 |
org_basename = get_idl_basename (file_args[i]); |
| 215 |
idl_basename = strdup (org_basename); |
| 216 |
if (strrchr (idl_basename, '.') != NULL) |
| 217 |
*strrchr (idl_basename, '.') = 0; |
| 218 |
|
| 219 |
for (start = 0; start < end; start++) |
| 220 |
{ |
| 221 |
genidl_pe_typelib_resource_read (gp, start, &dta, &len); |
| 222 |
if (generate_header == 0) |
| 223 |
{ |
| 224 |
if (end != 1) |
| 225 |
sprintf (s, "%s%s_%d.idl", idl_basename, basedumpname, start); |
| 226 |
else |
| 227 |
sprintf (s, "%s%s.idl", idl_basename, basedumpname); |
| 228 |
fp = fopen (s, "wb"); |
| 229 |
if (fp) |
| 230 |
{ |
| 231 |
sTI2TypLib *tl = TI2_typlib_init (dta, (size_t) len); |
| 232 |
if (tl) |
| 233 |
{ |
| 234 |
TI2_typlib_idl (fp, tl, org_basename); |
| 235 |
TI2_typlib_dest (tl); |
| 236 |
} |
| 237 |
if (show_dump_too) |
| 238 |
dumpInfo (fp, dta, len); |
| 239 |
fclose (fp); |
| 240 |
} |
| 241 |
} |
| 242 |
else if (generate_header == 1) |
| 243 |
{ |
| 244 |
if (end != 1) |
| 245 |
sprintf (s, "%s%s_%d.h", idl_basename, basedumpname, start); |
| 246 |
else |
| 247 |
sprintf (s, "%s%s.h", idl_basename, basedumpname); |
| 248 |
fp = fopen (s, "wb"); |
| 249 |
if (fp) |
| 250 |
{ |
| 251 |
sTI2TypLib *tl = TI2_typlib_init (dta, (size_t) len); |
| 252 |
if (tl) |
| 253 |
{ |
| 254 |
TI2_typlib_hdr (fp, tl, org_basename); |
| 255 |
TI2_typlib_dest (tl); |
| 256 |
} |
| 257 |
fclose (fp); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
free (idl_basename); |
| 262 |
free (org_basename); |
| 263 |
fclose (gp); |
| 264 |
} |
| 265 |
/* genidl_save_config_fp (stderr); */ |
| 266 |
genidl_save_config ("./genidl.conf"); |
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
static char * |
| 271 |
get_idl_basename (const char *file) |
| 272 |
{ |
| 273 |
char *h, *p; |
| 274 |
if (!file || *file == 0) |
| 275 |
return strdup (""); |
| 276 |
h = strrchr (file, '\\'); |
| 277 |
p = strrchr (file, '/'); |
| 278 |
if (!h && !p) |
| 279 |
h = strdup (file); |
| 280 |
else if (!h) |
| 281 |
h = strdup (p + 1); |
| 282 |
else if (!p) |
| 283 |
h = strdup (h + 1); |
| 284 |
else if (p < h) |
| 285 |
h = strdup (h + 1); |
| 286 |
else |
| 287 |
h = strdup (p + 1); |
| 288 |
genidl_strlwr (h); |
| 289 |
return h; |
| 290 |
} |
| 291 |
|
| 292 |
char * |
| 293 |
genidl_strlwr (char *s) |
| 294 |
{ |
| 295 |
char *h = s; |
| 296 |
if (!h) |
| 297 |
return NULL; |
| 298 |
while (*h != 0) |
| 299 |
{ |
| 300 |
if (h[0] >= 'A' && h[0] <= 'Z') |
| 301 |
h[0] = (h[0] - 'A') + 'a'; |
| 302 |
++h; |
| 303 |
} |
| 304 |
|
| 305 |
return s; |
| 306 |
} |