I’ve recently had need of a base64 codec for one of my projects. (For those who don’t know, a codec is a piece of software that encodes and decodes data using a certain encoding algorithm. If you’ve ever ripped to MP3 and/or played it back, you’ve used MP3 codecs. The base64 algorithm is one used in a lot of places to get binary data into places where binary data isn’t usually welcome.)
Anyway, the base64 algorithm is simple, standardized, and popular. Its use is mandated for certain tasks, and is a strong alternative in others. So, one would think, there should be plenty of good free implementations floating around that I could use, right?
All the base64 code I could find in Google suffered from at least one of several deficiencies. Quite a few examples were in languages I wasn’t interested in, such as Java. Some looked good, but had licensing difficulties, including a few which had no discernable license grants at all from the original author. (Why even put such things out on the net?) Some had serious dependencies on data structures or other features of the larger projects they were a part of. Codecs that assumed stream-based operation were very common. And, sprinkled among the various code references, were glimpses into the frustration other projects have felt when performing the same search I was.
Clearly, there is a niche here left to be filled.
Well, at least there was. Those searching for such a thing can now use my base64 codec, implemented in base64.c and base64.h. The license is XFree86-like, and is 100% original code. It should be suitable for in-memory or streaming en/decoding, and does not attempt to allocate memory on its own. Being context-based, it can support multiple simultaneous transformations. I believe it to be fairly frugal regarding system resources.
Being a new implementation, I’m sure it has bugs. It’s not thread-safe, both because of a lack of locking mechanisms on contexts and because of the use of a single global variable internally. Additionally, I’m fairly sure the decode operation could be a lot smarter, and thus could perform much better. I’d like to think I’ve been careful regarding buffer management, though I’m not so stupid as to be confident of it. And it seems to be faithful to the spec, at least according to my testing. If I get a chance, I might improve on it later; alternatively, patches and other feedback are welcome!