Just An Application

November 13, 2014

Swift vs. The Compound File Binary File Format (aka OLE/COM): Part One — First Open Your File

Supplies of booby trapped PDFs masquerading as invoices seem to have been exhausted. The same is true of .EXE’s concealed in ZIPs.

Fortunately an enterprising prospector somewhere has struck a rich seam of ‘word’ documents of dubious provenance which are now being made freely available to me and doubtless to many other lucky people as well.

Not being able to open my document in the approved manner I am unable to experience at first hand the planned ‘surprise’, but I don’t see why I should miss all the fun so I thought I would open my document in an unapproved manner by pulling it to bits programatically to see if I could find out what it was intended to do, although I could probably guess, and more interestingly, how.

In the case of PDFs the surprise involved Javascript and a buffer overflow. This time I though it was more likely to involve macros so I wondered if some Visual Basic code might put in an appearance at some point.

After a bit of rummaging around I came up with the specification for the likely format of my ‘word’ document.

Strangely the specification is a PDF.

According to the first paragraph of the specification the format has no less than three names

This document specifies a new structure called the Microsoft Compound File Binary (CFB) file format, also known as the Object Linking and Embedding (OLE) or Component Object Model (COM) structured storage compound file implementation binary file format. This structure name can be shortened to compound file.

or is that two names ?

Anyway in what follows I will follow the suggestion that the name can be shortened to compound file although I do reserve the right to put it in ‘quotes’ on occasion.

I was vaguely aware that whatever it was called it was some kind of container format but I was somewhat taken aback do discover exactly how ‘baroque’ it was. Still, in for a penny, in for a pound.

The choice of Swift for the task in hand was based on the fact that I wanted to do something real-worldish with it as a break from my ongoing forays into the low-level implementation details.

Using Xcode to create a ‘Command Line Tool’ with the language set to Swift results in a file called


which contains this

    import Foundation
    println("Hello, World!")

which is all very canonical and stuff, but where is main and where are the arguments ?

In a dangerous break with tradition a Swift command line program does not have to have a main function. This is clearly ‘not right’.

Not being irredeemably iconoclastic however it does enable access to the command line arguments via the variables




As is apparent from the names these correspond to the the




arguments passed to the standard C/C++ main function.

Using these we can set the world to rights by building ourselves an argument list and handing it to our very own main function.

    let argc          = Int(C_ARGC)
    var argp          = C_ARGV
    var argv:[String] = []
    for i in 0 ..< argc
        if let s = String(UTF8String: argp.memory)
    func main(args: [String])
    if argv.count == argc

Now how do you open a file in Swift ?

It is very easy to forget when starting to use Swift that if you know how to do something in Objective-C you will nearly always know how to do it in Swift.

If you are happy to work with the contents of the file as one lump you can do this

    let data = NSData(contentsOfFile:fileName)

This is simply the Swift equivalent of this

    NSData* data = [NSData dataWithContentsOfFile:fileName];.

in Objective-C

Alternatively you can do this and then read the contents as required

    let fh = NSFileHandle(forReadingAtPath:fileName)

This is the Swift equivalent of doing this

     NSFileHandle* fh = [NSFileHandle fileHandleForReadingAtPath:fileName];

in Objective-C

A general rule of thumb is that if it is an Objective-C class factory method it will be a Swift constructor with a keyword argument that results from chopping off the Objective-C class name from the front of the method name, except when it isn't as in the first example when the 'with' goes missing as well.

Copyright (c) 2014 By Simon Lewis. All Rights Reserved.

Unauthorized use and/or duplication of this material without express and written permission from this blog's author and owner Simon Lewis is strictly prohibited.

Excerpts and links may be used, provided that full and clear credit is given to Simon Lewis and justanapplication.wordpress.com with appropriate and specific direction to the original content.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.

%d bloggers like this: