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[] = {
143 static const char *known_ptr_types[] = {
167 "PMEMORY_BASIC_INFORMATION",
178 static const char *ignored_keywords[] = {
186 // returns ptr to char after type ends
187 static int typecmp(const char *n, const char *t)
189 for (; *t != 0; n++, t++) {
190 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
192 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
201 static const char *skip_type_mod(const char *n)
206 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
207 len = strlen(known_type_mod[i]);
208 if (strncmp(n, known_type_mod[i], len) != 0)
210 if (!my_isblank(n[len]))
214 while (my_isblank(*n))
222 static int check_type(const char *name, struct parsed_type *type)
228 n = skip_type_mod(name);
230 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
231 if (typecmp(n, known_ptr_types[i]))
238 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
241 // assume single word
242 while (!my_isblank(*n) && !my_issep(*n))
247 while (my_isblank(*n))
258 type->name = strndup(name, ret);
259 if (IS(type->name, "VOID"))
260 memcpy(type->name, "void", 4);
265 /* args are always expanded to 32bit */
266 static const char *map_reg(const char *reg)
268 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
269 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
270 const char *regs_b[] = { "al", "bl", "cl", "dl" };
273 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
274 if (IS(reg, regs_w[i]))
277 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
278 if (IS(reg, regs_b[i]))
284 static int check_struct_arg(struct parsed_proto_arg *arg)
286 if (IS(arg->type.name, "POINT"))
292 static int parse_protostr(char *protostr, struct parsed_proto *pp)
294 struct parsed_proto_arg *arg;
304 if (p[0] == '/' && p[1] == '/') {
305 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
309 // strip unneeded stuff
310 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
311 if ((p1[0] == '/' && p1[1] == '*')
312 || (p1[0] == '*' && p1[1] == '/'))
316 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
321 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
322 l = strlen(ignored_keywords[i]);
323 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
324 p = sskip(p + l + 1);
327 ret = check_type(p, &pp->ret_type);
329 printf("%s:%d:%zd: unhandled return in '%s'\n",
330 hdrfn, hdrfline, (p - protostr) + 1, protostr);
335 if (!strncmp(p, "noreturn ", 9)) {
340 if (!strchr(p, ')')) {
341 p = next_idt(buf, sizeof(buf), p);
344 printf("%s:%d:%zd: var name missing\n",
345 hdrfn, hdrfline, (p - protostr) + 1);
348 strcpy(pp->name, buf);
353 pp->ret_type.is_array = 1;
365 p = next_word(cconv, sizeof(cconv), p);
368 printf("%s:%d:%zd: cconv missing\n",
369 hdrfn, hdrfline, (p - protostr) + 1);
372 if (IS(cconv, "__cdecl"))
374 else if (IS(cconv, "__stdcall"))
376 else if (IS(cconv, "__fastcall")) {
378 pp->is_stdcall = 1; // sort of..
380 else if (IS(cconv, "__thiscall"))
382 else if (IS(cconv, "__userpurge"))
383 pp->is_stdcall = 1; // IDA
384 else if (IS(cconv, "__usercall"))
385 pp->is_stdcall = 0; // IDA
386 else if (IS(cconv, "WINAPI"))
389 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
390 hdrfn, hdrfline, (p - protostr) + 1, cconv);
396 printf("%s:%d:%zd: '*' expected\n",
397 hdrfn, hdrfline, (p - protostr) + 1);
401 // XXX: skipping extra asterisks, for now
407 p = next_idt(buf, sizeof(buf), p);
410 //printf("%s:%d:%zd: func name missing\n",
411 // hdrfn, hdrfline, (p - protostr) + 1);
414 strcpy(pp->name, buf);
416 ret = get_regparm(regparm, sizeof(regparm), p);
418 if (!IS(regparm, "eax") && !IS(regparm, "ax")
419 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
421 printf("%s:%d:%zd: bad regparm: %s\n",
422 hdrfn, hdrfline, (p - protostr) + 1, regparm);
431 // not really ret_type is array, but ohwell
432 pp->ret_type.is_array = 1;
433 p = strchr(p + 1, ']');
435 printf("%s:%d:%zd: ']' expected\n",
436 hdrfn, hdrfline, (p - protostr) + 1);
442 printf("%s:%d:%zd: ')' expected\n",
443 hdrfn, hdrfline, (p - protostr) + 1);
450 printf("%s:%d:%zd: '(' expected, got '%c'\n",
451 hdrfn, hdrfline, (p - protostr) + 1, *p);
458 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
459 && *sskip(p + 4) == ')')
470 printf("%s:%d:%zd: ',' expected\n",
471 hdrfn, hdrfline, (p - protostr) + 1);
477 if (!strncmp(p, "...", 3)) {
484 printf("%s:%d:%zd: ')' expected\n",
485 hdrfn, hdrfline, (p - protostr) + 1);
489 arg = &pp->arg[xarg];
493 ret = check_type(p, &arg->type);
495 printf("%s:%d:%zd: unhandled type for arg%d\n",
496 hdrfn, hdrfline, (p - protostr) + 1, xarg);
503 arg->fptr = calloc(1, sizeof(*arg->fptr));
504 ret = parse_protostr(p1, arg->fptr);
506 printf("%s:%d:%zd: funcarg parse failed\n",
507 hdrfn, hdrfline, p1 - protostr);
510 // we'll treat it as void * for non-calls
511 arg->type.name = strdup("void *");
512 arg->type.is_ptr = 1;
517 p = next_idt(buf, sizeof(buf), p);
521 printf("%s:%d:%zd: idt missing for arg%d\n",
522 hdrfn, hdrfline, (p - protostr) + 1, xarg);
528 ret = get_regparm(regparm, sizeof(regparm), p);
533 arg->reg = strdup(map_reg(regparm));
536 ret = check_struct_arg(arg);
538 pp->has_structarg = 1;
539 arg->type.is_struct = 1;
540 free(arg->type.name);
541 arg->type.name = strdup("int");
542 for (l = 0; l < ret; l++) {
543 pp_copy_arg(&pp->arg[xarg], arg);
549 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
550 if (pp->arg[0].reg != NULL) {
551 printf("%s:%d: %s with arg1 spec %s?\n",
552 hdrfn, hdrfline, cconv, pp->arg[0].reg);
554 pp->arg[0].reg = strdup("ecx");
557 if (xarg > 1 && IS(cconv, "__fastcall")) {
558 if (pp->arg[1].reg != NULL) {
559 printf("%s:%d: %s with arg2 spec %s?\n",
560 hdrfn, hdrfline, cconv, pp->arg[1].reg);
562 pp->arg[1].reg = strdup("edx");
567 for (i = 0; i < pp->argc; i++) {
568 if (pp->arg[i].reg == NULL)
574 if (pp->argc == 1 && pp->arg[0].reg != NULL
575 && IS(pp->arg[0].reg, "ecx"))
579 else if (pp->argc_reg == 2
580 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
581 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
586 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
587 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
594 static int pp_name_cmp(const void *p1, const void *p2)
596 const struct parsed_proto *pp1 = p1, *pp2 = p2;
597 return strcmp(pp1->name, pp2->name);
600 static struct parsed_proto *pp_cache;
601 static int pp_cache_size;
602 static int pp_cache_alloc;
604 static int b_pp_c_handler(char *proto, const char *fname)
608 if (pp_cache_size >= pp_cache_alloc) {
609 pp_cache_alloc = pp_cache_alloc * 2 + 64;
610 pp_cache = realloc(pp_cache, pp_cache_alloc
611 * sizeof(pp_cache[0]));
612 my_assert_not(pp_cache, NULL);
613 memset(pp_cache + pp_cache_size, 0,
614 (pp_cache_alloc - pp_cache_size)
615 * sizeof(pp_cache[0]));
618 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
626 static void build_pp_cache(FILE *fhdr)
634 ret = do_protostrs(fhdr, hdrfn);
638 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
639 fseek(fhdr, pos, SEEK_SET);
642 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
645 const struct parsed_proto *pp_ret;
646 struct parsed_proto pp_search;
648 if (pp_cache == NULL)
649 build_pp_cache(fhdr);
651 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
654 strcpy(pp_search.name, sym);
655 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
656 sizeof(pp_cache[0]), pp_name_cmp);
657 if (pp_ret == NULL && !quiet)
658 printf("%s: sym '%s' is missing\n", hdrfn, sym);
663 static void pp_copy_arg(struct parsed_proto_arg *d,
664 const struct parsed_proto_arg *s)
666 memcpy(d, s, sizeof(*d));
668 if (s->reg != NULL) {
669 d->reg = strdup(s->reg);
670 my_assert_not(d->reg, NULL);
672 if (s->type.name != NULL) {
673 d->type.name = strdup(s->type.name);
674 my_assert_not(d->type.name, NULL);
676 if (s->fptr != NULL) {
677 d->fptr = malloc(sizeof(*d->fptr));
678 my_assert_not(d->fptr, NULL);
679 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
683 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
685 struct parsed_proto *pp;
688 pp = malloc(sizeof(*pp));
689 my_assert_not(pp, NULL);
690 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
692 // do the actual deep copy..
693 for (i = 0; i < pp_c->argc; i++)
694 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
695 if (pp_c->ret_type.name != NULL)
696 pp->ret_type.name = strdup(pp_c->ret_type.name);
701 static inline void pp_print(char *buf, size_t buf_size,
702 const struct parsed_proto *pp)
707 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
710 for (i = 0; i < pp->argc_reg; i++) {
711 snprintf(buf + l, buf_size - l, "%s%s",
712 i == 0 ? "" : ", ", pp->arg[i].reg);
715 if (pp->argc_stack > 0) {
716 snprintf(buf + l, buf_size - l, "%s{%d stack}",
717 i == 0 ? "" : ", ", pp->argc_stack);
720 snprintf(buf + l, buf_size - l, ")");
723 static inline void proto_release(struct parsed_proto *pp)
727 for (i = 0; i < pp->argc; i++) {
728 if (pp->arg[i].reg != NULL)
729 free(pp->arg[i].reg);
730 if (pp->arg[i].type.name != NULL)
731 free(pp->arg[i].type.name);
732 if (pp->arg[i].fptr != NULL)
733 free(pp->arg[i].fptr);
735 if (pp->ret_type.name != NULL)
736 free(pp->ret_type.name);