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[] = {
157 static const char *ignored_keywords[] = {
165 // returns ptr to char after type ends
166 static int typecmp(const char *n, const char *t)
168 for (; *t != 0; n++, t++) {
169 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
171 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
180 static const char *skip_type_mod(const char *n)
185 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
186 len = strlen(known_type_mod[i]);
187 if (strncmp(n, known_type_mod[i], len) != 0)
189 if (!my_isblank(n[len]))
193 while (my_isblank(*n))
201 static int check_type(const char *name, struct parsed_type *type)
207 n = skip_type_mod(name);
209 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
210 if (typecmp(n, known_ptr_types[i]))
217 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
220 // assume single word
221 while (!my_isblank(*n) && !my_issep(*n))
226 while (my_isblank(*n))
237 type->name = strndup(name, ret);
241 /* args are always expanded to 32bit */
242 static const char *map_reg(const char *reg)
244 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
245 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
246 const char *regs_b[] = { "al", "bl", "cl", "dl" };
249 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
250 if (IS(reg, regs_w[i]))
253 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
254 if (IS(reg, regs_b[i]))
260 static int check_struct_arg(struct parsed_proto_arg *arg)
262 if (IS(arg->type.name, "POINT"))
268 static int parse_protostr(char *protostr, struct parsed_proto *pp)
270 struct parsed_proto_arg *arg;
280 if (p[0] == '/' && p[1] == '/') {
281 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
285 // strip unneeded stuff
286 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
287 if ((p1[0] == '/' && p1[1] == '*')
288 || (p1[0] == '*' && p1[1] == '/'))
292 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
297 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
298 l = strlen(ignored_keywords[i]);
299 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
300 p = sskip(p + l + 1);
303 ret = check_type(p, &pp->ret_type);
305 printf("%s:%d:%zd: unhandled return in '%s'\n",
306 hdrfn, hdrfline, (p - protostr) + 1, protostr);
311 if (!strchr(p, ')')) {
312 p = next_idt(buf, sizeof(buf), p);
315 printf("%s:%d:%zd: var name missing\n",
316 hdrfn, hdrfline, (p - protostr) + 1);
319 strcpy(pp->name, buf);
324 pp->ret_type.is_array = 1;
336 p = next_word(cconv, sizeof(cconv), p);
339 printf("%s:%d:%zd: cconv missing\n",
340 hdrfn, hdrfline, (p - protostr) + 1);
343 if (IS(cconv, "__cdecl"))
345 else if (IS(cconv, "__stdcall"))
347 else if (IS(cconv, "__fastcall"))
349 else if (IS(cconv, "__thiscall"))
351 else if (IS(cconv, "__userpurge"))
352 pp->is_stdcall = 1; // IDA
353 else if (IS(cconv, "__usercall"))
354 pp->is_stdcall = 0; // IDA
355 else if (IS(cconv, "WINAPI"))
358 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
359 hdrfn, hdrfline, (p - protostr) + 1, cconv);
365 printf("%s:%d:%zd: '*' expected\n",
366 hdrfn, hdrfline, (p - protostr) + 1);
370 // XXX: skipping extra asterisks, for now
376 p = next_idt(buf, sizeof(buf), p);
379 //printf("%s:%d:%zd: func name missing\n",
380 // hdrfn, hdrfline, (p - protostr) + 1);
383 strcpy(pp->name, buf);
385 ret = get_regparm(regparm, sizeof(regparm), p);
387 if (!IS(regparm, "eax") && !IS(regparm, "ax")
388 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
390 printf("%s:%d:%zd: bad regparm: %s\n",
391 hdrfn, hdrfline, (p - protostr) + 1, regparm);
400 // not really ret_type is array, but ohwell
401 pp->ret_type.is_array = 1;
402 p = strchr(p + 1, ']');
404 printf("%s:%d:%zd: ']' expected\n",
405 hdrfn, hdrfline, (p - protostr) + 1);
411 printf("%s:%d:%zd: ')' expected\n",
412 hdrfn, hdrfline, (p - protostr) + 1);
419 printf("%s:%d:%zd: '(' expected, got '%c'\n",
420 hdrfn, hdrfline, (p - protostr) + 1, *p);
427 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
428 && *sskip(p + 4) == ')')
439 printf("%s:%d:%zd: ',' expected\n",
440 hdrfn, hdrfline, (p - protostr) + 1);
446 if (!strncmp(p, "...", 3)) {
453 printf("%s:%d:%zd: ')' expected\n",
454 hdrfn, hdrfline, (p - protostr) + 1);
458 arg = &pp->arg[xarg];
462 ret = check_type(p, &arg->type);
464 printf("%s:%d:%zd: unhandled type for arg%d\n",
465 hdrfn, hdrfline, (p - protostr) + 1, xarg);
472 arg->fptr = calloc(1, sizeof(*arg->fptr));
473 ret = parse_protostr(p1, arg->fptr);
475 printf("%s:%d:%zd: funcarg parse failed\n",
476 hdrfn, hdrfline, p1 - protostr);
479 // we'll treat it as void * for non-calls
480 arg->type.name = strdup("void *");
481 arg->type.is_ptr = 1;
486 p = next_idt(buf, sizeof(buf), p);
490 printf("%s:%d:%zd: idt missing for arg%d\n",
491 hdrfn, hdrfline, (p - protostr) + 1, xarg);
497 ret = get_regparm(regparm, sizeof(regparm), p);
502 arg->reg = strdup(map_reg(regparm));
505 ret = check_struct_arg(arg);
507 pp->has_structarg = 1;
508 arg->type.is_struct = 1;
509 free(arg->type.name);
510 arg->type.name = strdup("int");
511 for (l = 0; l < ret; l++) {
512 pp_copy_arg(&pp->arg[xarg], arg);
518 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
519 if (pp->arg[0].reg != NULL) {
520 printf("%s:%d: %s with arg1 spec %s?\n",
521 hdrfn, hdrfline, cconv, pp->arg[0].reg);
523 pp->arg[0].reg = strdup("ecx");
526 if (xarg > 1 && IS(cconv, "__fastcall")) {
527 if (pp->arg[1].reg != NULL) {
528 printf("%s:%d: %s with arg2 spec %s?\n",
529 hdrfn, hdrfline, cconv, pp->arg[1].reg);
531 pp->arg[1].reg = strdup("edx");
534 if (pp->is_vararg && pp->is_stdcall) {
535 printf("%s:%d: vararg stdcall?\n", hdrfn, hdrfline);
541 for (i = 0; i < pp->argc; i++) {
542 if (pp->arg[i].reg == NULL)
551 static int pp_name_cmp(const void *p1, const void *p2)
553 const struct parsed_proto *pp1 = p1, *pp2 = p2;
554 return strcmp(pp1->name, pp2->name);
557 static struct parsed_proto *pp_cache;
558 static int pp_cache_size;
559 static int pp_cache_alloc;
561 static int b_pp_c_handler(char *proto, const char *fname)
565 if (pp_cache_size >= pp_cache_alloc) {
566 pp_cache_alloc = pp_cache_alloc * 2 + 64;
567 pp_cache = realloc(pp_cache, pp_cache_alloc
568 * sizeof(pp_cache[0]));
569 my_assert_not(pp_cache, NULL);
570 memset(pp_cache + pp_cache_size, 0,
571 (pp_cache_alloc - pp_cache_size)
572 * sizeof(pp_cache[0]));
575 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
583 static void build_pp_cache(FILE *fhdr)
589 ret = do_protostrs(fhdr, hdrfn);
593 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
596 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym)
598 const struct parsed_proto *pp_ret;
599 struct parsed_proto pp_search;
601 if (pp_cache == NULL)
602 build_pp_cache(fhdr);
604 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
607 strcpy(pp_search.name, sym);
608 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
609 sizeof(pp_cache[0]), pp_name_cmp);
611 printf("%s: sym '%s' is missing\n", hdrfn, sym);
616 static void pp_copy_arg(struct parsed_proto_arg *d,
617 const struct parsed_proto_arg *s)
619 memcpy(d, s, sizeof(*d));
621 if (s->reg != NULL) {
622 d->reg = strdup(s->reg);
623 my_assert_not(d->reg, NULL);
625 if (s->type.name != NULL) {
626 d->type.name = strdup(s->type.name);
627 my_assert_not(d->type.name, NULL);
629 if (s->fptr != NULL) {
630 d->fptr = malloc(sizeof(*d->fptr));
631 my_assert_not(d->fptr, NULL);
632 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
636 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
638 struct parsed_proto *pp;
641 pp = malloc(sizeof(*pp));
642 my_assert_not(pp, NULL);
643 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
645 // do the actual deep copy..
646 for (i = 0; i < pp_c->argc; i++)
647 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
648 if (pp_c->ret_type.name != NULL)
649 pp->ret_type.name = strdup(pp_c->ret_type.name);
654 static inline void proto_release(struct parsed_proto *pp)
658 for (i = 0; i < pp->argc; i++) {
659 if (pp->arg[i].reg != NULL)
660 free(pp->arg[i].reg);
661 if (pp->arg[i].type.name != NULL)
662 free(pp->arg[i].type.name);
663 if (pp->arg[i].fptr != NULL)
664 free(pp->arg[i].fptr);
666 if (pp->ret_type.name != NULL)
667 free(pp->ret_type.name);