Created
May 27, 2025 07:46
-
-
Save Romern/49b7eeb6ae6fa1bfc8b786aa67cd8e3f to your computer and use it in GitHub Desktop.
DOS Binary EPS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import struct | |
import sys | |
def xor_checksum(data): | |
checksum = 0 | |
for byte in data: | |
checksum ^= byte | |
return checksum & 0xFFFF | |
def create_dos_eps(postscript_path, tiff_path, output_path): | |
# Read input files | |
with open(postscript_path, 'rb') as f: | |
ps_data = f.read() | |
with open(tiff_path, 'rb') as f: | |
tiff_data = f.read() | |
padding_size = 4096 | |
header_size = 30 | |
ps_start = header_size + padding_size # PostScript starts after header + padding | |
ps_length = len(ps_data) | |
metafile_start = 0 | |
metafile_length = 0 | |
tiff_start = ps_start + ps_length | |
tiff_length = len(tiff_data) | |
# Build the header (first 28 bytes) | |
header = struct.pack( | |
'<I6I', # little-endian: magic + 6 x 4-byte ints | |
0xC6D3D0C5, # Magic number | |
ps_start, # Start of PostScript section | |
ps_length, # Length of PostScript | |
metafile_start, # Metafile start (unused) | |
metafile_length, # Metafile length | |
tiff_start, # TIFF start | |
tiff_length # TIFF length | |
) | |
# Compute checksum over bytes 0–27 | |
checksum = xor_checksum(header) | |
header += struct.pack('<H', checksum) # 28-29: Checksum | |
# Total header is now 30 bytes | |
assert len(header) == 30 | |
# Generate 4096 bytes of padding | |
padding = b'\x00' * padding_size | |
# Write the output file | |
with open(output_path, 'wb') as f: | |
f.write(header) | |
f.write(padding) | |
f.write(ps_data) | |
f.write(tiff_data) | |
print(f'DOS EPS with 4KB padding created: {output_path}') | |
# Example usage | |
if __name__ == "__main__": | |
if len(sys.argv) != 4: | |
print("Usage: python create_dos_eps.py input.ps input.tiff output.eps") | |
sys.exit(1) | |
ps_file = sys.argv[1] | |
tiff_file = sys.argv[2] | |
out_file = sys.argv[3] | |
create_dos_eps(ps_file, tiff_file, out_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment