| 1 | /* $Id: libconv.c,v 1.2 1999-11-10 01:45:29 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 |  | 
|---|
| 22 | int fCodeToCode16 = 0; | 
|---|
| 23 |  | 
|---|
| 24 | /*@IntFunc********************************************************************** | 
|---|
| 25 | *   Internal Functions                                                         * | 
|---|
| 26 | *******************************************************************************/ | 
|---|
| 27 | int processFile(const char *pszFilename, const char *pszFilenameOut); | 
|---|
| 28 | void *processRecord(void *pvRecord, void *pvBase, FILE *phNew); | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | int 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 |  | 
|---|
| 48 | int 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 |  | 
|---|
| 105 | void *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 | "DATA",     "FAR_DATA", | 
|---|
| 177 | "_DATA",    "DATA16", | 
|---|
| 178 | "DGROUP",   "DATA16_GROUP", | 
|---|
| 179 | "_BSS",     "DATA16BSS", | 
|---|
| 180 | "BSS",      "DATA16BSS", | 
|---|
| 181 | "_CONST",   "DATA16CONST", | 
|---|
| 182 | "CONST",    "DATA16CONST", | 
|---|
| 183 | NULL, NULL | 
|---|
| 184 | }; | 
|---|
| 185 |  | 
|---|
| 186 | int     cb; | 
|---|
| 187 | int     cbNew; /* record size */ | 
|---|
| 188 | char   *psz; | 
|---|
| 189 | iCode16 = -1; | 
|---|
| 190 |  | 
|---|
| 191 | cpszLNames = 0; | 
|---|
| 192 | cbRecord = *((unsigned short*)((int)pvRecord+1)) + 3; | 
|---|
| 193 | cb = 3; | 
|---|
| 194 | cbNew = 1; | 
|---|
| 195 | psz = (char*)pvRecord + 3; | 
|---|
| 196 | while (cb < cbRecord-1) | 
|---|
| 197 | { | 
|---|
| 198 | int j = 0; | 
|---|
| 199 | char szName[256]; | 
|---|
| 200 |  | 
|---|
| 201 | strncpy(szName, psz+1, *psz); | 
|---|
| 202 | szName[*psz] = '\0'; | 
|---|
| 203 |  | 
|---|
| 204 | while (apszConv[j] != NULL && stricmp(szName, apszConv[j]) != 0) | 
|---|
| 205 | j += 2; | 
|---|
| 206 |  | 
|---|
| 207 | if (apszConv[j] == NULL) | 
|---|
| 208 | { | 
|---|
| 209 | cbNew += *psz + 1; | 
|---|
| 210 | if (*psz != 0) | 
|---|
| 211 | printf("LNAMES: %.*s was not converted\n", *psz, psz+1); | 
|---|
| 212 | apszLNames[cpszLNames++] = strdup(&szName[0]); | 
|---|
| 213 | } | 
|---|
| 214 | else | 
|---|
| 215 | { | 
|---|
| 216 | cbNew += strlen(apszConv[j+1]) + 1; | 
|---|
| 217 | if (strcmp(apszConv[j+1], "CODE16") == 0) | 
|---|
| 218 | iCode16 = cpszLNames; | 
|---|
| 219 | apszLNames[cpszLNames++] = strdup(apszConv[j+1]); | 
|---|
| 220 | } | 
|---|
| 221 | cb += *psz + 1; | 
|---|
| 222 | psz += 1 + *psz; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | if (iCode16 == -1) | 
|---|
| 226 | { | 
|---|
| 227 | iCode16 = cpszLNames; | 
|---|
| 228 | apszLNames[cpszLNames++] = strdup("CODE16"); | 
|---|
| 229 | cbNew += 1 + 6; | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | fChanged = 1; | 
|---|
| 233 | fputc(LNAMES, phNew); | 
|---|
| 234 | fwrite(&cbNew, 1, 2, phNew); | 
|---|
| 235 |  | 
|---|
| 236 | for (i = 0; i < cpszLNames; i++) | 
|---|
| 237 | { | 
|---|
| 238 | fputc(strlen(apszLNames[i]), phNew); | 
|---|
| 239 | fwrite(apszLNames[i], 1, strlen(apszLNames[i]), phNew); | 
|---|
| 240 | } | 
|---|
| 241 |  | 
|---|
| 242 | fputc(0, phNew); | 
|---|
| 243 | break; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | case LIBEND: | 
|---|
| 247 | cbRecord = *((unsigned short*)((int)pvRecord+1)) + 3; | 
|---|
| 248 | fwrite(pvRecord, 1, cbRecord, phNew); | 
|---|
| 249 | return (void*)0xffffffff; /* FINE */ | 
|---|
| 250 | break; | 
|---|
| 251 |  | 
|---|
| 252 | default: | 
|---|
| 253 | cbRecord = *((unsigned short*)((int)pvRecord+1)) + 3; | 
|---|
| 254 | break; | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | if (!fChanged) | 
|---|
| 258 | fwrite(pvRet, 1, cbRecord, phNew); | 
|---|
| 259 |  | 
|---|
| 260 |  | 
|---|
| 261 | return (void*)((int)pvRet + cbRecord); | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 |  | 
|---|