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 ret = check_struct_arg(arg);
541 pp->has_structarg = 1;
542 arg->type.is_struct = 1;
543 free(arg->type.name);
544 arg->type.name = strdup("int");
545 for (l = 0; l < ret; l++) {
546 pp_copy_arg(&pp->arg[xarg], arg);
552 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
553 if (pp->arg[0].reg != NULL) {
554 printf("%s:%d: %s with arg1 spec %s?\n",
555 hdrfn, hdrfline, cconv, pp->arg[0].reg);
557 pp->arg[0].reg = strdup("ecx");
560 if (xarg > 1 && IS(cconv, "__fastcall")) {
561 if (pp->arg[1].reg != NULL) {
562 printf("%s:%d: %s with arg2 spec %s?\n",
563 hdrfn, hdrfline, cconv, pp->arg[1].reg);
565 pp->arg[1].reg = strdup("edx");
570 for (i = 0; i < pp->argc; i++) {
571 if (pp->arg[i].reg == NULL)
577 if (pp->argc == 1 && pp->arg[0].reg != NULL
578 && IS(pp->arg[0].reg, "ecx"))
582 else if (pp->argc_reg == 2
583 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
584 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
589 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
590 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
597 static int pp_name_cmp(const void *p1, const void *p2)
599 const struct parsed_proto *pp1 = p1, *pp2 = p2;
600 return strcmp(pp1->name, pp2->name);
603 static struct parsed_proto *pp_cache;
604 static int pp_cache_size;
605 static int pp_cache_alloc;
607 static int b_pp_c_handler(char *proto, const char *fname)
611 if (pp_cache_size >= pp_cache_alloc) {
612 pp_cache_alloc = pp_cache_alloc * 2 + 64;
613 pp_cache = realloc(pp_cache, pp_cache_alloc
614 * sizeof(pp_cache[0]));
615 my_assert_not(pp_cache, NULL);
616 memset(pp_cache + pp_cache_size, 0,
617 (pp_cache_alloc - pp_cache_size)
618 * sizeof(pp_cache[0]));
621 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
629 static void build_pp_cache(FILE *fhdr)
637 ret = do_protostrs(fhdr, hdrfn);
641 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
642 fseek(fhdr, pos, SEEK_SET);
645 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
648 const struct parsed_proto *pp_ret;
649 struct parsed_proto pp_search;
651 if (pp_cache == NULL)
652 build_pp_cache(fhdr);
654 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
657 strcpy(pp_search.name, sym);
658 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
659 sizeof(pp_cache[0]), pp_name_cmp);
660 if (pp_ret == NULL && !quiet)
661 printf("%s: sym '%s' is missing\n", hdrfn, sym);
666 static void pp_copy_arg(struct parsed_proto_arg *d,
667 const struct parsed_proto_arg *s)
669 memcpy(d, s, sizeof(*d));
671 if (s->reg != NULL) {
672 d->reg = strdup(s->reg);
673 my_assert_not(d->reg, NULL);
675 if (s->type.name != NULL) {
676 d->type.name = strdup(s->type.name);
677 my_assert_not(d->type.name, NULL);
679 if (s->fptr != NULL) {
680 d->fptr = malloc(sizeof(*d->fptr));
681 my_assert_not(d->fptr, NULL);
682 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
686 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
688 struct parsed_proto *pp;
691 pp = malloc(sizeof(*pp));
692 my_assert_not(pp, NULL);
693 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
695 // do the actual deep copy..
696 for (i = 0; i < pp_c->argc; i++)
697 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
698 if (pp_c->ret_type.name != NULL)
699 pp->ret_type.name = strdup(pp_c->ret_type.name);
704 static inline void pp_print(char *buf, size_t buf_size,
705 const struct parsed_proto *pp)
710 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
713 for (i = 0; i < pp->argc_reg; i++) {
714 snprintf(buf + l, buf_size - l, "%s%s",
715 i == 0 ? "" : ", ", pp->arg[i].reg);
718 if (pp->argc_stack > 0) {
719 snprintf(buf + l, buf_size - l, "%s{%d stack}",
720 i == 0 ? "" : ", ", pp->argc_stack);
723 snprintf(buf + l, buf_size - l, ")");
726 static inline void proto_release(struct parsed_proto *pp)
730 for (i = 0; i < pp->argc; i++) {
731 if (pp->arg[i].reg != NULL)
732 free(pp->arg[i].reg);
733 if (pp->arg[i].type.name != NULL)
734 free(pp->arg[i].type.name);
735 if (pp->arg[i].fptr != NULL)
736 free(pp->arg[i].fptr);
738 if (pp->ret_type.name != NULL)
739 free(pp->ret_type.name);