Source Code of exe2com.

简介: /*     exe2com - exe2bin replacement by Chris Dunford/Cove Software     usage: exe2com [/I] infile [outfile]     usage is the same as exe2bin except:         1.

/*
    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 original 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 versions, we defined an
        error code for nonzero CS but didn't actually check it; now
        we do.  Source will now compile under either Microsoft C or
        Turbo 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 <stdio.h>
#include <string.h>
#include <ctype.h>

/* Version coding */
#define MAJVER   1
#define MINVER   0
#define REVISION 4

/* Conversion error codes */
#define BADREAD  0
#define BADWRITE 1
#define BADSIG   2
#define HASRELO  3
#define HAS_SS   4
#define HAS_CS   5
#define BAD_IP   6
#define TOO_BIG  7
/* This must be the last code */
#define UNKNOWN  8

/* Define size of console output buffer */
#define CONBUFSIZ 2048

/*
**  Define structure of fixed-format part of EXE file header
*/
struct exe_header {
        char exe_sig[2];    /* EXE file signature: "MZ" */
    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,   /* 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);
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 < argc; i++) {
while ( (cp = strchr (cp, '/')) != (char *) NULL) {
*cp++ = '/0';
c = *cp++;
switch (toupper (c)) {
case 'I':
info = 1;
break;
default:
usage ();
}
}

if (**argv)
if (fin[0] == '/0')
strcpy (fin, strlwr (*argv));
else if (fon[0] == '/0')
strcpy (fon, strlwr (*argv));
else
usage ();

cp = *(++argv);
}

/* Check to ensure that an input filename was found *.
if (fin[0] == '/0') usage ();

/* If the input file has no extension, add .EXE */
if (strchr (fin, '.') == (char *) NULL)
strcat (fin, ".exe");

/* Copy input name to output if unspecified */
if (fon[0] == '/0')
strcpy (fon, fin);

/* Check output extension--change EXE to COM, or add COM */
if ((cp = strchr (fon, '.')) == (char *) NULL)
strcat (fon, defext);
else if (strcmp (cp, ".exe") == 0)
strcpy (cp, defext);

/* Try to open input file */
if ((fi = fopen (fin, "rb")) == (FILE *) NULL) {
fprintf (stderr, "exe2com: can't find input file %s/n", fin);
exit (1);
}

/* Try to create output file, if INFO not requested */
if (!info)
if ((fo = fopen (fon, "wb")) == (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 read_hdr(void)
{
char *cp;

/* Read the formatted portion of the header */
if (!fread (&xh, sizeof (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 computed as follows; it cannot exceed 64K bytes:
    **     512 * (# EXE pages - 1)
    **   + valid bytes in last EXE page
    **   - offset of program image in EXE file
    **
    ** Note that if the IP is nonzero, we will skip the first
    ** 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 requested */
if (info) return;

/* Check header; to be convertible, must have:
** -- no relocatable items
** -- no stack segment
** -- no code segment
** -- IP == 0 or 100
** -- code size < 65536
*/
if (xh.relo_ct)
err_xit (HASRELO);
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 > <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />65536L)
        err_xit (TOO_BIG);

    /* Issue a warning if COM file and IP != 0x100 */
    if (!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 = (unsigned long) (xh.pages-1) * 512 + (xh.excess ? xh.excess : 512);
    printf ("  EXE file size               %5lX    %7lu/n", k, k);

    printf ("  EXE header size (para)       %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_num, xh.ovl_num);

    printf ("* = this item prevents conversion to BIN/COM/n");
}

/*
**  Convert the file.  Nothing to do, really, other than
**  reading the image (which follows the header), and
**  dumping it back out to disk.
*/
void convert (void)
{
#define BUFSIZE 16384
static char buffer[BUFSIZE];  /* 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; code_size -= bsize) {

        /* Set count of bytes to read/write */
        bsize = code_size > BUFSIZE ? BUFSIZE : code_size;

        /* Read and write block */
        if (!fread (buffer, bsize, 1, fi))
            err_xit (BADREAD);
        if (!fwrite (buffer, bsize, 1, fo))
            err_xit (BADWRITE);
    }

    /* All done, close the two files */
    fclose (fi);
    fclose (fo);
}


/*
**  Display an error message, delete output file, exit.
*/
void err_xit (code)
unsigned code;
{
static char *msg[UNKNOWN+1] = {
        "error reading EXE header",
        "error writing output file",
        "invalid EXE file signature",
        "EXE has relocatable items",
        "EXE has stack segment",
        "EXE has nonzero CS",
        "IP not 0 or 100H",
        "program exceeds 64K",
        "unknown internal error"
};

    if (code > UNKNOWN) code = UNKNOWN;
    fprintf (stderr, "exe2com: %s, can't convert/n", msg[code]);

    /* Close two files and delete partial output */
    fclose (fi);
    fclose (fo);
    unlink (fon);

    /* Exit with errorlevel 1 */
    exit (1);
}

目录
相关文章
|
5天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
6680 15
|
5天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
2288 8
|
8天前
|
人工智能 前端开发 API
阿里云Qwen4-Max 大模型详解:定位、特征、能力、适用场景与相对Qwen3-Max差异解析
2026年11月11日,阿里发布第四代旗舰模型Qwen4-Max预览版,首发千问AI平台Token Plan并接入千问办公、Qoder等入口。该版本聚焦知识理解、世界感知、编程与Agentic四项升级,主打复杂编程、专业办公、前端设计与专业软件操作等真实任务。预览期为11月11日10:00至11月25日24:00,仅支持Token Plan调用,到期后模型标识下线;限时2折、新用户再享5折,最低1折可体验。
|
8天前
|
JSON 前端开发 API
阿里云最新Qwen4-Max大模型介绍:模型定位、特征、能力、适用场景解析
2026 年 11 月 11 日,阿里发布第四代旗舰模型Qwen4-Max预览版,首发千问 AI 平台 Token Plan,并同步接入千问办公、Qoder 等入口。该版本聚焦知识理解、世界感知、编程与 Agentic 四项升级,主打复杂编程、专业办公、前端设计与专业软件操作。预览期至 11 月 25 日 24:00,到期后模型标识下线;限时 2 折、新用户再享 5 折,最低 1 折可体验。
|
3天前
|
传感器 安全 区块链
Windows 电脑运行安卓应用教程:开源工具 ApkStart 双击 APK 即装、独立窗口运行
ApkStart 是一款开源Windows工具,让安卓APP像原生软件一样运行:双击APK即安装、生成真实图标、独立窗口启动、开始菜单可搜索。无需传统模拟器,内置QEMU运行时,支持数据互通、ARM兼容与命令行操作。(239字)
|
21天前
|
Linux 开发工具 iOS开发
Codex保姆级教程(下载+安装+登录+使用,含所有版本,2026最新)
Codex是OpenAI推出的开源AI编程智能体,本地运行,可理解整个代码库,支持自然语言指令改代码、执行命令、跑测试、修Bug,兼容Win/macOS/Linux。(239字)
10140 59
|
4天前
|
人工智能 弹性计算 开发工具
零成本玩各大模型!DeepSeek Harness dsh-our-free-model 插件,13 个厂商模型直接用
总结一下:装这个插件,你不用注册、不用绑卡、不用填一个 Key,就能在 DeepSeek Harness 里用上 DeepSeek V4.1 Flash、Kimi K3 这一批前沿模型,而且是白嫖的、还不限量。三步搞定:下载插件,复制包含 `package.json` 的插件目录路径,在「添加插件」里粘贴路径安装并重启。无论是在阿里云 ECS 上通过命令行部署的 Web 版,还是本地的 Electron 桌面端,都能顺利跑通,快速搭建一套零成本的免费模型使用环境。
1037 0
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。
|
15天前
|
Linux 开发工具 iOS开发
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
本文详解OpenAI Codex国内快速上手指南:提供Win/macOS/Linux全平台免安装包、npm/Homebrew等多方式安装、账号/API Key双登录、简体中文切换,覆盖下载、安装、汉化、使用及高频避坑(如微软商店失败、npm超时等),10分钟即可跑通。
3465 9