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 has_structarg:1;
36 static const char *hdrfn;
37 static int hdrfline = 0;
39 static void pp_copy_arg(struct parsed_proto_arg *d,
40 const struct parsed_proto_arg *s);
42 static int b_pp_c_handler(char *proto, const char *fname);
44 static int do_protostrs(FILE *fhdr, const char *fname)
46 const char *finc_name;
47 const char *hdrfn_saved;
57 while (fgets(protostr, sizeof(protostr), fhdr))
60 if (strncmp(protostr, "//#include ", 11) == 0) {
61 finc_name = protostr + 11;
62 p = strpbrk(finc_name, "\r\n ");
66 finc = fopen(finc_name, "r");
68 printf("%s:%d: can't open '%s'\n",
69 fname, line, finc_name);
72 ret = do_protostrs(finc, finc_name);
78 if (strncmp(sskip(protostr), "//", 2) == 0)
81 p = protostr + strlen(protostr);
82 for (p--; p >= protostr && my_isblank(*p); --p)
89 ret = b_pp_c_handler(protostr, hdrfn);
102 static int get_regparm(char *dst, size_t dlen, char *p)
109 for (o = 0, i = 1; o < dlen; i++) {
121 static const char *known_type_mod[] = {
130 static const char *known_ptr_types[] = {
158 static const char *ignored_keywords[] = {
166 // returns ptr to char after type ends
167 static int typecmp(const char *n, const char *t)
169 for (; *t != 0; n++, t++) {
170 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
172 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
181 static const char *skip_type_mod(const char *n)
186 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
187 len = strlen(known_type_mod[i]);
188 if (strncmp(n, known_type_mod[i], len) != 0)
190 if (!my_isblank(n[len]))
194 while (my_isblank(*n))
202 static int check_type(const char *name, struct parsed_type *type)
208 n = skip_type_mod(name);
210 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
211 if (typecmp(n, known_ptr_types[i]))
218 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
221 // assume single word
222 while (!my_isblank(*n) && !my_issep(*n))
227 while (my_isblank(*n))
238 type->name = strndup(name, ret);
242 /* args are always expanded to 32bit */
243 static const char *map_reg(const char *reg)
245 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
246 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
247 const char *regs_b[] = { "al", "bl", "cl", "dl" };
250 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
251 if (IS(reg, regs_w[i]))
254 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
255 if (IS(reg, regs_b[i]))
261 static int check_struct_arg(struct parsed_proto_arg *arg)
263 if (IS(arg->type.name, "POINT"))
269 static int parse_protostr(char *protostr, struct parsed_proto *pp)
271 struct parsed_proto_arg *arg;
281 if (p[0] == '/' && p[1] == '/') {
282 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
286 // strip unneeded stuff
287 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
288 if ((p1[0] == '/' && p1[1] == '*')
289 || (p1[0] == '*' && p1[1] == '/'))
293 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
298 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
299 l = strlen(ignored_keywords[i]);
300 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
301 p = sskip(p + l + 1);
304 ret = check_type(p, &pp->ret_type);
306 printf("%s:%d:%zd: unhandled return in '%s'\n",
307 hdrfn, hdrfline, (p - protostr) + 1, protostr);
312 if (!strchr(p, ')')) {
313 p = next_idt(buf, sizeof(buf), p);
316 printf("%s:%d:%zd: var name missing\n",
317 hdrfn, hdrfline, (p - protostr) + 1);
320 strcpy(pp->name, buf);
325 pp->ret_type.is_array = 1;
337 p = next_word(cconv, sizeof(cconv), p);
340 printf("%s:%d:%zd: cconv missing\n",
341 hdrfn, hdrfline, (p - protostr) + 1);
344 if (IS(cconv, "__cdecl"))
346 else if (IS(cconv, "__stdcall"))
348 else if (IS(cconv, "__fastcall"))
350 else if (IS(cconv, "__thiscall"))
352 else if (IS(cconv, "__userpurge"))
353 pp->is_stdcall = 1; // IDA
354 else if (IS(cconv, "__usercall"))
355 pp->is_stdcall = 0; // IDA
356 else if (IS(cconv, "WINAPI"))
359 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
360 hdrfn, hdrfline, (p - protostr) + 1, cconv);
366 printf("%s:%d:%zd: '*' expected\n",
367 hdrfn, hdrfline, (p - protostr) + 1);
371 // XXX: skipping extra asterisks, for now
377 p = next_idt(buf, sizeof(buf), p);
380 //printf("%s:%d:%zd: func name missing\n",
381 // hdrfn, hdrfline, (p - protostr) + 1);
384 strcpy(pp->name, buf);
386 ret = get_regparm(regparm, sizeof(regparm), p);
388 if (!IS(regparm, "eax") && !IS(regparm, "ax")
389 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
391 printf("%s:%d:%zd: bad regparm: %s\n",
392 hdrfn, hdrfline, (p - protostr) + 1, regparm);
401 // not really ret_type is array, but ohwell
402 pp->ret_type.is_array = 1;
403 p = strchr(p + 1, ']');
405 printf("%s:%d:%zd: ']' expected\n",
406 hdrfn, hdrfline, (p - protostr) + 1);
412 printf("%s:%d:%zd: ')' expected\n",
413 hdrfn, hdrfline, (p - protostr) + 1);
420 printf("%s:%d:%zd: '(' expected, got '%c'\n",
421 hdrfn, hdrfline, (p - protostr) + 1, *p);
428 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
429 && *sskip(p + 4) == ')')
440 printf("%s:%d:%zd: ',' expected\n",
441 hdrfn, hdrfline, (p - protostr) + 1);
447 if (!strncmp(p, "...", 3)) {
454 printf("%s:%d:%zd: ')' expected\n",
455 hdrfn, hdrfline, (p - protostr) + 1);
459 arg = &pp->arg[xarg];
463 ret = check_type(p, &arg->type);
465 printf("%s:%d:%zd: unhandled type for arg%d\n",
466 hdrfn, hdrfline, (p - protostr) + 1, xarg);
473 arg->fptr = calloc(1, sizeof(*arg->fptr));
474 ret = parse_protostr(p1, arg->fptr);
476 printf("%s:%d:%zd: funcarg parse failed\n",
477 hdrfn, hdrfline, p1 - protostr);
480 // we'll treat it as void * for non-calls
481 arg->type.name = strdup("void *");
482 arg->type.is_ptr = 1;
487 p = next_idt(buf, sizeof(buf), p);
491 printf("%s:%d:%zd: idt missing for arg%d\n",
492 hdrfn, hdrfline, (p - protostr) + 1, xarg);
498 ret = get_regparm(regparm, sizeof(regparm), p);
503 arg->reg = strdup(map_reg(regparm));
506 ret = check_struct_arg(arg);
508 pp->has_structarg = 1;
509 arg->type.is_struct = 1;
510 free(arg->type.name);
511 arg->type.name = strdup("int");
512 for (l = 0; l < ret; l++) {
513 pp_copy_arg(&pp->arg[xarg], arg);
519 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
520 if (pp->arg[0].reg != NULL) {
521 printf("%s:%d: %s with arg1 spec %s?\n",
522 hdrfn, hdrfline, cconv, pp->arg[0].reg);
524 pp->arg[0].reg = strdup("ecx");
527 if (xarg > 1 && IS(cconv, "__fastcall")) {
528 if (pp->arg[1].reg != NULL) {
529 printf("%s:%d: %s with arg2 spec %s?\n",
530 hdrfn, hdrfline, cconv, pp->arg[1].reg);
532 pp->arg[1].reg = strdup("edx");
535 if (pp->is_vararg && pp->is_stdcall) {
536 printf("%s:%d: vararg stdcall?\n", hdrfn, hdrfline);
542 for (i = 0; i < pp->argc; i++) {
543 if (pp->arg[i].reg == NULL)
552 static int pp_name_cmp(const void *p1, const void *p2)
554 const struct parsed_proto *pp1 = p1, *pp2 = p2;
555 return strcmp(pp1->name, pp2->name);
558 static struct parsed_proto *pp_cache;
559 static int pp_cache_size;
560 static int pp_cache_alloc;
562 static int b_pp_c_handler(char *proto, const char *fname)
566 if (pp_cache_size >= pp_cache_alloc) {
567 pp_cache_alloc = pp_cache_alloc * 2 + 64;
568 pp_cache = realloc(pp_cache, pp_cache_alloc
569 * sizeof(pp_cache[0]));
570 my_assert_not(pp_cache, NULL);
571 memset(pp_cache + pp_cache_size, 0,
572 (pp_cache_alloc - pp_cache_size)
573 * sizeof(pp_cache[0]));
576 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
584 static void build_pp_cache(FILE *fhdr)
590 ret = do_protostrs(fhdr, hdrfn);
594 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
597 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym)
599 const struct parsed_proto *pp_ret;
600 struct parsed_proto pp_search;
602 if (pp_cache == NULL)
603 build_pp_cache(fhdr);
605 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
608 strcpy(pp_search.name, sym);
609 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
610 sizeof(pp_cache[0]), pp_name_cmp);
612 printf("%s: sym '%s' is missing\n", hdrfn, sym);
617 static void pp_copy_arg(struct parsed_proto_arg *d,
618 const struct parsed_proto_arg *s)
620 memcpy(d, s, sizeof(*d));
622 if (s->reg != NULL) {
623 d->reg = strdup(s->reg);
624 my_assert_not(d->reg, NULL);
626 if (s->type.name != NULL) {
627 d->type.name = strdup(s->type.name);
628 my_assert_not(d->type.name, NULL);
630 if (s->fptr != NULL) {
631 d->fptr = malloc(sizeof(*d->fptr));
632 my_assert_not(d->fptr, NULL);
633 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
637 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
639 struct parsed_proto *pp;
642 pp = malloc(sizeof(*pp));
643 my_assert_not(pp, NULL);
644 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
646 // do the actual deep copy..
647 for (i = 0; i < pp_c->argc; i++)
648 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
649 if (pp_c->ret_type.name != NULL)
650 pp->ret_type.name = strdup(pp_c->ret_type.name);
655 static inline void proto_release(struct parsed_proto *pp)
659 for (i = 0; i < pp->argc; i++) {
660 if (pp->arg[i].reg != NULL)
661 free(pp->arg[i].reg);
662 if (pp->arg[i].type.name != NULL)
663 free(pp->arg[i].type.name);
664 if (pp->arg[i].fptr != NULL)
665 free(pp->arg[i].fptr);
667 if (pp->ret_type.name != NULL)
668 free(pp->ret_type.name);