diff options
author | lloyd <[email protected]> | 2010-10-28 19:34:18 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-10-28 19:34:18 +0000 |
commit | 576c5c901375d1ba5bfdfc5873562c63228abab5 (patch) | |
tree | 266fe177d66e961a2c7f38e5dc110926a47f08a6 /src | |
parent | 066c0618aaddf751989ee327660b567d88b2e198 (diff) |
Simple standalone base64 encoder
Diffstat (limited to 'src')
-rw-r--r-- | src/codec/base64/base64.cpp | 103 | ||||
-rw-r--r-- | src/codec/base64/base64.h | 55 | ||||
-rw-r--r-- | src/codec/base64/info.txt | 1 |
3 files changed, 159 insertions, 0 deletions
diff --git a/src/codec/base64/base64.cpp b/src/codec/base64/base64.cpp new file mode 100644 index 000000000..96a686e38 --- /dev/null +++ b/src/codec/base64/base64.cpp @@ -0,0 +1,103 @@ +/* +* Base64 Encoding and Decoding +* (C) 2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/base64.h> +#include <botan/mem_ops.h> +#include <botan/internal/rounding.h> +#include <stdexcept> + +#include <stdio.h> +#include <assert.h> + +namespace Botan { + +namespace { + +static const byte BIN_TO_BASE64[64] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' +}; + +void do_base64_encode(char out[4], const byte in[3]) + { + out[0] = BIN_TO_BASE64[((in[0] & 0xFC) >> 2)]; + out[1] = BIN_TO_BASE64[((in[0] & 0x03) << 4) | (in[1] >> 4)]; + out[2] = BIN_TO_BASE64[((in[1] & 0x0F) << 2) | (in[2] >> 6)]; + out[3] = BIN_TO_BASE64[((in[2] & 0x3F) )]; + } + +} + +size_t base64_encode(char out[], + const byte in[], + size_t input_length, + size_t& input_consumed, + bool final_inputs) + { + input_consumed = 0; + + size_t input_remaining = input_length; + size_t output_produced = 0; + + while(input_remaining >= 3) + { + do_base64_encode(out + output_produced, in + input_consumed); + + input_consumed += 3; + output_produced += 4; + input_remaining -= 3; + } + + if(final_inputs && input_remaining) + { + byte remainder[3] = { 0 }; + for(size_t i = 0; i != input_remaining; ++i) + remainder[i] = in[input_consumed + i]; + + do_base64_encode(out + output_produced, remainder); + + size_t empty_bits = 8 * (3 - input_remaining); + size_t index = output_produced + 4 - 1; + while(empty_bits >= 8) + { + out[index--] = '='; + empty_bits -= 6; + } + + input_consumed += input_remaining; + output_produced += 4; + } + + return output_produced; + } + +std::string base64_encode(const byte input[], + size_t input_length) + { + std::string output((round_up<size_t>(input_length, 3) / 3) * 4, 0); + + size_t consumed = 0; + size_t produced = base64_encode(&output[0], + input, input_length, + consumed, true); + + assert(consumed == input_length); + assert(produced == output.size()); + + return output; + } + +std::string base64_encode(const MemoryRegion<byte>& input) + { + return base64_encode(&input[0], input.size()); + } + + +} diff --git a/src/codec/base64/base64.h b/src/codec/base64/base64.h new file mode 100644 index 000000000..551f3daf8 --- /dev/null +++ b/src/codec/base64/base64.h @@ -0,0 +1,55 @@ +/* +* Base64 Encoding and Decoding +* (C) 2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BASE64_CODEC_H__ +#define BOTAN_BASE64_CODEC_H__ + +#include <botan/secmem.h> +#include <string> + +namespace Botan { + +/** +* Perform base64 encoding +* @param output an array of at least input_length*4/3 bytes +* @param input is some binary data +* @param input_length length of input in bytes +* @param input_consumed is an output parameter which says how many +* bytes of input were actually consumed. If less than +* input_length, then the range input[consumed:length] +* should be passed in later along with more input. +* @param final_inputs true iff this is the last input, in which case + padding chars will be applied if needed +* @return number of bytes written to output +*/ +size_t BOTAN_DLL base64_encode(char output[], + const byte input[], + size_t input_length, + size_t& input_consumed, + bool final_inputs); + +/** +* Perform base64 encoding +* @param input some input +* @param input_length length of input in bytes +* @param uppercase should output be upper or lower case? +* @return base64adecimal representation of input +*/ +std::string BOTAN_DLL base64_encode(const byte input[], + size_t input_length); + +/** +* Perform base64 encoding +* @param input some input +* @param uppercase should output be upper or lower case? +* @return base64adecimal representation of input +*/ +std::string BOTAN_DLL base64_encode(const MemoryRegion<byte>& input); + +} + +#endif diff --git a/src/codec/base64/info.txt b/src/codec/base64/info.txt new file mode 100644 index 000000000..aed5db3b6 --- /dev/null +++ b/src/codec/base64/info.txt @@ -0,0 +1 @@ +define BASE64_CODEC |