DOS MZ executable: Java parsing library

DOS MZ file format is a traditional format for executables in MS-DOS environment. Many modern formats (i.e. Windows PE) still maintain compatibility stub with this format.

As opposed to .com file format (which basically sports one 64K code segment of raw CPU instructions), DOS MZ .exe file format allowed more flexible memory management, loading of larger programs and added support for relocations.

File extension

["exe", "ovl"]

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of DOS MZ executable using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Parse a local file and get structure in memory:

DosMz data = DosMz.fromFile("path/to/local/file.["exe", "ovl"]");

Or parse structure from a byte array:

byte[] someArray = new byte[] { ... };
DosMz data = new DosMz(new ByteBufferKaitaiStream(someArray));

After that, one can get various attributes from the structure by invoking getter methods like:

data.hdr() // => get hdr

Java source code to parse DOS MZ executable

DosMz.java

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

import io.kaitai.struct.ByteBufferKaitaiStream;
import io.kaitai.struct.KaitaiStruct;
import io.kaitai.struct.KaitaiStream;
import java.io.IOException;
import java.util.ArrayList;


/**
 * DOS MZ file format is a traditional format for executables in MS-DOS
 * environment. Many modern formats (i.e. Windows PE) still maintain
 * compatibility stub with this format.
 * 
 * As opposed to .com file format (which basically sports one 64K code
 * segment of raw CPU instructions), DOS MZ .exe file format allowed
 * more flexible memory management, loading of larger programs and
 * added support for relocations.
 */
public class DosMz extends KaitaiStruct {
    public static DosMz fromFile(String fileName) throws IOException {
        return new DosMz(new ByteBufferKaitaiStream(fileName));
    }

    public DosMz(KaitaiStream _io) {
        this(_io, null, null);
    }

    public DosMz(KaitaiStream _io, KaitaiStruct _parent) {
        this(_io, _parent, null);
    }

    public DosMz(KaitaiStream _io, KaitaiStruct _parent, DosMz _root) {
        super(_io);
        this._parent = _parent;
        this._root = _root == null ? this : _root;
        _read();
    }
    private void _read() {
        this.hdr = new MzHeader(this._io, this, _root);
        this.mzHeader2 = this._io.readBytes((hdr().ofsRelocations() - 28));
        relocations = new ArrayList<Relocation>((int) (hdr().numRelocations()));
        for (int i = 0; i < hdr().numRelocations(); i++) {
            this.relocations.add(new Relocation(this._io, this, _root));
        }
        this.body = this._io.readBytesFull();
    }
    public static class MzHeader extends KaitaiStruct {
        public static MzHeader fromFile(String fileName) throws IOException {
            return new MzHeader(new ByteBufferKaitaiStream(fileName));
        }

        public MzHeader(KaitaiStream _io) {
            this(_io, null, null);
        }

        public MzHeader(KaitaiStream _io, DosMz _parent) {
            this(_io, _parent, null);
        }

        public MzHeader(KaitaiStream _io, DosMz _parent, DosMz _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
            _read();
        }
        private void _read() {
            this.magic = this._io.readBytes(2);
            this.lastPageExtraBytes = this._io.readU2le();
            this.numPages = this._io.readU2le();
            this.numRelocations = this._io.readU2le();
            this.headerSize = this._io.readU2le();
            this.minAllocation = this._io.readU2le();
            this.maxAllocation = this._io.readU2le();
            this.initialSs = this._io.readU2le();
            this.initialSp = this._io.readU2le();
            this.checksum = this._io.readU2le();
            this.initialIp = this._io.readU2le();
            this.initialCs = this._io.readU2le();
            this.ofsRelocations = this._io.readU2le();
            this.overlayId = this._io.readU2le();
        }
        private byte[] magic;
        private int lastPageExtraBytes;
        private int numPages;
        private int numRelocations;
        private int headerSize;
        private int minAllocation;
        private int maxAllocation;
        private int initialSs;
        private int initialSp;
        private int checksum;
        private int initialIp;
        private int initialCs;
        private int ofsRelocations;
        private int overlayId;
        private DosMz _root;
        private DosMz _parent;
        public byte[] magic() { return magic; }
        public int lastPageExtraBytes() { return lastPageExtraBytes; }
        public int numPages() { return numPages; }
        public int numRelocations() { return numRelocations; }
        public int headerSize() { return headerSize; }
        public int minAllocation() { return minAllocation; }
        public int maxAllocation() { return maxAllocation; }
        public int initialSs() { return initialSs; }
        public int initialSp() { return initialSp; }
        public int checksum() { return checksum; }
        public int initialIp() { return initialIp; }
        public int initialCs() { return initialCs; }
        public int ofsRelocations() { return ofsRelocations; }
        public int overlayId() { return overlayId; }
        public DosMz _root() { return _root; }
        public DosMz _parent() { return _parent; }
    }
    public static class Relocation extends KaitaiStruct {
        public static Relocation fromFile(String fileName) throws IOException {
            return new Relocation(new ByteBufferKaitaiStream(fileName));
        }

        public Relocation(KaitaiStream _io) {
            this(_io, null, null);
        }

        public Relocation(KaitaiStream _io, DosMz _parent) {
            this(_io, _parent, null);
        }

        public Relocation(KaitaiStream _io, DosMz _parent, DosMz _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
            _read();
        }
        private void _read() {
            this.ofs = this._io.readU2le();
            this.seg = this._io.readU2le();
        }
        private int ofs;
        private int seg;
        private DosMz _root;
        private DosMz _parent;
        public int ofs() { return ofs; }
        public int seg() { return seg; }
        public DosMz _root() { return _root; }
        public DosMz _parent() { return _parent; }
    }
    private MzHeader hdr;
    private byte[] mzHeader2;
    private ArrayList<Relocation> relocations;
    private byte[] body;
    private DosMz _root;
    private KaitaiStruct _parent;
    public MzHeader hdr() { return hdr; }
    public byte[] mzHeader2() { return mzHeader2; }
    public ArrayList<Relocation> relocations() { return relocations; }
    public byte[] body() { return body; }
    public DosMz _root() { return _root; }
    public KaitaiStruct _parent() { return _parent; }
}