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[] = {
151 "PMEMORY_BASIC_INFORMATION",
161 static const char *ignored_keywords[] = {
169 // returns ptr to char after type ends
170 static int typecmp(const char *n, const char *t)
172 for (; *t != 0; n++, t++) {
173 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
175 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
184 static const char *skip_type_mod(const char *n)
189 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
190 len = strlen(known_type_mod[i]);
191 if (strncmp(n, known_type_mod[i], len) != 0)
193 if (!my_isblank(n[len]))
197 while (my_isblank(*n))
205 static int check_type(const char *name, struct parsed_type *type)
211 n = skip_type_mod(name);
213 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
214 if (typecmp(n, known_ptr_types[i]))
221 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
224 // assume single word
225 while (!my_isblank(*n) && !my_issep(*n))
230 while (my_isblank(*n))
241 type->name = strndup(name, ret);
242 if (IS(type->name, "VOID"))
243 memcpy(type->name, "void", 4);
248 /* args are always expanded to 32bit */
249 static const char *map_reg(const char *reg)
251 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
252 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
253 const char *regs_b[] = { "al", "bl", "cl", "dl" };
256 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
257 if (IS(reg, regs_w[i]))
260 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
261 if (IS(reg, regs_b[i]))
267 static int check_struct_arg(struct parsed_proto_arg *arg)
269 if (IS(arg->type.name, "POINT"))
275 static int parse_protostr(char *protostr, struct parsed_proto *pp)
277 struct parsed_proto_arg *arg;
287 if (p[0] == '/' && p[1] == '/') {
288 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
292 // strip unneeded stuff
293 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
294 if ((p1[0] == '/' && p1[1] == '*')
295 || (p1[0] == '*' && p1[1] == '/'))
299 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
304 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
305 l = strlen(ignored_keywords[i]);
306 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
307 p = sskip(p + l + 1);
310 ret = check_type(p, &pp->ret_type);
312 printf("%s:%d:%zd: unhandled return in '%s'\n",
313 hdrfn, hdrfline, (p - protostr) + 1, protostr);
318 if (!strchr(p, ')')) {
319 p = next_idt(buf, sizeof(buf), p);
322 printf("%s:%d:%zd: var name missing\n",
323 hdrfn, hdrfline, (p - protostr) + 1);
326 strcpy(pp->name, buf);
331 pp->ret_type.is_array = 1;
343 p = next_word(cconv, sizeof(cconv), p);
346 printf("%s:%d:%zd: cconv missing\n",
347 hdrfn, hdrfline, (p - protostr) + 1);
350 if (IS(cconv, "__cdecl"))
352 else if (IS(cconv, "__stdcall"))
354 else if (IS(cconv, "__fastcall"))
356 else if (IS(cconv, "__thiscall"))
358 else if (IS(cconv, "__userpurge"))
359 pp->is_stdcall = 1; // IDA
360 else if (IS(cconv, "__usercall"))
361 pp->is_stdcall = 0; // IDA
362 else if (IS(cconv, "WINAPI"))
365 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
366 hdrfn, hdrfline, (p - protostr) + 1, cconv);
372 printf("%s:%d:%zd: '*' expected\n",
373 hdrfn, hdrfline, (p - protostr) + 1);
377 // XXX: skipping extra asterisks, for now
383 p = next_idt(buf, sizeof(buf), p);
386 //printf("%s:%d:%zd: func name missing\n",
387 // hdrfn, hdrfline, (p - protostr) + 1);
390 strcpy(pp->name, buf);
392 ret = get_regparm(regparm, sizeof(regparm), p);
394 if (!IS(regparm, "eax") && !IS(regparm, "ax")
395 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
397 printf("%s:%d:%zd: bad regparm: %s\n",
398 hdrfn, hdrfline, (p - protostr) + 1, regparm);
407 // not really ret_type is array, but ohwell
408 pp->ret_type.is_array = 1;
409 p = strchr(p + 1, ']');
411 printf("%s:%d:%zd: ']' expected\n",
412 hdrfn, hdrfline, (p - protostr) + 1);
418 printf("%s:%d:%zd: ')' expected\n",
419 hdrfn, hdrfline, (p - protostr) + 1);
426 printf("%s:%d:%zd: '(' expected, got '%c'\n",
427 hdrfn, hdrfline, (p - protostr) + 1, *p);
434 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
435 && *sskip(p + 4) == ')')
446 printf("%s:%d:%zd: ',' expected\n",
447 hdrfn, hdrfline, (p - protostr) + 1);
453 if (!strncmp(p, "...", 3)) {
460 printf("%s:%d:%zd: ')' expected\n",
461 hdrfn, hdrfline, (p - protostr) + 1);
465 arg = &pp->arg[xarg];
469 ret = check_type(p, &arg->type);
471 printf("%s:%d:%zd: unhandled type for arg%d\n",
472 hdrfn, hdrfline, (p - protostr) + 1, xarg);
479 arg->fptr = calloc(1, sizeof(*arg->fptr));
480 ret = parse_protostr(p1, arg->fptr);
482 printf("%s:%d:%zd: funcarg parse failed\n",
483 hdrfn, hdrfline, p1 - protostr);
486 // we'll treat it as void * for non-calls
487 arg->type.name = strdup("void *");
488 arg->type.is_ptr = 1;
493 p = next_idt(buf, sizeof(buf), p);
497 printf("%s:%d:%zd: idt missing for arg%d\n",
498 hdrfn, hdrfline, (p - protostr) + 1, xarg);
504 ret = get_regparm(regparm, sizeof(regparm), p);
509 arg->reg = strdup(map_reg(regparm));
512 ret = check_struct_arg(arg);
514 pp->has_structarg = 1;
515 arg->type.is_struct = 1;
516 free(arg->type.name);
517 arg->type.name = strdup("int");
518 for (l = 0; l < ret; l++) {
519 pp_copy_arg(&pp->arg[xarg], arg);
525 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
526 if (pp->arg[0].reg != NULL) {
527 printf("%s:%d: %s with arg1 spec %s?\n",
528 hdrfn, hdrfline, cconv, pp->arg[0].reg);
530 pp->arg[0].reg = strdup("ecx");
533 if (xarg > 1 && IS(cconv, "__fastcall")) {
534 if (pp->arg[1].reg != NULL) {
535 printf("%s:%d: %s with arg2 spec %s?\n",
536 hdrfn, hdrfline, cconv, pp->arg[1].reg);
538 pp->arg[1].reg = strdup("edx");
541 if (pp->is_vararg && pp->is_stdcall) {
542 printf("%s:%d: vararg stdcall?\n", hdrfn, hdrfline);
548 for (i = 0; i < pp->argc; i++) {
549 if (pp->arg[i].reg == NULL)
558 static int pp_name_cmp(const void *p1, const void *p2)
560 const struct parsed_proto *pp1 = p1, *pp2 = p2;
561 return strcmp(pp1->name, pp2->name);
564 static struct parsed_proto *pp_cache;
565 static int pp_cache_size;
566 static int pp_cache_alloc;
568 static int b_pp_c_handler(char *proto, const char *fname)
572 if (pp_cache_size >= pp_cache_alloc) {
573 pp_cache_alloc = pp_cache_alloc * 2 + 64;
574 pp_cache = realloc(pp_cache, pp_cache_alloc
575 * sizeof(pp_cache[0]));
576 my_assert_not(pp_cache, NULL);
577 memset(pp_cache + pp_cache_size, 0,
578 (pp_cache_alloc - pp_cache_size)
579 * sizeof(pp_cache[0]));
582 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
590 static void build_pp_cache(FILE *fhdr)
596 ret = do_protostrs(fhdr, hdrfn);
600 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
603 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym)
605 const struct parsed_proto *pp_ret;
606 struct parsed_proto pp_search;
608 if (pp_cache == NULL)
609 build_pp_cache(fhdr);
611 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
614 strcpy(pp_search.name, sym);
615 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
616 sizeof(pp_cache[0]), pp_name_cmp);
618 printf("%s: sym '%s' is missing\n", hdrfn, sym);
623 static void pp_copy_arg(struct parsed_proto_arg *d,
624 const struct parsed_proto_arg *s)
626 memcpy(d, s, sizeof(*d));
628 if (s->reg != NULL) {
629 d->reg = strdup(s->reg);
630 my_assert_not(d->reg, NULL);
632 if (s->type.name != NULL) {
633 d->type.name = strdup(s->type.name);
634 my_assert_not(d->type.name, NULL);
636 if (s->fptr != NULL) {
637 d->fptr = malloc(sizeof(*d->fptr));
638 my_assert_not(d->fptr, NULL);
639 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
643 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
645 struct parsed_proto *pp;
648 pp = malloc(sizeof(*pp));
649 my_assert_not(pp, NULL);
650 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
652 // do the actual deep copy..
653 for (i = 0; i < pp_c->argc; i++)
654 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
655 if (pp_c->ret_type.name != NULL)
656 pp->ret_type.name = strdup(pp_c->ret_type.name);
661 static inline void proto_release(struct parsed_proto *pp)
665 for (i = 0; i < pp->argc; i++) {
666 if (pp->arg[i].reg != NULL)
667 free(pp->arg[i].reg);
668 if (pp->arg[i].type.name != NULL)
669 free(pp->arg[i].type.name);
670 if (pp->arg[i].fptr != NULL)
671 free(pp->arg[i].fptr);
673 if (pp->ret_type.name != NULL)
674 free(pp->ret_type.name);