digital Utopia

A new suit

Published by . Filed under Uncategorized. Total of no comments in the discussion.

Back in August 2010 I bought my first race suit. Took several attempts to get it fitting properly and I ended up wearing a suit for someone who was apparently 6’3″. I am 6’0″. Constant use over 5 years including several 24 hour events had taken its toll so I bought a replacement but decided to push the boat out and get the more expensive xlight style. I used the same size as my current suit and this one is for the size measured. In hind sight I should of sent it back for a smaller size but impatience got the better of me and I started racing in it. The extra bagginess makes me look fat and the lightness is used up in the extra size.

Shoes and gloves are okay to buy online but suits (especially Sparco ones) really need fitting especially considering it was my second most expensive item of clothing I have bought.

Evolution of code

Published by . Filed under C++, Programming. Total of no comments in the discussion.

A few years ago I worked on a project for a company that used a base64 class to encode a binary object into xml.  It was a class in that loosest of senses it being a simple wrapper around a pair of static C functions.  In fact it also made some assumptions which was painfully obvious as soon as I saw them – the biggest of which was there would always be enough space to put the resulting streams. The code assumed that there would always be enough.

	static int encode(const char* bytes_to_encode, unsigned int in_len, char* encoded_bytes, unsigned int out_len);

Also it was painful to use – the code we were writing had std::string  objects and std::vector  all over the place and passing them as pointers with offsets was a pain in the proverbial. So I threw together some unit tests via the help of Wikipedia and set about improving it. Once I the tests in place I set about improving the interface. This became templated with an input container and an output container. The result looked something like:

	template<class InContainer, class OutContainer>
	static int encode(InContainer& bytes_to_encode, OutContainer& encoded_bytes);

Since this was the property of the company I was working for I wanted one for myself I sat down an wrote one from scratch, different to the one I had used at work (which to be honest looked like it could of been grabbed from any one of a dozen places on the ‘Net).

Complete, this sat in my Subversion repository until recently. Dusting it off I saw that the code could be improved to look more like the STL algorithm library with iterators instead of containers. The first pass looked like this:

	template<class InputIt, class OutputIt>
	static OutputIt encode(InputIt init_first, InputIt init_last, OutputIt outit_first);

Which is very similar to transform function from the STL. Could I create one that allows us to use the transform function. Checking what the transform does and what the RFC of the base64/32/16 says we can easily see they are incompatible. However we can work with the for_each() function.

Rather than hack together a bit of code I decided to rewrite it from the ground up. I started off with a helper function. It looked originally looked like this:

template < class OutContainer >
encoder_<OutContainer> encoder(OutContainer& c)
	return encoder_<OutContainer>(c);

But I eventually changed it to use an output iterator.  The change allows such use as to inject the stream directly into the transport stream rather than simply pushing back after via use of insert_iterator.

template< class OutputIt >
encoder_<OutputIt> encode(OutputIt ref)
        return encoder_<OutputIt>(ref);

I decided to use the call it encoder and and place it in the encoder namespace as I may wish in the future to extend it with a Motorola SREC encoder. 

Before we go much further we need to write a few tests to verify what we are doing is correct.  As mentioned earlier, both the Wikipedia page and the RFC4648 pages are very good places to get ‘inspiration’ for the test matter.

The RFC4648 which is the standard for not only the base64 encodings, but the lesser known base32 and base16 encodings plus the alternative  alphabets for each. Most encoders on the ‘Net only work with the base64…can we provide support for these? Actually yes we can and fairly easily too.  First of all we create a bunch of alphabet objects. Simple const string s. One for each encoding type.  For each of these we need to create the encoding objects.  These can be simple structs. I think they are referred to as traits in the STL but I might be using them slightly differently to how traits are used.

struct base64
static const std::string alphabet_;
struct base64url
static const std::string alphabet_;

const std::string base64::alphabet_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const std::string base64url::alphabet_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

These become a template parameter to function and class.  We specify the standard base64 alphabet as the default.  Giving us the new signature of:

template< typename Alphabet = base64, class OutputIt >
encoder_<Alphabet, OutputIt> encoder(OutputIt it)
        return encoder_<Alphabet, OutputIt>(it);

So now write some tests to use both alphabets. To support the base32 and base16 we add various information to the various alphabet structs for the masks, shifts and modulus.

Eventually we are left with a working encoder.  The decoder is slightly harder but not much.  Again with the single code base we can decode base64/32/16 with different alphabets.  The only difference is the handling the 3.3:

Implementations MUST reject the encoded data if it contain characters outside the base alphabet when interpreting base-encoded data, unless the specification referring to this document explicitly states otherwise.

This is handled by another parameter class, one which decides whether to allow or to reject non alphabet characters. By default we should reject (I throw). We are left with this as an interface:

template< typename Alphabet = base64, typename InvalidAlphabet = invalid_data_throw<Alphabet>, class OutputIt >
decoder_<Alphabet, InvalidAlphabet, OutputIt> decode(OutputIt ref)
return decoder_<Alphabet, InvalidAlphabet, OutputIt>(ref);

While we have a nice looking code, it doesn’t help if it isn’t fast.  I did a simple Google search and grabbed the first C++ base64 encoder I came across. A quick bit of hacking and the result was not good.  I am slightly more than 2x slower on the encode.  Why is this?  Looking at the code by Rene he is working on data in blocks of threes while I am doing it byte by byte – to which I would expect my code to be 3x slower.

Encoding is only half the story. We need to decode too. In this we are faster, but only just. When it comes to base32 (and base16) mine wins:-)

So I have pretty much done as much as I wanted to do with it. There is a little more I could do – better code coverage, tidier code, different encodings – but for now I will upload it to GitHub.  See it here.

29C outside and…

Published by . Filed under Life. Total of no comments in the discussion.

…Tesco are putting the Christmas stock on the shelves! Hard to feel festive when it is hotter than the Mediterranean!

Disney vs Disney

Published by . Filed under Leisure, Life. Total of no comments in the discussion.

I have been to EuroDisney aka DisneyLand Resort Paris.  I have now been to DisneyWorld aka DisneyLand Resort Florida so can compare the two.

  • EuroDisney is smaller than DisneyWorld.
  • EuroDisney rides are more extreme than DisneyWorld.
  • EuroDisney queues are quicker than DisneyWorld.
  • EuroDisney seems more expensive.
  • DisneyWorld staff are way more friendlier.
  • DisneyWorld has more varied rides.

Apart from the oppressive Florida summer heat (92°F/33°C with 85% humidity) you could of been on Main Street in EuroDisney: Every other shop was merchandise and if it wasn’t merchandise it was food.  Still dollars but Euros instead of USD.

Please Amazon…

Published by . Filed under Leisure, Life. Total of no comments in the discussion.

…stop using HDNL. I have finished reading a book.  I then decide to order some more from Amazon. Since Jeff Bezos has direct access to my bank account I pay for next day delivery service (aka Prime).  So it was on early Friday morning (think 00:00) I decided to order not 1 or 2 books, but 7 (voracious).  Amazon said, that, by ordering these books they would be with me on the 16th, which is a Saturday. That would mean they would turn up at an empty workplace (making it hard for me to collect them from the depot) or I could have them delivered to my house.  So it was my house I picked.  These were duly dispatched on the Friday (think 15:00) and would be with me on the 16th, delivered by the HDNL.  I haven’t had good experience with HDNL. In the past they have failed to deliver stuff to my house and sent it back.  Said they had sent it back but had delivered it to my neighbours house when I was actually at the depot being told that it had been returned and even refused to give it to my wife because the card didn’t have a name on it and the bill she took was her mobile and was in her name, not mine (lets ignore the address is the same and shall we?). The craziest was when HDNL made 3 seperate deliveries to our work place with 3 seperate vans to deliver 3 copies of Windows 7 on the launch day.

Anyway, I work in Manchester. Therefore I will not be in during the week.  And unable to collect my books. So I have nothing to read and HDNL will probably send them back before next week end like they have done in the past and the first I will know about it is the credit note I will receive.

Please Amazon, stop using HDNL or give us the option.