http://blog.9cbs.net/alonesword/archive/2004/08/26/85392.aspx
/ * Exe2com - exe2bin replacement by Chris Dunford / Cove Software usage: exe2com [/ I] infile [outfile] usage is the same as exe2bin except: 1. Output defaults to COM rather than BIN 2. Binary fixup option not supported 3. Checksum not verified 4. Provides more useful error messages and a warning if a COM file is being created with initial IP = 0x100 5. / I switch provides EXE file info instead of converting Compiler notes:! This source was written for Microsoft C version 5.0. It should be reasonably portable Watch out for fseek (); what it returns seems to vary widely between compilers to compile with MSC, use:.. cl exe2com.c (no switches necessary) We have checked that the source (as of version 1.04 Compiles WITHOUT ERROR Under Turbo C 1.5. IT APPEARS TO OPERATE CORRECTLY, But We Ran Only Some Quick Tests; There May Be Subtle Errors Here. The Orig inal version of this program was knocked together in about an hour in response to the removal of EXE2BIN from the standard DOS distribution disks. Improvements / corrections are encouraged, but please try to coordinate public releases through me. Program donated to the public domain by the Author. CJD 4/17/87 Version History --------------- Version 1.04 03/02/88 (CJD) cleaned up Some Ugly Code from The Original Quickie. Added / i (Info ) Switch. In previous version, we defined an error code for nonzero cs but didn't actual check it;
Now We do. Source Will Now Compile Under Microsoft C. Version 1.03 12/30/87 (CJD) C86 Version Converted to Microsoft C (5.0) by Chris Dunford. IncreaSed Size of I / O Buffer To 4k To Improve speed;.. EXE2COM 1.03 is twice as fast as 1.02 and is now slightly faster than EXE2BIN The C86 version will no longer be distributed Version 1.02 11/22/87 by Chris Blum (CompuServe 76625,1041) Fix for even 512-byte boundary file losing last 512 bytes. Also corrected signon per request of Chris Dunford (his name was lost in the translation to Turbo C). Version 1.02 existed in both Turbo C and C86 versions, although only the C86 executable was "officially" distributed. Version 1.01 Was A Turbo C Conversion. Version 1.00 04/17/87 Original C86 Version By Chris Dunford * / #include
* / Unsigned Excess, / * Image Size MOD 512 (Valid Bytes in Last Page) * / Pages, / * # 512-Byte Pages In Image * / Relo_CT, / * Count of Relocation Table Entries * / HDR_Size, / * Size OF Header, in paragraphs * / min_mem, / * min required memory * / max_mem, / * max required memory * / ss, / * stack seg offset in load module * / sp, / * initial value of sp * / cksum, / * File checksum * / ip, / * initial value of ip * / cs, / * cs offset in load module * / RELO_START, / * OFFSET OF FIRST RELO ITEM * / OVL_NUM; / * OVERLAY NUMBER * /} XH; File * Fi , / * Input file stream * / * fo; / * Output file stream * / char fin [129], / * input file name * / fon [129]; / * Output file name * / int info = 0; / * Nonzero if / i found * / char buf [conbufsiz]; / * printf I / o buffer * / char defext [] = ".com"; / * default output extension - Change if you want * / unsigned long code_start, ENSIGNED long code_start, / * Offset of program image in EXE file * / code_size; / * Size of program image, in bytes * / / * Function prototypes * / void init (unsigned, char * []); void read_hdr (void); void disp_info ( Void Convert (Void); void err_xit (unsigned); void usage (void); / * ** Program mainline * / main (argc, argv) unsigned argc; char * argv []; {init (Argc, Argv ); Read_hdr (); if (info) DISP_INFO (); Else Convert ();
} / * ** Initialize - Parse Arguments, Get FileNames, Open / Create Files * / Void Init (argc, argv) unsigned argc; char ** argv; {char C, * cp; int i; / * set up buffered Output , Display logo * / setvbuf (stdout, buf, _iofbf, conbufsiz); Printf ("EXE2COM% u.% U% u by chris dunford / the cove software group / n", Majver, minver, revision; / * Get Arguments * / Cp = * ( argv); for (i = 1; i (File *) NULL) {FPRINTF (stderr, "exe2com: can't open output file% s / n", fin); exit (1);}} / * ** usage display * / void usage (void) { FPRINTF (stderr, "usage: exe2com [/ i] infile [outfile] / n"); exit (1);} / * ** read and check the exe file header * / void ready_hdr (void) {char * cp; / * Read the formatted portion of the header * / if (! FREAD (& XH (Struct EXE_HEADER), 1, FI)) err_xit (badread); / * check for "mz" signature * / if (Strncmp (XH. EXE_SIG, "MZ", 2)) Err_Xit (Badsig); / * Compute Offset Of Program Image In Module, and Program Size. ** ** The Program Size Is Comput As Follows; It cannot Exceed 64K bytes: ** 512 * (# EXE PAGES - 1) ** VALID BYTES in Last EXE Page In EXE File ** ** Note That IF Skip is Nonzero, We Well Skip The first ** ip bytes of the programt ** ip bytes of the program Image, and copy ip bytes fewer ** Than the actual size. * / code_start = ((unsigned long) xh.hdr_size) << 4; code_size = (unsigned long) (XH .pages-1) * 512 (XH.Excess? Xh.Excess: 512) / * fixed 11/19/87 - cjb * / - code_start; / * don't check Anything else if / i recommended * / if ( INFO); / * Check header; to be converatible, must has: ** - no relocatable items ** - no stack segment ** - no code segment ** - ip == 0 or 100 ** - - Code Size <65536 * / if (xh.relo_ct) err_xit (haasrelo); if (xh.ss || xh.sp) err_xit (HAS_SS); if (xh.cs) err_xit (HAS_CS); if (xh.ip ! = 0 && xh.ip! = 0x100) Err_xit (BAD_IP); if (Code_Size> 65536L Err_XIT (TOO_BIG); / * Issue A Warning IF COM File and IP! = 0x100 * / if (! StrCMP (StrCMP (Strchr (FON, '.'), ".Com" && xh.ip! = 0x100) FPRINTF STDERR, "EXE2COM WARNING: COM File, Initial IP Not 100H / N");} / * ** / i output: Display Exe file info * / void disp_info (void) {char * cp; unsigned long k; cp = strrchr (fin, '//'); if (! cp) cp = strchr (fin, ':'); CP = CP? CP : FIN; Printf ("/ n% -20s (hex) (DEC) / N" , CP); K = (XH.PAGES-1) * 512 (XH.Excess? XH.Excess: 512); Printf ("Exe File Size% 5LX% 7lu / N", K, K) PRINTF ("Exe Header Size% 4X% 7U / N", XH.HDR_SIZE, XH.HDR_SIZE); PUTCHAR (CODE_SIZE> 65536L ? '*': ''); Printf ("Program Image Size (Bytes)% 5LX% 7lu / N", Code_Size, Code_SIZE); K = (unsigned long) xh.min_mem * 16 code_size; printf ("Minimum Load) Size (Bytes)% 5LX% 7lu / N ", K, K); Printf (" Min Allocation (PARA) (4X% 7U / N ", XH.min_MEM, XH.min_MEM); Printf (" Max Allocation (PARA) % 4X% 7U / N ", xh.max_mem, xh.max_mem); Putchar (xh.cs || (xh.ip! = 0x100)? '*': '); Printf (" Initial CS: IP% 04x :% 04x / n ", xh.cs, xh.ip); putchar (xh.ss || xh.sp? '*': '); Printf (" Initial SS: sp% 04x:% 04x% 7u) Stack size) / n ", XH.SS, XH.SP, XH.SP); putchar (xh.relo_ct? '*': '); Printf (" Relocation Count% 4x% 7u / n ", xh.relo_ct Xh.relo_ct); Printf ("Relo Table Start% 04x% 7u / N", XH.relo_start, Xh.relo_Start; Printf ("EXE File Checksum% 04x% 7u / N", XH.CKSUM, XH.CKSUM PRINTF ("Overlay Number% 4X% 7U / N", XH.OVL_N Um, xh.ovl_num); Printf ("* = this item prevents converness to bin / com / n");} / * ** Convert the file. Nothing to do, real,} Than ** Reading The Image (Which Follows The header, and ** dumping it back {#define buffs 16384 static char buffer [buffs]; / * forces buffer out of program stack * / unsigned bsize; / * seek to Start of Program Image, Skipping IP Bytes * / IF (FSeek (Fi, Code_Start XH.IP, 0)) Err_XIT (Badread); / * Read Blocks and Copy To Output * / for (Code_Size - = xh.ip; Code_Size ;