• Home
  • RSS Feed
  • Log in

@Composite for Unpacking COFF Data
Posted by Wilfred Springer just before lunchtime: July 4th, 2009

A while ago, I compared Preon with Erlang's bit syntax. I looked at one one of the examples from "Programming Erlang" 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.

The second example from the bit syntax chapter in "Programming Erlang" is about unpacking COFF data. The thing about COFF is that it doesn'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:

typedef struct _IMAGE_RESOURCE_DIRECTORY {
DWORD Characteristics;
DWORD TimeDateStamp;
WORD MajorVersion;
WORD MinorVersion;
WORD NumberOfNamedEntries;
WORD NumberOfIdEntries;
} IMAGE_RESOURCE_DIRECTORY, *PIMAGE_RESOURCE_DIRECTORY;

In his book, Joe Armstrong explains that using Erlang'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.

unpack_image_resource_directory(Dir) ->
  <<Characteristics : ?DWORD,
    TimeDateStamp : ?DWORD,
    MajorVersion : ?WORD,
    MinorVersion : ?WORD,
    NumberOfNamedEntries : ?WORD,
    NumberOfIdEntries : ?WORD, _/binary>> = Dir,
...

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.

Now, if you would use Preon only, the C++ data structure above would translate to this:

class ImageResourceDirectory {
  @BoundNumber(size="32") long characteristics;
  @BoundNumber(size="32") long timeDateStamp;
  @BoundNumber(size="16") int majorVersion;
  @BoundNumber(size="16") int minorVersion;
  @BoundNumber(size="16") int numberOfNamedEntries;
  @BoundNumber(size="16") int numberOfIdEntries;
}

... and you would be able to decode it by this:

Codec<ImageResourceDirectory> codec = Codecs.create(ImageResourceDirectory.class);
Codecs.decode(codec, ...);

Now, that's not bad, but it doesn't have that similarity to the original C++ API code the Erlang example has. However, using Andrew Philips' @Composite framework, you would actually be able to write this:

class ImageResourceDirectory {
  @DWORD long characteristics;
  @DWORD long timeDateStamp;
  @WORD int majorVersion;
  @WORD int minorVersion;
  @WORD int numberOfNamedEntries;
  @WORD int numberOfIdEntries;
}

... which is already a lot closer to the original C++ struct than what we had before.

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.

However, there may be an alternative way to get it woven in. Preon already defines a CodecDecorator 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.

public interface CodecFactory {
    <T> Codec<T> create(AnnotatedElement metadata, Class<T> type,
            ResolverContext context);
}

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 AnnotatedElements 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.

It'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.

  • Share/Bookmark

Tags: annotations, bit syntax, erlang, Java
Filed under Java | No Comments »



No Responses to “@Composite for Unpacking COFF Data”



Leave a Reply

Click here to cancel reply.

Deployment automation for Java application running on Websphere, WebLogic and JBoss

Archives

  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009

Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India

Categories

  • Java (282)
  • Agile (109)
  • General (50)
  • Testing (42)
  • Performance (42)
  • Hibernate (36)
  • Scrum (33)
  • Podcast (31)
  • Architecture (31)
  • Spring (28)
  • SOA (24)
  • Maven (22)
  • Project Management (22)
  • Middleware (23)
    • Deployment (14)
  • Flex (17)
  • JPA (17)
  • Eclipse (15)
  • Xebia Labs (15)
  • Quality Assurance (14)

Tag Cloud

    XML Xebia Agile Performance Spring Introduction to Agile Scrum Poppendieck Testing Agile Awareness Workshop Functional Programming qcon Grails Lean Ajax product owner SOA Groovy esb Architecture fitnesse IntelliJ JavaOne Maven Scala Hibernate Closures Seam Semantic Web Java