In Vitro Transformation of Compiled SWFs
Ever wish you could alter the variables in a compiled SWF file without having to re-publish the SWF? Or perhaps inject different XML into a compiled SWF? It turns out it’s not as hard as you might think. Variables are stored in uncompressed SWFs as plain text. So, it’s just a matter of finding that text and replacing it. It’s a little trickier than that, though…
The first question that may come to your mind is, “Why would I want to do that, when I can load variables and XML dynamically from the server?” Well, there are times when you may want to offer a SWF for download or offline browsing, or some sort of personally packaged SWF for your users. Also, search engines that can peer into SWF files don’t execute the code, so they will never see any data from your database.
The tricky part is, the variables you will be placing into your SWF need to be the exact same length as your original variables. So, you need to plan ahead. The steps are as follows:
- Create your dummy variable, and assign a very long string to it. Make sure that it has unique markers at the beginning and end of the string. For instance “
xxxxxx… “. Your variable must be at least as long as the longest possible value you would replace it with. - Publish your SWF, uncompressed, and upload it to your server. The SWF will be noticably larger than before, but we will use ZLIB compression to get it back down to size.
- Customize the following code snippet, and upload that as well.
-
$newXML =“insert new XML or variable value here”;
-
-
// read source SWF
-
-
// get XML marker and find length
-
$markerlength =$markerend -$markerstart;
-
-
// set length of XML to same length as marker by padding with spaces
-
-
// replace marker with new XML
-
-
// compress SWF file
-
$sourceswf =“CWS”.substr($sourceswf,3);
-
-
// save new SWF
-
$swfout =“destination.swf”;
This little snippet will load in your SWF file, search for the beginning and end markers you’ve specified, and replace that string with the new string containing the new value of that variable. It will pad the new string if necessary to match the exact length of your original value. It will then compress your SWF using ZLIB compression, and save it out.
If you are populating this variable with XML, make sure you have actionscript in your movie that will parse the string into an XML object.
There you have it! You can now create customized SWFs with fresh embedded variables. You could use this to create something as simple as a graphical title, to an form confirmation image, to a full offline RSS browser. I hope you find this useful!


