<feed xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns="http://www.w3.org/2005/Atom" xml:lang="de-CH">
    <title>Cédric Menzi</title>
    <link rel="self" type="application/xml" href="http://geekswithblogs.net/cmenzi/Atom.aspx" />
    <subtitle type="html">Yet Another Burst Programming Session Will Solve It</subtitle>
    <id>http://geekswithblogs.net/cmenzi/Default.aspx</id>
    <author>
        <name>Cédric Menzi</name>
        <uri>http://geekswithblogs.net/cmenzi/Default.aspx</uri>
    </author>
    <generator uri="http://subtextproject.com" version="Subtext Version 0.0.0.0">Subtext</generator>
    <updated>2010-03-09T09:33:55Z</updated>
    <entry>
        <title>From Binary to Data Structures</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/cmenzi/archive/2010/03/07/from-binary-to-data-structures-again.aspx" />
        <id>http://geekswithblogs.net/cmenzi/archive/2010/03/07/from-binary-to-data-structures-again.aspx</id>
        <published>2010-03-07T14:07:4701:00:00</published>
        <updated>2010-03-09T09:33:55Z</updated>
        <content type="html">&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="#heading0000"&gt;Introduction&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#heading0001"&gt;PE file format and COFF header&lt;/a&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href="#heading0002"&gt;COFF file header&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#heading0003"&gt;BaseCoffReader&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#heading0004"&gt;Byte4ByteCoffReader&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#heading0005"&gt;UnsafeCoffReader&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#heading0006"&gt;ManagedCoffReader&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#heading0007"&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="#heading0008"&gt;History&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This article is also available on &lt;a href="http://www.codeproject.com/KB/recipes/bin2structs.aspx"&gt;CodeProject&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;&lt;a name="heading0000" id="heading0000"&gt;Introduction&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Sometimes, you want to parse well-formed binary data and bring it into your objects to do some dirty stuff with it.&lt;/p&gt;
&lt;p&gt;In the Windows world most data structures are stored in special binary format. Either we call a WinApi function or we want to read from special files like images, spool files, executables or may be the previously announced  &lt;a&gt;Outlook Personal Folders File.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Most specifications for these files can be found on the MSDN Libarary:  &lt;a href="http://msdn.microsoft.com/en-us/library/dd208104%28PROT.10%29.aspx"&gt;Open Specification&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In my example, we are going to get the COFF (Common Object File Format) file header from a PE (Portable Executable). The exact specification can be found here:  &lt;a href="http://www.microsoft.com/whdc/system/platform/firmware/PECOFFdwn.mspx"&gt;PECOFF&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;&lt;a name="heading0001" id="heading0001"&gt;PE file format and COFF header&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Before we start we need to know how this file is formatted. The following figure shows an overview of the Microsoft PE executable format.  &lt;br /&gt;
&lt;img height="489" width="418" src="http://www.codeproject.com/KB/recipes/bin2structs/bin2structs1.png" alt="Microsoft PE executable format" /&gt; &lt;small&gt;Source: &lt;a href="http://www.microsoft.com/whdc/system/platform/firmware/PECOFFdwn.mspx"&gt;Microsoft&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Our goal is to get the PE header. As we can see, the image starts with a MS-DOS 2.0  header with is not important for us. From the documentation we can read  &lt;em&gt;"...After the MS DOS stub, at the file offset specified at offset 0x3c, is a 4-byte..."&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;With this information we know our reader has to jump to location 0x3c   and read the offset to the signature. The signature is always 4 bytes that ensures that the image is a PE file. The signature is: PE\0\0.&lt;/p&gt;
&lt;p&gt;To prove this we first seek to the offset 0x3c, read if the file  consist the signature.&lt;/p&gt;
&lt;p&gt;So we need to declare some constants, because we do not want magic numbers.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;pre lang="C#" class="brush: csharp"&gt;
private const int PeSignatureOffsetLocation = 0x3c;
private const int PeSignatureSize           = 4;
private const string PeSignatureContent     = "PE";
&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Then a method for moving the reader to the correct location to read the offset of signature. With this method we always move the underlining Stream of the  BinaryReader to the start location of the PE signature.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;pre lang="C#" class="brush: csharp"&gt;
private void SeekToPeSignature(BinaryReader br) {
	// seek to the offset for the PE signagure
	br.BaseStream.Seek(PeSignatureOffsetLocation, SeekOrigin.Begin);
	// read the offset
	int offsetToPeSig = br.ReadInt32();
	// seek to the start of the PE signature
	br.BaseStream.Seek(offsetToPeSig, SeekOrigin.Begin); 
}
&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Now, we can check if it is a valid PE image by reading of the next 4 byte contains the content PE.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;pre lang="C#" class="brush: csharp"&gt;
private bool IsValidPeSignature(BinaryReader br) {
	// read 4 bytes to get the PE signature
	byte[] peSigBytes = br.ReadBytes(PeSignatureSize);
	// convert it to a string and trim \0 at the end of the content
	string peContent = Encoding.Default.GetString(peSigBytes).TrimEnd('\0');
	// check if PE is in the content
	return peContent.Equals(PeSignatureContent);
}
&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;With this basic functionality we have a good base reader class to try the different  methods of parsing the COFF file header.&lt;/p&gt;
&lt;h3&gt;&lt;a name="heading0002" id="heading0002"&gt;COFF file header&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The COFF header has the following structure:&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="3" bordercolor="#000000" border="1px"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td bgcolor="#9fc5e8" width="33%"&gt;&lt;strong&gt;Offset&lt;/strong&gt;&lt;/td&gt;
            &lt;td bgcolor="#9fc5e8" width="33%"&gt;&lt;strong&gt;Size&lt;/strong&gt;&lt;/td&gt;
            &lt;td bgcolor="#9fc5e8" width="33%"&gt;&lt;strong&gt;Field&lt;/strong&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="33%"&gt;0&lt;/td&gt;
            &lt;td width="33%"&gt;2&lt;/td&gt;
            &lt;td width="34%"&gt;Machine&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="33"&gt;2&lt;/td&gt;
            &lt;td width="33"&gt;2&lt;/td&gt;
            &lt;td width="33%"&gt;NumberOfSections&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="33%"&gt;4&lt;/td&gt;
            &lt;td width="33%"&gt;4&lt;/td&gt;
            &lt;td width="33%"&gt;TimeDateStamp&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="33%"&gt;8&lt;/td&gt;
            &lt;td width="33%"&gt;4&lt;/td&gt;
            &lt;td width="33%"&gt;PointerToSymbolTable&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="33%"&gt;12&lt;/td&gt;
            &lt;td width="33%"&gt;4&lt;/td&gt;
            &lt;td width="33%"&gt;NumberOfSymbols&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="33%"&gt;16&lt;/td&gt;
            &lt;td width="33%"&gt;2&lt;/td&gt;
            &lt;td width="33%"&gt;SizeOfOptionalHeader&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="33%"&gt;18&lt;/td&gt;
            &lt;td width="33%"&gt;2&lt;/td&gt;
            &lt;td width="33%"&gt;Characteristics&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;If we translate this table to code, we get something like this:&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;pre lang="C#" class="brush: csharp"&gt;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CoffHeader {
	public MachineType Machine;
	
	public ushort NumberOfSections;
	
	public uint TimeDateStamp;
	
	public uint PointerToSymbolTable;
	
	public uint NumberOfSymbols;
	
	public ushort SizeOfOptionalHeader;
	
	public Characteristic Characteristics;
}
&lt;/pre&gt;
&lt;h2&gt;&lt;a name="heading0003" id="heading0003"&gt;BaseCoffReader&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;All readers do the same thing, so we go to the patterns library in our head and see that &lt;a href="http://en.wikipedia.org/wiki/Strategy_pattern"&gt;Strategy pattern&lt;/a&gt; or &lt;a href="http://en.wikipedia.org/wiki/Template_method_pattern"&gt;Template method pattern&lt;/a&gt;  is sticked out in the bookshelf.&lt;/p&gt;
&lt;p&gt;I have decided to take the template method pattern in this case, because the  Parse() should handle the IO for all implementations  and the concrete parsing should done in its derived classes.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;pre lang="C#" class="brush: csharp"&gt;
public CoffHeader Parse() {
	using (var br = new BinaryReader(File.Open(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read))) {
	    SeekToPeSignature(br);
	    if (!IsValidPeSignature(br)) {
	        throw new BadImageFormatException();
	    }
	    return ParseInternal(br);
	}
}

protected abstract CoffHeader ParseInternal(BinaryReader br);
&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;First we open the BinaryReader, seek to the PE signature then we check if it contains a valid PE signature and rest is done by the derived  implementations.&lt;/p&gt;
&lt;h2&gt;&lt;a name="heading0004" id="heading0004"&gt;Byte4ByteCoffReader&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The first solution is using the BinaryReader.  It is the general way to get the data. We only need to know which order,  which data-type and its size. If we read byte for byte we could comment out the first line in  the CoffHeader structure, because we have control about  the order of the member assignment.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;pre lang="C#" class="brush: csharp"&gt;
protected override CoffHeader ParseInternal(BinaryReader br) {
	CoffHeader coff = new CoffHeader();
	coff.Machine = (MachineType)br.ReadInt16();
	coff.NumberOfSections = (ushort)br.ReadInt16();
	coff.TimeDateStamp = br.ReadUInt32();
	coff.PointerToSymbolTable = br.ReadUInt32();
	coff.NumberOfSymbols = br.ReadUInt32();
	coff.SizeOfOptionalHeader = (ushort)br.ReadInt16();
	coff.Characteristics = (Characteristic)br.ReadInt16();
	return coff;
}
&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;If the structure is as short as the COFF header here and the specification will never changed,  there is probably no reason to change the strategy.  But if a data-type will be changed, a new member will be added or ordering of member  will be changed the maintenance costs of this method are very high.&lt;/p&gt;
&lt;h2&gt;&lt;a name="heading0005" id="heading0005"&gt;UnsafeCoffReader&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Another way to bring the data into this structure is using a "magically" unsafe trick.  As above, we know the layout and order of the data structure.  Now, we need the StructLayout attribute, because we have to ensure that the .NET Runtime  allocates the structure in the same order as it is specified in the source code.  We also need to enable "Allow unsafe code (/unsafe)" in the project's build properties.&lt;/p&gt;
&lt;p&gt;Then we need to add the following constructor to the CoffHeader structure.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;pre lang="C#" class="brush: csharp"&gt;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CoffHeader {
	public CoffHeader(byte[] data) {
		unsafe {
			fixed (byte* packet = &amp;amp;data[0]) {
			    this = *(CoffHeader*)packet;
			}
		}
	}
}

&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;The "magic" trick is in the statement: this = *(CoffHeader*)packet;. What happens here? We have a fixed size of data somewhere in the memory and  because a struct in C# is a value-type, the assignment  operator = copies the whole data of the structure and not only the reference.&lt;/p&gt;
&lt;p&gt;To fill the structure with data, we need to pass the data as bytes into the  CoffHeader structure. This can be achieved by reading the  exact size of the structure from the PE file.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;pre lang="C#" class="brush: csharp"&gt;
protected override CoffHeader ParseInternal(BinaryReader br) {
	return new CoffHeader(br.ReadBytes(Marshal.SizeOf(typeof(CoffHeader))));
}
&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;This solution is the fastest way to parse the data and bring it into the structure, but it is unsafe and it could introduce some security and stability risks.&lt;/p&gt;
&lt;h2&gt;&lt;a name="heading0006" id="heading0006"&gt;ManagedCoffReader&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this solution we are using the same approach of the structure assignment as above.  But we need to replace the unsafe part in the constructor with the following managed part:&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;pre lang="C#" class="brush: csharp"&gt;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CoffHeader {
	public CoffHeader(byte[] data) {
		IntPtr coffPtr = IntPtr.Zero;
		try {
			int size = Marshal.SizeOf(typeof(CoffHeader));
			coffPtr = Marshal.AllocHGlobal(size);
			Marshal.Copy(data, 0, coffPtr, size);
			this = (CoffHeader)Marshal.PtrToStructure(coffPtr, typeof(CoffHeader));
		} finally {
			Marshal.FreeHGlobal(coffPtr);
		}
	}
}
&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;h2&gt;&lt;a name="heading0007" id="heading0007"&gt;Conclusion&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;We saw that we can parse well-formed binary data to our data structures using  different approaches. The first is probably the clearest way, because we know each member and its size and ordering and we have control about the reading the data for each member. But if add member or the structure is going change by some reason, we need to change the reader.&lt;/p&gt;
&lt;p&gt;The two other solutions use the approach of the structure assignment. In the unsafe implementation we need to compile the project with the /unsafe option.  We increase the performance, but we get some security risks.&lt;/p&gt;
&lt;p&gt;&lt;a href="FC2HWAZE6QXN" style="display:none"&gt;FC2HWAZE6QXN&lt;/a&gt;&lt;/p&gt;&lt;img src="http://geekswithblogs.net/cmenzi/aggbug/138375.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/cmenzi/comments/138375.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/cmenzi/comments/commentRss/138375.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/cmenzi/services/trackbacks/138375.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Spring Geek Night </title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/cmenzi/archive/2010/02/25/spring-geek-night.aspx" />
        <id>http://geekswithblogs.net/cmenzi/archive/2010/02/25/spring-geek-night.aspx</id>
        <published>2010-02-25T11:38:0001:00:00</published>
        <updated>2010-03-02T11:07:40Z</updated>
        <content type="html">&lt;p&gt;&lt;img height="41" align="left" width="135" alt="SpingSource" style="margin-right: 10px; padding-bottom: 10px;" src="http://www.springsource.org/sites/all/themes/zen/framework/logo.png" /&gt;&lt;/p&gt;
&lt;h3&gt;Spring Geek Night at Technopark Zurich&lt;/h3&gt;
&lt;p&gt; &lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Yesterday, I was at the spring geek night in Zurich. &lt;a href="http://sambrannen.com"&gt;Sam Brannen&lt;/a&gt;,  the author of the &lt;a target="blank" href="http://static.springframework.org/spring/docs/2.5.x/reference/testing.html#testcontext-framework" title="Spring TestContext Framework"&gt;Spring TestContext Framework&lt;/a&gt; and previously a member of the SpringSource dm Server development team, has presented the new features of Spring 3.0.&lt;/p&gt;
&lt;p&gt;With Spring 3.0 nearly everything is annotable. So, for all those who hate XML with Spring 3.0 you can now declare all things in your code.&lt;/p&gt;
&lt;p&gt;A very magic stuff are these meta-annotations. Means, "annotate your annotations" and use the combination of it.&lt;/p&gt;
&lt;p&gt;Normally you have done something like this.&lt;/p&gt;
&lt;pre class="brush: java"&gt;
@Service
@Transactional
public class CustomerService{
...
}
&lt;/pre&gt;
&lt;p&gt;Then you can define the combination.&lt;/p&gt;
&lt;pre class="brush: java"&gt;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Service
@Transactional
public @interface TransactionalService {}
&lt;/pre&gt;
&lt;p&gt;Now you can use this new annotation for your services.&lt;/p&gt;
&lt;pre class="brush: java"&gt;
@TransactionalService 
public class CustomerService{
...
}
&lt;/pre&gt;
&lt;p&gt;I know this is a Java feature, but I've never realised that's possible.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;&lt;img src="http://geekswithblogs.net/cmenzi/aggbug/138166.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/cmenzi/comments/138166.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/cmenzi/comments/commentRss/138166.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/cmenzi/services/trackbacks/138166.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Hello Geeks!</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/cmenzi/archive/2010/02/25/hello-geeks.aspx" />
        <id>http://geekswithblogs.net/cmenzi/archive/2010/02/25/hello-geeks.aspx</id>
        <published>2010-02-25T11:19:0701:00:00</published>
        <updated>2010-02-25T11:22:27Z</updated>
        <content type="html">&lt;p&gt;Welcome to my geeky blog.&lt;/p&gt;
&lt;p&gt;I'm going to focus on following topics:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;software architecture&lt;/li&gt;
    &lt;li&gt;patterns and anti-patterns&lt;/li&gt;
    &lt;li&gt;best pratices / clean code&lt;/li&gt;
    &lt;li&gt;.NET (WCF / WF /WPF)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keep checking...&lt;/p&gt;
&lt;p&gt;Cheers&lt;/p&gt;
&lt;p&gt;Cédric&lt;/p&gt;&lt;img src="http://geekswithblogs.net/cmenzi/aggbug/138165.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/cmenzi/comments/138165.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/cmenzi/comments/commentRss/138165.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/cmenzi/services/trackbacks/138165.aspx</trackback:ping>
    </entry>
</feed>