bankdump.c
5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*====================================================================
* bankdump.c
*
* Copyright 1995, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics,
* Inc.; the contents of this file may not be disclosed to third
* parties, copied or duplicated in any form, in whole or in part,
* without the prior written permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to
* restrictions as set forth in subdivision (c)(1)(ii) of the Rights
* in Technical Data and Computer Software clause at DFARS
* 252.227-7013, and/or in similar or successor clauses in the FAR,
* DOD or NASA FAR Supplement. Unpublished - rights reserved under the
* Copyright Laws of the United States.
*====================================================================*/
#include <libaudio.h>
#include <stdio.h>
#include <stdlib.h>
#include <bstring.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <ultratypes.h>
#include <audiotools.h>
#define TABLESIZE 8192 /* must be a power of 2 */
#define M1 71
#define M2 578393
#define M3 358723
ALSymFileObj *hashtable[TABLESIZE];
static char usage[] = "<symbol file>";
static int name2offset(char *name);
static char *offset2name(int offset);
static char *readFile(char *name);
static int hash(char *str);
static int rehash(int h);
static int enter(char *str, ALSymFileObj *obj);
static ALSymFileObj *lookup(char *name);
ALSymFile *symFile;
ALSymFileObj *symFileObj;
char *class[] = {
"",
"Bank ",
"Instrument",
"Sound ",
"Key Map ",
"Wave Table",
"Loop ",
"Bank File ",
"Envelope ",
"ADPCM Book",
"Sound File"
};
main(int argc, char **argv)
{
char *ofile = NULL;
extern char *optarg;
extern int optind;
int errflg=0;
int c;
int verbose;
char *ptr;
int i;
ALSymFileObj *obj;
while ((c = getopt(argc, argv, "v")) != EOF) {
switch (c) {
case 'v':
verbose = 1;
case '?':
errflg++;
break;
}
}
if (errflg || optind == argc) {
(void)fprintf(stderr, "%s %s\n", argv[0], usage);
exit (2);
}
symFile = (ALSymFile *)readFile(argv[optind]);
symFileObj = (ALSymFileObj *)((char *)symFile + sizeof(ALSymFile));
if(symFile->revision != AL_SYM_FILE_VERSION)
{
fprintf(stderr,"File %s is not a supported symbol file\n",argv[optind]);
exit(-1);
}
/*
* Enter all the strings in the hash table. Assume that there is
* one string for every object.
*/
ptr = (char *)&symFileObj[symFile->objectCount];
printf("Id\tClass\t\tOffset\tRef Count\tName\n");
/*
* Enter the strings in the hash table for later reference
*/
for (i = 0; i < symFile->objectCount; i++) {
obj = &symFileObj[i];
printf("[%4d]\t%s\t0x%x\t\t%d\t%s\n", obj->objectId,
class[obj->objectClass], obj->bankFileOffset, obj->refCount,
ptr);
enter(ptr, &symFileObj[i]);
ptr += (strlen(ptr) + 1);
}
}
/*
* A simple and mediocre hash table implementation. This will go into
* an infinite loop when the hash table fills up.
*/
/* This returns an integer hash value for a string. */
static int hash(char *str)
{
char c;
int h = 0;
while ((c = *str++) != '\0')
h = (h * M1 + (int) c);
return(h);
}
/*
* If hash location is already full, this computes a new hash address
* to try.
*/
static int rehash(int h)
{
return(h +1);
}
static int enter(char *str, ALSymFileObj *obj)
{
int h = hash(str);
while (1) {
int i = (h & TABLESIZE-1);
if (!hashtable[i]) {
hashtable[i] = obj;
return(i);
} else {
/* try again */
h = rehash(h);
}
}
}
static ALSymFileObj *lookup(char *name)
{
char *str;
int h = hash(name);
while (1) {
int i = (h & TABLESIZE-1);
ALSymFileObj *entry = hashtable[i];
str = (char *)symFile + entry->stringOffset;
if (entry == NULL) {
return 0;
} else if (strcmp(name, str) == 0) {
return(entry);
} else {
/* try again */
h = rehash(h);
}
}
}
/*
* matches bank object name to bank file object offset
*/
static int name2offset(char *name)
{
ALSymFileObj *symFileObj = lookup(name);
if (symFileObj)
return symFileObj->bankFileOffset;
else
return 0;
}
/*
* matches bank object offset to bank file object name
*/
static char *offset2name(int offset)
{
int i;
/*
* could use a quicker search algorithm here
*/
for (i = 0; i < symFile->objectCount; i++) {
if (symFileObj[i].bankFileOffset = offset)
return (char *)symFile + symFileObj[i].stringOffset;
}
return 0;
}
static char *readFile(char *name)
{
FILE *file;
char *ptr;
int len;
int rv;
file = fopen(name, "r");
if (!file) {
fprintf(stderr, "error: couldn't open file %s\n", name);
exit(1);
}
fseek(file, 0, SEEK_END);
len = ftell(file);
rewind(file);
ptr = malloc(len);
if (!ptr) {
fprintf(stderr, "error: couldn't allocate memory for file %s\n", name);
exit(1);
}
rv= fread(ptr, len, 1, file);
if (rv != 1) {
fprintf(stderr, "error reading file %s\n", name);
exit(1);
}
return ptr;
}