WebBiscuit

WebBiscuit

The Webbiscuit Blog: Taking the Internet crumb by crumb…

  • Home
  • Software
    • Palette Parser
    • XHTML Generator
  • Articles
    • The Strategy Pattern
    • The Factory Pattern
    • Memory leaks
  • About Web Biscuit
  • Contact

Base64 Encoder and Boost

Posted in Boost, C++, code, CodeProject by Daniel
Apr 02 2012
TrackBack Address.

Well, I was looking for a Base64 library recently, and I thought, “I know, I bet it is in Boost, I have Boost, and Boost has EVERYTHING.” And it turns out that it does! Kind of. But it’s a bit odd and sadly incomplete.

So let’s fix it.

To start with, I didn’t really have a clue how to plug these Boost components together, so a quick scout on the beloved StackOverflow (a Programmer’s Guide to the Galaxy) yields the following code, which I have slightly modified:

using namespace boost::archive::iterators;

typedef 
  insert_linebreaks<         // insert line breaks every 76 characters
    base64_from_binary<    // convert binary values to base64 characters
      transform_width<   // retrieve 6 bit integers from a sequence of 8 bit bytes
        const unsigned char *
        ,6
        ,8
        >
      > 
      ,76
    > 
  base64Iterator; // compose all the above operations in to a new iterator

Disgusting, isn’t it? It also doesn’t work. It will only work when the data you use is a multiple of three, so we’ll have to pad it ourselves. Here’s the full code for that:

#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/transform_width.hpp>

namespace Base64Utilities
{
  std::string ToBase64(std::vector<unsigned char> data)
  {
    using namespace boost::archive::iterators;

    // Pad with 0 until a multiple of 3
    unsigned int paddedCharacters = 0;
    while(data.size() % 3 != 0)
    {
      paddedCharacters++;
      data.push_back(0x00);
    }

    // Crazy typedef black magic
    typedef 
      insert_linebreaks<         // insert line breaks every 76 characters
        base64_from_binary<    // convert binary values to base64 characters
          transform_width<   // retrieve 6 bit integers from a sequence of 8 bit bytes
            const unsigned char *
            ,6
            ,8
            >
          > 
          ,76
        > 
        base64Iterator; // compose all the above operations in to a new iterator

    // Encode the buffer and create a string
    std::string encodedString(
      base64Iterator(&data[0]),
      base64Iterator(&data[0] + (data.size() - paddedCharacters)));

    // Add '=' for each padded character used
    for(unsigned int i = 0; i < paddedCharacters; i++)
    {
      encodedString.push_back('=');
    }

    return encodedString;
  }
}

It’s not that elegant but it seems to work. Can you improve on this code? Can you write the decode function? Check your answers with this excellent Online Base64 Converter.
Leave your comments below!

Share
2 Comments »
Tagged as: base64, encoding

RSS Other Nibbles

  • Palette Parser Update – Now supports CMYK format
  • Are you a biscuit?
  • Base64 Encoder and Boost
  • The Sleep Experiment
  • A macro to create a C++ implementation from a header declaration
  • A macro to flip between the source and header file (and back again)
  • An introduction to three VC++ Macros: How they came to be
  • Making Eclipse more like Visual Studio
  • A tokeniser using STL
  • More of WebBiscuit’s WordPress Plugins

Categories

  • Boost
  • C++
  • code
  • CodeProject
  • CSharp
  • domains
  • Eclipse
  • experiments
  • funny
  • games
  • IDE
  • macro
  • migrating
  • plugins
  • productivity
  • reviews
  • sleeping
  • STL
  • Test
  • tips
  • VB
  • Visual Studio
  • web tips
  • WebBiscuit Software
  • wordpress
  • workarounds

Blogroll

  • Create colour palettes with Kuler
  • Free Icons with Axialis Software

Stale Biscuits

  • December 2012 (1)
  • August 2012 (1)
  • April 2012 (1)
  • January 2012 (1)
  • December 2011 (2)
  • November 2011 (1)
  • September 2011 (1)
  • August 2011 (1)
  • July 2011 (9)

Eating the web bit by bit

Powered by WordPress | “Blend” from Spectacu.la WP Themes Club