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;
32 unsigned int is_fptr:1;
33 unsigned int is_noreturn:1;
34 unsigned int is_unresolved:1;
35 unsigned int has_structarg:1;
38 static const char *hdrfn;
39 static int hdrfline = 0;
41 static void pp_copy_arg(struct parsed_proto_arg *d,
42 const struct parsed_proto_arg *s);
44 static int b_pp_c_handler(char *proto, const char *fname);
46 static int do_protostrs(FILE *fhdr, const char *fname)
48 const char *finc_name;
49 const char *hdrfn_saved;
61 while (fgets(protostr, sizeof(protostr), fhdr))
64 if (strncmp(protostr, "//#include ", 11) == 0) {
65 finc_name = protostr + 11;
66 p = strpbrk(finc_name, "\r\n ");
71 p = strrchr(hdrfn_saved, '/');
73 memcpy(path, hdrfn_saved,
75 path[p - hdrfn_saved + 1] = 0;
77 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
79 finc = fopen(fname_inc, "r");
81 printf("%s:%d: can't open '%s'\n",
82 fname_inc, line, finc_name);
85 ret = do_protostrs(finc, finc_name);
91 if (strncmp(sskip(protostr), "//", 2) == 0)
94 p = protostr + strlen(protostr);
95 for (p--; p >= protostr && my_isblank(*p); --p)
102 ret = b_pp_c_handler(protostr, hdrfn);
115 static int get_regparm(char *dst, size_t dlen, char *p)
122 for (o = 0, i = 1; o < dlen; i++) {
134 static const char *known_type_mod[] = {
144 static const char *known_ptr_types[] = {
170 "PMEMORY_BASIC_INFORMATION",
181 static const char *ignored_keywords[] = {
189 // returns ptr to char after type ends
190 static int typecmp(const char *n, const char *t)
192 for (; *t != 0; n++, t++) {
193 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
195 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
204 static const char *skip_type_mod(const char *n)
209 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
210 len = strlen(known_type_mod[i]);
211 if (strncmp(n, known_type_mod[i], len) != 0)
213 if (!my_isblank(n[len]))
217 while (my_isblank(*n))
225 static int check_type(const char *name, struct parsed_type *type)
231 n = skip_type_mod(name);
233 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
234 if (typecmp(n, known_ptr_types[i]))
241 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
244 // assume single word
245 while (!my_isblank(*n) && !my_issep(*n))
250 while (my_isblank(*n))
261 type->name = strndup(name, ret);
262 if (IS(type->name, "VOID"))
263 memcpy(type->name, "void", 4);
268 /* args are always expanded to 32bit */
269 static const char *map_reg(const char *reg)
271 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
272 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
273 const char *regs_b[] = { "al", "bl", "cl", "dl" };
276 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
277 if (IS(reg, regs_w[i]))
280 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
281 if (IS(reg, regs_b[i]))
287 static int check_struct_arg(struct parsed_proto_arg *arg)
289 if (IS(arg->type.name, "POINT"))
295 static int parse_protostr(char *protostr, struct parsed_proto *pp)
297 struct parsed_proto_arg *arg;
307 if (p[0] == '/' && p[1] == '/') {
308 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
312 // strip unneeded stuff
313 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
314 if ((p1[0] == '/' && p1[1] == '*')
315 || (p1[0] == '*' && p1[1] == '/'))
319 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
324 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
325 l = strlen(ignored_keywords[i]);
326 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
327 p = sskip(p + l + 1);
330 ret = check_type(p, &pp->ret_type);
332 printf("%s:%d:%zd: unhandled return in '%s'\n",
333 hdrfn, hdrfline, (p - protostr) + 1, protostr);
338 if (!strncmp(p, "noreturn ", 9)) {
343 if (!strchr(p, ')')) {
344 p = next_idt(buf, sizeof(buf), p);
347 printf("%s:%d:%zd: var name missing\n",
348 hdrfn, hdrfline, (p - protostr) + 1);
351 strcpy(pp->name, buf);
356 pp->ret_type.is_array = 1;
368 p = next_word(cconv, sizeof(cconv), p);
371 printf("%s:%d:%zd: cconv missing\n",
372 hdrfn, hdrfline, (p - protostr) + 1);
375 if (IS(cconv, "__cdecl"))
377 else if (IS(cconv, "__stdcall"))
379 else if (IS(cconv, "__fastcall")) {
381 pp->is_stdcall = 1; // sort of..
383 else if (IS(cconv, "__thiscall"))
385 else if (IS(cconv, "__userpurge"))
386 pp->is_stdcall = 1; // IDA
387 else if (IS(cconv, "__usercall"))
388 pp->is_stdcall = 0; // IDA
389 else if (IS(cconv, "WINAPI"))
392 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
393 hdrfn, hdrfline, (p - protostr) + 1, cconv);
399 printf("%s:%d:%zd: '*' expected\n",
400 hdrfn, hdrfline, (p - protostr) + 1);
404 // XXX: skipping extra asterisks, for now
410 p = next_idt(buf, sizeof(buf), p);
413 //printf("%s:%d:%zd: func name missing\n",
414 // hdrfn, hdrfline, (p - protostr) + 1);
417 strcpy(pp->name, buf);
419 ret = get_regparm(regparm, sizeof(regparm), p);
421 if (!IS(regparm, "eax") && !IS(regparm, "ax")
422 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
424 printf("%s:%d:%zd: bad regparm: %s\n",
425 hdrfn, hdrfline, (p - protostr) + 1, regparm);
434 // not really ret_type is array, but ohwell
435 pp->ret_type.is_array = 1;
436 p = strchr(p + 1, ']');
438 printf("%s:%d:%zd: ']' expected\n",
439 hdrfn, hdrfline, (p - protostr) + 1);
445 printf("%s:%d:%zd: ')' expected\n",
446 hdrfn, hdrfline, (p - protostr) + 1);
453 printf("%s:%d:%zd: '(' expected, got '%c'\n",
454 hdrfn, hdrfline, (p - protostr) + 1, *p);
461 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
462 && *sskip(p + 4) == ')')
473 printf("%s:%d:%zd: ',' expected\n",
474 hdrfn, hdrfline, (p - protostr) + 1);
480 if (!strncmp(p, "...", 3)) {
487 printf("%s:%d:%zd: ')' expected\n",
488 hdrfn, hdrfline, (p - protostr) + 1);
492 arg = &pp->arg[xarg];
496 ret = check_type(p, &arg->type);
498 printf("%s:%d:%zd: unhandled type for arg%d\n",
499 hdrfn, hdrfline, (p - protostr) + 1, xarg);
506 arg->fptr = calloc(1, sizeof(*arg->fptr));
507 ret = parse_protostr(p1, arg->fptr);
509 printf("%s:%d:%zd: funcarg parse failed\n",
510 hdrfn, hdrfline, p1 - protostr);
513 // we'll treat it as void * for non-calls
514 arg->type.name = strdup("void *");
515 arg->type.is_ptr = 1;
520 p = next_idt(buf, sizeof(buf), p);
524 printf("%s:%d:%zd: idt missing for arg%d\n",
525 hdrfn, hdrfline, (p - protostr) + 1, xarg);
531 ret = get_regparm(regparm, sizeof(regparm), p);
536 arg->reg = strdup(map_reg(regparm));
539 if (strstr(arg->type.name, "int64")
540 || IS(arg->type.name, "double"))
543 free(arg->type.name);
544 arg->type.name = strdup("int");
545 pp_copy_arg(&pp->arg[xarg], arg);
549 ret = check_struct_arg(arg);
551 pp->has_structarg = 1;
552 arg->type.is_struct = 1;
553 free(arg->type.name);
554 arg->type.name = strdup("int");
555 for (l = 0; l < ret; l++) {
556 pp_copy_arg(&pp->arg[xarg], arg);
562 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
563 if (pp->arg[0].reg != NULL) {
564 printf("%s:%d: %s with arg1 spec %s?\n",
565 hdrfn, hdrfline, cconv, pp->arg[0].reg);
567 pp->arg[0].reg = strdup("ecx");
570 if (xarg > 1 && IS(cconv, "__fastcall")) {
571 if (pp->arg[1].reg != NULL) {
572 printf("%s:%d: %s with arg2 spec %s?\n",
573 hdrfn, hdrfline, cconv, pp->arg[1].reg);
575 pp->arg[1].reg = strdup("edx");
580 for (i = 0; i < pp->argc; i++) {
581 if (pp->arg[i].reg == NULL)
587 if (pp->argc == 1 && pp->arg[0].reg != NULL
588 && IS(pp->arg[0].reg, "ecx"))
592 else if (pp->argc_reg == 2
593 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
594 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
599 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
600 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
607 static int pp_name_cmp(const void *p1, const void *p2)
609 const struct parsed_proto *pp1 = p1, *pp2 = p2;
610 return strcmp(pp1->name, pp2->name);
613 static struct parsed_proto *pp_cache;
614 static int pp_cache_size;
615 static int pp_cache_alloc;
617 static int b_pp_c_handler(char *proto, const char *fname)
621 if (pp_cache_size >= pp_cache_alloc) {
622 pp_cache_alloc = pp_cache_alloc * 2 + 64;
623 pp_cache = realloc(pp_cache, pp_cache_alloc
624 * sizeof(pp_cache[0]));
625 my_assert_not(pp_cache, NULL);
626 memset(pp_cache + pp_cache_size, 0,
627 (pp_cache_alloc - pp_cache_size)
628 * sizeof(pp_cache[0]));
631 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
639 static void build_pp_cache(FILE *fhdr)
647 ret = do_protostrs(fhdr, hdrfn);
651 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
652 fseek(fhdr, pos, SEEK_SET);
655 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
658 const struct parsed_proto *pp_ret;
659 struct parsed_proto pp_search;
661 if (pp_cache == NULL)
662 build_pp_cache(fhdr);
664 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
667 strcpy(pp_search.name, sym);
668 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
669 sizeof(pp_cache[0]), pp_name_cmp);
670 if (pp_ret == NULL && !quiet)
671 printf("%s: sym '%s' is missing\n", hdrfn, sym);
676 static void pp_copy_arg(struct parsed_proto_arg *d,
677 const struct parsed_proto_arg *s)
679 memcpy(d, s, sizeof(*d));
681 if (s->reg != NULL) {
682 d->reg = strdup(s->reg);
683 my_assert_not(d->reg, NULL);
685 if (s->type.name != NULL) {
686 d->type.name = strdup(s->type.name);
687 my_assert_not(d->type.name, NULL);
689 if (s->fptr != NULL) {
690 d->fptr = malloc(sizeof(*d->fptr));
691 my_assert_not(d->fptr, NULL);
692 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
696 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
698 struct parsed_proto *pp;
701 pp = malloc(sizeof(*pp));
702 my_assert_not(pp, NULL);
703 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
705 // do the actual deep copy..
706 for (i = 0; i < pp_c->argc; i++)
707 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
708 if (pp_c->ret_type.name != NULL)
709 pp->ret_type.name = strdup(pp_c->ret_type.name);
714 static inline void pp_print(char *buf, size_t buf_size,
715 const struct parsed_proto *pp)
720 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
723 for (i = 0; i < pp->argc_reg; i++) {
724 snprintf(buf + l, buf_size - l, "%s%s",
725 i == 0 ? "" : ", ", pp->arg[i].reg);
728 if (pp->argc_stack > 0) {
729 snprintf(buf + l, buf_size - l, "%s{%d stack}",
730 i == 0 ? "" : ", ", pp->argc_stack);
733 snprintf(buf + l, buf_size - l, ")");
736 static inline void proto_release(struct parsed_proto *pp)
740 for (i = 0; i < pp->argc; i++) {
741 if (pp->arg[i].reg != NULL)
742 free(pp->arg[i].reg);
743 if (pp->arg[i].type.name != NULL)
744 free(pp->arg[i].type.name);
745 if (pp->arg[i].fptr != NULL)
746 free(pp->arg[i].fptr);
748 if (pp->ret_type.name != NULL)
749 free(pp->ret_type.name);