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",
162 static const char *ignored_keywords[] = {
170 // returns ptr to char after type ends
171 static int typecmp(const char *n, const char *t)
173 for (; *t != 0; n++, t++) {
174 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
176 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
185 static const char *skip_type_mod(const char *n)
190 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
191 len = strlen(known_type_mod[i]);
192 if (strncmp(n, known_type_mod[i], len) != 0)
194 if (!my_isblank(n[len]))
198 while (my_isblank(*n))
206 static int check_type(const char *name, struct parsed_type *type)
212 n = skip_type_mod(name);
214 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
215 if (typecmp(n, known_ptr_types[i]))
222 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
225 // assume single word
226 while (!my_isblank(*n) && !my_issep(*n))
231 while (my_isblank(*n))
242 type->name = strndup(name, ret);
243 if (IS(type->name, "VOID"))
244 memcpy(type->name, "void", 4);
249 /* args are always expanded to 32bit */
250 static const char *map_reg(const char *reg)
252 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
253 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
254 const char *regs_b[] = { "al", "bl", "cl", "dl" };
257 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
258 if (IS(reg, regs_w[i]))
261 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
262 if (IS(reg, regs_b[i]))
268 static int check_struct_arg(struct parsed_proto_arg *arg)
270 if (IS(arg->type.name, "POINT"))
276 static int parse_protostr(char *protostr, struct parsed_proto *pp)
278 struct parsed_proto_arg *arg;
288 if (p[0] == '/' && p[1] == '/') {
289 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
293 // strip unneeded stuff
294 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
295 if ((p1[0] == '/' && p1[1] == '*')
296 || (p1[0] == '*' && p1[1] == '/'))
300 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
305 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
306 l = strlen(ignored_keywords[i]);
307 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
308 p = sskip(p + l + 1);
311 ret = check_type(p, &pp->ret_type);
313 printf("%s:%d:%zd: unhandled return in '%s'\n",
314 hdrfn, hdrfline, (p - protostr) + 1, protostr);
319 if (!strchr(p, ')')) {
320 p = next_idt(buf, sizeof(buf), p);
323 printf("%s:%d:%zd: var name missing\n",
324 hdrfn, hdrfline, (p - protostr) + 1);
327 strcpy(pp->name, buf);
332 pp->ret_type.is_array = 1;
344 p = next_word(cconv, sizeof(cconv), p);
347 printf("%s:%d:%zd: cconv missing\n",
348 hdrfn, hdrfline, (p - protostr) + 1);
351 if (IS(cconv, "__cdecl"))
353 else if (IS(cconv, "__stdcall"))
355 else if (IS(cconv, "__fastcall"))
357 else if (IS(cconv, "__thiscall"))
359 else if (IS(cconv, "__userpurge"))
360 pp->is_stdcall = 1; // IDA
361 else if (IS(cconv, "__usercall"))
362 pp->is_stdcall = 0; // IDA
363 else if (IS(cconv, "WINAPI"))
366 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
367 hdrfn, hdrfline, (p - protostr) + 1, cconv);
373 printf("%s:%d:%zd: '*' expected\n",
374 hdrfn, hdrfline, (p - protostr) + 1);
378 // XXX: skipping extra asterisks, for now
384 p = next_idt(buf, sizeof(buf), p);
387 //printf("%s:%d:%zd: func name missing\n",
388 // hdrfn, hdrfline, (p - protostr) + 1);
391 strcpy(pp->name, buf);
393 ret = get_regparm(regparm, sizeof(regparm), p);
395 if (!IS(regparm, "eax") && !IS(regparm, "ax")
396 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
398 printf("%s:%d:%zd: bad regparm: %s\n",
399 hdrfn, hdrfline, (p - protostr) + 1, regparm);
408 // not really ret_type is array, but ohwell
409 pp->ret_type.is_array = 1;
410 p = strchr(p + 1, ']');
412 printf("%s:%d:%zd: ']' expected\n",
413 hdrfn, hdrfline, (p - protostr) + 1);
419 printf("%s:%d:%zd: ')' expected\n",
420 hdrfn, hdrfline, (p - protostr) + 1);
427 printf("%s:%d:%zd: '(' expected, got '%c'\n",
428 hdrfn, hdrfline, (p - protostr) + 1, *p);
435 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
436 && *sskip(p + 4) == ')')
447 printf("%s:%d:%zd: ',' expected\n",
448 hdrfn, hdrfline, (p - protostr) + 1);
454 if (!strncmp(p, "...", 3)) {
461 printf("%s:%d:%zd: ')' expected\n",
462 hdrfn, hdrfline, (p - protostr) + 1);
466 arg = &pp->arg[xarg];
470 ret = check_type(p, &arg->type);
472 printf("%s:%d:%zd: unhandled type for arg%d\n",
473 hdrfn, hdrfline, (p - protostr) + 1, xarg);
480 arg->fptr = calloc(1, sizeof(*arg->fptr));
481 ret = parse_protostr(p1, arg->fptr);
483 printf("%s:%d:%zd: funcarg parse failed\n",
484 hdrfn, hdrfline, p1 - protostr);
487 // we'll treat it as void * for non-calls
488 arg->type.name = strdup("void *");
489 arg->type.is_ptr = 1;
494 p = next_idt(buf, sizeof(buf), p);
498 printf("%s:%d:%zd: idt missing for arg%d\n",
499 hdrfn, hdrfline, (p - protostr) + 1, xarg);
505 ret = get_regparm(regparm, sizeof(regparm), p);
510 arg->reg = strdup(map_reg(regparm));
513 ret = check_struct_arg(arg);
515 pp->has_structarg = 1;
516 arg->type.is_struct = 1;
517 free(arg->type.name);
518 arg->type.name = strdup("int");
519 for (l = 0; l < ret; l++) {
520 pp_copy_arg(&pp->arg[xarg], arg);
526 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
527 if (pp->arg[0].reg != NULL) {
528 printf("%s:%d: %s with arg1 spec %s?\n",
529 hdrfn, hdrfline, cconv, pp->arg[0].reg);
531 pp->arg[0].reg = strdup("ecx");
534 if (xarg > 1 && IS(cconv, "__fastcall")) {
535 if (pp->arg[1].reg != NULL) {
536 printf("%s:%d: %s with arg2 spec %s?\n",
537 hdrfn, hdrfline, cconv, pp->arg[1].reg);
539 pp->arg[1].reg = strdup("edx");
542 if (pp->is_vararg && pp->is_stdcall) {
543 printf("%s:%d: vararg stdcall?\n", hdrfn, hdrfline);
549 for (i = 0; i < pp->argc; i++) {
550 if (pp->arg[i].reg == NULL)
559 static int pp_name_cmp(const void *p1, const void *p2)
561 const struct parsed_proto *pp1 = p1, *pp2 = p2;
562 return strcmp(pp1->name, pp2->name);
565 static struct parsed_proto *pp_cache;
566 static int pp_cache_size;
567 static int pp_cache_alloc;
569 static int b_pp_c_handler(char *proto, const char *fname)
573 if (pp_cache_size >= pp_cache_alloc) {
574 pp_cache_alloc = pp_cache_alloc * 2 + 64;
575 pp_cache = realloc(pp_cache, pp_cache_alloc
576 * sizeof(pp_cache[0]));
577 my_assert_not(pp_cache, NULL);
578 memset(pp_cache + pp_cache_size, 0,
579 (pp_cache_alloc - pp_cache_size)
580 * sizeof(pp_cache[0]));
583 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
591 static void build_pp_cache(FILE *fhdr)
597 ret = do_protostrs(fhdr, hdrfn);
601 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
604 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym)
606 const struct parsed_proto *pp_ret;
607 struct parsed_proto pp_search;
609 if (pp_cache == NULL)
610 build_pp_cache(fhdr);
612 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
615 strcpy(pp_search.name, sym);
616 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
617 sizeof(pp_cache[0]), pp_name_cmp);
619 printf("%s: sym '%s' is missing\n", hdrfn, sym);
624 static void pp_copy_arg(struct parsed_proto_arg *d,
625 const struct parsed_proto_arg *s)
627 memcpy(d, s, sizeof(*d));
629 if (s->reg != NULL) {
630 d->reg = strdup(s->reg);
631 my_assert_not(d->reg, NULL);
633 if (s->type.name != NULL) {
634 d->type.name = strdup(s->type.name);
635 my_assert_not(d->type.name, NULL);
637 if (s->fptr != NULL) {
638 d->fptr = malloc(sizeof(*d->fptr));
639 my_assert_not(d->fptr, NULL);
640 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
644 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
646 struct parsed_proto *pp;
649 pp = malloc(sizeof(*pp));
650 my_assert_not(pp, NULL);
651 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
653 // do the actual deep copy..
654 for (i = 0; i < pp_c->argc; i++)
655 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
656 if (pp_c->ret_type.name != NULL)
657 pp->ret_type.name = strdup(pp_c->ret_type.name);
662 static inline void proto_release(struct parsed_proto *pp)
666 for (i = 0; i < pp->argc; i++) {
667 if (pp->arg[i].reg != NULL)
668 free(pp->arg[i].reg);
669 if (pp->arg[i].type.name != NULL)
670 free(pp->arg[i].type.name);
671 if (pp->arg[i].fptr != NULL)
672 free(pp->arg[i].fptr);
674 if (pp->ret_type.name != NULL)
675 free(pp->ret_type.name);