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_fastcall:1;
31 unsigned int is_vararg:1; // vararg func
32 unsigned int is_fptr:1;
33 unsigned int is_noreturn:1;
34 unsigned int is_unresolved:1;
35 unsigned int is_arg:1; // decl in func arg
36 unsigned int has_structarg:1;
39 static const char *hdrfn;
40 static int hdrfline = 0;
42 static void pp_copy_arg(struct parsed_proto_arg *d,
43 const struct parsed_proto_arg *s);
45 static int b_pp_c_handler(char *proto, const char *fname);
47 static int do_protostrs(FILE *fhdr, const char *fname)
49 const char *finc_name;
50 const char *hdrfn_saved;
62 while (fgets(protostr, sizeof(protostr), fhdr))
65 if (strncmp(protostr, "//#include ", 11) == 0) {
66 finc_name = protostr + 11;
67 p = strpbrk(finc_name, "\r\n ");
72 p = strrchr(hdrfn_saved, '/');
74 memcpy(path, hdrfn_saved,
76 path[p - hdrfn_saved + 1] = 0;
78 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
80 finc = fopen(fname_inc, "r");
82 printf("%s:%d: can't open '%s'\n",
83 fname_inc, line, finc_name);
86 ret = do_protostrs(finc, finc_name);
92 if (strncmp(sskip(protostr), "//", 2) == 0)
95 p = protostr + strlen(protostr);
96 for (p--; p >= protostr && my_isblank(*p); --p)
103 ret = b_pp_c_handler(protostr, hdrfn);
116 static int get_regparm(char *dst, size_t dlen, char *p)
123 for (o = 0, i = 1; o < dlen; i++) {
135 static const char *known_type_mod[] = {
145 static const char *known_ptr_types[] = {
172 "PMEMORY_BASIC_INFORMATION",
184 static const char *ignored_keywords[] = {
192 // returns ptr to char after type ends
193 static int typecmp(const char *n, const char *t)
195 for (; *t != 0; n++, t++) {
196 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
198 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
207 static const char *skip_type_mod(const char *n)
212 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
213 len = strlen(known_type_mod[i]);
214 if (strncmp(n, known_type_mod[i], len) != 0)
216 if (!my_isblank(n[len]))
220 while (my_isblank(*n))
228 static int check_type(const char *name, struct parsed_type *type)
234 n = skip_type_mod(name);
236 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
237 if (typecmp(n, known_ptr_types[i]))
244 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
247 // assume single word
248 while (!my_isblank(*n) && !my_issep(*n))
253 while (my_isblank(*n))
264 type->name = strndup(name, ret);
265 if (IS(type->name, "VOID"))
266 memcpy(type->name, "void", 4);
271 /* args are always expanded to 32bit */
272 static const char *map_reg(const char *reg)
274 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
275 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
276 const char *regs_b[] = { "al", "bl", "cl", "dl" };
279 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
280 if (IS(reg, regs_w[i]))
283 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
284 if (IS(reg, regs_b[i]))
290 static int check_struct_arg(struct parsed_proto_arg *arg)
292 if (IS(arg->type.name, "POINT"))
298 static int parse_protostr(char *protostr, struct parsed_proto *pp)
300 struct parsed_proto_arg *arg;
310 if (p[0] == '/' && p[1] == '/') {
311 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
315 // strip unneeded stuff
316 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
317 if ((p1[0] == '/' && p1[1] == '*')
318 || (p1[0] == '*' && p1[1] == '/'))
322 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
327 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
328 l = strlen(ignored_keywords[i]);
329 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
330 p = sskip(p + l + 1);
333 ret = check_type(p, &pp->ret_type);
335 printf("%s:%d:%zd: unhandled return in '%s'\n",
336 hdrfn, hdrfline, (p - protostr) + 1, protostr);
341 if (!strncmp(p, "noreturn ", 9)) {
346 if (!strchr(p, ')')) {
347 p = next_idt(buf, sizeof(buf), p);
350 printf("%s:%d:%zd: var name missing\n",
351 hdrfn, hdrfline, (p - protostr) + 1);
354 strcpy(pp->name, buf);
359 pp->ret_type.is_array = 1;
371 p = next_word(cconv, sizeof(cconv), p);
374 printf("%s:%d:%zd: cconv missing\n",
375 hdrfn, hdrfline, (p - protostr) + 1);
378 if (IS(cconv, "__cdecl"))
380 else if (IS(cconv, "__stdcall"))
382 else if (IS(cconv, "__fastcall")) {
384 pp->is_stdcall = 1; // sort of..
386 else if (IS(cconv, "__thiscall"))
388 else if (IS(cconv, "__userpurge"))
389 pp->is_stdcall = 1; // IDA
390 else if (IS(cconv, "__usercall"))
391 pp->is_stdcall = 0; // IDA
392 else if (IS(cconv, "WINAPI"))
395 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
396 hdrfn, hdrfline, (p - protostr) + 1, cconv);
402 printf("%s:%d:%zd: '*' expected\n",
403 hdrfn, hdrfline, (p - protostr) + 1);
407 // XXX: skipping extra asterisks, for now
413 p = next_idt(buf, sizeof(buf), p);
416 //printf("%s:%d:%zd: func name missing\n",
417 // hdrfn, hdrfline, (p - protostr) + 1);
420 strcpy(pp->name, buf);
422 ret = get_regparm(regparm, sizeof(regparm), p);
424 if (!IS(regparm, "eax") && !IS(regparm, "ax")
425 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
427 printf("%s:%d:%zd: bad regparm: %s\n",
428 hdrfn, hdrfline, (p - protostr) + 1, regparm);
437 // not really ret_type is array, but ohwell
438 pp->ret_type.is_array = 1;
439 p = strchr(p + 1, ']');
441 printf("%s:%d:%zd: ']' expected\n",
442 hdrfn, hdrfline, (p - protostr) + 1);
448 printf("%s:%d:%zd: ')' expected\n",
449 hdrfn, hdrfline, (p - protostr) + 1);
456 printf("%s:%d:%zd: '(' expected, got '%c'\n",
457 hdrfn, hdrfline, (p - protostr) + 1, *p);
464 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
465 && *sskip(p + 4) == ')')
476 printf("%s:%d:%zd: ',' expected\n",
477 hdrfn, hdrfline, (p - protostr) + 1);
483 if (!strncmp(p, "...", 3)) {
490 printf("%s:%d:%zd: ')' expected\n",
491 hdrfn, hdrfline, (p - protostr) + 1);
495 arg = &pp->arg[xarg];
499 ret = check_type(p, &arg->type);
501 printf("%s:%d:%zd: unhandled type for arg%d\n",
502 hdrfn, hdrfline, (p - protostr) + 1, xarg);
509 arg->fptr = calloc(1, sizeof(*arg->fptr));
510 ret = parse_protostr(p1, arg->fptr);
512 printf("%s:%d:%zd: funcarg parse failed\n",
513 hdrfn, hdrfline, p1 - protostr);
516 arg->fptr->is_arg = 1;
517 // we don't use actual names right now..
518 snprintf(arg->fptr->name,
519 sizeof(arg->fptr->name), "a%d", xarg);
520 // we'll treat it as void * for non-calls
521 arg->type.name = strdup("void *");
522 arg->type.is_ptr = 1;
527 p = next_idt(buf, sizeof(buf), p);
531 printf("%s:%d:%zd: idt missing for arg%d\n",
532 hdrfn, hdrfline, (p - protostr) + 1, xarg);
538 ret = get_regparm(regparm, sizeof(regparm), p);
543 arg->reg = strdup(map_reg(regparm));
546 if (strstr(arg->type.name, "int64")
547 || IS(arg->type.name, "double"))
550 free(arg->type.name);
551 arg->type.name = strdup("int");
552 pp_copy_arg(&pp->arg[xarg], arg);
556 ret = check_struct_arg(arg);
558 pp->has_structarg = 1;
559 arg->type.is_struct = 1;
560 free(arg->type.name);
561 arg->type.name = strdup("int");
562 for (l = 0; l < ret; l++) {
563 pp_copy_arg(&pp->arg[xarg], arg);
569 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
570 if (pp->arg[0].reg != NULL) {
571 printf("%s:%d: %s with arg1 spec %s?\n",
572 hdrfn, hdrfline, cconv, pp->arg[0].reg);
574 pp->arg[0].reg = strdup("ecx");
577 if (xarg > 1 && IS(cconv, "__fastcall")) {
578 if (pp->arg[1].reg != NULL) {
579 printf("%s:%d: %s with arg2 spec %s?\n",
580 hdrfn, hdrfline, cconv, pp->arg[1].reg);
582 pp->arg[1].reg = strdup("edx");
587 for (i = 0; i < pp->argc; i++) {
588 if (pp->arg[i].reg == NULL)
594 if (pp->argc == 1 && pp->arg[0].reg != NULL
595 && IS(pp->arg[0].reg, "ecx"))
599 else if (pp->argc_reg == 2
600 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
601 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
606 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
607 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
614 static int pp_name_cmp(const void *p1, const void *p2)
616 const struct parsed_proto *pp1 = p1, *pp2 = p2;
617 return strcmp(pp1->name, pp2->name);
620 static struct parsed_proto *pp_cache;
621 static int pp_cache_size;
622 static int pp_cache_alloc;
624 static int b_pp_c_handler(char *proto, const char *fname)
628 if (pp_cache_size >= pp_cache_alloc) {
629 pp_cache_alloc = pp_cache_alloc * 2 + 64;
630 pp_cache = realloc(pp_cache, pp_cache_alloc
631 * sizeof(pp_cache[0]));
632 my_assert_not(pp_cache, NULL);
633 memset(pp_cache + pp_cache_size, 0,
634 (pp_cache_alloc - pp_cache_size)
635 * sizeof(pp_cache[0]));
638 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
646 static void build_pp_cache(FILE *fhdr)
654 ret = do_protostrs(fhdr, hdrfn);
658 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
659 fseek(fhdr, pos, SEEK_SET);
662 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
665 const struct parsed_proto *pp_ret;
666 struct parsed_proto pp_search;
668 if (pp_cache == NULL)
669 build_pp_cache(fhdr);
671 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
674 strcpy(pp_search.name, sym);
675 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
676 sizeof(pp_cache[0]), pp_name_cmp);
677 if (pp_ret == NULL && !quiet)
678 printf("%s: sym '%s' is missing\n", hdrfn, sym);
683 static void pp_copy_arg(struct parsed_proto_arg *d,
684 const struct parsed_proto_arg *s)
686 memcpy(d, s, sizeof(*d));
688 if (s->reg != NULL) {
689 d->reg = strdup(s->reg);
690 my_assert_not(d->reg, NULL);
692 if (s->type.name != NULL) {
693 d->type.name = strdup(s->type.name);
694 my_assert_not(d->type.name, NULL);
696 if (s->fptr != NULL) {
697 d->fptr = malloc(sizeof(*d->fptr));
698 my_assert_not(d->fptr, NULL);
699 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
703 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
705 struct parsed_proto *pp;
708 pp = malloc(sizeof(*pp));
709 my_assert_not(pp, NULL);
710 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
712 // do the actual deep copy..
713 for (i = 0; i < pp_c->argc; i++)
714 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
715 if (pp_c->ret_type.name != NULL)
716 pp->ret_type.name = strdup(pp_c->ret_type.name);
721 static inline void pp_print(char *buf, size_t buf_size,
722 const struct parsed_proto *pp)
727 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
730 for (i = 0; i < pp->argc_reg; i++) {
731 snprintf(buf + l, buf_size - l, "%s%s",
732 i == 0 ? "" : ", ", pp->arg[i].reg);
735 if (pp->argc_stack > 0) {
736 snprintf(buf + l, buf_size - l, "%s{%d stack}",
737 i == 0 ? "" : ", ", pp->argc_stack);
740 snprintf(buf + l, buf_size - l, ")");
743 static inline void proto_release(struct parsed_proto *pp)
747 for (i = 0; i < pp->argc; i++) {
748 if (pp->arg[i].reg != NULL)
749 free(pp->arg[i].reg);
750 if (pp->arg[i].type.name != NULL)
751 free(pp->arg[i].type.name);
752 if (pp->arg[i].fptr != NULL)
753 free(pp->arg[i].fptr);
755 if (pp->ret_type.name != NULL)
756 free(pp->ret_type.name);