<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GhostWire Studios &#187; ByteArray</title>
	<atom:link href="http://www.ghostwire.com/blog/archives/tag/bytearray/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ghostwire.com/blog</link>
	<description>Flash UI Components</description>
	<lastBuildDate>Thu, 22 Apr 2010 03:33:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>[AS3] Serializing A Bundle Of Bitmaps As Data Objects</title>
		<link>http://www.ghostwire.com/blog/archives/as3-serializing-a-bundle-of-bitmaps-as-data-objects/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-serializing-a-bundle-of-bitmaps-as-data-objects/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 14:31:19 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[BitmapData]]></category>
		<category><![CDATA[ByteArray]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1601</guid>
		<description><![CDATA[This post is a supplement to &#8220;Serializing Bitmaps (Storing BitmapData As Raw Binary/ByteArray)&#8221;. In that article, we looked at how to convert BitmapData to a ByteArray, save that ByteArray, and re-construct the BitmapData from the saved ByteArray.
It is important to note that the technique saves the ByteArray &#8220;as is&#8221; in a flat binary file without [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a supplement to <a href="http://www.ghostwire.com/blog/archives/as3-serializing-bitmaps-storing-bitmapdata-as-raw-binarybytearray/">&#8220;Serializing Bitmaps (Storing BitmapData As Raw Binary/ByteArray)&#8221;</a>. In that article, we looked at how to convert <code>BitmapData</code> to a <code>ByteArray</code>, save that <code>ByteArray</code>, and re-construct the <code>BitmapData</code> from the saved <code>ByteArray</code>.</p>
<p>It is important to note that the technique saves the <code>ByteArray</code> &#8220;as is&#8221; in a flat binary file without any header or any block of metadata &#8211; this means that the file will in itself not be able to communicate its data structure and therefore, proper usage of the data requires prior knowledge of how the data has been packed (we used the first four bytes for storing the value of the <code>width</code> of the image).  As a result, that method may be deemed as an &#8220;unorthodox&#8221; hack and unsuitable in team development.</p>
<p>In this post, we look at how you can employ the same basic idea while making the saved data more &#8220;consumable&#8221; by other developers.</p>
<p><span id="more-1601"></span><br />
<strong>Action Message Format (AMF)</strong><br />
ByteArray objects store ActionScript objects using the AMF format.  So instead of saving the binary bitmap data in raw form in the way we described previously, we will put the serialized data in an <code>Object</code> and write that object to a <code>ByteArray</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// assume you have Bitmap object &quot;bitmapImage&quot; you want to save</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// convert the BitmapData to ByteArray</span>
<span style="color: #808080; font-style: italic;">// this time, do not pack any other data in the same ByteArray object</span>
<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// create a new data object</span>
<span style="color: #000000; font-weight: bold;">var</span> bmObj:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
bmObj.<span style="color: #0066CC;">width</span> = bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">width</span>;
bmObj.<span style="color: #0066CC;">height</span> = bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">height</span>;
bmObj.<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">data</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// create a new ByteArray object for saving</span>
<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">writeObject</span><span style="color: #66cc66;">&#40;</span>bmObj<span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>So that does it &#8211; you can now save the binary data normally (by posting it to a server script, via AIR local filesystem API, via SharedObject, via FP10 FileReference, etc.).  Although we are still saving our data in a flat binary file, the difference here is that developers consuming the data only need to know that they must read in an <code>Object</code> &#8211; they do not need to have the low-level knowledge of how exactly the data is packed.</p>
<p><!-- --><br />
<strong>ByteArray To BitmapData</strong><br />
Let&#8217;s look at how the binary data can now be consumed in an application.</p>
<p>First, load the file as normal:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> ldr:URLLoader	= <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">dataFormat</span>	= URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>; <span style="color: #808080; font-style: italic;">// ** make sure you do this **</span>
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_fileLoad<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, on_fileLoadError<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>pathToBitmapDataFile<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Process the loaded data:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_fileLoad<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">type</span> == Event.<span style="color: #006600;">COMPLETE</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span> as ByteArray;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">// bytes is now the uncompressed byte array</span>
			<span style="color: #808080; font-style: italic;">// ... process bytes ...</span>
			<span style="color: #000000; font-weight: bold;">var</span> obj:<span style="color: #0066CC;">Object</span> = bytes.<span style="color: #006600;">readObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// obj now has the properties &quot;width&quot;, &quot;height&quot; and &quot;data&quot; as saved previously</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> bmd:BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>obj.<span style="color: #0066CC;">width</span>, obj.<span style="color: #0066CC;">height</span>, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 32 bit transparent bitmap</span>
			bmd.<span style="color: #006600;">setPixels</span><span style="color: #66cc66;">&#40;</span>bmd.<span style="color: #006600;">rect</span>, obj.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> bm:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>bmd<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>bm<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Bundle of Bitmaps</strong><br />
What if you have multiple images you want to save and use later?  You can consider serializing the bitmaps and store them together in a single binary file.  To do that, we will give each image a unique id (such as the path of the image file if you are attempting to load and serialize external image files), store the serialized data in a hash, write that hash to a ByteArray, and then write that ByteArray to file.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// for storing the serialized images</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> imgLibrary:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> saveImage<span style="color: #66cc66;">&#40;</span>id:<span style="color: #0066CC;">String</span>, image:Bitmap<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>id == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> id == <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>image == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">null</span>; <span style="color: #808080; font-style: italic;">// ** remove previously stored data **</span>
		<span style="color: #b1b100;">return</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>image.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>image.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// create a new data object</span>
	<span style="color: #000000; font-weight: bold;">var</span> bmObj:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	bmObj.<span style="color: #0066CC;">width</span> = image.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">width</span>;
	bmObj.<span style="color: #0066CC;">height</span> = image.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">height</span>;
	bmObj.<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">data</span>;
&nbsp;
	imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> = bmObj; <span style="color: #808080; font-style: italic;">// ** will overwrite previously stored data **</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
saveImage<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;iconA&quot;</span>, imageIconA<span style="color: #66cc66;">&#41;</span>;
saveImage<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;iconB&quot;</span>, imageIconB<span style="color: #66cc66;">&#41;</span>;
saveImage<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;iconC&quot;</span>, imageIconC<span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// imgLibrary now contains three data objects each representing an image</span></pre></div></div>

<p>Now, write imgLibrary to file:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ** must target Flash Player version 10+ **</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	bytes.<span style="color: #006600;">writeObject</span><span style="color: #66cc66;">&#40;</span>imgLibrary<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// store width of image</span>
	bytes.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>bytes, <span style="color: #ff0000;">&quot;image.bmd&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// default name &quot;image.bmd&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In another application, we load the saved binary file as usual:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> ldr:URLLoader	= <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">dataFormat</span>	= URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>; <span style="color: #808080; font-style: italic;">// ** make sure you do this **</span>
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_fileLoad<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, on_fileLoadError<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>pathToFile<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And process and use the loaded binary data as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> imgLibrary:<span style="color: #0066CC;">Object</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_fileLoad<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">type</span> == Event.<span style="color: #006600;">COMPLETE</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span> as ByteArray;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">// bytes is now the uncompressed byte array</span>
			<span style="color: #808080; font-style: italic;">// ... process bytes ...</span>
			imgLibrary = bytes.<span style="color: #006600;">readObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// imgLibrary is now initialized and contains data objects each representing a bitmap image</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>After populating the imgLibrary hash, you can use a method like the one below to query it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getImage<span style="color: #66cc66;">&#40;</span>id:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Bitmap
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>id == <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">||</span> id == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> imgLibrary == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">var</span> obj:<span style="color: #0066CC;">Object</span> = imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span>;
	<span style="color: #0066CC;">try</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// obj now has the properties &quot;width&quot;, &quot;height&quot; and &quot;data&quot; as saved previously</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">var</span> bmd:BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>obj.<span style="color: #0066CC;">width</span>, obj.<span style="color: #0066CC;">height</span>, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 32 bit transparent bitmap</span>
		bmd.<span style="color: #006600;">setPixels</span><span style="color: #66cc66;">&#40;</span>bmd.<span style="color: #006600;">rect</span>, obj.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
 		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>bmd<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
getImage<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;iconA&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// returns a Bitmap with BitmapData constructed from saved byte array</span></pre></div></div>

<p>The <code>getImage()</code> method can also be improved such that the <code>ByteArray-to-BitmapData</code> conversion is only done once per image:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getImage<span style="color: #66cc66;">&#40;</span>id:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Bitmap
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>id == <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">||</span> id == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> imgLibrary == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">var</span> obj:<span style="color: #66cc66;">*</span> = imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span>;
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>obj is BitmapData<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>BitmapData<span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">try</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// obj now has the properties &quot;width&quot;, &quot;height&quot; and &quot;data&quot; as saved previously</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">var</span> bmd:BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>obj.<span style="color: #0066CC;">width</span>, obj.<span style="color: #0066CC;">height</span>, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 32 bit transparent bitmap</span>
		bmd.<span style="color: #006600;">setPixels</span><span style="color: #66cc66;">&#40;</span>bmd.<span style="color: #006600;">rect</span>, obj.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> = bmd;
&nbsp;
 		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>bmd.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Note: If you are confident that the <code>BitmapData</code> is not going to be modified by instances, remove the <code>clone()</code> calls above &#8211; it will save memory since that means all <code>Bitmap</code> instances with the same <code>id</code> will be using the exact same <code>BitmapData</code>.</p>
<p><!-- --><br />
<strong>Embedding Image Asset(s)</strong><br />
After you have saved the binary file, remember that you are not limited to using it only as an external resource &#8211; you can also embed the file into your SWF:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ActionScript 3.0</span>
<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;assets/images/icons.bin&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const iconsBundle:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> imgLibrary:<span style="color: #0066CC;">Object</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> initIcons<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> iconsBundle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">try</span>
		<span style="color: #66cc66;">&#123;</span>
			bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// data not compressed</span>
		<span style="color: #66cc66;">&#125;</span>
		imgLibrary = bytes.<span style="color: #006600;">readObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">// imgLibrary is now initialized and contains data objects each representing a bitmap image</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-serializing-a-bundle-of-bitmaps-as-data-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Embedding Binary XML</title>
		<link>http://www.ghostwire.com/blog/archives/as3-embedding-binary-xml/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-embedding-binary-xml/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 06:12:54 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1554</guid>
		<description><![CDATA[This post is a supplement to &#8220;Saving XML As Binary&#8221;.  I neglected to mention in that post that you can, if so desired, embed the binary XML within your SWF instead of loading it during run-time.  That means you still get to keep the XML externally, not as part of your application code, [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a supplement to <a href="http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/">&#8220;Saving XML As Binary&#8221;</a>.  I neglected to mention in that post that you can, if so desired, embed the binary XML within your SWF instead of loading it during run-time.  That means you still get to keep the XML externally, not as part of your application code, so that the code and data can still be kept separate and maintained more easily.</p>
<p>However, it must be mentioned that embedding a text XML within SWF will get it compressed as part of the SWF compression anyway.  Therefore, if compression is the only motivation, don&#8217;t convert the XML to binary and embed the binary version &#8211; it does not make sense.  But if you wish to do some data encryption, then the additional work may be appropriate.</p>
<p><span id="more-1554"></span><br />
<strong>Embedding External Data</strong><br />
You can embed any external source, including any external XML file, within your SWF using the [Embed] tag:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ActionScript 3.0</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// assuming you have an XML file named &quot;data.xml&quot; in folder &quot;assets/data/&quot;</span>
&nbsp;
<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;assets/data/data.xml&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const binaryXML:<span style="color: #000000; font-weight: bold;">Class</span>;</pre></div></div>

<p><!-- --><br />
<strong>Back To XML</strong><br />
You can then instantiate the binary data and convert it back to XML in your code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> xmlData:<span style="color: #0066CC;">XML</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> binaryXML<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">try</span>
	<span style="color: #66cc66;">&#123;</span>
		bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#125;</span>
	xmlData = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>NOTE: The above code will be able to handle both binary XML and text XML files.</p>
<p><!-- --><br />
<strong>ROT128 Encrypted Binary XML</strong><br />
If you had performed <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/">ROT128 encryption</a> on the binary XML, you decrypt it accordingly before calling <code>uncompress()</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> xmlData:<span style="color: #0066CC;">XML</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> binaryXML<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
	<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #808080; font-style: italic;">// END ROT128</span>
	bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	xmlData = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>NOTE: The above code assumes the data is binary and ROT128 encrypted; it will not be able to handle text XML.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-embedding-binary-xml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Serializing Bitmaps (Storing BitmapData As Raw Binary/ByteArray)</title>
		<link>http://www.ghostwire.com/blog/archives/as3-serializing-bitmaps-storing-bitmapdata-as-raw-binarybytearray/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-serializing-bitmaps-storing-bitmapdata-as-raw-binarybytearray/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 14:46:00 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[BitmapData]]></category>
		<category><![CDATA[ByteArray]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1029</guid>
		<description><![CDATA[Whenever an application needs to save bitmap images to local storage or post them to a server script, a common practice is to encode the image as JPEG or PNG before sending that binary data off as the respective mimeType.  However, if the intention is simply to save the bitmap image, ie to serialize [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever an application needs to save bitmap images to local storage or post them to a server script, a common practice is to encode the image as JPEG or PNG before sending that binary data off as the respective mimeType.  However, if the intention is simply to save the bitmap image, ie to serialize the <code>BitmapData</code>, then converting the image to JPEG/PNG would actually be unnecessary.</p>
<p><span id="more-1029"></span><br />
<strong>BitmapData to ByteArray</strong><br />
To get a byte array representing the <code>BitmapData</code>, all you need to do is to call the <code>getPixels()</code> method.  The <code>getPixels()</code> method requires you to specify the rectangular area to capture; the most convenient way to specify this would be to use the <code>rect</code> property of the <code>BitmapData</code> you are serializing.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ActionScript 3.0</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// assuming you have a Bitmap object &quot;bitmapImage&quot; that you would like to serialize</span>
<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The method returns a <code>ByteArray</code> object, with each pixel in the <code>BitmapData</code> represented by an unsigned integer, ie four (4) bytes, in the <code>ByteArray</code>.  This means that you can expect a 20&#215;20 bitmap image to be represented by a <code>ByteArray</code> object that has 1600 bytes (20 x 20 x 4 = 1600) before compression.</p>
<p>After you have the <code>ByteArray</code> object, compress it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>That effectively gives you the lossless compressed binary data of the bitmap image.</p>
<p><!-- --><br />
<strong>Bitmap Dimensions (Width And Height)</strong><br />
So, getting the <code>ByteArray</code> representation of a bitmap image is simple &#8211; just call the <code>getPixels()</code> method.</p>
<p>However, in order for the data to be useful, we must be able to re-construct the bitmap image from it.  The byte array in itself does not give any clue as to the dimensions of the bitmap image, only the total number of pixels.  This means that you must store the dimensions of the bitmap image as well. Actually, you only need to store either the <code>width</code> or the <code>height</code>, because you can compute the other side once you have either one since you also know the total number of pixels.</p>
<p>In the code below, we will store information about the <code>width</code> of the <code>BitmapData</code> in the first four (4) bytes, followed by the byte array representing the image:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">writeUnsignedInt</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p><!-- --><br />
<strong>Saving To File</strong><br />
Once the above is done, you can save the binary data normally (by posting it to a server script, via AIR local filesystem API, via <code>SharedObject</code>, via FP10 <code>FileReference</code>, etc.).</p>
<p>For this example, we will save the binary data to file using the <code>save()</code> method of the <code>FileReference</code> class (available when targeting Flash Player 10).  As a security measure, the save() method will only work in the Flash Player if you call it in response to a user event (for example, MouseEvent.CLICK event). Therefore, you need to create a button, attach a listener to it, and call the save() method in the event handler.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ** must target Flash Player version 10+ **</span>
<span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	bytes.<span style="color: #006600;">writeUnsignedInt</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// store width of image</span>
	bytes.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// store bitmapdata as bytearray</span>
	bytes.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>bytes, <span style="color: #ff0000;">&quot;image.bmd&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// default name &quot;image.bmd&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>You can give the file any name, any extension.  In the example code above, I gave the saved file a &#8220;.bmd&#8221; extension (BitmapData), but this is just a fictitious file type.  The resulting file has no valid mimeType and does not work as any known file type &#8211; it is our own custom binary file format that is useful only for storage and subsequently re-used by our own application code.</p>
<p><!-- --><br />
<strong>ByteArray to BitmapData</strong><br />
As mentioned above, in order for the saved data to be useful, we must be able to re-construct the original bitmap image from it.</p>
<p>First, we load the file normally via <code>URLLoader</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> ldr:URLLoader	= <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">dataFormat</span>	= URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>; <span style="color: #808080; font-style: italic;">// ** make sure you do this **</span>
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_fileLoad<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, on_fileLoadError<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>pathToBitmapDataFile<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The following shows a simple <code>on_fileLoad</code> handler:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> on_fileLoad<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">type</span> == Event.<span style="color: #006600;">COMPLETE</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span> as ByteArray;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">data</span>.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">// data is now the uncompressed byte array</span>
			<span style="color: #808080; font-style: italic;">// ... process data ...</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>We need to recall the dimensions of the bitmap image.  Remember that we saved the value of the <code>width</code> in the first four (4) bytes of the binary data:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// after data.uncompress()</span>
<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">width</span>:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #006600;">readUnsignedInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// first 4 bytes (unsigned integer)</span></pre></div></div>

<p>We can then derive the <code>height</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// after data.uncompress()</span>
<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">height</span>:<span style="color: #0066CC;">int</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #0066CC;">width</span>;
<span style="color: #808080; font-style: italic;">// (data.length - 4) ** byte array less the first four bytes gives the representation of the bitmap **</span>
<span style="color: #808080; font-style: italic;">// ((data.length - 4) / 4) ** length of the representation is divided by four because each pixel takes four bytes **</span>
<span style="color: #808080; font-style: italic;">// ((data.length - 4) / 4) / width ** remember, it is a rectangle, so we can get the height this way **</span></pre></div></div>

<p>NOTE: If you want to skip &#8220;dimension derivation&#8221;, you can store both <code>width</code> and <code>height</code> in your binary data.  Either way will work, the choice is yours.</p>
<p>Once you know the dimensions, you can re-construct the <code>Bitmap</code> object using the <code>setPixels()</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> bmd:BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">width</span>, <span style="color: #0066CC;">height</span>, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 32 bit transparent bitmap</span>
bmd.<span style="color: #006600;">setPixels</span><span style="color: #66cc66;">&#40;</span>bmd.<span style="color: #006600;">rect</span>, <span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// position of data is now at 5th byte</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> bm:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>bmd<span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>bm<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p><!-- --><br />
<strong>Conclusion</strong><br />
The above shows how you can convert <code>BitmapData</code> to a <code>ByteArray</code>, save that <code>ByteArray</code>, and re-construct the <code>BitmapData </code>from the saved <code>ByteArray</code>.</p>
<p>Although the basic purpose would be to save bitmap images to server/local storage, there would also be other scenarios where the above technique will be useful.  For example, after you obtain the <code>ByteArray</code> representation of the image, you can post it to your server for further processing.  You can also use the technique to trim down your external JPEG/PNG image files, stripping away all meta information from the JPEG/PNG encoding leaving behind only the raw image data (possibly resulting in smaller file sizes).  Of course, the resulting binary files will no longer work as JPEG/PNG image files, but your applications will be able to reconstruct the images easily during run-time.  Actually, you can also consider this as a way to protect your external image files from unwanted hot linking.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-serializing-bitmaps-storing-bitmapdata-as-raw-binarybytearray/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>[AS3] Applying ROT128 Encryption On Binary XML</title>
		<link>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 11:05:12 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1381</guid>
		<description><![CDATA[This is Part III of our discussion on ROT128 Encryption.
Part I: &#8220;Applying ROT128 Encryption On ByteArray&#8221;
Part II: &#8220;Applying ROT128 Encryption On Embedded/Module SWFs&#8221;
In &#8220;Saving XML As Binary&#8221;, we looked at how text XML can be stored in a ByteArray object so that it can be compressed and made non-human-readable.  In this post, we look [...]]]></description>
			<content:encoded><![CDATA[<p>This is Part III of our discussion on ROT128 Encryption.</p>
<p>Part I: <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">&#8220;Applying ROT128 Encryption On ByteArray&#8221;</a><br />
Part II: <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/">&#8220;Applying ROT128 Encryption On Embedded/Module SWFs&#8221;</a></p>
<p>In <a href="http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/">&#8220;Saving XML As Binary&#8221;</a>, we looked at how text XML can be stored in a ByteArray object so that it can be compressed and made non-human-readable.  In this post, we look at how you can integrate ROT128 into the XML-to-ByteArray-to-XML routines.  Binary XML is used only as an example; you can definitely apply the same concept to other binary data.</p>
<p><span id="more-1381"></span><br />
<strong>XML-to-ByteArray:</strong><br />
ROT128 is applied after the XML is stored and compressed as ByteArray.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>xmlData<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// xmlData is original XML string</span>
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span>
<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;bindata.xml&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// default name &quot;bindata.xml&quot;</span></pre></div></div>

<p><!-- --><br />
<strong>ByteArray-to-XML:</strong><br />
With ROT128 applied, the saved binary file is no longer a valid compressed ByteArray &#8211; it cannot be uncompressed and converted back to XML without first reversing the encryption on the raw data.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// event handler for Event.COMPLETE of URLLoader loading external XML</span>
<span style="color: #000000; font-weight: bold;">function</span> on_XML<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:<span style="color: #66cc66;">*</span> = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span> is ByteArray<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">try</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
			<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
			<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">// END ROT128</span>
			<span style="color: #0066CC;">data</span>.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// handle data as XML</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Partial Encryption</strong><br />
In order to make the process of reversing ROT128 encryption less predictable, you could vary the conditions under which the encryption is to be applied.</p>
<p>Here is an example (alternate bytes):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
	j--; <span style="color: #808080; font-style: italic;">// skip next byte</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span></pre></div></div>

<p>Another example (first 1024 bytes):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span> : <span style="color: #cc66cc;">1024</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span></pre></div></div>

<p>Correspondingly, your applications would reverse the encryption under the same conditions.  Effectively, these conditions determine the complexity of your cipher and become the &#8220;secret key&#8221; necessary to decrypt the data.</p>
<p><!-- --><br />
<strong>Conclusion</strong><br />
ROT128 is a weak encryption technique that you should not use to protect sensitive data.  However, the light-weight algorithm can be used to make binary blobs inaccessible to unauthorized parties while allowing your applications to easily restore the data when needed.</p>
<p>This method of encryption may seem amateurish, but it does what it is supposed to do well enough while remaining light-weight and flexible.</p>
<p>It worked well enough for Julius Caesar, after all.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Applying ROT128 Encryption On Embedded/Module SWFs</title>
		<link>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 04:02:43 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Copy Protection]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Module SWF]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1382</guid>
		<description><![CDATA[This post is a supplement to &#8220;Applying ROT128 Encryption On ByteArray&#8221;.
Some time back, we posted a simple technique for hiding assets and AS3 code from prying eyes by embedding one SWF within another SWF. In this post, we revisit that topic and look at how ROT128 can be used to provide an additional layer of [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a supplement to <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">&#8220;Applying ROT128 Encryption On ByteArray&#8221;</a>.</p>
<p>Some time back, we posted <a href="http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/">a simple technique</a> for hiding assets and AS3 code from prying eyes by embedding one SWF within another SWF. In this post, we revisit that topic and look at how ROT128 can be used to provide an additional layer of protection.</p>
<p><span id="more-1382"></span><br />
<strong>Step I: Apply ROT128 To Actual SWF</strong><br />
Using the following code, we will apply ROT128 to the SWF that is going to be embedded:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// assuming the raw data of the SWF has been stored in a variable swfBytes</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swfBytes.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	swfBytes<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The following shows a simple tool you can create to load a SWF file, apply ROT128 to it, and save the encrypted file.</p>
<ul>
<li>Flash Player 10 is required.</li>
<li>Click the Browse button to bring up a native file dialog.</li>
<li>Select a Flash Movie (.swf) file.</li>
<li>Click the Save button that will be shown after loading the SWF file.</li>
</ul>
<p>(80&#215;80 SWF, 2KB)<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ROT128SWF_2019818272"
			class="flashmovie"
			width="80"
			height="80">
	<param name="movie" value="/swf/ROT128SWF.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/swf/ROT128SWF.swf"
			name="fm_ROT128SWF_2019818272"
			width="80"
			height="80">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>You can verify that encryption has been done by running the saved SWF file &#8220;ActualSWF.swf&#8221; &#8211; it should show a blank screen.</p>
<p>Remember, applying ROT128 twice restores the file, so the tool can also be used to restore a previously ROT128-encrypted file if the algorithm used was the same (same n increment, same conditional loop).</p>
<p>The ROT128SWF class:<br />
(please feel free to customize to fit your own requirements)</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">Stage</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">IOErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileFilter</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileReference</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldType</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ROT128SWF <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** minimalist text button **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> opButton:<span style="color: #0066CC;">TextField</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** browse/load/save **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> swfFile:FileReference;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ROT128SWF<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">else</span> addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// entry point</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span>			= StageAlign.<span style="color: #0066CC;">LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span>			= StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">showDefaultContextMenu</span>	= <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** draw minimalist text button **</span>
			opButton			= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">autoSize</span>		= TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			opButton.<span style="color: #0066CC;">background</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			opButton.<span style="color: #0066CC;">backgroundColor</span>	= 0x000000;
			opButton.<span style="color: #006600;">defaultTextFormat</span>	= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Tahoma&quot;</span>, <span style="color: #cc66cc;">14</span>, 0xFFFFFF, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">selectable</span>		= <span style="color: #000000; font-weight: bold;">false</span>;
			opButton.<span style="color: #0066CC;">text</span>		= <span style="color: #ff0000;">&quot;BROWSE&quot;</span>;
			opButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - opButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			opButton.<span style="color: #006600;">y</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span> - opButton.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			addChild<span style="color: #66cc66;">&#40;</span>opButton<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** button click listener **</span>
			opButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, on_buttonClick, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse or save
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> btn:<span style="color: #0066CC;">TextField</span> = evt.<span style="color: #0066CC;">target</span> as <span style="color: #0066CC;">TextField</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;BROWSE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					swfFile = <span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					swfFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_swfSelect, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
					swfFile.<span style="color: #006600;">browse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> FileFilter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Flash Movie&quot;</span>,<span style="color: #ff0000;">&quot;*.swf&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;SAVE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>swfFile<span style="color: #66cc66;">&#41;</span>
					<span style="color: #66cc66;">&#123;</span>
						<span style="color: #808080; font-style: italic;">// ** BEGIN ROT128 **</span>
						<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swfFile.<span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
						<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
						<span style="color: #66cc66;">&#123;</span>
							swfFile.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
						<span style="color: #66cc66;">&#125;</span>
						<span style="color: #808080; font-style: italic;">// ** END ROT128 **</span>
						<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>swfFile.<span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;ActualSWF.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse, load swf file
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_swfSelect<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			swfFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_swfSelect<span style="color: #66cc66;">&#41;</span>;
			swfFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_swfComplete, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			swfFile.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle load, change browse button to save button
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_swfComplete<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			swfFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_swfComplete<span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">text</span>		= <span style="color: #ff0000;">&quot;SAVE&quot;</span>;
			opButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - opButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Step II: Embed Actual SWF In Shell SWF</strong><br />
If you have not done so already, please see <a href="http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/">&#8220;Hiding Assets And Code By Embedding SWF Within Another SWF&#8221;</a> for the original discussion on how the technique works.</p>
<p>The following is a <code>MainShell</code> class you can use to embed the ROT128-encrypted SWF:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainShell <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>		
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;ActualSWF.swf&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const bytes:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* REMINDER
		* 
		* MAKE SURE THAT THIS SHELL SWF IS PUBLISHED USING THE
		* ORIGINAL WIDTH/HEIGHT DIMENSIONS OF THE EMBEDDED SWF
		*/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainShell<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> swf:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> bytes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">// ** BEGIN ROT128 **</span>
				<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swf.<span style="color: #0066CC;">length</span>;
				<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					swf<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #808080; font-style: italic;">// ** END ROT128 **</span>
				Loader<span style="color: #66cc66;">&#40;</span>addChild<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">loadBytes</span><span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>With the above code, the &#8220;ActualSWF.swf&#8221; file is embedded into the shell SWF.  It is then instantiated as a ByteArray during run-time.  ROT128 is then applied to the ByteArray before it is loaded into a Loader object.</p>
<p><!-- --><br />
<strong>Why Do This At All?</strong><br />
Hiding assets and code by embedding one SWF within another SWF is simple to implement and so far, there has not been any report on any decompiler overcoming the protection.</p>
<p>In the original post on the technique, I suggested that you could, if you wish, add another layer of protection by encrypting the embedded SWF just in case a decompiler may in future automatically identify a binary blob and &#8220;guess&#8221; that it is an embedded SWF (and decompile it separately).</p>
<p>I feel that ROT128 is light-weight and simple enough that can achieve that objective.  Remember that the objective here is not to overcome meticulous hacking, but merely to thwart possible attempts to implement <em>automatic</em> extraction and identification of embedded binary blobs as separate SWFs.</p>
<p>The above is only an example of how the concept can be applied.  As mentioned in <a href=""http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">the previous post on ROT128</a>, you can also consider applying ROT128 only to part of the file so that the logic for reversing the encryption could be a little less predictable.</p>
<p><!-- --><br />
<strong>Module SWFs</strong><br />
Although the above mentioned only embedded SWFs, you can definitely apply the same concept to module SWFs that will be loaded into your application during run-time.  By applying ROT128 to module SWFs, even if they are obtained via unauthorized means such as via the web browser&#8217;s cache, they are non-executable (or will just show a blank screen when run) if the ROT128 encryption is not reversed first.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>[AS3] Applying ROT128 Encryption On ByteArray</title>
		<link>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 07:49:27 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Encryption]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1023</guid>
		<description><![CDATA[In this post, we will look at a very simple algorithm for weak encryption.  You should not use this method for real cryptographic security.  However, because it is so simple to implement, the light-weight algorithm could easily escape prying eyes and avoid being the target for decryption in the first place.
I should also [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, we will look at a very simple algorithm for weak encryption.  You should not use this method for real cryptographic security.  However, because it is so simple to implement, the light-weight algorithm could easily escape prying eyes and avoid being the target for decryption in the first place.</p>
<p>I should also clarify that the term &#8220;ROT128&#8243; does not actually exist.  The original idea comes from ROT13, a variant of the Caesar Cipher (named after Julius Caesar of ancient Rome who used it to encrypt messages, but it is not clear who first invented or started using the cipher).</p>
<p><span id="more-1023"></span><br />
<strong>ROT13</strong><br />
With ROT13, you obfuscate a piece of text by substituting each character with one that is 13 positions higher up in the English alphabet (A-Z) sequence, with positions wrapping back to the beginning after Z. Therefore, you are <em>rotating</em> the positions (thus the name of the cipher).  The choice of 13 positions is used because there are 26 alphabets (positions), which means applying ROT13 twice restores the original text.  In other words, the exact same algorithm is used for both encoding and decoding. Effectively, the alphabet A becomes N and vice versa.  Likewise, M becomes Z and vice versa, etc.</p>
<p><!-- --><br />
<strong>ROT128</strong><br />
With ROT128, instead of obfuscating text, we will corrupt a <code>ByteArray</code> object by rotating all its byte values by 128 positions each.  A byte value has 256 possible positions, and so in the spirit of using the same algorithm for encoding and decoding, we will rotate values by 128 positions.  Therefore, 0 becomes 128 and vice versa, 1 becomes 129 and vice versa, 127 becomes 255 and vice versa, etc.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> ROT128<span style="color: #66cc66;">&#40;</span>bytes:ByteArray<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// bytes is ByteArray object to encrypt/decrypt</span>
	<span style="color: #808080; font-style: italic;">// you are modifying the object directly, not a clone</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = bytes.<span style="color: #0066CC;">length</span>;
		<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			bytes<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As you can see, the algorithm is very simple.  The ROT128 function listed above is intended for reference only.  You don&#8217;t even really need to create such a function since you can easily sneak the code in-line into the part(s) of your application code where it will actually be used.  After all, if your SWF is decompiled, having a function named ROT128() is more likely to alert the hacker to the usage of the cipher.</p>
<p><!-- --><br />
<strong>Partial ROT128</strong><br />
Sometimes, less is more.  Instead of applying ROT128 to every byte in the ByteArray object, you may choose to do a variant of that &#8211; apply to just the first 50%, last 50%, first and last 1024 bytes, every two bytes, etc.  Doing so would make the cipher slightly more complex, and yet requiring only a slight change to the code (the condition of the loop).</p>
<p><!-- --><br />
<strong>ROTn</strong><br />
Instead of rotating by 128 positions, you can choose to rotate by some other number between 1 to 127. Of course, 128 is the only number that will allow you to use the exact same code for encoding and decoding.  Using any other number would require opposite operations &#8211; if you increment for encoding, then you need to decrement for decoding.</p>
<p><!-- --><br />
<strong>Usage Examples</strong><br />
In the next couple of posts, we will revisit two topics discussed in this blog previously and see how ROT128 can be applied in those scenarios:<br />
(i) <a href="http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/">Hiding Assets And Code By Embedding SWF Within Another SWF</a>; and<br />
(ii) <a href="http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/">Saving XML As Binary</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[AS3] Simple Editor For Loading And Saving XML As Binary</title>
		<link>http://www.ghostwire.com/blog/archives/as3-simple-editor-for-loading-and-saving-xml-as-binary/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-simple-editor-for-loading-and-saving-xml-as-binary/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 02:35:22 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1312</guid>
		<description><![CDATA[This post is a supplement to &#8220;Saving XML As Binary&#8221;.
As mentioned in the previous post, you can save XML in binary and get the benefits of compression, but doing so would make it impossible to edit the data through a text editor.
This post explores a simple tool you can create easily using Flash/ActionScript 3, a [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a supplement to <a href="http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/">&#8220;Saving XML As Binary&#8221;</a>.</p>
<p>As mentioned in the previous post, you can save XML in binary and get the benefits of compression, but doing so would make it impossible to edit the data through a text editor.</p>
<p>This post explores a simple tool you can create easily using Flash/ActionScript 3, a tool that will allow you (or your clients and end-users) to load, read, edit and save XML in compressed binary format.  As shown below, this is a minimalist approach &#8211; feel free to beautify and/or customize it to fit your own requirements.</p>
<p>You can find the source code at the end of this post.  The code does not use any UI component, not from Aspire UI, Flex or Flash, just plain native Flash Player classes so you can compile the AS3 class alone without any additional library.</p>
<p><span id="more-1312"></span><br />
(480&#215;480 SWF, 2KB)<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_BinaryXMLTool_145370240"
			class="flashmovie"
			width="480"
			height="480">
	<param name="movie" value="/swf/BinaryXMLTool.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/swf/BinaryXMLTool.swf"
			name="fm_BinaryXMLTool_145370240"
			width="480"
			height="480">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<ul>
<li>You need Flash Player 10+ to view the above SWF.</li>
<li>Click the &#8220;Browse&#8221; button to open a native local filesystem dialog &#8211; choose an XML file to load.</li>
<li>Once loaded, the XML will be displayed in the input text field above.</li>
<li>After that, click the &#8220;Save&#8221; button to open a native local filesystem dialog &#8211; choose location and file name.</li>
<li>Look for the saved file in your local storage &#8211; notice that the file size is much smaller than the original.</li>
<li>If you open the saved file in a text editor, you will see garbage text because it is in compressed binary format.</li>
<li>You can load a binary XML file into the above tool (if it was saved in &#8220;.xml&#8221; extension).</li>
<li>You can edit the XML in the text field before saving.</li>
</ul>
<p><!-- --><br />
<strong>BinaryXMLTool Class</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">Stage</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">IOErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileFilter</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileReference</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldType</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">xml</span>.<span style="color: #006600;">XMLDocument</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BinaryXMLTool <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** minimalist buttons **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> browseButton:<span style="color: #0066CC;">TextField</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> saveButton:<span style="color: #0066CC;">TextField</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** minimalist editor **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xmlText:<span style="color: #0066CC;">TextField</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** browse/load/save **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xmlFile:FileReference;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BinaryXMLTool<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">else</span> addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// entry point</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span>			= StageAlign.<span style="color: #0066CC;">LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span>			= StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">showDefaultContextMenu</span>	= <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** draw minimalist text editor **</span>
			xmlText				= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			xmlText.<span style="color: #0066CC;">multiline</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			xmlText.<span style="color: #0066CC;">background</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			xmlText.<span style="color: #0066CC;">backgroundColor</span>		= 0xEEEEEE;
			xmlText.<span style="color: #0066CC;">type</span>			= TextFieldType.<span style="color: #006600;">INPUT</span>;
			xmlText.<span style="color: #0066CC;">width</span>			= <span style="color: #cc66cc;">400</span>;
			xmlText.<span style="color: #0066CC;">height</span>			= <span style="color: #cc66cc;">400</span>;
			xmlText.<span style="color: #0066CC;">border</span>			= <span style="color: #000000; font-weight: bold;">true</span>;
			xmlText.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - xmlText.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			addChild<span style="color: #66cc66;">&#40;</span>xmlText<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** draw minimalist browse button **</span>
			browseButton			= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			browseButton.<span style="color: #0066CC;">autoSize</span>		= TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			browseButton.<span style="color: #0066CC;">background</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			browseButton.<span style="color: #0066CC;">backgroundColor</span>	= 0x000000;
			browseButton.<span style="color: #006600;">defaultTextFormat</span>	= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Tahoma&quot;</span>, <span style="color: #cc66cc;">14</span>, 0xFFFFFF, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
			browseButton.<span style="color: #0066CC;">selectable</span>		= <span style="color: #000000; font-weight: bold;">false</span>;
			browseButton.<span style="color: #0066CC;">text</span>		= <span style="color: #ff0000;">&quot;BROWSE&quot;</span>;
			browseButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - browseButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			browseButton.<span style="color: #006600;">y</span>			= <span style="color: #cc66cc;">420</span>;
			addChild<span style="color: #66cc66;">&#40;</span>browseButton<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** draw minimalist save button **</span>
			saveButton			= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			saveButton.<span style="color: #0066CC;">autoSize</span>		= TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			saveButton.<span style="color: #0066CC;">background</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			saveButton.<span style="color: #0066CC;">backgroundColor</span>	= 0xAAAAAA;
			saveButton.<span style="color: #006600;">defaultTextFormat</span>	= browseButton.<span style="color: #006600;">defaultTextFormat</span>;
			saveButton.<span style="color: #0066CC;">selectable</span>		= <span style="color: #000000; font-weight: bold;">false</span>;
			saveButton.<span style="color: #0066CC;">text</span>			= <span style="color: #ff0000;">&quot;SAVE&quot;</span>;
			saveButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - saveButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			saveButton.<span style="color: #006600;">y</span>			= <span style="color: #cc66cc;">460</span>;
			addChild<span style="color: #66cc66;">&#40;</span>saveButton<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** button listeners **</span>
			browseButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, on_buttonClick, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			saveButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, on_buttonClick, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse or save
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> btn:<span style="color: #0066CC;">TextField</span> = evt.<span style="color: #0066CC;">target</span> as <span style="color: #0066CC;">TextField</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;BROWSE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					xmlFile = <span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					xmlFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_xmlSelect, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
					xmlFile.<span style="color: #006600;">browse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> FileFilter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;XML Documents&quot;</span>,<span style="color: #ff0000;">&quot;*.xml&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;SAVE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>xmlFile<span style="color: #66cc66;">&#41;</span>
					<span style="color: #66cc66;">&#123;</span>
						<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>xmlText.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span>
						<span style="color: #66cc66;">&#123;</span>
							<span style="color: #808080; font-style: italic;">// ** saving as binary **</span>
							<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
							<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>xmlText.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#41;</span>;
							<span style="color: #0066CC;">data</span>.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
							<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;bin&quot;</span> + xmlFile.<span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#41;</span>;
						<span style="color: #66cc66;">&#125;</span>
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse, load XML file
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_xmlSelect<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			xmlFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_xmlSelect<span style="color: #66cc66;">&#41;</span>;
			xmlFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_xmlComplete, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			xmlFile.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle load, check if it is binary, uncompress, display XML in editor
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_xmlComplete<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			xmlFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_xmlComplete<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			saveButton.<span style="color: #0066CC;">backgroundColor</span> = 0x000000;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:<span style="color: #66cc66;">*</span> = FileReference<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span> is ByteArray<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">try</span>
				<span style="color: #66cc66;">&#123;</span>
					ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
				<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
			xmlText.<span style="color: #0066CC;">text</span> = <span style="color: #0066CC;">data</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-simple-editor-for-loading-and-saving-xml-as-binary/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>[AS3] Saving XML As Binary</title>
		<link>http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 07:44:53 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1032</guid>
		<description><![CDATA[This could be useful if you have an external large, verbose XML file which your application must load during run-time.  By saving the XML as binary, you can compress the data and get a much smaller file.  Of course, the amount of compression you can get depends on the complexity of your data, [...]]]></description>
			<content:encoded><![CDATA[<p>This could be useful if you have an external large, verbose XML file which your application must load during run-time.  By saving the XML as binary, you can compress the data and get a much smaller file.  Of course, the amount of compression you can get depends on the complexity of your data, but it would typically be over 50% (conservative estimate).</p>
<p>Admittedly, having the XML data in compressed binary format contradicts the original intention of using XML in the first place &#8211; to allow human-readable data.  Therefore, you will have to decide what exactly is important for your application before proceeding.  Perhaps having cryptic external data <code>is</code> indeed what you want &#8211; allowing data to be externalized so they can be changed without requiring the SWF to be recompiled, and yet would prefer the data to be non-human-readable.</p>
<p><span id="more-1032"></span><br />
<strong>Converting XML To Binary Data (ByteArray)</strong><br />
First, we look at how to store the XML data in a <code>ByteArray</code>.  The following code assumes that you already have the XML data stored as a <code>String</code> value in a variable named &#8220;xmlData&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>xmlData<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// xmlData is original XML string</span>
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And that does it &#8211; the XML data is now in compressed binary.</p>
<p><!-- --><br />
<strong>Saving to File</strong><br />
Now that you have the binary data as a <code>ByteArray</code> object, you can save that object to file as raw data.  You can do that either by<br />
(i) posting to a server script;<br />
(ii) using AIR API to save to local storage; or<br />
(iii) using Flash Player 10 API to save to local storage.</p>
<p>We will look at (iii), because that is most accessible to every Flash developer.  The class you will use is <code>flash.net.FileReference</code>.  The <code>FileReference.save()</code> method is available when targeting Flash Player 10 &#8211; it allows you to save the <code>data</code> in the <code>FileReference</code> object to a local file.</p>
<p>As a security measure, the <code>save()</code> method will only work in the Flash Player if you call it in response to a user event (for example, <code>MouseEvent.CLICK</code> event).  Therefore, you need to create a button, attach a listener to it, and call the <code>save()</code> method in the listener.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>xmlData<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// xmlData is original XML string</span>
	<span style="color: #0066CC;">data</span>.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;bindata.xml&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// default name &quot;bindata.xml&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The <code>save()</code> method will open a native dialog box &#8211; choose a file name and location and you will save the XML data in your local filesystem.  Notice that you can save the file in the &#8220;.xml&#8221; extension, but you will no longer get a human-readable XML file &#8211; you will see garbage text if you open the file in a text editor because the data is in compressed binary.</p>
<p><!-- --><br />
<strong>Loading Binary XML</strong><br />
You can load the binary file the same way you would a normal XML file:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> ldr:URLLoader	= <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">dataFormat</span>	= URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>; <span style="color: #808080; font-style: italic;">// ** make sure you do this **</span>
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_XML<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, on_XML<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>pathToXMLFile<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Therefore, the code for loading is the same as for normal text XML files, except that you need to set <code>dataFormat</code> to <code>URLLoaderDataFormat.BINARY</code>.  Even if you end up loading a normal text XML file, the above code will work just fine.</p>
<p><!-- --><br />
<strong>Converting Binary Data To XML</strong><br />
After the binary XML file is loaded, you convert the binary data back to text XML.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> xmlData:<span style="color: #0066CC;">XML</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> on_XML<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">type</span> == Event.<span style="color: #006600;">COMPLETE</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:<span style="color: #66cc66;">*</span> = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span> is ByteArray<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		xmlData = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>With the code above, your application will be able to handle both text XML and binary XML files.  Therefore, this code is something you can use even if you are not currently using XML stored as compressed binary data.  If at a later time you decide to compress your XML in binary format, the application will be able to handle it just fine.</p>
<p>In the next post, we will look at a simple tool you can create easily using Flash/ActionScript 3 to load, read, edit and save XML files in compressed binary format.</p>
<p><!-- --><br />
<strong>Note:</strong><br />
The above technique can be used for any text file, not just XML.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>[AS3] Truncating ByteArray Does Not Dispose Contents, Free Up Memory</title>
		<link>http://www.ghostwire.com/blog/archives/as3-truncating-bytearray-does-not-dispose-contents-free-up-memory/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-truncating-bytearray-does-not-dispose-contents-free-up-memory/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 06:35:51 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Flash Player Bug]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1097</guid>
		<description><![CDATA[When targeting Flash Player 10 or AIR 1.5, you can use the clear() method of the ByteArray class to explicitly clear the contents of the byte array and free up the memory otherwise used by the bytes.  The length and position properties are reset to zero after calling the clear() method.
Unfortunately, when targeting Flash [...]]]></description>
			<content:encoded><![CDATA[<p>When targeting Flash Player 10 or AIR 1.5, you can use the <code>clear()</code> method of the <code>ByteArray</code> class to explicitly clear the contents of the byte array and free up the memory otherwise used by the bytes.  The <code>length</code> and <code>position</code> properties are reset to zero after calling the <code>clear()</code> method.</p>
<p>Unfortunately, when targeting Flash Player 9, this <code>clear()</code> method is not available.  If you are using a <code>ByteArray</code> object as a data store, keeping a reference to the object and therefore not allowing the object to be garbage collected, do take note that there is no way to clear the contents. This means that the size of <code>ByteArray</code> objects can only be enlarged and never shrunk.</p>
<p>It is important to note that while you can truncate the <code>ByteArray</code> to zero byte by setting its <code>length</code> property to zero, this will not dispose the contents or free up the memory used.</p>
<p><span id="more-1097"></span>This is actually a rather odd implementation.  After you truncate a <code>ByteArray</code>, the bytes stay in memory and is in fact restorable simply by setting the <code>length</code> property back to a non-zero value.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ABCDE&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 65</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 66</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 67</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 68</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 69</span>
bytes.<span style="color: #0066CC;">length</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// undefined</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// undefined</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// undefined</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// undefined</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// undefined</span>
bytes.<span style="color: #0066CC;">length</span> = <span style="color: #cc66cc;">5</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 65 ** what the ??? necromancy at work ??? **</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 66</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 67</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 68</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 69</span></pre></div></div>

<p>In my opinion, this implementation is flawed.  The Flash Player really should clear the data once the array of bytes is truncated; this should not require a new API method <code>clear()</code> to accomplish. To introduce a new API method that does what setting the <code>length</code> property to zero <em>should</em> have done in the first place really baffles me.</p>
<p>Anyway, rant aside, so that is that &#8211; when you truncate a <code>ByteArray</code>, remember that the data is not lost.  When targeting Flash Player 10 or AIR 1.5, do remember to call the <code>clear()</code> method instead of attempting to clear the contents by truncating.</p>
<p><!-- --><br />
<strong>Zerofying Bytes</strong><br />
Since the data stays in memory, I reckon it could sometimes be useful to erase the data explicitly, especially if it contains sensitive data:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> zerofy<span style="color: #66cc66;">&#40;</span>bytes:ByteArray<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = bytes.<span style="color: #0066CC;">length</span>;
		<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			bytes<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">0</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I am not sure if the <code>clear()</code> method in FP10 API does this (or something similar to erase the data in memory) &#8211; I sure hope it does.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-truncating-bytearray-does-not-dispose-contents-free-up-memory/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Finding Occurrences Of A Sequence Of Bytes Within A ByteArray</title>
		<link>http://www.ghostwire.com/blog/archives/as3-finding-occurrences-of-a-sequence-of-bytes-within-a-bytearray/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-finding-occurrences-of-a-sequence-of-bytes-within-a-bytearray/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 11:05:39 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1092</guid>
		<description><![CDATA[The following is a simple way to search within a ByteArray for specific patterns of bytes. At the end of this post is a ByteArrayUtils class that contains the indexOf(), indicesOf and lastIndexOf() methods discussed below.  If you just want to grab the code and skip all the walk-through, please feel free to jump [...]]]></description>
			<content:encoded><![CDATA[<p>The following is a simple way to search within a <code>ByteArray</code> for specific patterns of bytes. At the end of this post is a <code>ByteArrayUtils</code> class that contains the <code>indexOf()</code>, <code>indicesOf</code> and <code>lastIndexOf()</code> methods discussed below.  If you just want to grab the code and skip all the walk-through, please feel free to <a href="/blog/archives/as3-finding-occurrences-of-a-sequence-of-bytes-within-a-bytearray#zip">jump there</a>.</p>
<p><span id="more-1092"></span><br />
<strong>Finding The First Occurrence Of The Pattern &#8211; indexOf()</strong></p>
<ul>
<li>We want to know the index position of the first occurrence of the pattern, if any.</li>
<li>We take the first byte of the sequence we are looking for, and scan through the target <code>ByteArray</code> byte-by-byte to find a match.</li>
<li>Once a match is found, we take the last byte of the sequence we are looking for, and check if the byte at the corresponding position in the target <code>ByteArray</code> also matches. If not, we have found a false candidate and therefore the bytes in-between need not be tested.</li>
<li>We continue working backwards until all the bytes in the sequence are tested, aborting if a comparison is false.</li>
<li>Once all the bytes in the sequence are tested positive, we have found an occurrence of the pattern.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:ByteArray, pattern:<span style="color: #66cc66;">*</span>, fromIndex:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> arr:<span style="color: #0066CC;">Array</span>, <span style="color: #0066CC;">end</span>:<span style="color: #0066CC;">Boolean</span>, found:<span style="color: #0066CC;">Boolean</span>, a:<span style="color: #0066CC;">int</span>, i:<span style="color: #0066CC;">int</span>, j:<span style="color: #0066CC;">int</span>, k:<span style="color: #0066CC;">int</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> toFind:ByteArray = toByteArray<span style="color: #66cc66;">&#40;</span>pattern<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>toFind == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// ** type of pattern unsupported **</span>
		<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Unsupported Pattern&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">return</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	a	= toFind.<span style="color: #0066CC;">length</span>;
	j	= <span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">length</span> - a;
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>fromIndex <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		i = j + fromIndex;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> -<span style="color: #cc66cc;">1</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #66cc66;">&#123;</span>
		i = fromIndex;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #0066CC;">end</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> == toFind<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// ** found a possible candidate **</span>
			found	= <span style="color: #000000; font-weight: bold;">true</span>;
			k		= a;
			<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>--k<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#91;</span>i + k<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= toFind<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #808080; font-style: italic;">// ** doesn't match, false candidate **</span>
					found = <span style="color: #000000; font-weight: bold;">false</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>found<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">return</span> i;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>fromIndex <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">end</span> = <span style="color: #66cc66;">&#40;</span>--i <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">end</span> = <span style="color: #66cc66;">&#40;</span>++i <span style="color: #66cc66;">&gt;</span> j<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> -<span style="color: #cc66cc;">1</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Defining The Pattern</strong><br />
The <code>indexOf()</code> method has the following signature:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:ByteArray, pattern:<span style="color: #66cc66;">*</span>, fromIndex:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span></pre></div></div>

<p>If you have the sequence of bytes to find already defined as a <code>ByteArray</code>, you can use that for the <code>pattern</code> parameter (ie, you can look for a <code>ByteArray</code> within a <code>ByteArray</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">ByteArrayUtils.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>, sequence<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// where sequence is a ByteArray object</span></pre></div></div>

<p>Otherwise, you can define the sequence of bytes as an <code>Array</code> of byte values:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">ByteArrayUtils.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>, <span style="color: #66cc66;">&#91;</span>0x72, 0x69, 0x76, 0x76, 0x79<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>You can also define a <code>String</code> to look up:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">ByteArrayUtils.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>, <span style="color: #ff0000;">&quot;HELLO&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

</ul>
<p>The <code>toByteArray()</code> method helps to convert Array or String to ByteArray:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> toByteArray<span style="color: #66cc66;">&#40;</span>obj:<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span>:ByteArray
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> toFind:ByteArray;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>obj is ByteArray<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		toFind = obj;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #66cc66;">&#123;</span>
		toFind	= <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>obj is <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// ** looking for a sequence of target **</span>
			<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = obj.<span style="color: #0066CC;">length</span>;
			<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>i--<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				toFind<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = obj<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>obj is <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// ** looking for a sequence of string characters **</span>
			toFind.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> toFind;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Specifying Where To Begin Searching &#8211; fromIndex</strong></p>
<ul>
<li>By default, we will scan from index position zero to the last valid index position (which is <em>&#8220;length of target byte array less the length of sequence of bytes to find&#8221;</em>).</li>
<li>To begin searching with an offset, specify the <code>fromIndex</code> parameter.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">ByteArrayUtils.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>, <span style="color: #ff0000;">&quot;HELLO&quot;</span>, <span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// skip the first 1000 bytes</span></pre></div></div>

<p><!-- --><br />
<strong>Finding Multiple Occurrences &#8211; indicesOf()</strong></p>
<ul>
<li>The <code>indicesOf()</code> method returns an array containing the index positions found. If no match is found, an empty array is returned.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> indices:<span style="color: #0066CC;">Array</span> = ByteArrayUtils.<span style="color: #006600;">indicesOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>, <span style="color: #66cc66;">&#91;</span>0x72, 0x69, 0x76, 0x76, 0x79<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> indicesOf<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:ByteArray, pattern:<span style="color: #66cc66;">*</span>, fromIndex:uint = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> arr:<span style="color: #0066CC;">Array</span>			= <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> toFind:ByteArray	= toByteArray<span style="color: #66cc66;">&#40;</span>pattern<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>toFind == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// ** type of pattern unsupported **</span>
		<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Unsupported Pattern&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">return</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>			= <span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>, toFind, fromIndex<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #66cc66;">!</span>= -<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		arr<span style="color: #66cc66;">&#91;</span>arr.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#93;</span> = i;
		i = <span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>, toFind, i + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> arr;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Finding The Last Occurrence &#8211; lastIndexOf()</strong></p>
<ul>
<li>Use the <code>lastIndexOf()</code> method to find the last, instead of the first, occurrence of the pattern.</li>
<li>The <code>indexOf()</code> method has been coded to search backwards if the <code>fromIndex</code> parameter is a negative integer.</li>
<li>The <code>lastIndexOf()</code> therefore simply calls the <code>indexOf()</code> method passing <code>-1</code> for the <code>fromIndex</code> parameter.</li>
</ul>
<p><!-- --><br />
<strong>Testing By Unsigned Integers</strong><br />
I tried testing four bytes at a time, ie by comparing unsigned integers, but the performance ended up worse off.  I have therefore removed that implementation.  The following was the idea behind that implementation:</p>
<ul>
<li>If the sequence we are looking for is more than five bytes, we will test for matches using unsigned integers (so four bytes at a time). If the sequence is five bytes or less, we stick to byte-by-byte scan because the first and last byte would have been tested (so left three or less bytes to test).</li>
<li>As long as there are four or more bytes left to test, unsigned integer comparison is used, otherwise byte value comparison is used.</li>
</ul>
<p><!-- --><a name="zip" /><br />
<strong>ByteArrayUtils Class</strong><br />
<a href="http://www.ghostwire.com/blog/wp-content/uploads/ByteArrayUtils.as">ByteArrayUtils ActionScript 3.0 Class</a></p>
<p><a href="http://www.ghostwire.com/blog/wp-content/uploads/ByteArrayUtils.zip">ByteArrayUtils.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-finding-occurrences-of-a-sequence-of-bytes-within-a-bytearray/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
