C# – How to write a file format handler


Today i'm cutting video at work (yea me!), and I came across a strange video format, an MOD file format with an companion MOI file.

I found this article online from the wiki, and I wanted to write a file format handler, but I'm not sure how to begin.

I want to write a file format handler to read the information files, has anyone ever done this and how would I begin?


Thanks for all the suggestions, I'm going to attempt this tonight, and I'll let you know. The MOI files are not very large, maybe 5KB in size at most (I don't have them in front of me).

Best Solution

You're in luck in that the MOI format at least spells out the file definition. All you need to do is read in the file and interpret the results based on the file definition.

Following the definition, you should be able to create a class that could read and interpret a file which returns all of the file format definitions as properties in their respective types.

Reading the file requires opening the file and generally reading it on a byte-by-byte progression, such as:

        using(FileStream fs = File.OpenRead(path-to-your-file)) {
            while(true) {
                int b = fs.ReadByte();
                if(b == -1) {
                //Interpret byte or bytes here....

Per the wiki article's referenced PDF, it looks like someone already reverse engineered the format. From the PDF, here's the first entry in the format:

Hex-Address: 0x00
Data Type: 2 Byte ASCII
Value (Hex): "V6"
Meaning: Version

So, a simplistic implementation could pull the first 2 bytes of data from the file stream and convert to ASCII, which would provide a property value for the Version.

Next entry in the format definition:

Hex-Address: 0x02
Data Type: 4 Byte Unsigned Integer
Value (Hex): 
Meaning: Total size of MOI-file

Interpreting the next 4 bytes and converting to an unsigned int would provide a property value for the MOI file size.

Hope this helps.