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[] = {
171 "PMEMORY_BASIC_INFORMATION",
183 static const char *ignored_keywords[] = {
191 // returns ptr to char after type ends
192 static int typecmp(const char *n, const char *t)
194 for (; *t != 0; n++, t++) {
195 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
197 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
206 static const char *skip_type_mod(const char *n)
211 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
212 len = strlen(known_type_mod[i]);
213 if (strncmp(n, known_type_mod[i], len) != 0)
215 if (!my_isblank(n[len]))
219 while (my_isblank(*n))
227 static int check_type(const char *name, struct parsed_type *type)
233 n = skip_type_mod(name);
235 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
236 if (typecmp(n, known_ptr_types[i]))
243 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
246 // assume single word
247 while (!my_isblank(*n) && !my_issep(*n))
252 while (my_isblank(*n))
263 type->name = strndup(name, ret);
264 if (IS(type->name, "VOID"))
265 memcpy(type->name, "void", 4);
270 /* args are always expanded to 32bit */
271 static const char *map_reg(const char *reg)
273 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
274 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
275 const char *regs_b[] = { "al", "bl", "cl", "dl" };
278 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
279 if (IS(reg, regs_w[i]))
282 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
283 if (IS(reg, regs_b[i]))
289 static int check_struct_arg(struct parsed_proto_arg *arg)
291 if (IS(arg->type.name, "POINT"))
297 static int parse_protostr(char *protostr, struct parsed_proto *pp)
299 struct parsed_proto_arg *arg;
309 if (p[0] == '/' && p[1] == '/') {
310 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
314 // strip unneeded stuff
315 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
316 if ((p1[0] == '/' && p1[1] == '*')
317 || (p1[0] == '*' && p1[1] == '/'))
321 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
326 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
327 l = strlen(ignored_keywords[i]);
328 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
329 p = sskip(p + l + 1);
332 ret = check_type(p, &pp->ret_type);
334 printf("%s:%d:%zd: unhandled return in '%s'\n",
335 hdrfn, hdrfline, (p - protostr) + 1, protostr);
340 if (!strncmp(p, "noreturn ", 9)) {
345 if (!strchr(p, ')')) {
346 p = next_idt(buf, sizeof(buf), p);
349 printf("%s:%d:%zd: var name missing\n",
350 hdrfn, hdrfline, (p - protostr) + 1);
353 strcpy(pp->name, buf);
358 pp->ret_type.is_array = 1;
370 p = next_word(cconv, sizeof(cconv), p);
373 printf("%s:%d:%zd: cconv missing\n",
374 hdrfn, hdrfline, (p - protostr) + 1);
377 if (IS(cconv, "__cdecl"))
379 else if (IS(cconv, "__stdcall"))
381 else if (IS(cconv, "__fastcall")) {
383 pp->is_stdcall = 1; // sort of..
385 else if (IS(cconv, "__thiscall"))
387 else if (IS(cconv, "__userpurge"))
388 pp->is_stdcall = 1; // IDA
389 else if (IS(cconv, "__usercall"))
390 pp->is_stdcall = 0; // IDA
391 else if (IS(cconv, "WINAPI"))
394 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
395 hdrfn, hdrfline, (p - protostr) + 1, cconv);
401 printf("%s:%d:%zd: '*' expected\n",
402 hdrfn, hdrfline, (p - protostr) + 1);
406 // XXX: skipping extra asterisks, for now
412 p = next_idt(buf, sizeof(buf), p);
415 //printf("%s:%d:%zd: func name missing\n",
416 // hdrfn, hdrfline, (p - protostr) + 1);
419 strcpy(pp->name, buf);
421 ret = get_regparm(regparm, sizeof(regparm), p);
423 if (!IS(regparm, "eax") && !IS(regparm, "ax")
424 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
426 printf("%s:%d:%zd: bad regparm: %s\n",
427 hdrfn, hdrfline, (p - protostr) + 1, regparm);
436 // not really ret_type is array, but ohwell
437 pp->ret_type.is_array = 1;
438 p = strchr(p + 1, ']');
440 printf("%s:%d:%zd: ']' expected\n",
441 hdrfn, hdrfline, (p - protostr) + 1);
447 printf("%s:%d:%zd: ')' expected\n",
448 hdrfn, hdrfline, (p - protostr) + 1);
455 printf("%s:%d:%zd: '(' expected, got '%c'\n",
456 hdrfn, hdrfline, (p - protostr) + 1, *p);
463 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
464 && *sskip(p + 4) == ')')
475 printf("%s:%d:%zd: ',' expected\n",
476 hdrfn, hdrfline, (p - protostr) + 1);
482 if (!strncmp(p, "...", 3)) {
489 printf("%s:%d:%zd: ')' expected\n",
490 hdrfn, hdrfline, (p - protostr) + 1);
494 arg = &pp->arg[xarg];
498 ret = check_type(p, &arg->type);
500 printf("%s:%d:%zd: unhandled type for arg%d\n",
501 hdrfn, hdrfline, (p - protostr) + 1, xarg);
508 arg->fptr = calloc(1, sizeof(*arg->fptr));
509 ret = parse_protostr(p1, arg->fptr);
511 printf("%s:%d:%zd: funcarg parse failed\n",
512 hdrfn, hdrfline, p1 - protostr);
515 // we'll treat it as void * for non-calls
516 arg->type.name = strdup("void *");
517 arg->type.is_ptr = 1;
522 p = next_idt(buf, sizeof(buf), p);
526 printf("%s:%d:%zd: idt missing for arg%d\n",
527 hdrfn, hdrfline, (p - protostr) + 1, xarg);
533 ret = get_regparm(regparm, sizeof(regparm), p);
538 arg->reg = strdup(map_reg(regparm));
541 if (strstr(arg->type.name, "int64")
542 || IS(arg->type.name, "double"))
545 free(arg->type.name);
546 arg->type.name = strdup("int");
547 pp_copy_arg(&pp->arg[xarg], arg);
551 ret = check_struct_arg(arg);
553 pp->has_structarg = 1;
554 arg->type.is_struct = 1;
555 free(arg->type.name);
556 arg->type.name = strdup("int");
557 for (l = 0; l < ret; l++) {
558 pp_copy_arg(&pp->arg[xarg], arg);
564 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
565 if (pp->arg[0].reg != NULL) {
566 printf("%s:%d: %s with arg1 spec %s?\n",
567 hdrfn, hdrfline, cconv, pp->arg[0].reg);
569 pp->arg[0].reg = strdup("ecx");
572 if (xarg > 1 && IS(cconv, "__fastcall")) {
573 if (pp->arg[1].reg != NULL) {
574 printf("%s:%d: %s with arg2 spec %s?\n",
575 hdrfn, hdrfline, cconv, pp->arg[1].reg);
577 pp->arg[1].reg = strdup("edx");
582 for (i = 0; i < pp->argc; i++) {
583 if (pp->arg[i].reg == NULL)
589 if (pp->argc == 1 && pp->arg[0].reg != NULL
590 && IS(pp->arg[0].reg, "ecx"))
594 else if (pp->argc_reg == 2
595 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
596 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
601 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
602 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
609 static int pp_name_cmp(const void *p1, const void *p2)
611 const struct parsed_proto *pp1 = p1, *pp2 = p2;
612 return strcmp(pp1->name, pp2->name);
615 static struct parsed_proto *pp_cache;
616 static int pp_cache_size;
617 static int pp_cache_alloc;
619 static int b_pp_c_handler(char *proto, const char *fname)
623 if (pp_cache_size >= pp_cache_alloc) {
624 pp_cache_alloc = pp_cache_alloc * 2 + 64;
625 pp_cache = realloc(pp_cache, pp_cache_alloc
626 * sizeof(pp_cache[0]));
627 my_assert_not(pp_cache, NULL);
628 memset(pp_cache + pp_cache_size, 0,
629 (pp_cache_alloc - pp_cache_size)
630 * sizeof(pp_cache[0]));
633 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
641 static void build_pp_cache(FILE *fhdr)
649 ret = do_protostrs(fhdr, hdrfn);
653 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
654 fseek(fhdr, pos, SEEK_SET);
657 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
660 const struct parsed_proto *pp_ret;
661 struct parsed_proto pp_search;
663 if (pp_cache == NULL)
664 build_pp_cache(fhdr);
666 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
669 strcpy(pp_search.name, sym);
670 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
671 sizeof(pp_cache[0]), pp_name_cmp);
672 if (pp_ret == NULL && !quiet)
673 printf("%s: sym '%s' is missing\n", hdrfn, sym);
678 static void pp_copy_arg(struct parsed_proto_arg *d,
679 const struct parsed_proto_arg *s)
681 memcpy(d, s, sizeof(*d));
683 if (s->reg != NULL) {
684 d->reg = strdup(s->reg);
685 my_assert_not(d->reg, NULL);
687 if (s->type.name != NULL) {
688 d->type.name = strdup(s->type.name);
689 my_assert_not(d->type.name, NULL);
691 if (s->fptr != NULL) {
692 d->fptr = malloc(sizeof(*d->fptr));
693 my_assert_not(d->fptr, NULL);
694 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
698 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
700 struct parsed_proto *pp;
703 pp = malloc(sizeof(*pp));
704 my_assert_not(pp, NULL);
705 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
707 // do the actual deep copy..
708 for (i = 0; i < pp_c->argc; i++)
709 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
710 if (pp_c->ret_type.name != NULL)
711 pp->ret_type.name = strdup(pp_c->ret_type.name);
716 static inline void pp_print(char *buf, size_t buf_size,
717 const struct parsed_proto *pp)
722 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
725 for (i = 0; i < pp->argc_reg; i++) {
726 snprintf(buf + l, buf_size - l, "%s%s",
727 i == 0 ? "" : ", ", pp->arg[i].reg);
730 if (pp->argc_stack > 0) {
731 snprintf(buf + l, buf_size - l, "%s{%d stack}",
732 i == 0 ? "" : ", ", pp->argc_stack);
735 snprintf(buf + l, buf_size - l, ")");
738 static inline void proto_release(struct parsed_proto *pp)
742 for (i = 0; i < pp->argc; i++) {
743 if (pp->arg[i].reg != NULL)
744 free(pp->arg[i].reg);
745 if (pp->arg[i].type.name != NULL)
746 free(pp->arg[i].type.name);
747 if (pp->arg[i].fptr != NULL)
748 free(pp->arg[i].fptr);
750 if (pp->ret_type.name != NULL)
751 free(pp->ret_type.name);