<?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>Xebia Blog &#187; bit syntax</title>
	<atom:link href="http://blog.xebia.com/tag/bit-syntax/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com</link>
	<description>Software development done right!</description>
	<lastBuildDate>Wed, 01 Feb 2012 00:30:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>@Composite for Unpacking COFF Data</title>
		<link>http://blog.xebia.com/2009/07/04/composite-for-unpacking-coff-data/</link>
		<comments>http://blog.xebia.com/2009/07/04/composite-for-unpacking-coff-data/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 10:37:38 +0000</pubDate>
		<dc:creator>Wilfred Springer</dc:creator>
		<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/blog.xebia.com/www/wp-content/plugins/autometa/autometa.php</b> on line <b>303</b><br />
		<category><![CDATA[Java]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[bit syntax]]></category>
		<category><![CDATA[erlang]]></category>

	<!-- AutoMeta Start -->
	<!-- AutoMeta End -->
	
		<guid isPermaLink="false">http://blog.xebia.com/?p=2387</guid>
		<description><![CDATA[A while ago, I compared Preon with Erlang&#8217;s bit syntax. I looked at one one of the examples from &#8220;Programming Erlang&#8221; in particular; an example that illustrates how to decode MPEG headers using Erlang. However, this is not the only example in that chapter, so I decided to take a stab at one of the [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, I <a href="http://blog.flotsam.nl/2009/02/bit-syntax-for-java-i.html">compared <a href="http://preon.flotsam.nl/">Preon</a> with Erlang&#8217;s bit syntax</a>. I looked at one one of the examples from &#8220;Programming Erlang&#8221; in particular; an example that illustrates how to decode MPEG headers using Erlang. However, this is not the only example in that chapter, so I decided to take a stab at one of the other examples as well.</p>
<p><span id="more-2387"></span></p>
<p>The second example from the bit syntax chapter in &#8220;Programming Erlang&#8221; is about unpacking <a href="http://en.wikipedia.org/wiki/COFF">COFF</a> data. The thing about COFF is that it doesn&#8217;t have an IDL-alike language or anything for defining the data structures: all you have is the definition of C++ data structures, such as the one below:</p>
<pre class="brush: cpp; title: ; notranslate">
typedef struct _IMAGE_RESOURCE_DIRECTORY {
DWORD Characteristics;
DWORD TimeDateStamp;
WORD MajorVersion;
WORD MinorVersion;
WORD NumberOfNamedEntries;
WORD NumberOfIdEntries;
} IMAGE_RESOURCE_DIRECTORY, *PIMAGE_RESOURCE_DIRECTORY;
</pre>
<p>In his book, Joe Armstrong explains that using Erlang&#8217;s bit syntax and macro solutions, you would be able to unpack COFF data characterized by the C++ struct above using the Erlang code listed below.</p>
<pre class="brush: plain; title: ; notranslate">
unpack_image_resource_directory(Dir) -&gt;
  &lt;&lt;Characteristics : ?DWORD,
    TimeDateStamp : ?DWORD,
    MajorVersion : ?WORD,
    MinorVersion : ?WORD,
    NumberOfNamedEntries : ?WORD,
    NumberOfIdEntries : ?WORD, _/binary&gt;&gt; = Dir,
...
</pre>
<p>The key message here is that Erlang not only allows you to unpack binary data easily, but that it also allows you to express that clearly maps to the only source of definition of the data structure: the C++ API.</p>
<p>Now, if you would use Preon only, the C++ data structure above would translate to this:</p>
<pre class="brush: java; title: ; notranslate">
class ImageResourceDirectory {
  @BoundNumber(size=&quot;32&quot;) long characteristics;
  @BoundNumber(size=&quot;32&quot;) long timeDateStamp;
  @BoundNumber(size=&quot;16&quot;) int majorVersion;
  @BoundNumber(size=&quot;16&quot;) int minorVersion;
  @BoundNumber(size=&quot;16&quot;) int numberOfNamedEntries;
  @BoundNumber(size=&quot;16&quot;) int numberOfIdEntries;
}
</pre>
<p>&#8230; and you would be able to decode it by this:</p>
<pre class="brush: java; title: ; notranslate">
Codec&lt;ImageResourceDirectory&gt; codec = Codecs.create(ImageResourceDirectory.class);
Codecs.decode(codec, ...);
</pre>
<p>Now, that&#8217;s not bad, but it doesn&#8217;t have that similarity to the original C++ API code the Erlang example has. However, using Andrew Philips&#8217; <a href="http://blog.xebia.com/2009/06/23/composite-macro-annotations-for-java/">@Composite framework</a>, you <i>would</i> actually be able to write this:</p>
<pre class="brush: java; title: ; notranslate">
class ImageResourceDirectory {
  @DWORD long characteristics;
  @DWORD long timeDateStamp;
  @WORD int majorVersion;
  @WORD int minorVersion;
  @WORD int numberOfNamedEntries;
  @WORD int numberOfIdEntries;
}
</pre>
<p>&#8230; which is already a lot closer to the original C++ struct than what we had before. </p>
<p>Support for @Composite has not been included in Preon yet. At first sight, there appear to be two ways to deal with it. First of all, it could be build into the framework, by the AnnotatedElements interface all over the place. That should work, and it might actually be the most sensible thing to do.</p>
<p>However, there may be an alternative way to get it woven in. Preon already defines a <a href="http://preon.flotsam.nl/preon-binding/apidocs/nl/flotsam/preon/CodecDecorator.html">CodecDecorator</a> interface that allows you wrap Codec implementations around Codec instances created. Looking at that, I started to think that it might actually be quite attractive to also define a CodecFactoryDecorator, wrapping CodecFactories around other CodecFactories in the chain of responsibility.</p>
<pre class="brush: java; title: ; notranslate">
public interface CodecFactory {
    &lt;T&gt; Codec&lt;T&gt; create(AnnotatedElement metadata, Class&lt;T&gt; type,
            ResolverContext context);
}
</pre>
<p>The wrappers created by the CodecFactoryDecorator would be able to intercept any reference to annotations passed down below down the chain, and replace it with an AnnotatedElement that uses <a href="http://code.google.com/p/aphillips/source/browse/at-composite/trunk/src/main/java/com/qrmedia/pattern/compositeannotation/api/AnnotatedElements.java">AnnotatedElements</a> to gain access to the annotations. As a consequence, CodecFactories responsible for creating the actual Codec from metadata passed in would get to see Preon annotations only, instead of the @DWORD and @WORD annotations.</p>
<p>It&#8217;s just a thought. None of this has been implemented yet. Feel free to comment. Expect to see some more of this in the future.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin-left: 10px;"><g:plusone size="small" count="1" href="http://blog.xebia.com/2009/07/04/composite-for-unpacking-coff-data/"></g:plusone></div><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.xebia.com%2F2009%2F07%2F04%2Fcomposite-for-unpacking-coff-data%2F&amp;title=%40Composite%20for%20Unpacking%20COFF%20Data" id="wpa2a_2"><img src="http://blog.xebia.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.xebia.com/2009/07/04/composite-for-unpacking-coff-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  blog.xebia.com/tag/bit-syntax/feed/ ) in 0.51902 seconds, on Feb 9th, 2012 at 3:48 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 9th, 2012 at 4:48 pm UTC -->
