Tutorial - Decode a PNG image file
Follow this tutorial to learn how a new grammar can be built for a file format like PNG.
Another tutorial worth following with Synalyze It! is this one for Adobe Swatch Exchange files (click here)
It's quite easy to create a grammar for a binary file interactively. Simply follow these steps to decode the contents of a PNG image file. (Refer to https://www.w3.org/TR/PNG/ for details)
First, Synalyze It! welcomes you like this:
In most cases you'll first want to open the file to analyze - click Open File
Hint: press Cmd-Shift-G to change to the folder /System/Library/Image Capture/Support/Icons as seen here:
Select GenericCard.png and click Open.
If you didn't disable the suggestion of grammars in the preferences, you're asked if you want to download the existing PNG grammar from the Synalyze It! website:
For this tutorial, say No because you want to create your own PNG grammar.
You'll get an ordinary hex view:
You could press Cmd-Shift-N to create a new grammar, but it’s easier to click the Grammar listbox in the toolbar and...
Click Create Grammar….
A new grammar document will show up:
If you click the Grammar Info button in its toolbar you see that all fields are filled for you automatically...
Hint: you can also use this dialog to do translations of UTI, MIME Type, File extension and OSType. Just edit the value you want to translate.
Hit Enter or press OK.
If you go back now to your reference document, the hex view should look like this:
Now let's continue with building our PNG grammar...
The header of PNG files consists of 8 bytes. To add these bytes to your grammar simply select the first 8 bytes in the hex view, Ctrl-click (or right click) the selection and choose Insert/Binary.
The next step is to name and color the new binary structure element you are adding:
All properties like names and colors can be changed later in the grammar editor if needed.
The hex and result views reflect these changes:
Let's add the other components of the grammar....
You may prefer to see start, end and length of the selection in decimal numbers.
Simply Ctrl-Click on the start, end or length value of the selection and change its number format (or all of them).
Each chunk of a PNG file consists of a 4-byte length field, a 4-byte chunk name, the data of the chunk and a 4-byte CRC check sum.
To add the chunk structure to the grammar, select the 25 (19h) bytes behind the header bytes (bytes 8-32), Ctrl-Click the selection and choose Insert/Structure:
Not to get confused, name the new structure "Chunk" and - if you like - color it using the Color field.
Now you can add the elements of the Chunk structure: select the first 4 bytes in the chunk (Length field, bytes 8-11), add them as a Number...
Add the next 4 bytes (chunk Type, 12-15) as a String...
... bytes 16-28 (chunk Data) as a Structure...
... and the last 4 bytes (CRC, 29-32) as a Number.
Your parsed file should look now like this:
... and the grammar should look now like this:
According to the PNG specification the length field takes the number of bytes in the data section of a chunk. As you created the structure elements from the first chunk, the length is set to 13.
To use the value of the length field in the Chunk structure, enter Length in the length field of the Data element:
The length field of the Chunk structure should be cleared now so it will be considered to have variable size (actually the size of the elements it contains).
Additionally the Repeat max count should be set to Unlimited.
Now the chunk structure is complete and can be applied repeatedly to the file...
The whole file should be decoded now:
Wasn't this easy? Even better: the grammar you just created can be applied to any PNG file! Check out the free PNG grammar to see how the different PNG chunks can be analyzed in detail.
There are dozens of grammars for other formats that can teach you what else grammars can accomplish: grammar files