Skip to main content

In praise of... text files and protocols

The other night I had to debug a problem where CMYK colors specified in an OmniGraffle file weren't making it into an exported PDF (or at least appeared not to be). At first it looked like it might be a nightmare because what I really wanted to do was ignore the OmniGraffle UI and look inside the .graffle file and the PDF itself. But salvation was at hand: both .graffle and PDF are text formats. The OmniGraffle file is actually an XML document (in some cases it's a gzipped XML document but it can be decompressed with gunzip). Here, for example is part of the Colors.graffle file that's provided as a sample. It's easy to see the RGB colors that are specified and just as easy to modify them by adjusting the text file.
 
Yes, it's an image of text.  Just like a binary file is an 'image' of something that could have been easy to manipulate.
While fiddling around in the .graffle file looking at the CMYK colors I spotted that some straight lines that had been drawn in OmniGraffle were not quite straight. That's quite tricky to see in the UI, but dead easy in the XML document and you can simply fix the coordinates. Here, for example, are segments of a line drawn from the Nucleobases.graffle sample file:

The text format made it easy to examine what was happening under the hood of the fancy UI, to quickly fix small problems and to manipulate the file using other programs. Similarly, once I'd determined that in the file I was working with the CMYK colors were fine I exported a PDF and decompressed the result using pdftk. It was fairly easy to follow a color specification through from the .graffle file and into the PDF. Here, for example, is an RGB color specified in the Nucleobases.graffle and the corresponding color appearing in the exported PDF of the same file:

And with that I was able to determine that the CMYK colors were correct and that any problem lay with the person I was sending the PDF to.

The deeper story is that human-readable text formats are wonderful: they are easily debugged, they are easily manipulated (with text editors and other tools like awk and sed), and they can be compressed using common compression programs if space is a problem.

Similarly text based protocols (such as HTTP, IMAP, SMTP, FTP and POP3) make it easy for humans to write, read and debug. One of the things that made POPFile easy to implement was that all the mail protocols are text based (the entire POP3 proxying module is able to use simple string matching and regular expressions to handle POP3). And they are also line oriented (a command is read by reading to the line ending). That makes programs to handle them very easy to implement.

Recently I used an undocumented API that was entirely text-based (using JSON) to obtain live bus arrival times in London and make an Ambient Bus Arrival Monitor.

Another great example of a text format appears in the code that's behind Hacker News and UseTheSource.  In the Lisp philosophy your program is also data it can consume and the data about users is simply sent to a file as Arc code meaning that any admin tasks that don't (yet) have UI can be performed trivially by hand:

Of course, the downside is that text takes up extra space and for low-level protocols (such as IP) it makes sense to use binary. But for almost everything else it's best to use text. Only use binary protocols where the performance is so sensitive that it's worth the implementation and debugging downside. The upside is that no special tools are needed.

I wonder how much of the success of the Internet can be put down to the decision to use text-based protocols for almost everything that people will need to implement.  And how much we owe the early writes of the RFCs in deciding that text was best.

PS A reader points to Eric Raymond's Art of Unix Programming and specifically the chapter called Textuality.

PPS A commenter over at Hacker News makes the very good point that it's easy to version/diff text files and very hard with binary.

PPPS Another commenter over at Hacker News points out that there's a chapter in The Pragmatic Programmer called The Power of Plain Text.

Comments

Anonymous said…
Text +1

The Art of Unix Programming has an entire chapter devoted to this (protocols, file formats, configuration files etc).
Unknown said…
While I agree with the point of the post, it was juxtaposed on Hacker News with the excellent presentation on Unicode, Love Hotels and Unicode (it's pretty standard until about halfway through - then it gets interesting fast). Because of that lucky coincidence I am now thinking, "This is all fine and dandy, but what, exactly, is 'text'?" :)
axilmar said…
No, text is the wrong medium of communication. What we should have is a simple binary format that allows us to express the fundamental data structures (8/16/32/64-bit integers, 32/64-bit floats, 8/16/32/bit characters, records, arrays, references). Writing an editing tool for such a simple format would be just as easy as a text editor, but our files would have structure and be much more compact at the same time.
Unknown said…
See also http://www.cryptonomicon.com/beginning.html, especially the section on 'Metaphor Shear'.
Stephan Wehner said…
Another example: Open Office odt files, which are compressed but easily expanded with unzip. Then one gets to look at the files making up the document, for example: Configurations2/ content.xml META-INF/ meta.xml mimetype settings.xml styles.xml Thumbnails/

Stephan
Anonymous said…
try ASN.1 - it exists for that exact purpose.
Anonymous said…
try ASN.1 - it exists for this exact purpose.
MCAndre said…
....yep. Binary formats are impediments in day-to-day tasks and in furthering technology for tomorrow.
phonokraft said…
Yes, text files really make these things easy to work with! Thanks for the examples!

It is also very interesting to think about the efficiency. To represent a color in binary might take, say, 3 single precision floating point numbers, of 4 bytes each. That would be 12 bytes.

Let's compare this to the dictionary approach:

Color

b
0.147909
g
0.237903
r
0.0450865


Without any indenting, saving this file on my machine uses 159 bytes. This approach uses 13 times as much storage (or bandwidth) as the floating point approach. Of course, compression mitigates this somewhat, as you note.

And yet...

On an individual use case, one might say this doesn't matter.

But if one considered the energy consumed by servers that store the world's data, an efficiency factor of 13 might make a difference. Which would you choose: 13 megawatts, or 1 megawatt? $13 million, or $1 million? Etc.

Recently, I haven't been able to stop thinking about this sort of issue as it applies to global energy use. I wish I could, but I can't make the thoughts go away!

Well, just a few thoughts.

Popular posts from this blog

Your last name contains invalid characters

My last name is "Graham-Cumming". But here's a typical form response when I enter it:


Does the web site have any idea how rude it is to claim that my last name contains invalid characters? Clearly not. What they actually meant is: our web site will not accept that hyphen in your last name. But do they say that? No, of course not. They decide to shove in my face the claim that there's something wrong with my name.

There's nothing wrong with my name, just as there's nothing wrong with someone whose first name is Jean-Marie, or someone whose last name is O'Reilly.

What is wrong is that way this is being handled. If the system can't cope with non-letters and spaces it needs to say that. How about the following error message:

Our system is unable to process last names that contain non-letters, please replace them with spaces.

Don't blame me for having a last name that your system doesn't like, whose fault is that? Saying "Your last name …

Importing an existing SSL key/certificate pair into a Java keystore

I'm writing this blog post in case anyone else has to Google that. In Java 6 keytool has been improved so that it now becomes possible to import an existing key and certificate (say one you generated outside of the Java world) into a keystore.

You need: Java 6 and openssl.

1. Suppose you have a certificate and key in PEM format. The key is named host.key and the certificate host.crt.

2. The first step is to convert them into a single PKCS12 file using the command: openssl pkcs12 -export -in host.crt -inkey host.key > host.p12. You will be asked for various passwords (the password to access the key (if set) and then the password for the PKCS12 file being created).

3. Then import the PKCS12 file into a keystore using the command: keytool -importkeystore -srckeystore host.p12 -destkeystore host.jks -srcstoretype pkcs12. You now have a keystore named host.jks containing the certificate/key you need.

For the sake of completeness here's the output of a full session I performe…

More fun with toys: the Ikea LILLABO Train Set

As further proof of my unsuitability to be a child minder (see previous post) I found myself playing with an Ikea LILLABO 20-piece basic set train.


The train set has 16 pieces of track (12 curves, two straight pieces and a two part bridge) and 4 pieces of train. What I wondered was... how many possible looping train tracks can be made using all 16 pieces?

The answer is... 9. Here's a picture of the 9 different layouts.


The picture was generated using a little program written in Processing. The bridge is red, the straight pieces are green and the curves are blue or magenta depending on whether they are oriented clockwise or anticlockwise. The curved pieces can be oriented in either way.

To generate those layouts I wrote a small program which runs through all the possible layouts and determines which form a loop. The program eliminates duplicate layouts (such as those that are mirror images of each other).

It outputs a list of instructions for building loops. These instructions con…