home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / HILFEN / PACKER / EXE2COM / EXE2COM.C next >
C/C++ Source or Header  |  1993-12-01  |  9KB  |  295 lines

  1. /*
  2.     exe2com - exe2bin replacement by Chris Dunford/Cove Software
  3.  
  4.     usage: exe2com file [file]
  5.     usage is the same as exe2bin except:
  6.         1. Output defaults to COM rather than BIN
  7.         2. Binary fixup option not supported
  8.         3. Checksum not verified
  9.         4. Provides more useful error messages, and a warning if a
  10.            COM file is being created with initial IP != 0x100.
  11.  
  12.     Compiler notes:
  13.         The original executable of this file was produced with the
  14.         Computer Innovations C86 compiler (v2.2), but it should work
  15.         with just about any C compiler, with the possible exception
  16.         of the lower() function.  This function is part of the C86
  17.         library; it simply lowercases a string without moving it and
  18.         returns the address of the string.  If your compiler doesn't
  19.         have lower() or an equivalent, add this function:
  20.  
  21.             char *lower (cp)
  22.             char *cp;
  23.             {
  24.             char *cp0;
  25.  
  26.                 for (cp0=cp; *cp=tolower (*cp); ++cp)
  27.                     ;
  28.                 return cp0;
  29.             }
  30.  
  31.     This program was knocked together in about an hour in response to
  32.     the removal of EXE2BIN from the standard DOS distribution disks.
  33.     Improvements/corrections are encouraged.
  34.  
  35.     Program donated to the public domain by the author.
  36.  
  37.     cjd 4/17/87
  38. */
  39.  
  40. #include <stdio.h>
  41.  
  42. #define void int
  43.  
  44. /* Conversion error codes */
  45. #define BADREAD  1
  46. #define BADWRITE 2
  47. #define BADSIG   3
  48. #define HASRELO  4
  49. #define HAS_SS   5
  50. #define HAS_CS   6
  51. #define BAD_IP   7
  52. #define TOO_BIG  8
  53.  
  54. /*
  55. **  Define structure of fixed-format part of EXE file header
  56. */
  57. struct exe_header {
  58.         char exe_sig[2];    /* EXE file signature: "MZ" */
  59.     unsigned excess,        /* Image size mod 512 (valid bytes in last page) */
  60.              pages,         /* # 512-byte pages in image */
  61.              relo_ct,       /* Count of relocation table entries */
  62.              hdr_size,      /* Size of header, in paragraphs */
  63.              min_mem,       /* Min required memory */
  64.              max_mem,       /* Max required memory */
  65.              ss,            /* Stack seg offset in load module */
  66.              sp,            /* Initial value of SP */
  67.              cksum,         /* File checksum */
  68.              ip,            /* Initial value of IP */
  69.              cs,            /* CS offset in load module */
  70.              relo_start,    /* Offset of first relo item */
  71.              ovl_num;       /* Overlay number */
  72. } xh;
  73.  
  74. FILE *fi,                   /* Input file stream */
  75.      *fo,                   /* Output file stream */
  76.      *fopen();
  77.  
  78. char fin[129],              /* Input file name */
  79.      fon[129];              /* Output file name */
  80.  
  81.  
  82. unsigned long code_start,   /* Offset of program image in EXE file */
  83.               code_size;    /* Size of program image, in bytes */
  84.  
  85. extern char *strcpy(), *strcat(), *index(), *lower();
  86.  
  87. main(argc, argv)
  88. unsigned argc;
  89. char *argv[];
  90. {
  91.     init (argc, argv);
  92.     read_hdr ();
  93.     convert ();
  94. }
  95.  
  96.  
  97. /*
  98. **  Initialize - get filenames and open/create files
  99. */
  100. void init (argc, argv)
  101. unsigned argc;
  102. char *argv[];
  103. {
  104. char *cp;
  105.  
  106.     printf ("exe2com 1.00 by Chris Dunford/The Cove Software Group\n");
  107.  
  108.     /* Check arg count */
  109.     if (argc < 2 || argc > 3) {
  110.         fprintf (stderr, "usage: exe2com file [file]\n");
  111.         exit (1);
  112.     }
  113.  
  114.     /* If argv[1] (the input file) has no extension, add .EXE */
  115.     strcpy (fin, lower (argv[1]));
  116.     if (!index (fin, '.'))
  117.         strcat (fin, ".exe");
  118.  
  119.     /* Get or construct output file name */
  120.     if (argc == 3)
  121.         strcpy (fon, lower (argv[2]));
  122.     else
  123.         strcpy (fon, fin);
  124.  
  125.     /* Check output extension--change EXE to COM, or add COM */
  126.     if (!(cp = index (fon, '.')))
  127.         strcat (fon, ".com");
  128.     else if (strcmp (cp, ".exe") == 0)
  129.         strcpy (cp, ".com");
  130.  
  131. #ifdef DEBUG
  132.     printf ("input=%s, output=%s\n", fin, fon);
  133. #endif
  134.  
  135.     /* Try to open input file */
  136.     if (!(fi = fopen (fin, "rb"))) {
  137.         fprintf (stderr, "exe2com: can't find input file %s\n", fin);
  138.         exit (1);
  139.     }
  140.  
  141.     /* Try to create output file */
  142.     if (!(fo = fopen (fon, "wb"))) {
  143.         fprintf (stderr, "exe2com: can't open output file %s\n", fin);
  144.         exit (1);
  145.     }
  146. }
  147.  
  148.  
  149. /*
  150. **  Read and check the EXE file header
  151. */
  152. void read_hdr()
  153. {
  154.  
  155.     /* Read the formatted portion of the header */
  156.     if (!fread (&xh, sizeof (struct exe_header), 1, fi))
  157.         err_xit (BADREAD);
  158.  
  159. #ifdef DEBUG
  160.     printf ("EXE file header:\n");
  161.     printf ("  signature      %c%c\n", xh.exe_sig[0], xh.exe_sig[1]);
  162.     printf ("  bytes last pg  %04x\n", xh.excess);
  163.     printf ("  pages          %04x\n", xh.pages);
  164.     printf ("  relo count     %04x\n", xh.relo_ct);
  165.     printf ("  header size    %04x\n", xh.hdr_size);
  166.     printf ("  min mem        %04x\n", xh.min_mem);
  167.     printf ("  max mem        %04x\n", xh.max_mem);
  168.     printf ("  ss             %04x\n", xh.ss);
  169.     printf ("  sp             %04x\n", xh.sp);
  170.     printf ("  checksum       %04x\n", xh.cksum);
  171.     printf ("  ip             %04x\n", xh.ip);
  172.     printf ("  cs             %04x\n", xh.cs);
  173.     printf ("  relo tbl start %04x\n", xh.relo_start);
  174.     printf ("  overlay nbr    %04x\n", xh.ovl_num);
  175. #endif
  176.  
  177.     /* Check header; to be convertible, must have:
  178.     **      -- first two bytes == "MZ"
  179.     **      -- no relocatable items
  180.     **      -- no stack segment
  181.     **      -- no code segment
  182.     **      -- IP == 0 or 100
  183.     */
  184.     if (strncmp (xh.exe_sig, "MZ", 2))
  185.         err_xit (BADSIG);
  186.     if (xh.relo_ct)
  187.         err_xit (HASRELO);
  188.     if (xh.ss || xh.sp)
  189.         err_xit (HAS_SS);
  190.     if (xh.ip != 0 && xh.ip != 0x100)
  191.         err_xit (BAD_IP);
  192.  
  193.     /* Compute offset of program image in module, and program size.
  194.     **
  195.     ** The program size is computed as follows; it cannot exceed 64K bytes:
  196.     **     512 * (# EXE pages - 1)
  197.     **   + valid bytes in last EXE page
  198.     **   - offset of program image in EXE file
  199.     **
  200.     ** Note that if the IP is nonzero, we will skip the first
  201.     ** IP bytes of the program image, and copy IP bytes fewer
  202.     ** than the actual size.
  203.     */
  204.     code_start = ((unsigned long) xh.hdr_size) << 4;
  205.     code_size = (unsigned long) (xh.pages-1) * 512 + xh.excess - code_start;
  206.     if (code_size > 65536L)
  207.         err_xit (TOO_BIG);
  208.  
  209.     /* Issue a warning if COM file and IP != 0x100 */
  210.     if (!strcmp (index (fon, '.'), ".com") && xh.ip != 0x100)
  211.         fprintf (stderr, "exe2com warning: COM file, initial IP not 100H\n");
  212.  
  213. }
  214.  
  215.  
  216.  
  217. /*
  218. **  Convert the file.  Nothing to do, really, other than
  219. **  reading the image (which follows the header), and
  220. **  dumping it back out to disk.
  221. */
  222. void convert ()
  223. {
  224. char buffer[512];
  225. unsigned wsize;
  226. extern unsigned long fseek();
  227.  
  228.     /* Seek to start of program image, skipping IP bytes */
  229.     if (fseek (fi, code_start+xh.ip, 0) != code_start+xh.ip)
  230.         err_xit (BADREAD);
  231.  
  232.     /* Reduce the "remaining" byte count by IP bytes */
  233.     code_size -= xh.ip;
  234.  
  235.     /* Read blocks and copy to output */
  236.     while (code_size) {
  237.  
  238.         /* Read block */
  239.         if (!fread (buffer, 512, 1, fi))
  240.             err_xit (BADREAD);
  241.  
  242.         /* Set count of bytes to write, write block */
  243.         wsize = code_size > 512 ? 512 : code_size;
  244.         if (!fwrite (buffer, wsize, 1, fo))
  245.             err_xit (BADWRITE);
  246.  
  247.         /* Subtract bytes written from remaining byte count */
  248.         code_size -= wsize;
  249.     }
  250.  
  251.     /* All done, close the two files */
  252.     fclose (fi);
  253.     fclose (fo);
  254. }
  255.  
  256.  
  257. /*
  258. **  Display an error message, delete output file, exit.
  259. */
  260. void err_xit (code)
  261. unsigned code;
  262. {
  263. char msg[64];
  264.  
  265.     switch (code) {
  266.         case BADREAD:   strcpy (msg, "error reading EXE header");
  267.                         break;
  268.         case BADWRITE:  strcpy (msg, "error writing output file");
  269.                         break;
  270.         case BADSIG:    strcpy (msg, "invalid EXE file signature");
  271.                         break;
  272.         case HASRELO:   strcpy (msg, "EXE has relocatable items");
  273.                         break;
  274.         case HAS_SS:    strcpy (msg, "EXE has stack segment");
  275.                         break;
  276.         case HAS_CS:    strcpy (msg, "EXE has nonzero CS");
  277.                         break;
  278.         case BAD_IP:    strcpy (msg, "IP not 0 or 100H");
  279.                         break;
  280.         case TOO_BIG:   strcpy (msg, "program exceeds 64K");
  281.                         break;
  282.         default:        strcpy (msg, "unknown error");
  283.     }
  284.  
  285.     fprintf (stderr, "exe2com: %s, can't convert\n", msg);
  286.  
  287.     /* Close two files and delete partial output */
  288.     fclose (fi);
  289.     fclose (fo);
  290.     unlink (fon);
  291.  
  292.     /* Exit with errorlevel 1 */
  293.     exit (1);
  294. }
  295.