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;
59 while (fgets(protostr, sizeof(protostr), fhdr))
62 if (strncmp(protostr, "//#include ", 11) == 0) {
63 finc_name = protostr + 11;
64 p = strpbrk(finc_name, "\r\n ");
69 p = strrchr(hdrfn_saved, '/');
71 memcpy(path, hdrfn_saved,
73 path[p - hdrfn_saved + 1] = 0;
75 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
77 finc = fopen(fname_inc, "r");
79 printf("%s:%d: can't open '%s'\n",
80 fname_inc, line, finc_name);
83 ret = do_protostrs(finc, finc_name);
89 if (strncmp(sskip(protostr), "//", 2) == 0)
92 p = protostr + strlen(protostr);
93 for (p--; p >= protostr && my_isblank(*p); --p)
100 ret = b_pp_c_handler(protostr, hdrfn);
113 static int get_regparm(char *dst, size_t dlen, char *p)
120 for (o = 0, i = 1; o < dlen; i++) {
132 static const char *known_type_mod[] = {
141 static const char *known_ptr_types[] = {
162 "PMEMORY_BASIC_INFORMATION",
173 static const char *ignored_keywords[] = {
181 // returns ptr to char after type ends
182 static int typecmp(const char *n, const char *t)
184 for (; *t != 0; n++, t++) {
185 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
187 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
196 static const char *skip_type_mod(const char *n)
201 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
202 len = strlen(known_type_mod[i]);
203 if (strncmp(n, known_type_mod[i], len) != 0)
205 if (!my_isblank(n[len]))
209 while (my_isblank(*n))
217 static int check_type(const char *name, struct parsed_type *type)
223 n = skip_type_mod(name);
225 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
226 if (typecmp(n, known_ptr_types[i]))
233 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
236 // assume single word
237 while (!my_isblank(*n) && !my_issep(*n))
242 while (my_isblank(*n))
253 type->name = strndup(name, ret);
254 if (IS(type->name, "VOID"))
255 memcpy(type->name, "void", 4);
260 /* args are always expanded to 32bit */
261 static const char *map_reg(const char *reg)
263 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
264 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
265 const char *regs_b[] = { "al", "bl", "cl", "dl" };
268 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
269 if (IS(reg, regs_w[i]))
272 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
273 if (IS(reg, regs_b[i]))
279 static int check_struct_arg(struct parsed_proto_arg *arg)
281 if (IS(arg->type.name, "POINT"))
287 static int parse_protostr(char *protostr, struct parsed_proto *pp)
289 struct parsed_proto_arg *arg;
299 if (p[0] == '/' && p[1] == '/') {
300 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
304 // strip unneeded stuff
305 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
306 if ((p1[0] == '/' && p1[1] == '*')
307 || (p1[0] == '*' && p1[1] == '/'))
311 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
316 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
317 l = strlen(ignored_keywords[i]);
318 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
319 p = sskip(p + l + 1);
322 ret = check_type(p, &pp->ret_type);
324 printf("%s:%d:%zd: unhandled return in '%s'\n",
325 hdrfn, hdrfline, (p - protostr) + 1, protostr);
330 if (!strchr(p, ')')) {
331 p = next_idt(buf, sizeof(buf), p);
334 printf("%s:%d:%zd: var name missing\n",
335 hdrfn, hdrfline, (p - protostr) + 1);
338 strcpy(pp->name, buf);
343 pp->ret_type.is_array = 1;
355 p = next_word(cconv, sizeof(cconv), p);
358 printf("%s:%d:%zd: cconv missing\n",
359 hdrfn, hdrfline, (p - protostr) + 1);
362 if (IS(cconv, "__cdecl"))
364 else if (IS(cconv, "__stdcall"))
366 else if (IS(cconv, "__fastcall"))
368 else if (IS(cconv, "__thiscall"))
370 else if (IS(cconv, "__userpurge"))
371 pp->is_stdcall = 1; // IDA
372 else if (IS(cconv, "__usercall"))
373 pp->is_stdcall = 0; // IDA
374 else if (IS(cconv, "WINAPI"))
377 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
378 hdrfn, hdrfline, (p - protostr) + 1, cconv);
384 printf("%s:%d:%zd: '*' expected\n",
385 hdrfn, hdrfline, (p - protostr) + 1);
389 // XXX: skipping extra asterisks, for now
395 p = next_idt(buf, sizeof(buf), p);
398 //printf("%s:%d:%zd: func name missing\n",
399 // hdrfn, hdrfline, (p - protostr) + 1);
402 strcpy(pp->name, buf);
404 ret = get_regparm(regparm, sizeof(regparm), p);
406 if (!IS(regparm, "eax") && !IS(regparm, "ax")
407 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
409 printf("%s:%d:%zd: bad regparm: %s\n",
410 hdrfn, hdrfline, (p - protostr) + 1, regparm);
419 // not really ret_type is array, but ohwell
420 pp->ret_type.is_array = 1;
421 p = strchr(p + 1, ']');
423 printf("%s:%d:%zd: ']' expected\n",
424 hdrfn, hdrfline, (p - protostr) + 1);
430 printf("%s:%d:%zd: ')' expected\n",
431 hdrfn, hdrfline, (p - protostr) + 1);
438 printf("%s:%d:%zd: '(' expected, got '%c'\n",
439 hdrfn, hdrfline, (p - protostr) + 1, *p);
446 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
447 && *sskip(p + 4) == ')')
458 printf("%s:%d:%zd: ',' expected\n",
459 hdrfn, hdrfline, (p - protostr) + 1);
465 if (!strncmp(p, "...", 3)) {
472 printf("%s:%d:%zd: ')' expected\n",
473 hdrfn, hdrfline, (p - protostr) + 1);
477 arg = &pp->arg[xarg];
481 ret = check_type(p, &arg->type);
483 printf("%s:%d:%zd: unhandled type for arg%d\n",
484 hdrfn, hdrfline, (p - protostr) + 1, xarg);
491 arg->fptr = calloc(1, sizeof(*arg->fptr));
492 ret = parse_protostr(p1, arg->fptr);
494 printf("%s:%d:%zd: funcarg parse failed\n",
495 hdrfn, hdrfline, p1 - protostr);
498 // we'll treat it as void * for non-calls
499 arg->type.name = strdup("void *");
500 arg->type.is_ptr = 1;
505 p = next_idt(buf, sizeof(buf), p);
509 printf("%s:%d:%zd: idt missing for arg%d\n",
510 hdrfn, hdrfline, (p - protostr) + 1, xarg);
516 ret = get_regparm(regparm, sizeof(regparm), p);
521 arg->reg = strdup(map_reg(regparm));
524 ret = check_struct_arg(arg);
526 pp->has_structarg = 1;
527 arg->type.is_struct = 1;
528 free(arg->type.name);
529 arg->type.name = strdup("int");
530 for (l = 0; l < ret; l++) {
531 pp_copy_arg(&pp->arg[xarg], arg);
537 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
538 if (pp->arg[0].reg != NULL) {
539 printf("%s:%d: %s with arg1 spec %s?\n",
540 hdrfn, hdrfline, cconv, pp->arg[0].reg);
542 pp->arg[0].reg = strdup("ecx");
545 if (xarg > 1 && IS(cconv, "__fastcall")) {
546 if (pp->arg[1].reg != NULL) {
547 printf("%s:%d: %s with arg2 spec %s?\n",
548 hdrfn, hdrfline, cconv, pp->arg[1].reg);
550 pp->arg[1].reg = strdup("edx");
553 if (pp->is_vararg && pp->is_stdcall) {
554 printf("%s:%d: vararg stdcall?\n", hdrfn, hdrfline);
560 for (i = 0; i < pp->argc; i++) {
561 if (pp->arg[i].reg == NULL)
570 static int pp_name_cmp(const void *p1, const void *p2)
572 const struct parsed_proto *pp1 = p1, *pp2 = p2;
573 return strcmp(pp1->name, pp2->name);
576 static struct parsed_proto *pp_cache;
577 static int pp_cache_size;
578 static int pp_cache_alloc;
580 static int b_pp_c_handler(char *proto, const char *fname)
584 if (pp_cache_size >= pp_cache_alloc) {
585 pp_cache_alloc = pp_cache_alloc * 2 + 64;
586 pp_cache = realloc(pp_cache, pp_cache_alloc
587 * sizeof(pp_cache[0]));
588 my_assert_not(pp_cache, NULL);
589 memset(pp_cache + pp_cache_size, 0,
590 (pp_cache_alloc - pp_cache_size)
591 * sizeof(pp_cache[0]));
594 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
602 static void build_pp_cache(FILE *fhdr)
608 ret = do_protostrs(fhdr, hdrfn);
612 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
615 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
618 const struct parsed_proto *pp_ret;
619 struct parsed_proto pp_search;
621 if (pp_cache == NULL)
622 build_pp_cache(fhdr);
624 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
627 strcpy(pp_search.name, sym);
628 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
629 sizeof(pp_cache[0]), pp_name_cmp);
630 if (pp_ret == NULL && !quiet)
631 printf("%s: sym '%s' is missing\n", hdrfn, sym);
636 static void pp_copy_arg(struct parsed_proto_arg *d,
637 const struct parsed_proto_arg *s)
639 memcpy(d, s, sizeof(*d));
641 if (s->reg != NULL) {
642 d->reg = strdup(s->reg);
643 my_assert_not(d->reg, NULL);
645 if (s->type.name != NULL) {
646 d->type.name = strdup(s->type.name);
647 my_assert_not(d->type.name, NULL);
649 if (s->fptr != NULL) {
650 d->fptr = malloc(sizeof(*d->fptr));
651 my_assert_not(d->fptr, NULL);
652 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
656 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
658 struct parsed_proto *pp;
661 pp = malloc(sizeof(*pp));
662 my_assert_not(pp, NULL);
663 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
665 // do the actual deep copy..
666 for (i = 0; i < pp_c->argc; i++)
667 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
668 if (pp_c->ret_type.name != NULL)
669 pp->ret_type.name = strdup(pp_c->ret_type.name);
674 static inline void proto_release(struct parsed_proto *pp)
678 for (i = 0; i < pp->argc; i++) {
679 if (pp->arg[i].reg != NULL)
680 free(pp->arg[i].reg);
681 if (pp->arg[i].type.name != NULL)
682 free(pp->arg[i].type.name);
683 if (pp->arg[i].fptr != NULL)
684 free(pp->arg[i].fptr);
686 if (pp->ret_type.name != NULL)
687 free(pp->ret_type.name);