C code to dump a New executables Imported and expoted functions
Assistant
User
What were the standard execuatble format in the 16 bit era of Mac?
Assistant
During the 16-bit era of Macintosh computers, the standard executable format was the Macintosh Resource Fork and Data Fork structure, commonly associated with Mac OS applications from the late 1980s through the 1990s.
Key Characteristics:
:
The NE file format, also known as the New Executable format, is a file format used for executables in 16-bit operating systems like Windows 3.x. This format is more advanced than the older MZ format and was used to allow more complex executables.
Below is a C struct that represents the NE file header. This struct follows the format specified in the NE executable specification:
c
#include <stdint.h>
#pragma pack(push, 1) // Disable padding to ensure the struct matches the file format.
typedef struct {
uint16_t ne_magic; // Magic number, should be 0x454E ('NE' in ASCII)
uint8_t ne_ver; // Linker version number
uint8_t ne_rev; // Linker revision number
uint16_t ne_enttab; // Offset to the entry table
uint16_t ne_cbenttab; // Length of the entry table in bytes
uint32_t ne_crc; // Checksum of whole file
uint16_t ne_flags; // Flag word
uint16_t ne_autodata; // Automatic data segment number
uint16_t ne_heap; // Initial size of the local heap in bytes
uint16_t ne_stack; // Initial size of the stack in bytes
uint32_t ne_csip; // Initial CS:IP
uint32_t ne_sssp; // Initial SS:SP
uint16_t ne_cseg; // Count of file segments
uint16_t ne_cmod; // Entries in the module reference table
uint16_t ne_cbnrestab; // Size of the non-resident name table in bytes
uint16_t ne_segtab; // Offset to the segment table
uint16_t ne_rsrctab; // Offset to the resource table
uint16_t ne_restab; // Offset to the resident name table
uint16_t ne_modtab; // Offset to the module reference table
uint16_t ne_imptab; // Offset to the imported names table
uint32_t ne_nrestab; // Offset to the non-resident names table
uint16_t ne_cmovent; // Count of movable entry points
uint16_t ne_align; // Logical sector alignment shift count
uint16_t ne_cres; // Count of resource segments
uint8_t ne_exetyp; // Target operating system
uint8_t ne_flagsothers; // Other .EXE flags
uint16_t ne_pretthunks; // Offset to return thunks
uint16_t ne_psegrefbytes; // Offset to segment reference bytes
uint16_t ne_swaparea; // Minimum code swap area size
uint16_t ne_expver; // Expected Windows version number
} NE_Header;
#pragma pack(pop)
Explanation
Magic Number: The ne_magic field should contain 0x454E, which corresponds to the string "NE" indicating that this is an NE executable.
Version and Revision: ne_ver and ne_rev are used to specify the linker version and revision.
Offset Fields: Many fields like ne_enttab, ne_segtab, ne_rsrctab, etc. are offsets from the beginning of the NE header to different tables in the file.
Segment and Entry Counts: ne_cseg and ne_cmovent indicate counts of segments and movable entry points, respectively.
Checksum and Flags: ne_crc is used for a checksum of the file, and ne_flags hold various flags related to the executable.
This struct should be packed to prevent the compiler from adding any padding between the fields, ensuring that the layout of the struct matches the layout of the NE header in an executable file. The use of #pragma pack(push, 1) achieves this.
To dump the imported and exported functions from a New Executable (NE) file, you need to parse the NE header and navigate to the relevant tables in the file. I'll provide a simplified version of how you might achieve this.
Please note that NE files have a complex structure, and accurately parsing them requires careful attention to the specification. This code does not handle all edge cases but gives a basic outline for extracting the information.
Here's an example C code outline to dump imported and exported functions from a NE file:
Header Validation: First, check for the 'NE' magic number to make sure the file is a valid NE executable.
Dump Imports: The import names are read from ne_imptab until a zero-length name is encountered. Each name is preceded by its length.
Dump Exports: The export names are typically found in the entry table (ne_enttab). The example above reads export names from the resident name table (ne_restab) using offsets from the entry table.
File Navigation: Use fseek to navigate to different parts of the file based on offsets found in the NE header.
Note: This example is greatly simplified and may need additional logic for comprehensive NE file parsing and handling edge cases.
Resource Fork and Data Fork
Resource Fork: This fork contained structured data like icons, images, sounds, menus, window layouts, and code resources (such as those where executable code might be stored).
Data Fork: This fork typically stored unstructured data, similar to the primary data in files on other systems.
Resources:
Resources were used to store various elements needed by applications, like user interface components and executable code segments.
Different types of resources were identified by unique type codes (e.g., 'CODE' for code resources, 'ICN#' for icons).
Code Execution:
The actual executable code was often stored as a resource within the resource fork, usually under the 'CODE' resource type. These resources were segmented parts of the program code.
Icons and Metadata:
The resource fork included the application's icon, essential for the classic Mac OS finder display.
File Types and Creator Codes:
Classic Mac OS used metadata associated with files to identify file types and the "creator" application through four-character codes stored in the file metadata rather than through file extensions.
Key Tools and Formats:
Resource Editor (ResEdit):
A tool provided for developers to create and edit resources within a Macintosh application's resource fork.
Executable Format:
The actual format of the executable code within the resource fork could vary, but applications often contained a mix of 68000 assembly or compiled C/C++ code based on the Macintosh programming environment, such as MacApp.
This format persisted throughout the era of 68k (Motorola 68000) Macintosh computers and into the early PowerPC era until the transition to Mac OS X, which introduced the Mach-O file format along with the move towards UNIX-based underpinnings and away from the classic Mac OS architecture.