Base64 quirks

Base64 is an amazing text-based encoding scheme.  That said, it has its quirks.

For example, RFC 2045 states "Encoded lines must not be longer than 76 characters" but that add excessive processing overhead when, say, converting a 3MB image to base64.  However, if you don't add line breaks at all, some browsers and rendering engines choke on large data sets.  So, a general rule of thumb is to add a line break char ("\n") every 8191 characters (or every 8000 to keep it simple and allow some cushion).  This should fix any browser/renderer issues and make the data much more readable in formats such as SVG.

Another issue: adding base64 data to JSON is problematic because JSON doesn't allow line breaks within a single key/value pair (line breaks are generally stripped out automatically).  Viewing a JSON file with a large base64 encoded object will typically give you garbled lines.  I've tried a number of editors (gedit, leafpad, tea, eclipse, nano, emacs, scribes, geany) for a decent read-only viewing experience and the best options I've found so far are vim and SciTE.

Note: if you plan on using SciTE, install it and then from the menu choose "Options > Open User Options File" and enter the following:

open.filter=All Files|*.*
line.margin.visible=1
line.margin.width=3+
title.full.path=1
title.show.buffers=1
split.vertical=0
wrap=0
buffers=20
check.if.already.open=1
position.maximize=1
font.base=$(font.monospace),size:11

Comments

Popular Posts