1 | /* $Id: libconv.c,v 1.1 1999-09-08 20:50:02 bird Exp $ */
|
---|
2 | /*
|
---|
3 | * Very simple OMF/LIB dumper.
|
---|
4 | *
|
---|
5 | * Copyright (c) 1999 knut st. osmundsen
|
---|
6 | *
|
---|
7 | */
|
---|
8 |
|
---|
9 |
|
---|
10 | /*@Header***********************************************************************
|
---|
11 | * Header Files *
|
---|
12 | *******************************************************************************/
|
---|
13 | #include <stdio.h>
|
---|
14 | #include <string.h>
|
---|
15 | #include <malloc.h>
|
---|
16 |
|
---|
17 |
|
---|
18 | #include "include\omf.h"
|
---|
19 |
|
---|
20 | int fCodeToCode16 = 0;
|
---|
21 |
|
---|
22 | /*@IntFunc**********************************************************************
|
---|
23 | * Internal Functions *
|
---|
24 | *******************************************************************************/
|
---|
25 | int processFile(const char *pszFilename, const char *pszFilenameOut);
|
---|
26 | void *processRecord(void *pvRecord, void *pvBase, FILE *phNew);
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 | int main(int argc, char **argv)
|
---|
31 | {
|
---|
32 | int rc = 0;
|
---|
33 | int argi = 1;
|
---|
34 |
|
---|
35 | if (argc != 3)
|
---|
36 | printf("syntax error - requires two filename, in.lib and out.lib\n");
|
---|
37 |
|
---|
38 | rc = processFile(argv[argi], argv[argi+1]);
|
---|
39 |
|
---|
40 | return rc;
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | int processFile(const char *pszFilename, const char *pszFilenameOut)
|
---|
47 | {
|
---|
48 | FILE *phFile;
|
---|
49 | FILE *phNew;
|
---|
50 | char *psz;
|
---|
51 | int rc = 0;
|
---|
52 |
|
---|
53 | psz = strrchr(pszFilename, '\\');
|
---|
54 | if (psz)
|
---|
55 | fCodeToCode16 = stricmp(psz, "\\dhcalls.lib") == 0;
|
---|
56 |
|
---|
57 | phFile = fopen(pszFilename, "rb");
|
---|
58 | phNew = fopen(pszFilenameOut, "wb");
|
---|
59 | if (phFile != NULL && phNew != NULL)
|
---|
60 | {
|
---|
61 | int cbFile;
|
---|
62 |
|
---|
63 | /* get size of file */
|
---|
64 | if (!fseek(phFile, 0, SEEK_END)
|
---|
65 | && (cbFile = ftell(phFile) ) != -1
|
---|
66 | && !fseek(phFile, 0, SEEK_SET)
|
---|
67 | )
|
---|
68 | {
|
---|
69 | void *pvFile;
|
---|
70 |
|
---|
71 | /* allocater memory */
|
---|
72 | pvFile = malloc(cbFile);
|
---|
73 | if (pvFile != NULL)
|
---|
74 | {
|
---|
75 | /* read the whole file */
|
---|
76 | if (fread(pvFile, cbFile, 1, phFile) == 1)
|
---|
77 | {
|
---|
78 | void *pvNew = pvFile;
|
---|
79 |
|
---|
80 | /* main loop */
|
---|
81 | while (pvNew < (void*)(cbFile + (int)pvFile))
|
---|
82 | pvNew = processRecord(pvNew, pvFile, phNew);
|
---|
83 | }
|
---|
84 | else
|
---|
85 | rc = 4;
|
---|
86 | }
|
---|
87 | else
|
---|
88 | rc = 3;
|
---|
89 | }
|
---|
90 | else
|
---|
91 | rc = 2;
|
---|
92 | fclose(phFile);
|
---|
93 | fclose(phNew);
|
---|
94 | }
|
---|
95 | else
|
---|
96 | rc = 1;
|
---|
97 |
|
---|
98 | return rc;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 |
|
---|
103 | void *processRecord(void *pvRecord, void *pvBase, FILE *phNew)
|
---|
104 | {
|
---|
105 | static char *apszLNames[256];
|
---|
106 | static int cpszLNames = 0;
|
---|
107 | static int iCode16 = 0;
|
---|
108 | void *pvRet = pvRecord;
|
---|
109 | int cbRecord;
|
---|
110 | int i;
|
---|
111 | int fChanged = 0;
|
---|
112 |
|
---|
113 | switch (*(unsigned char*)pvRecord)
|
---|
114 | {
|
---|
115 | case LIBHDR:
|
---|
116 | fChanged = 1;
|
---|
117 | fwrite(pvRecord, 1, 3, phNew);
|
---|
118 | if (*(unsigned short*)((int)pvRecord+1) < 6)
|
---|
119 | fwrite((char*)pvRecord+3, 1, *(unsigned short*)((int)pvRecord+1), phNew);
|
---|
120 | else
|
---|
121 | {
|
---|
122 | i = 0;
|
---|
123 | fwrite(&i, 1, 2, phNew);
|
---|
124 | fwrite(&i, 1, 2, phNew);
|
---|
125 | fwrite(&i, 1, 2, phNew);
|
---|
126 | fwrite((char*)pvRecord+3+6, 1, *(unsigned short*)((int)pvRecord+1) - 6, phNew);
|
---|
127 | }
|
---|
128 | cbRecord = *((unsigned short*)((int)pvRecord+1)) + 3;
|
---|
129 | break;
|
---|
130 |
|
---|
131 | case 0x98: /* 16-bit segdef */
|
---|
132 | cbRecord = *((unsigned short*)((int)pvRecord+1)) + 3;
|
---|
133 | i = *(char*)((int)pvRecord + 6);
|
---|
134 | if (cbRecord == 10 && i <= cpszLNames && strcmp(apszLNames[i-1], "CODE") == 0)
|
---|
135 | {
|
---|
136 | *(char*)((int)pvRecord + 6) = iCode16+1;
|
---|
137 | }
|
---|
138 | break;
|
---|
139 |
|
---|
140 | case MODEND:
|
---|
141 | case MODEND2:
|
---|
142 | {
|
---|
143 | unsigned long ul = (unsigned long)pvRecord - (unsigned long)pvBase;
|
---|
144 |
|
---|
145 | cbRecord = *((unsigned short*)((int)pvRecord+1)) + 3;
|
---|
146 | /* it seems to be somthing funny here! - lets try aligning it to on a 16 bytes boundrary... */
|
---|
147 | /* PS. I know this I have a wrong approch to the lib files, not using the directory... */
|
---|
148 | fChanged = 1;
|
---|
149 | fwrite(pvRecord, 1, cbRecord, phNew);
|
---|
150 | ul += cbRecord;
|
---|
151 | if (*((unsigned char*)((int)pvRecord+cbRecord)) == 0x00)
|
---|
152 | {
|
---|
153 | if ((ul % 16) > 0)
|
---|
154 | cbRecord += 16 - (ul % 16);
|
---|
155 | }
|
---|
156 | /*if (*((unsigned char*)pvRecord + cbRecord) != LIBEND)*/
|
---|
157 | {
|
---|
158 | ul = ftell(phNew);
|
---|
159 | while (ul++ % 16 != 0)
|
---|
160 | fputc(0, phNew);
|
---|
161 | }
|
---|
162 | while (cpszLNames-- > 0)
|
---|
163 | free(apszLNames[cpszLNames]);
|
---|
164 | } break;
|
---|
165 |
|
---|
166 | case LNAMES:
|
---|
167 | {
|
---|
168 | char *apszConv[] =
|
---|
169 | {
|
---|
170 | "CODE", "CODE",
|
---|
171 | "_CODE", "CODE16",
|
---|
172 | "TEXT", "CODE16",
|
---|
173 | "_TEXT", "CODE16",
|
---|
174 | "DATA", "FAR_DATA",
|
---|
175 | "_DATA", "DATA16",
|
---|
176 | "DGROUP", "DATA16_GROUP",
|
---|
177 | "_BSS", "DATA16BSS",
|
---|
178 | "BSS", "DATA16BSS",
|
---|
179 | "_CONST", "DATA16CONST",
|
---|
180 | "CONST", "DATA16CONST",
|
---|
181 | NULL, NULL
|
---|
182 | };
|
---|
183 |
|
---|
184 | int cb;
|
---|
185 | int cbNew; /* record size */
|
---|
186 | char *psz;
|
---|
187 | iCode16 = -1;
|
---|
188 |
|
---|
189 | cpszLNames = 0;
|
---|
190 | cbRecord = *((unsigned short*)((int)pvRecord+1)) + 3;
|
---|
191 | cb = 3;
|
---|
192 | cbNew = 1;
|
---|
193 | psz = (char*)pvRecord + 3;
|
---|
194 | while (cb < cbRecord-1)
|
---|
195 | {
|
---|
196 | int j = 0;
|
---|
197 | char szName[256];
|
---|
198 |
|
---|
199 | strncpy(szName, psz+1, *psz);
|
---|
200 | szName[*psz] = '\0';
|
---|
201 |
|
---|
202 | while (apszConv[j] != NULL && stricmp(szName, apszConv[j]) != 0)
|
---|
203 | j += 2;
|
---|
204 |
|
---|
205 | if (apszConv[j] == NULL)
|
---|
206 | {
|
---|
207 | cbNew += *psz + 1;
|
---|
208 | if (*psz != 0)
|
---|
209 | printf("LNAMES: %.*s was not converted\n", *psz, psz+1);
|
---|
210 | apszLNames[cpszLNames++] = strdup(&szName[0]);
|
---|
211 | }
|
---|
212 | else
|
---|
213 | {
|
---|
214 | cbNew += strlen(apszConv[j+1]) + 1;
|
---|
215 | if (strcmp(apszConv[j+1], "CODE16") == 0)
|
---|
216 | iCode16 = cpszLNames;
|
---|
217 | apszLNames[cpszLNames++] = strdup(apszConv[j+1]);
|
---|
218 | }
|
---|
219 | cb += *psz + 1;
|
---|
220 | psz += 1 + *psz;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (iCode16 == -1)
|
---|
224 | {
|
---|
225 | iCode16 = cpszLNames;
|
---|
226 | apszLNames[cpszLNames++] = strdup("CODE16");
|
---|
227 | cbNew += 1 + 6;
|
---|
228 | }
|
---|
229 |
|
---|
230 | fChanged = 1;
|
---|
231 | fputc(LNAMES, phNew);
|
---|
232 | fwrite(&cbNew, 1, 2, phNew);
|
---|
233 |
|
---|
234 | for (i = 0; i < cpszLNames; i++)
|
---|
235 | {
|
---|
236 | fputc(strlen(apszLNames[i]), phNew);
|
---|
237 | fwrite(apszLNames[i], 1, strlen(apszLNames[i]), phNew);
|
---|
238 | }
|
---|
239 |
|
---|
240 | fputc(0, phNew);
|
---|
241 | break;
|
---|
242 | }
|
---|
243 |
|
---|
244 | case LIBEND:
|
---|
245 | cbRecord = *((unsigned short*)((int)pvRecord+1)) + 3;
|
---|
246 | fwrite(pvRecord, 1, cbRecord, phNew);
|
---|
247 | return (void*)0xffffffff; /* FINE */
|
---|
248 | break;
|
---|
249 |
|
---|
250 | default:
|
---|
251 | cbRecord = *((unsigned short*)((int)pvRecord+1)) + 3;
|
---|
252 | break;
|
---|
253 | }
|
---|
254 |
|
---|
255 | if (!fChanged)
|
---|
256 | fwrite(pvRet, 1, cbRecord, phNew);
|
---|
257 |
|
---|
258 |
|
---|
259 | return (void*)((int)pvRet + cbRecord);
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|