1#include <stdio.h>2#include <stdlib.h>3#include <string.h>45#include <scc/mach.h>6#include <scc/cstd.h>7#include <scc/coff32.h>89#include "libmach.h"1011int12getidx32(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];1819 if (fread(buf, 4, 1, fp) != 1)20 return -1;21 unpack(order, buf, "l", &n);2223 if (n <= 0)24 return -1;2526 if ((names = calloc(sizeof(char *), n)) == NULL)27 return -1;2829 if ((offs = calloc(sizeof(long), n)) == NULL)30 goto err1;3132 for (i = 0; i < n; i++) {33 fread(buf, 4, 1, fp);34 unpack(order, buf, "l", offs[i]);35 }3637 for (i = 0; i < n; i++) {38 int j, c;39 char *s;4041 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';4950 if ((s = malloc(j)) == NULL)51 goto err2;52 memcpy(s, buf, j);53 names[i]= s;54 }5556 if (ferror(fp))57 goto err2;5859 *offsp = offs;60 *namep = names;61 *nsyms = n;6263 return 0;6465err2:66 free(offs);67err1:68 for (i = 0; i < n; i++)69 free(names[i]);70 free(*names);71 return -1;72}