source: branches/Grace/src/win32k/libconv.c@ 3834

Last change on this file since 3834 was 3834, checked in by bird, 25 years ago

Checkin of current code in the Grace brance for backup purpose.

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