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_vararg:1;
31 unsigned int is_fptr:1;
32 unsigned int is_noreturn:1;
33 unsigned int is_unresolved:1;
34 unsigned int has_structarg:1;
37 static const char *hdrfn;
38 static int hdrfline = 0;
40 static void pp_copy_arg(struct parsed_proto_arg *d,
41 const struct parsed_proto_arg *s);
43 static int b_pp_c_handler(char *proto, const char *fname);
45 static int do_protostrs(FILE *fhdr, const char *fname)
47 const char *finc_name;
48 const char *hdrfn_saved;
60 while (fgets(protostr, sizeof(protostr), fhdr))
63 if (strncmp(protostr, "//#include ", 11) == 0) {
64 finc_name = protostr + 11;
65 p = strpbrk(finc_name, "\r\n ");
70 p = strrchr(hdrfn_saved, '/');
72 memcpy(path, hdrfn_saved,
74 path[p - hdrfn_saved + 1] = 0;
76 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
78 finc = fopen(fname_inc, "r");
80 printf("%s:%d: can't open '%s'\n",
81 fname_inc, line, finc_name);
84 ret = do_protostrs(finc, finc_name);
90 if (strncmp(sskip(protostr), "//", 2) == 0)
93 p = protostr + strlen(protostr);
94 for (p--; p >= protostr && my_isblank(*p); --p)
101 ret = b_pp_c_handler(protostr, hdrfn);
114 static int get_regparm(char *dst, size_t dlen, char *p)
121 for (o = 0, i = 1; o < dlen; i++) {
133 static const char *known_type_mod[] = {
142 static const char *known_ptr_types[] = {
166 "PMEMORY_BASIC_INFORMATION",
177 static const char *ignored_keywords[] = {
185 // returns ptr to char after type ends
186 static int typecmp(const char *n, const char *t)
188 for (; *t != 0; n++, t++) {
189 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
191 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
200 static const char *skip_type_mod(const char *n)
205 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
206 len = strlen(known_type_mod[i]);
207 if (strncmp(n, known_type_mod[i], len) != 0)
209 if (!my_isblank(n[len]))
213 while (my_isblank(*n))
221 static int check_type(const char *name, struct parsed_type *type)
227 n = skip_type_mod(name);
229 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
230 if (typecmp(n, known_ptr_types[i]))
237 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
240 // assume single word
241 while (!my_isblank(*n) && !my_issep(*n))
246 while (my_isblank(*n))
257 type->name = strndup(name, ret);
258 if (IS(type->name, "VOID"))
259 memcpy(type->name, "void", 4);
264 /* args are always expanded to 32bit */
265 static const char *map_reg(const char *reg)
267 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
268 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
269 const char *regs_b[] = { "al", "bl", "cl", "dl" };
272 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
273 if (IS(reg, regs_w[i]))
276 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
277 if (IS(reg, regs_b[i]))
283 static int check_struct_arg(struct parsed_proto_arg *arg)
285 if (IS(arg->type.name, "POINT"))
291 static int parse_protostr(char *protostr, struct parsed_proto *pp)
293 struct parsed_proto_arg *arg;
303 if (p[0] == '/' && p[1] == '/') {
304 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
308 // strip unneeded stuff
309 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
310 if ((p1[0] == '/' && p1[1] == '*')
311 || (p1[0] == '*' && p1[1] == '/'))
315 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
320 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
321 l = strlen(ignored_keywords[i]);
322 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
323 p = sskip(p + l + 1);
326 ret = check_type(p, &pp->ret_type);
328 printf("%s:%d:%zd: unhandled return in '%s'\n",
329 hdrfn, hdrfline, (p - protostr) + 1, protostr);
334 if (!strncmp(p, "noreturn ", 9)) {
339 if (!strchr(p, ')')) {
340 p = next_idt(buf, sizeof(buf), p);
343 printf("%s:%d:%zd: var name missing\n",
344 hdrfn, hdrfline, (p - protostr) + 1);
347 strcpy(pp->name, buf);
352 pp->ret_type.is_array = 1;
364 p = next_word(cconv, sizeof(cconv), p);
367 printf("%s:%d:%zd: cconv missing\n",
368 hdrfn, hdrfline, (p - protostr) + 1);
371 if (IS(cconv, "__cdecl"))
373 else if (IS(cconv, "__stdcall"))
375 else if (IS(cconv, "__fastcall"))
377 else if (IS(cconv, "__thiscall"))
379 else if (IS(cconv, "__userpurge"))
380 pp->is_stdcall = 1; // IDA
381 else if (IS(cconv, "__usercall"))
382 pp->is_stdcall = 0; // IDA
383 else if (IS(cconv, "WINAPI"))
386 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
387 hdrfn, hdrfline, (p - protostr) + 1, cconv);
393 printf("%s:%d:%zd: '*' expected\n",
394 hdrfn, hdrfline, (p - protostr) + 1);
398 // XXX: skipping extra asterisks, for now
404 p = next_idt(buf, sizeof(buf), p);
407 //printf("%s:%d:%zd: func name missing\n",
408 // hdrfn, hdrfline, (p - protostr) + 1);
411 strcpy(pp->name, buf);
413 ret = get_regparm(regparm, sizeof(regparm), p);
415 if (!IS(regparm, "eax") && !IS(regparm, "ax")
416 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
418 printf("%s:%d:%zd: bad regparm: %s\n",
419 hdrfn, hdrfline, (p - protostr) + 1, regparm);
428 // not really ret_type is array, but ohwell
429 pp->ret_type.is_array = 1;
430 p = strchr(p + 1, ']');
432 printf("%s:%d:%zd: ']' expected\n",
433 hdrfn, hdrfline, (p - protostr) + 1);
439 printf("%s:%d:%zd: ')' expected\n",
440 hdrfn, hdrfline, (p - protostr) + 1);
447 printf("%s:%d:%zd: '(' expected, got '%c'\n",
448 hdrfn, hdrfline, (p - protostr) + 1, *p);
455 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
456 && *sskip(p + 4) == ')')
467 printf("%s:%d:%zd: ',' expected\n",
468 hdrfn, hdrfline, (p - protostr) + 1);
474 if (!strncmp(p, "...", 3)) {
481 printf("%s:%d:%zd: ')' expected\n",
482 hdrfn, hdrfline, (p - protostr) + 1);
486 arg = &pp->arg[xarg];
490 ret = check_type(p, &arg->type);
492 printf("%s:%d:%zd: unhandled type for arg%d\n",
493 hdrfn, hdrfline, (p - protostr) + 1, xarg);
500 arg->fptr = calloc(1, sizeof(*arg->fptr));
501 ret = parse_protostr(p1, arg->fptr);
503 printf("%s:%d:%zd: funcarg parse failed\n",
504 hdrfn, hdrfline, p1 - protostr);
507 // we'll treat it as void * for non-calls
508 arg->type.name = strdup("void *");
509 arg->type.is_ptr = 1;
514 p = next_idt(buf, sizeof(buf), p);
518 printf("%s:%d:%zd: idt missing for arg%d\n",
519 hdrfn, hdrfline, (p - protostr) + 1, xarg);
525 ret = get_regparm(regparm, sizeof(regparm), p);
530 arg->reg = strdup(map_reg(regparm));
533 ret = check_struct_arg(arg);
535 pp->has_structarg = 1;
536 arg->type.is_struct = 1;
537 free(arg->type.name);
538 arg->type.name = strdup("int");
539 for (l = 0; l < ret; l++) {
540 pp_copy_arg(&pp->arg[xarg], arg);
546 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
547 if (pp->arg[0].reg != NULL) {
548 printf("%s:%d: %s with arg1 spec %s?\n",
549 hdrfn, hdrfline, cconv, pp->arg[0].reg);
551 pp->arg[0].reg = strdup("ecx");
554 if (xarg > 1 && IS(cconv, "__fastcall")) {
555 if (pp->arg[1].reg != NULL) {
556 printf("%s:%d: %s with arg2 spec %s?\n",
557 hdrfn, hdrfline, cconv, pp->arg[1].reg);
559 pp->arg[1].reg = strdup("edx");
562 if (pp->is_vararg && pp->is_stdcall) {
563 printf("%s:%d: vararg stdcall?\n", hdrfn, hdrfline);
569 for (i = 0; i < pp->argc; i++) {
570 if (pp->arg[i].reg == NULL)
579 static int pp_name_cmp(const void *p1, const void *p2)
581 const struct parsed_proto *pp1 = p1, *pp2 = p2;
582 return strcmp(pp1->name, pp2->name);
585 static struct parsed_proto *pp_cache;
586 static int pp_cache_size;
587 static int pp_cache_alloc;
589 static int b_pp_c_handler(char *proto, const char *fname)
593 if (pp_cache_size >= pp_cache_alloc) {
594 pp_cache_alloc = pp_cache_alloc * 2 + 64;
595 pp_cache = realloc(pp_cache, pp_cache_alloc
596 * sizeof(pp_cache[0]));
597 my_assert_not(pp_cache, NULL);
598 memset(pp_cache + pp_cache_size, 0,
599 (pp_cache_alloc - pp_cache_size)
600 * sizeof(pp_cache[0]));
603 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
611 static void build_pp_cache(FILE *fhdr)
617 ret = do_protostrs(fhdr, hdrfn);
621 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
624 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
627 const struct parsed_proto *pp_ret;
628 struct parsed_proto pp_search;
630 if (pp_cache == NULL)
631 build_pp_cache(fhdr);
633 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
636 strcpy(pp_search.name, sym);
637 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
638 sizeof(pp_cache[0]), pp_name_cmp);
639 if (pp_ret == NULL && !quiet)
640 printf("%s: sym '%s' is missing\n", hdrfn, sym);
645 static void pp_copy_arg(struct parsed_proto_arg *d,
646 const struct parsed_proto_arg *s)
648 memcpy(d, s, sizeof(*d));
650 if (s->reg != NULL) {
651 d->reg = strdup(s->reg);
652 my_assert_not(d->reg, NULL);
654 if (s->type.name != NULL) {
655 d->type.name = strdup(s->type.name);
656 my_assert_not(d->type.name, NULL);
658 if (s->fptr != NULL) {
659 d->fptr = malloc(sizeof(*d->fptr));
660 my_assert_not(d->fptr, NULL);
661 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
665 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
667 struct parsed_proto *pp;
670 pp = malloc(sizeof(*pp));
671 my_assert_not(pp, NULL);
672 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
674 // do the actual deep copy..
675 for (i = 0; i < pp_c->argc; i++)
676 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
677 if (pp_c->ret_type.name != NULL)
678 pp->ret_type.name = strdup(pp_c->ret_type.name);
683 static inline void proto_release(struct parsed_proto *pp)
687 for (i = 0; i < pp->argc; i++) {
688 if (pp->arg[i].reg != NULL)
689 free(pp->arg[i].reg);
690 if (pp->arg[i].type.name != NULL)
691 free(pp->arg[i].type.name);
692 if (pp->arg[i].fptr != NULL)
693 free(pp->arg[i].fptr);
695 if (pp->ret_type.name != NULL)
696 free(pp->ret_type.name);