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[] = {
173 "PMEMORY_BASIC_INFORMATION",
185 static const char *ignored_keywords[] = {
193 // returns ptr to char after type ends
194 static int typecmp(const char *n, const char *t)
196 for (; *t != 0; n++, t++) {
197 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
199 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
208 static const char *skip_type_mod(const char *n)
213 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
214 len = strlen(known_type_mod[i]);
215 if (strncmp(n, known_type_mod[i], len) != 0)
217 if (!my_isblank(n[len]))
221 while (my_isblank(*n))
229 static int check_type(const char *name, struct parsed_type *type)
235 n = skip_type_mod(name);
237 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
238 if (typecmp(n, known_ptr_types[i]))
245 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
248 // assume single word
249 while (!my_isblank(*n) && !my_issep(*n))
254 while (my_isblank(*n))
265 type->name = strndup(name, ret);
266 if (IS(type->name, "VOID"))
267 memcpy(type->name, "void", 4);
272 /* args are always expanded to 32bit */
273 static const char *map_reg(const char *reg)
275 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
276 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
277 const char *regs_b[] = { "al", "bl", "cl", "dl" };
280 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
281 if (IS(reg, regs_w[i]))
284 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
285 if (IS(reg, regs_b[i]))
291 static int check_struct_arg(struct parsed_proto_arg *arg)
293 if (IS(arg->type.name, "POINT"))
299 static int parse_protostr(char *protostr, struct parsed_proto *pp)
301 struct parsed_proto_arg *arg;
311 if (p[0] == '/' && p[1] == '/') {
312 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
316 // strip unneeded stuff
317 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
318 if ((p1[0] == '/' && p1[1] == '*')
319 || (p1[0] == '*' && p1[1] == '/'))
323 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
328 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
329 l = strlen(ignored_keywords[i]);
330 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
331 p = sskip(p + l + 1);
334 ret = check_type(p, &pp->ret_type);
336 printf("%s:%d:%zd: unhandled return in '%s'\n",
337 hdrfn, hdrfline, (p - protostr) + 1, protostr);
342 if (!strncmp(p, "noreturn ", 9)) {
347 if (!strchr(p, ')')) {
348 p = next_idt(buf, sizeof(buf), p);
351 printf("%s:%d:%zd: var name missing\n",
352 hdrfn, hdrfline, (p - protostr) + 1);
355 strcpy(pp->name, buf);
360 pp->ret_type.is_array = 1;
372 p = next_word(cconv, sizeof(cconv), p);
375 printf("%s:%d:%zd: cconv missing\n",
376 hdrfn, hdrfline, (p - protostr) + 1);
379 if (IS(cconv, "__cdecl"))
381 else if (IS(cconv, "__stdcall"))
383 else if (IS(cconv, "__fastcall")) {
385 pp->is_stdcall = 1; // sort of..
387 else if (IS(cconv, "__thiscall"))
389 else if (IS(cconv, "__userpurge"))
390 pp->is_stdcall = 1; // IDA
391 else if (IS(cconv, "__usercall"))
392 pp->is_stdcall = 0; // IDA
393 else if (IS(cconv, "WINAPI"))
396 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
397 hdrfn, hdrfline, (p - protostr) + 1, cconv);
403 printf("%s:%d:%zd: '*' expected\n",
404 hdrfn, hdrfline, (p - protostr) + 1);
408 // XXX: skipping extra asterisks, for now
414 p = next_idt(buf, sizeof(buf), p);
417 //printf("%s:%d:%zd: func name missing\n",
418 // hdrfn, hdrfline, (p - protostr) + 1);
421 strcpy(pp->name, buf);
423 ret = get_regparm(regparm, sizeof(regparm), p);
425 if (!IS(regparm, "eax") && !IS(regparm, "ax")
426 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
428 printf("%s:%d:%zd: bad regparm: %s\n",
429 hdrfn, hdrfline, (p - protostr) + 1, regparm);
438 // not really ret_type is array, but ohwell
439 pp->ret_type.is_array = 1;
440 p = strchr(p + 1, ']');
442 printf("%s:%d:%zd: ']' expected\n",
443 hdrfn, hdrfline, (p - protostr) + 1);
449 printf("%s:%d:%zd: ')' expected\n",
450 hdrfn, hdrfline, (p - protostr) + 1);
457 printf("%s:%d:%zd: '(' expected, got '%c'\n",
458 hdrfn, hdrfline, (p - protostr) + 1, *p);
465 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
466 && *sskip(p + 4) == ')')
477 printf("%s:%d:%zd: ',' expected\n",
478 hdrfn, hdrfline, (p - protostr) + 1);
484 if (!strncmp(p, "...", 3)) {
491 printf("%s:%d:%zd: ')' expected\n",
492 hdrfn, hdrfline, (p - protostr) + 1);
496 arg = &pp->arg[xarg];
500 ret = check_type(p, &arg->type);
502 printf("%s:%d:%zd: unhandled type for arg%d\n",
503 hdrfn, hdrfline, (p - protostr) + 1, xarg);
510 arg->fptr = calloc(1, sizeof(*arg->fptr));
511 ret = parse_protostr(p1, arg->fptr);
513 printf("%s:%d:%zd: funcarg parse failed\n",
514 hdrfn, hdrfline, p1 - protostr);
517 arg->fptr->is_arg = 1;
518 // we don't use actual names right now..
519 snprintf(arg->fptr->name,
520 sizeof(arg->fptr->name), "a%d", xarg);
521 // we'll treat it as void * for non-calls
522 arg->type.name = strdup("void *");
523 arg->type.is_ptr = 1;
528 p = next_idt(buf, sizeof(buf), p);
532 printf("%s:%d:%zd: idt missing for arg%d\n",
533 hdrfn, hdrfline, (p - protostr) + 1, xarg);
539 ret = get_regparm(regparm, sizeof(regparm), p);
544 arg->reg = strdup(map_reg(regparm));
547 if (strstr(arg->type.name, "int64")
548 || IS(arg->type.name, "double"))
551 free(arg->type.name);
552 arg->type.name = strdup("int");
553 pp_copy_arg(&pp->arg[xarg], arg);
557 ret = check_struct_arg(arg);
559 pp->has_structarg = 1;
560 arg->type.is_struct = 1;
561 free(arg->type.name);
562 arg->type.name = strdup("int");
563 for (l = 0; l < ret; l++) {
564 pp_copy_arg(&pp->arg[xarg], arg);
570 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
571 if (pp->arg[0].reg != NULL) {
572 printf("%s:%d: %s with arg1 spec %s?\n",
573 hdrfn, hdrfline, cconv, pp->arg[0].reg);
575 pp->arg[0].reg = strdup("ecx");
578 if (xarg > 1 && IS(cconv, "__fastcall")) {
579 if (pp->arg[1].reg != NULL) {
580 printf("%s:%d: %s with arg2 spec %s?\n",
581 hdrfn, hdrfline, cconv, pp->arg[1].reg);
583 pp->arg[1].reg = strdup("edx");
588 for (i = 0; i < pp->argc; i++) {
589 if (pp->arg[i].reg == NULL)
595 if (pp->argc == 1 && pp->arg[0].reg != NULL
596 && IS(pp->arg[0].reg, "ecx"))
600 else if (pp->argc_reg == 2
601 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
602 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
607 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
608 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
615 static int pp_name_cmp(const void *p1, const void *p2)
617 const struct parsed_proto *pp1 = p1, *pp2 = p2;
618 return strcmp(pp1->name, pp2->name);
621 static struct parsed_proto *pp_cache;
622 static int pp_cache_size;
623 static int pp_cache_alloc;
625 static int b_pp_c_handler(char *proto, const char *fname)
629 if (pp_cache_size >= pp_cache_alloc) {
630 pp_cache_alloc = pp_cache_alloc * 2 + 64;
631 pp_cache = realloc(pp_cache, pp_cache_alloc
632 * sizeof(pp_cache[0]));
633 my_assert_not(pp_cache, NULL);
634 memset(pp_cache + pp_cache_size, 0,
635 (pp_cache_alloc - pp_cache_size)
636 * sizeof(pp_cache[0]));
639 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
647 static void build_pp_cache(FILE *fhdr)
655 ret = do_protostrs(fhdr, hdrfn);
659 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
660 fseek(fhdr, pos, SEEK_SET);
663 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
666 const struct parsed_proto *pp_ret;
667 struct parsed_proto pp_search;
670 if (pp_cache == NULL)
671 build_pp_cache(fhdr);
673 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
676 strcpy(pp_search.name, sym);
677 p = strchr(pp_search.name, '@');
681 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
682 sizeof(pp_cache[0]), pp_name_cmp);
683 if (pp_ret == NULL && !quiet)
684 printf("%s: sym '%s' is missing\n", hdrfn, sym);
689 static void pp_copy_arg(struct parsed_proto_arg *d,
690 const struct parsed_proto_arg *s)
692 memcpy(d, s, sizeof(*d));
694 if (s->reg != NULL) {
695 d->reg = strdup(s->reg);
696 my_assert_not(d->reg, NULL);
698 if (s->type.name != NULL) {
699 d->type.name = strdup(s->type.name);
700 my_assert_not(d->type.name, NULL);
702 if (s->fptr != NULL) {
703 d->fptr = malloc(sizeof(*d->fptr));
704 my_assert_not(d->fptr, NULL);
705 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
709 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
711 struct parsed_proto *pp;
714 pp = malloc(sizeof(*pp));
715 my_assert_not(pp, NULL);
716 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
718 // do the actual deep copy..
719 for (i = 0; i < pp_c->argc; i++)
720 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
721 if (pp_c->ret_type.name != NULL)
722 pp->ret_type.name = strdup(pp_c->ret_type.name);
727 static inline void pp_print(char *buf, size_t buf_size,
728 const struct parsed_proto *pp)
733 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
736 for (i = 0; i < pp->argc_reg; i++) {
737 snprintf(buf + l, buf_size - l, "%s%s",
738 i == 0 ? "" : ", ", pp->arg[i].reg);
741 if (pp->argc_stack > 0) {
742 snprintf(buf + l, buf_size - l, "%s{%d stack}",
743 i == 0 ? "" : ", ", pp->argc_stack);
746 snprintf(buf + l, buf_size - l, ")");
749 static inline void proto_release(struct parsed_proto *pp)
753 for (i = 0; i < pp->argc; i++) {
754 if (pp->arg[i].reg != NULL)
755 free(pp->arg[i].reg);
756 if (pp->arg[i].type.name != NULL)
757 free(pp->arg[i].type.name);
758 if (pp->arg[i].fptr != NULL)
759 free(pp->arg[i].fptr);
761 if (pp->ret_type.name != NULL)
762 free(pp->ret_type.name);