6 unsigned int is_array:1;
8 unsigned int is_struct:1; // split for args
11 struct parsed_proto_arg {
13 struct parsed_type type;
14 struct parsed_proto *fptr;
21 struct parsed_type ret_type;
22 struct parsed_type type;
24 struct parsed_proto_arg arg[16];
28 unsigned int is_func:1;
29 unsigned int is_stdcall:1;
30 unsigned int is_vararg:1;
31 unsigned int is_fptr:1;
32 unsigned int is_noreturn:1;
33 unsigned int has_structarg:1;
36 static const char *hdrfn;
37 static int hdrfline = 0;
39 static void pp_copy_arg(struct parsed_proto_arg *d,
40 const struct parsed_proto_arg *s);
42 static int b_pp_c_handler(char *proto, const char *fname);
44 static int do_protostrs(FILE *fhdr, const char *fname)
46 const char *finc_name;
47 const char *hdrfn_saved;
57 while (fgets(protostr, sizeof(protostr), fhdr))
60 if (strncmp(protostr, "//#include ", 11) == 0) {
61 finc_name = protostr + 11;
62 p = strpbrk(finc_name, "\r\n ");
66 finc = fopen(finc_name, "r");
68 printf("%s:%d: can't open '%s'\n",
69 fname, line, finc_name);
72 ret = do_protostrs(finc, finc_name);
78 if (strncmp(sskip(protostr), "//", 2) == 0)
81 p = protostr + strlen(protostr);
82 for (p--; p >= protostr && my_isblank(*p); --p)
89 ret = b_pp_c_handler(protostr, hdrfn);
102 static int get_regparm(char *dst, size_t dlen, char *p)
109 for (o = 0, i = 1; o < dlen; i++) {
121 static const char *known_type_mod[] = {
130 static const char *known_ptr_types[] = {
150 "PMEMORY_BASIC_INFORMATION",
160 static const char *ignored_keywords[] = {
168 // returns ptr to char after type ends
169 static int typecmp(const char *n, const char *t)
171 for (; *t != 0; n++, t++) {
172 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
174 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
183 static const char *skip_type_mod(const char *n)
188 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
189 len = strlen(known_type_mod[i]);
190 if (strncmp(n, known_type_mod[i], len) != 0)
192 if (!my_isblank(n[len]))
196 while (my_isblank(*n))
204 static int check_type(const char *name, struct parsed_type *type)
210 n = skip_type_mod(name);
212 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
213 if (typecmp(n, known_ptr_types[i]))
220 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
223 // assume single word
224 while (!my_isblank(*n) && !my_issep(*n))
229 while (my_isblank(*n))
240 type->name = strndup(name, ret);
241 if (IS(type->name, "VOID"))
242 memcpy(type->name, "void", 4);
247 /* args are always expanded to 32bit */
248 static const char *map_reg(const char *reg)
250 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
251 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
252 const char *regs_b[] = { "al", "bl", "cl", "dl" };
255 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
256 if (IS(reg, regs_w[i]))
259 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
260 if (IS(reg, regs_b[i]))
266 static int check_struct_arg(struct parsed_proto_arg *arg)
268 if (IS(arg->type.name, "POINT"))
274 static int parse_protostr(char *protostr, struct parsed_proto *pp)
276 struct parsed_proto_arg *arg;
286 if (p[0] == '/' && p[1] == '/') {
287 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
291 // strip unneeded stuff
292 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
293 if ((p1[0] == '/' && p1[1] == '*')
294 || (p1[0] == '*' && p1[1] == '/'))
298 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
303 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
304 l = strlen(ignored_keywords[i]);
305 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
306 p = sskip(p + l + 1);
309 ret = check_type(p, &pp->ret_type);
311 printf("%s:%d:%zd: unhandled return in '%s'\n",
312 hdrfn, hdrfline, (p - protostr) + 1, protostr);
317 if (!strchr(p, ')')) {
318 p = next_idt(buf, sizeof(buf), p);
321 printf("%s:%d:%zd: var name missing\n",
322 hdrfn, hdrfline, (p - protostr) + 1);
325 strcpy(pp->name, buf);
330 pp->ret_type.is_array = 1;
342 p = next_word(cconv, sizeof(cconv), p);
345 printf("%s:%d:%zd: cconv missing\n",
346 hdrfn, hdrfline, (p - protostr) + 1);
349 if (IS(cconv, "__cdecl"))
351 else if (IS(cconv, "__stdcall"))
353 else if (IS(cconv, "__fastcall"))
355 else if (IS(cconv, "__thiscall"))
357 else if (IS(cconv, "__userpurge"))
358 pp->is_stdcall = 1; // IDA
359 else if (IS(cconv, "__usercall"))
360 pp->is_stdcall = 0; // IDA
361 else if (IS(cconv, "WINAPI"))
364 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
365 hdrfn, hdrfline, (p - protostr) + 1, cconv);
371 printf("%s:%d:%zd: '*' expected\n",
372 hdrfn, hdrfline, (p - protostr) + 1);
376 // XXX: skipping extra asterisks, for now
382 p = next_idt(buf, sizeof(buf), p);
385 //printf("%s:%d:%zd: func name missing\n",
386 // hdrfn, hdrfline, (p - protostr) + 1);
389 strcpy(pp->name, buf);
391 ret = get_regparm(regparm, sizeof(regparm), p);
393 if (!IS(regparm, "eax") && !IS(regparm, "ax")
394 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
396 printf("%s:%d:%zd: bad regparm: %s\n",
397 hdrfn, hdrfline, (p - protostr) + 1, regparm);
406 // not really ret_type is array, but ohwell
407 pp->ret_type.is_array = 1;
408 p = strchr(p + 1, ']');
410 printf("%s:%d:%zd: ']' expected\n",
411 hdrfn, hdrfline, (p - protostr) + 1);
417 printf("%s:%d:%zd: ')' expected\n",
418 hdrfn, hdrfline, (p - protostr) + 1);
425 printf("%s:%d:%zd: '(' expected, got '%c'\n",
426 hdrfn, hdrfline, (p - protostr) + 1, *p);
433 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
434 && *sskip(p + 4) == ')')
445 printf("%s:%d:%zd: ',' expected\n",
446 hdrfn, hdrfline, (p - protostr) + 1);
452 if (!strncmp(p, "...", 3)) {
459 printf("%s:%d:%zd: ')' expected\n",
460 hdrfn, hdrfline, (p - protostr) + 1);
464 arg = &pp->arg[xarg];
468 ret = check_type(p, &arg->type);
470 printf("%s:%d:%zd: unhandled type for arg%d\n",
471 hdrfn, hdrfline, (p - protostr) + 1, xarg);
478 arg->fptr = calloc(1, sizeof(*arg->fptr));
479 ret = parse_protostr(p1, arg->fptr);
481 printf("%s:%d:%zd: funcarg parse failed\n",
482 hdrfn, hdrfline, p1 - protostr);
485 // we'll treat it as void * for non-calls
486 arg->type.name = strdup("void *");
487 arg->type.is_ptr = 1;
492 p = next_idt(buf, sizeof(buf), p);
496 printf("%s:%d:%zd: idt missing for arg%d\n",
497 hdrfn, hdrfline, (p - protostr) + 1, xarg);
503 ret = get_regparm(regparm, sizeof(regparm), p);
508 arg->reg = strdup(map_reg(regparm));
511 ret = check_struct_arg(arg);
513 pp->has_structarg = 1;
514 arg->type.is_struct = 1;
515 free(arg->type.name);
516 arg->type.name = strdup("int");
517 for (l = 0; l < ret; l++) {
518 pp_copy_arg(&pp->arg[xarg], arg);
524 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
525 if (pp->arg[0].reg != NULL) {
526 printf("%s:%d: %s with arg1 spec %s?\n",
527 hdrfn, hdrfline, cconv, pp->arg[0].reg);
529 pp->arg[0].reg = strdup("ecx");
532 if (xarg > 1 && IS(cconv, "__fastcall")) {
533 if (pp->arg[1].reg != NULL) {
534 printf("%s:%d: %s with arg2 spec %s?\n",
535 hdrfn, hdrfline, cconv, pp->arg[1].reg);
537 pp->arg[1].reg = strdup("edx");
540 if (pp->is_vararg && pp->is_stdcall) {
541 printf("%s:%d: vararg stdcall?\n", hdrfn, hdrfline);
547 for (i = 0; i < pp->argc; i++) {
548 if (pp->arg[i].reg == NULL)
557 static int pp_name_cmp(const void *p1, const void *p2)
559 const struct parsed_proto *pp1 = p1, *pp2 = p2;
560 return strcmp(pp1->name, pp2->name);
563 static struct parsed_proto *pp_cache;
564 static int pp_cache_size;
565 static int pp_cache_alloc;
567 static int b_pp_c_handler(char *proto, const char *fname)
571 if (pp_cache_size >= pp_cache_alloc) {
572 pp_cache_alloc = pp_cache_alloc * 2 + 64;
573 pp_cache = realloc(pp_cache, pp_cache_alloc
574 * sizeof(pp_cache[0]));
575 my_assert_not(pp_cache, NULL);
576 memset(pp_cache + pp_cache_size, 0,
577 (pp_cache_alloc - pp_cache_size)
578 * sizeof(pp_cache[0]));
581 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
589 static void build_pp_cache(FILE *fhdr)
595 ret = do_protostrs(fhdr, hdrfn);
599 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
602 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym)
604 const struct parsed_proto *pp_ret;
605 struct parsed_proto pp_search;
607 if (pp_cache == NULL)
608 build_pp_cache(fhdr);
610 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
613 strcpy(pp_search.name, sym);
614 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
615 sizeof(pp_cache[0]), pp_name_cmp);
617 printf("%s: sym '%s' is missing\n", hdrfn, sym);
622 static void pp_copy_arg(struct parsed_proto_arg *d,
623 const struct parsed_proto_arg *s)
625 memcpy(d, s, sizeof(*d));
627 if (s->reg != NULL) {
628 d->reg = strdup(s->reg);
629 my_assert_not(d->reg, NULL);
631 if (s->type.name != NULL) {
632 d->type.name = strdup(s->type.name);
633 my_assert_not(d->type.name, NULL);
635 if (s->fptr != NULL) {
636 d->fptr = malloc(sizeof(*d->fptr));
637 my_assert_not(d->fptr, NULL);
638 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
642 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
644 struct parsed_proto *pp;
647 pp = malloc(sizeof(*pp));
648 my_assert_not(pp, NULL);
649 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
651 // do the actual deep copy..
652 for (i = 0; i < pp_c->argc; i++)
653 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
654 if (pp_c->ret_type.name != NULL)
655 pp->ret_type.name = strdup(pp_c->ret_type.name);
660 static inline void proto_release(struct parsed_proto *pp)
664 for (i = 0; i < pp->argc; i++) {
665 if (pp->arg[i].reg != NULL)
666 free(pp->arg[i].reg);
667 if (pp->arg[i].type.name != NULL)
668 free(pp->arg[i].type.name);
669 if (pp->arg[i].fptr != NULL)
670 free(pp->arg[i].fptr);
672 if (pp->ret_type.name != NULL)
673 free(pp->ret_type.name);