Posted on 18-11-03, 14:07 in Embedding roms in images
Post: #1 of 2
Since: 11-03-18

Last post: 727 days
Last view: 720 days
Recently I had the idea of embedding roms in images. Extra data can be embedded in an image in variety of ways, but I wanted to create a file that could be loaded by an image viewer/web browser or in an emulator without any modifications. After a bit of playing around, I found that the PNG format and SNES roms are a good match for this purpose.

Here's a proof of concept that can be opened in bsnes/snes9x/zsnes:



(Rom source: https://github.com/gyuque/snes-m7)

The saving grace for SNES roms is copier headers. To detect and remove them, emulators will generally check if the the file size modulo 0x8000 equals 0x200. Therefore, the trick is to insert padding in the image file so that the embedded rom starts at offset 0x200. This can then be followed by some more padding (to get the file size just right) and finally the pixel data payload.

Most image formats break data into chunks, which is convenient for manipulation, but chunk size limits can be a problem. They constrain the amount of data that can be embedded verbatim, and we want to embed an entire unmodified rom. GIF, being a format originally designed in 1987, limits chunk sizes to 8 bits, which is far too restrictive. JFIF (the JPEG container format) has 16 bit chunks, which is better but still very restrictive. PNG uses 32 bits, which is way more than enough.

The file layout ends up looking like this:
- PNG file signature
- padding chunk
- rom chunk
- padding chunk
- original image payload (IHDR, IDAT, IEND chunks)
Posted on 18-11-03, 23:29 in Embedding roms in images
Post: #2 of 2
Since: 11-03-18

Last post: 727 days
Last view: 720 days
Posted by Screwtape
That's pretty cool! Although I imagine it's still a bit restrictive, since SNES emulators tend to change their behaviour based on the total file-size.
Yeah, you can sneak past the modulo N heuristics with padding, but other heuristics are messed up if the total file size is pushed beyond a certain threshold by the additional data.
Posted by Kawa
Me, I'm reminded of the complete works of Shakespeare hidden in a JPG file, and that one SNES demo that's also a valid MS-DOS executable.
Posted by tomman
Wasn't there a SNES ROM that was also a valid DOS executable?
That Shakespeare JPEG is neat! Windows explorer didn't like it, but 7-zip opened it just fine. It looks like the JFIF chunk size limit was worked around by breaking the data into smaller individual files within the zip. Also, apparently ZIP files don't have to immediately start with a file signature, which is pretty unusual and the only reason this trick was possible in the first place.

The dual DOS/SNES program is also a cool idea, but unfortunately it just hangs DOSBox for me. It did however inspire me to make a Win32 executable based on the same principle. It just prints "hello, world" under Windows though - nothing as fancy as having the same behavior as the embedded rom. I wasn't feeling that inspired.
    Main » invertigo » List of posts
    Yes, it's an ad.