1 | /* $Id: sdf.c,v 1.2 2001-04-17 04:24:17 bird Exp $
|
---|
2 | *
|
---|
3 | * SDF dumper. SDF == Structur Definition Files.
|
---|
4 | * SDF files are found with the PMDF utility.
|
---|
5 | * Note: SDFs for Warp 45 have to be UNPACK.EXEed first.
|
---|
6 | *
|
---|
7 | * stdout: log
|
---|
8 | * stderr: errors or "header" file.
|
---|
9 | *
|
---|
10 | * Copyright (c) 1999 knut st. osmundsen
|
---|
11 | *
|
---|
12 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
13 | *
|
---|
14 | */
|
---|
15 |
|
---|
16 | /*******************************************************************************
|
---|
17 | * Header Files *
|
---|
18 | *******************************************************************************/
|
---|
19 | #include <os2.h>
|
---|
20 | #include <stdio.h>
|
---|
21 | #include <string.h>
|
---|
22 | #include <malloc.h>
|
---|
23 | #include <assert.h>
|
---|
24 |
|
---|
25 |
|
---|
26 | /*******************************************************************************
|
---|
27 | * Structures and Typedefs *
|
---|
28 | *******************************************************************************/
|
---|
29 | typedef struct _SDFType
|
---|
30 | {
|
---|
31 | char szName[33];
|
---|
32 | char szStruct[34];
|
---|
33 | unsigned long cb;
|
---|
34 | unsigned long ulPointerLevel;
|
---|
35 | unsigned long aul[8];
|
---|
36 | } SDFTYPE, *PSDFTYPE;
|
---|
37 |
|
---|
38 | typedef struct _SDFMember
|
---|
39 | {
|
---|
40 | char szName[33];
|
---|
41 | char szType[33];
|
---|
42 | char szType2[34];
|
---|
43 | unsigned long cb;
|
---|
44 | unsigned long offset;
|
---|
45 | unsigned char ch1;
|
---|
46 | unsigned char ch2;
|
---|
47 | unsigned char ch3;
|
---|
48 | unsigned char chPointerLevel;
|
---|
49 | unsigned long aul[5];
|
---|
50 | } SDFMEMBER, *PSDFMEMBER;
|
---|
51 |
|
---|
52 |
|
---|
53 | typedef struct _SDFStruct
|
---|
54 | {
|
---|
55 | char szName[33];
|
---|
56 | char szDescription[35];
|
---|
57 | unsigned long cb;
|
---|
58 | unsigned long cMembers;
|
---|
59 | unsigned long ul1;
|
---|
60 | unsigned short us2a;
|
---|
61 | unsigned short us2b;
|
---|
62 | unsigned long ul3;
|
---|
63 | unsigned long ul4;
|
---|
64 | unsigned long ul5;
|
---|
65 | unsigned long ul6;
|
---|
66 | SDFMEMBER aMembers[1];
|
---|
67 | } SDFSTRUCT, *PSDFSTRUCT;
|
---|
68 |
|
---|
69 |
|
---|
70 | typedef struct _SDFHeader
|
---|
71 | {
|
---|
72 | unsigned long cStructs;
|
---|
73 | unsigned long cTypes;
|
---|
74 | unsigned long ul[12];
|
---|
75 | } SDFHEADER, *PSDFHEADER;
|
---|
76 |
|
---|
77 |
|
---|
78 | /*@IntFunc**********************************************************************
|
---|
79 | * Internal Functions *
|
---|
80 | *******************************************************************************/
|
---|
81 | void dumpSDF(void *pvFile, unsigned cbFile);
|
---|
82 | int makeName(char *pszName, PSDFMEMBER pMember, PSDFHEADER pHdr, PSDFTYPE paTypes);
|
---|
83 | void makeTypedefsForStruct(PSDFHEADER pHdr, PSDFTYPE paTypes, PSDFSTRUCT pStruct, FILE *phOut);
|
---|
84 | void makeTypedefsForTypedef(PSDFHEADER pHdr, PSDFTYPE paTypes, PSDFTYPE pTypedef, FILE *phOut);
|
---|
85 | PSDFTYPE sdfGetTypeArray(PSDFHEADER pHdr);
|
---|
86 | PSDFTYPE sdfGetType(PSDFHEADER pHdr, PSDFTYPE paTypes, const char *pszType);
|
---|
87 |
|
---|
88 |
|
---|
89 | int main(int argc, char **argv)
|
---|
90 | {
|
---|
91 | FILE *phFile;
|
---|
92 | char szBuffer[512];
|
---|
93 | char szOutput[512];
|
---|
94 |
|
---|
95 | if (argc != 2)
|
---|
96 | {
|
---|
97 | fprintf(stderr, "invalid parameter\n");
|
---|
98 | return 1;
|
---|
99 | }
|
---|
100 |
|
---|
101 | phFile = fopen(argv[1], "r"); /*Note: not "rb"! */
|
---|
102 | if (phFile != NULL)
|
---|
103 | {
|
---|
104 | unsigned cbFile;
|
---|
105 | void *pvFile;
|
---|
106 |
|
---|
107 | fseek(phFile, 0, SEEK_END);
|
---|
108 | cbFile = ftell(phFile);
|
---|
109 | fseek(phFile, 0, SEEK_SET);
|
---|
110 |
|
---|
111 | pvFile = malloc(cbFile+20);
|
---|
112 | if (pvFile != NULL)
|
---|
113 | {
|
---|
114 | if (fread(pvFile, 1, cbFile, phFile))
|
---|
115 | {
|
---|
116 | dumpSDF(pvFile, cbFile);
|
---|
117 | }
|
---|
118 | else
|
---|
119 | fprintf(stderr, "fread failed\n");
|
---|
120 | }
|
---|
121 | else
|
---|
122 | fprintf(stderr, "malloc(%d) failed\n", cbFile);
|
---|
123 | fclose(phFile);
|
---|
124 | }
|
---|
125 | else
|
---|
126 | fprintf(stderr, "unable to open input file, %s\n", argv[1]);
|
---|
127 |
|
---|
128 | return 0;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 | void dumpSDF(void *pvFile, unsigned cbFile)
|
---|
135 | {
|
---|
136 | PSDFHEADER pHdr = (PSDFHEADER)pvFile;
|
---|
137 | PSDFSTRUCT pStruct = (PSDFSTRUCT)(unsigned)(pHdr + 1);
|
---|
138 | PSDFTYPE paTypes = sdfGetTypeArray(pHdr);
|
---|
139 | PSDFTYPE pType;
|
---|
140 | FILE *phOut = stderr;
|
---|
141 | int i;
|
---|
142 |
|
---|
143 | printf("Header:\n"
|
---|
144 | "-------\n"
|
---|
145 | "cStructs=%d\n"
|
---|
146 | "cTypes=%d\n",
|
---|
147 | pHdr->cStructs,
|
---|
148 | pHdr->cTypes);
|
---|
149 | for (i = 0; i < sizeof(pHdr->ul) / sizeof(pHdr->ul[0]); i++)
|
---|
150 | printf("ul[%#x] = 0x%08x\n",
|
---|
151 | i, pHdr->ul[i]);
|
---|
152 |
|
---|
153 | printf("\nStructs:\n"
|
---|
154 | "--------\n");
|
---|
155 | for (i = 0; i < pHdr->cStructs; i++,
|
---|
156 | pStruct = (PSDFSTRUCT)((unsigned)(pStruct+1) + sizeof(SDFMEMBER)*(pStruct->cMembers - 1)) )
|
---|
157 | {
|
---|
158 | int j;
|
---|
159 | if (i > 0)
|
---|
160 | printf("\n");
|
---|
161 | printf("%3d struct: %-20s #members: %3d cb: 0x%04x offset: %08x description: %s\n",
|
---|
162 | i, pStruct->szName, pStruct->cMembers, pStruct->cb, (unsigned)pStruct - (unsigned)pvFile, pStruct->szDescription);
|
---|
163 | printf("(ul1=%08x us2a=%04x us2b=%04x ul3=%08x ul4=%08x ul5=%08x ul6=%08x)\n",
|
---|
164 | pStruct->ul1, pStruct->us2a, pStruct->us2b, pStruct->ul3, pStruct->ul4, pStruct->ul5, pStruct->ul6);
|
---|
165 | fprintf(phOut, "struct %s /* #memb %d size %d (0x%03x) */\n{\n",
|
---|
166 | pStruct->szName, pStruct->cMembers, pStruct->cb, pStruct->cb);
|
---|
167 |
|
---|
168 | for (j = 0; j < pStruct->cMembers; j++)
|
---|
169 | {
|
---|
170 | int k, cchName;
|
---|
171 | char szName[80];
|
---|
172 | printf("\tname: %-18s type: %-14s size: %04x offset: %02x chPointerLevel: %d ch1-3:",
|
---|
173 | pStruct->aMembers[j].szName,
|
---|
174 | pStruct->aMembers[j].szType,
|
---|
175 | pStruct->aMembers[j].cb,
|
---|
176 | pStruct->aMembers[j].offset,
|
---|
177 | pStruct->aMembers[j].chPointerLevel
|
---|
178 | );
|
---|
179 | if (pStruct->aMembers[j].szType2[0] != '\0')
|
---|
180 | printf("\n\tszType2=%.15s\n", pStruct->aMembers[j].szType2);
|
---|
181 |
|
---|
182 | makeName(szName, &pStruct->aMembers[j], pHdr, paTypes);
|
---|
183 | cchName = 20 - strlen(szName);
|
---|
184 | fprintf(phOut, " %-20s %s; %*s/* off: %3d(%02x) size: %2d(%02x) */\n",
|
---|
185 | pStruct->aMembers[j].szType,
|
---|
186 | szName,
|
---|
187 | cchName > 0 ? cchName : 0, "",
|
---|
188 | pStruct->aMembers[j].offset,
|
---|
189 | pStruct->aMembers[j].offset,
|
---|
190 | pStruct->aMembers[j].cb,
|
---|
191 | pStruct->aMembers[j].cb
|
---|
192 | );
|
---|
193 |
|
---|
194 | printf(" %02X %02X %02X aul:", pStruct->aMembers[i].ch1, pStruct->aMembers[i].ch2, pStruct->aMembers[i].ch3);
|
---|
195 | for (k = 0; k < sizeof(pStruct->aMembers[j].aul) / sizeof(pStruct->aMembers[j].aul[0]); k++)
|
---|
196 | printf(" %08X", pStruct->aMembers[j].aul[k]);
|
---|
197 | printf("\n");
|
---|
198 | }
|
---|
199 | fprintf(phOut, "};\n");
|
---|
200 | makeTypedefsForStruct(pHdr, paTypes, pStruct, phOut);
|
---|
201 | fprintf(phOut, "\n\n");
|
---|
202 | }
|
---|
203 |
|
---|
204 | fprintf(stderr, "\n/*debug: CurPos=0x%x cbFile=0x%x */\n",
|
---|
205 | (unsigned)pStruct - (unsigned)pvFile, cbFile);
|
---|
206 |
|
---|
207 |
|
---|
208 |
|
---|
209 | printf("\nTypes:\n"
|
---|
210 | "------\n");
|
---|
211 | pType = (PSDFTYPE)(unsigned)pStruct;
|
---|
212 | assert(pType == paTypes);
|
---|
213 | for (i = 0; i < pHdr->cTypes; i++, pType++)
|
---|
214 | {
|
---|
215 | int j;
|
---|
216 | printf("%3d Name: %-20s Struct: %-20s cb: %3d (0x%03x) PointerLevel: %d ",
|
---|
217 | i,
|
---|
218 | pType->szName,
|
---|
219 | pType->szStruct[0] != '\0' ? pType->szStruct : "<none>",
|
---|
220 | pType->cb,
|
---|
221 | pType->cb,
|
---|
222 | pType->ulPointerLevel
|
---|
223 | );
|
---|
224 | for (j = 0; j < sizeof(pType->aul) / sizeof(pType->aul[0]); j++)
|
---|
225 | printf(" 0x%08x", pType->aul[j]);
|
---|
226 | printf("\n");
|
---|
227 | }
|
---|
228 |
|
---|
229 | fprintf(stderr, "\n/*debug: CurPos=0x%x cbFile=0x%x */\n",
|
---|
230 | (unsigned)pType - (unsigned)pvFile, cbFile);
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | int makeName(char *pszName, PSDFMEMBER pMember, PSDFHEADER pHdr, PSDFTYPE paTypes)
|
---|
235 | {
|
---|
236 | char * psz = pszName;
|
---|
237 | PSDFTYPE pType = sdfGetType(pHdr, paTypes, pMember->szType);
|
---|
238 |
|
---|
239 | strcpy(pszName, " ");
|
---|
240 |
|
---|
241 | if (pType != NULL && pMember->cb > pType->cb && (pType->cb > 0 || pMember->chPointerLevel > 0))
|
---|
242 | {
|
---|
243 | int i = 0;
|
---|
244 | int c = pMember->chPointerLevel - pType->ulPointerLevel;
|
---|
245 | for (i = 0; i < c; i++)
|
---|
246 | pszName[i] = '*';
|
---|
247 | pszName[i > 3 ? i : 3] = '\0';
|
---|
248 |
|
---|
249 | psz = pszName + strlen(pszName);
|
---|
250 | if (c <= 0 && pType->cb > 0)
|
---|
251 | sprintf(psz, "%s[%d]", pMember->szName, pMember->cb / pType->cb);
|
---|
252 | else
|
---|
253 | sprintf(psz, "%s", pMember->szName);
|
---|
254 | }
|
---|
255 | else
|
---|
256 | strcat(pszName, pMember->szName);
|
---|
257 |
|
---|
258 | return strlen(pszName);
|
---|
259 | }
|
---|
260 |
|
---|
261 | void makeTypedefsForStruct(PSDFHEADER pHdr, PSDFTYPE paTypes, PSDFSTRUCT pStruct, FILE *phOut)
|
---|
262 | {
|
---|
263 | int i;
|
---|
264 | for (i = 0; i < pHdr->cTypes; i++)
|
---|
265 | if (strcmp(paTypes[i].szStruct, pStruct->szName) == 0)
|
---|
266 | {
|
---|
267 | int j;
|
---|
268 | fprintf(phOut, "typedef struct %s ", pStruct->szName);
|
---|
269 | for (j = 0; j < paTypes[i].ulPointerLevel; j++)
|
---|
270 | fprintf(phOut, "*");
|
---|
271 | fprintf(phOut, " %s;\n", paTypes[i].szName);
|
---|
272 | makeTypedefsForTypedef(pHdr, paTypes, &paTypes[i], phOut);
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | void makeTypedefsForTypedef(PSDFHEADER pHdr, PSDFTYPE paTypes, PSDFTYPE pTypedef, FILE *phOut)
|
---|
277 | {
|
---|
278 | int i;
|
---|
279 | for (i = 0; i < pHdr->cTypes; i++)
|
---|
280 | if (&paTypes[i] != pTypedef && strcmp(paTypes[i].szStruct, pTypedef->szName) == 0)
|
---|
281 | {
|
---|
282 | int j;
|
---|
283 | fprintf(phOut, "typedef %s ", pTypedef->szName);
|
---|
284 | for (j = 0; j < paTypes[i].ulPointerLevel; j++)
|
---|
285 | fprintf(phOut, "*");
|
---|
286 | fprintf(phOut, " %s;\n", paTypes[i].szName);
|
---|
287 | makeTypedefsForTypedef(pHdr, paTypes, &paTypes[i], phOut); /* a bit risky... */
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 |
|
---|
293 |
|
---|
294 | PSDFTYPE sdfGetTypeArray(PSDFHEADER pHdr)
|
---|
295 | {
|
---|
296 | PSDFSTRUCT pStruct = (PSDFSTRUCT)(unsigned)(pHdr + 1);
|
---|
297 | PSDFTYPE pType;
|
---|
298 | int i;
|
---|
299 |
|
---|
300 | for (i = 0; i < pHdr->cStructs; i++)
|
---|
301 | pStruct = (PSDFSTRUCT)((unsigned)(pStruct+1) + sizeof(SDFMEMBER)*(pStruct->cMembers - 1));
|
---|
302 |
|
---|
303 | printf("typearray offset: 0x%08x\n\n", (unsigned)pStruct - (unsigned)pHdr);
|
---|
304 | return (PSDFTYPE)(unsigned)pStruct;
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 | PSDFTYPE sdfGetType(PSDFHEADER pHdr, PSDFTYPE paTypes, const char *pszType)
|
---|
309 | {
|
---|
310 | if (*pszType != '\0')
|
---|
311 | {
|
---|
312 | int i;
|
---|
313 | for (i = 0; i < pHdr->cTypes; i++)
|
---|
314 | if (strcmp(paTypes[i].szName, pszType) == 0)
|
---|
315 | return &paTypes[i];
|
---|
316 | }
|
---|
317 |
|
---|
318 | return NULL;
|
---|
319 | }
|
---|