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[] = {
155 static const char *ignored_keywords[] = {
163 // returns ptr to char after type ends
164 static int typecmp(const char *n, const char *t)
166 for (; *t != 0; n++, t++) {
167 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
169 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
178 static const char *skip_type_mod(const char *n)
183 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
184 len = strlen(known_type_mod[i]);
185 if (strncmp(n, known_type_mod[i], len) != 0)
187 if (!my_isblank(n[len]))
191 while (my_isblank(*n))
199 static int check_type(const char *name, struct parsed_type *type)
205 n = skip_type_mod(name);
207 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
208 if (typecmp(n, known_ptr_types[i]))
215 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
218 // assume single word
219 while (!my_isblank(*n) && !my_issep(*n))
224 while (my_isblank(*n))
235 type->name = strndup(name, ret);
239 /* args are always expanded to 32bit */
240 static const char *map_reg(const char *reg)
242 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
243 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
244 const char *regs_b[] = { "al", "bl", "cl", "dl" };
247 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
248 if (IS(reg, regs_w[i]))
251 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
252 if (IS(reg, regs_b[i]))
258 static int check_struct_arg(struct parsed_proto_arg *arg)
260 if (IS(arg->type.name, "POINT"))
266 static int parse_protostr(char *protostr, struct parsed_proto *pp)
268 struct parsed_proto_arg *arg;
278 if (p[0] == '/' && p[1] == '/') {
279 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
283 // strip unneeded stuff
284 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
285 if ((p1[0] == '/' && p1[1] == '*')
286 || (p1[0] == '*' && p1[1] == '/'))
290 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
295 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
296 l = strlen(ignored_keywords[i]);
297 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
298 p = sskip(p + l + 1);
301 ret = check_type(p, &pp->ret_type);
303 printf("%s:%d:%zd: unhandled return in '%s'\n",
304 hdrfn, hdrfline, (p - protostr) + 1, protostr);
309 if (!strchr(p, ')')) {
310 p = next_idt(buf, sizeof(buf), p);
313 printf("%s:%d:%zd: var name missing\n",
314 hdrfn, hdrfline, (p - protostr) + 1);
317 strcpy(pp->name, buf);
322 pp->ret_type.is_array = 1;
334 p = next_word(cconv, sizeof(cconv), p);
337 printf("%s:%d:%zd: cconv missing\n",
338 hdrfn, hdrfline, (p - protostr) + 1);
341 if (IS(cconv, "__cdecl"))
343 else if (IS(cconv, "__stdcall"))
345 else if (IS(cconv, "__fastcall"))
347 else if (IS(cconv, "__thiscall"))
349 else if (IS(cconv, "__userpurge"))
350 pp->is_stdcall = 1; // IDA
351 else if (IS(cconv, "__usercall"))
352 pp->is_stdcall = 0; // IDA
353 else if (IS(cconv, "WINAPI"))
356 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
357 hdrfn, hdrfline, (p - protostr) + 1, cconv);
363 printf("%s:%d:%zd: '*' expected\n",
364 hdrfn, hdrfline, (p - protostr) + 1);
368 // XXX: skipping extra asterisks, for now
374 p = next_idt(buf, sizeof(buf), p);
377 //printf("%s:%d:%zd: func name missing\n",
378 // hdrfn, hdrfline, (p - protostr) + 1);
381 strcpy(pp->name, buf);
383 ret = get_regparm(regparm, sizeof(regparm), p);
385 if (!IS(regparm, "eax") && !IS(regparm, "ax")
386 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
388 printf("%s:%d:%zd: bad regparm: %s\n",
389 hdrfn, hdrfline, (p - protostr) + 1, regparm);
398 // not really ret_type is array, but ohwell
399 pp->ret_type.is_array = 1;
400 p = strchr(p + 1, ']');
402 printf("%s:%d:%zd: ']' expected\n",
403 hdrfn, hdrfline, (p - protostr) + 1);
409 printf("%s:%d:%zd: ')' expected\n",
410 hdrfn, hdrfline, (p - protostr) + 1);
417 printf("%s:%d:%zd: '(' expected, got '%c'\n",
418 hdrfn, hdrfline, (p - protostr) + 1, *p);
425 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
426 && *sskip(p + 4) == ')')
437 printf("%s:%d:%zd: ',' expected\n",
438 hdrfn, hdrfline, (p - protostr) + 1);
444 if (!strncmp(p, "...", 3)) {
451 printf("%s:%d:%zd: ')' expected\n",
452 hdrfn, hdrfline, (p - protostr) + 1);
456 arg = &pp->arg[xarg];
460 ret = check_type(p, &arg->type);
462 printf("%s:%d:%zd: unhandled type for arg%d\n",
463 hdrfn, hdrfline, (p - protostr) + 1, xarg);
470 arg->fptr = calloc(1, sizeof(*arg->fptr));
471 ret = parse_protostr(p1, arg->fptr);
473 printf("%s:%d:%zd: funcarg parse failed\n",
474 hdrfn, hdrfline, p1 - protostr);
477 // we'll treat it as void * for non-calls
478 arg->type.name = strdup("void *");
479 arg->type.is_ptr = 1;
484 p = next_idt(buf, sizeof(buf), p);
488 printf("%s:%d:%zd: idt missing for arg%d\n",
489 hdrfn, hdrfline, (p - protostr) + 1, xarg);
495 ret = get_regparm(regparm, sizeof(regparm), p);
500 arg->reg = strdup(map_reg(regparm));
503 ret = check_struct_arg(arg);
505 pp->has_structarg = 1;
506 arg->type.is_struct = 1;
507 free(arg->type.name);
508 arg->type.name = strdup("int");
509 for (l = 0; l < ret; l++) {
510 pp_copy_arg(&pp->arg[xarg], arg);
516 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
517 if (pp->arg[0].reg != NULL) {
518 printf("%s:%d: %s with arg1 spec %s?\n",
519 hdrfn, hdrfline, cconv, pp->arg[0].reg);
521 pp->arg[0].reg = strdup("ecx");
524 if (xarg > 1 && IS(cconv, "__fastcall")) {
525 if (pp->arg[1].reg != NULL) {
526 printf("%s:%d: %s with arg2 spec %s?\n",
527 hdrfn, hdrfline, cconv, pp->arg[1].reg);
529 pp->arg[1].reg = strdup("edx");
532 if (pp->is_vararg && pp->is_stdcall) {
533 printf("%s:%d: vararg stdcall?\n", hdrfn, hdrfline);
539 for (i = 0; i < pp->argc; i++) {
540 if (pp->arg[i].reg == NULL)
549 static int pp_name_cmp(const void *p1, const void *p2)
551 const struct parsed_proto *pp1 = p1, *pp2 = p2;
552 return strcmp(pp1->name, pp2->name);
555 static struct parsed_proto *pp_cache;
556 static int pp_cache_size;
557 static int pp_cache_alloc;
559 static int b_pp_c_handler(char *proto, const char *fname)
563 if (pp_cache_size >= pp_cache_alloc) {
564 pp_cache_alloc = pp_cache_alloc * 2 + 64;
565 pp_cache = realloc(pp_cache, pp_cache_alloc
566 * sizeof(pp_cache[0]));
567 my_assert_not(pp_cache, NULL);
568 memset(pp_cache + pp_cache_size, 0,
569 (pp_cache_alloc - pp_cache_size)
570 * sizeof(pp_cache[0]));
573 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
581 static void build_pp_cache(FILE *fhdr)
587 ret = do_protostrs(fhdr, hdrfn);
591 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
594 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym)
596 const struct parsed_proto *pp_ret;
597 struct parsed_proto pp_search;
599 if (pp_cache == NULL)
600 build_pp_cache(fhdr);
602 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
605 strcpy(pp_search.name, sym);
606 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
607 sizeof(pp_cache[0]), pp_name_cmp);
609 printf("%s: sym '%s' is missing\n", hdrfn, sym);
614 static void pp_copy_arg(struct parsed_proto_arg *d,
615 const struct parsed_proto_arg *s)
617 memcpy(d, s, sizeof(*d));
619 if (s->reg != NULL) {
620 d->reg = strdup(s->reg);
621 my_assert_not(d->reg, NULL);
623 if (s->type.name != NULL) {
624 d->type.name = strdup(s->type.name);
625 my_assert_not(d->type.name, NULL);
627 if (s->fptr != NULL) {
628 d->fptr = malloc(sizeof(*d->fptr));
629 my_assert_not(d->fptr, NULL);
630 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
634 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
636 struct parsed_proto *pp;
639 pp = malloc(sizeof(*pp));
640 my_assert_not(pp, NULL);
641 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
643 // do the actual deep copy..
644 for (i = 0; i < pp_c->argc; i++)
645 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
646 if (pp_c->ret_type.name != NULL)
647 pp->ret_type.name = strdup(pp_c->ret_type.name);
652 static inline void proto_release(struct parsed_proto *pp)
656 for (i = 0; i < pp->argc; i++) {
657 if (pp->arg[i].reg != NULL)
658 free(pp->arg[i].reg);
659 if (pp->arg[i].type.name != NULL)
660 free(pp->arg[i].type.name);
661 if (pp->arg[i].fptr != NULL)
662 free(pp->arg[i].fptr);
664 if (pp->ret_type.name != NULL)
665 free(pp->ret_type.name);