C/C++ Base64 codec using libtomcrypt

Problem:

In C/C++ you want to encode/decode something from/to Base64. libtomcrypt is WTFPL-licensed and therefore provides a good choice for both commercial and non-commercial projects.

Solution:

Use these snippets:

#include <tomcrypt.h>

/**
* Encodes a given string in Base64
* @param input The input string to Base64-encode
* @param inputSize The size of the input to decode
* @return A Base64-encoded version of the encoded string
*/
std::string encodeBase64(const char* input, const unsigned long inputSize) {
    unsigned long outlen = inputSize + (inputSize / 3.0) + 16;
    unsigned char* outbuf = new unsigned char[outlen]; //Reserve output memory
    base64_encode((unsigned char*) input, inputSize, outbuf, &outlen);
    std::string ret((char*) outbuf, outlen);
    delete[] outbuf;
    return ret;
}

/**
* Encodes a given string in Base64
* @param input The input string to Base64-encode
* @return A Base64-encoded version of the encoded string
*/
std::string encodeBase64(const std::string& input) {
    return encodeBase64(input.c_str(), input.size());
}

/**
* Decodes a Base64-encoded string.
* @param input The input string to decode
* @return A string (binary) that represents the Base64-decoded data of the input
*/
std::string decodeBase64(const std::string& input) {
    unsigned char* out = new unsigned char[input.size()];
    unsigned long outlen = input.size();
    base64_decode((unsigned char*) input.c_str(), input.size(), out, &outlen);
    std::string ret((char*) out, outlen);
    delete[] out;
    return ret;
}