scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

coff32xgetidx.c (1111B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include <scc/mach.h>
      6 #include <scc/cstd.h>
      7 
      8 #include "../libmach.h"
      9 #include "coff32.h"
     10 
     11 int
     12 coff32xgetidx(int order, long *nsyms, char ***namep, long **offsp, FILE *fp)
     13 {
     14 	long i, n;
     15 	long *offs;
     16 	char **names;
     17 	unsigned char buf[EXTIDENTSIZ+1];
     18 
     19 	if (fread(buf, 4, 1, fp) != 1)
     20 		return -1;
     21 	unpack(order, buf, "l", &n);
     22 
     23 	if (n <= 0)
     24 		return -1;
     25 
     26 	if ((names = calloc(sizeof(char *), n)) == NULL)
     27 		return -1;
     28 
     29 	if ((offs = calloc(sizeof(long), n)) == NULL)
     30 		goto err1;
     31 
     32 	for (i = 0; i < n; i++) {
     33 		fread(buf, 4, 1, fp);
     34 		unpack(order, buf, "l", offs[i]);
     35 	}
     36 
     37 	for (i = 0; i < n; i++) {
     38 		int j, c;
     39 		char *s;
     40 
     41 		for (j = 0; j < EXTIDENTSIZ; j++) {
     42 			if ((c = getc(fp)) == EOF || c == '\0')
     43 				break;
     44 			buf[j] = c;
     45 		}
     46 		if (c != '\0')
     47 			goto err2;
     48 		buf[j] = '\0';
     49 
     50 		if ((s = malloc(j)) == NULL)
     51 			goto err2;
     52 		memcpy(s, buf, j);
     53 		names[i]= s;
     54 	}
     55 
     56 	if (ferror(fp))
     57 		goto err2;
     58 
     59 	*offsp = offs;
     60 	*namep = names;
     61 	*nsyms = n;
     62 
     63 	return 0;
     64 
     65 err2:
     66 	free(offs);
     67 err1:
     68 	for (i = 0; i < n; i++)
     69 		free(names[i]);
     70 	free(*names);
     71 	return -1;
     72 }