Parsing an SWF File with Perl

The example in this section reads in the binary data from a provided filename, identifies the header fields, and lists each of the tags in the file. Although the script as shown ignores the body of each of the individual tags, it could easily be extended into a tool for querying a file for specific information or for manipulating an SWF file on a tag-by-tag basis. It would be a simple exercise, for example, to modify the script so that it removes any Protect control tags (tag id=24) that are sometimes used to lock or password-protect a file to prevent it from being edited.

The AnalyzeSWF.pl script prints a report to STDOUT. For example, running the script on a simple one-frame file (the file containing the example shape discussed in the previous section, in fact) yields the following output:

Header Information
Signature/Version: FWS5
File length: 81 bytes
Min X: 0 twips
Max X: 6400 twips
Min Y: 0 twips
Max Y: 4800 twips
Frame rate: 12
Frame count: 1

Tag type: SetBackgroundColor (3 bytes)
Tag type: DefineShape3 (42 bytes)
Tag type: PlaceObject2 (6 bytes)
Tag type: ShowFrame (0 bytes)
Tag type: End (0 bytes)

The script takes the name of an SWF file as an argument. All of the data is read into a buffer before it is analyzed. This may be inefficient memory-wise and time-wise, but it simplifies the example by allowing us to ignore buffer management issues. This could take up a lot of resources if the file in question contains embedded MP3 samples, for example. ...

Get Perl Graphics Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.