view C++/Base64.h @ 290:7433ebe6a8b0

Pack: * Stateful classes * Use << and >> operators for more convenience, * Use PackFileWriter and PackFileReader to write/read a file, * Use PackStringWriter and PackStringReader to write/read a string, * Pack now supports object serialization through Pack::TypeInfo<T>::(un)serialize.
author David Demelier <markand@malikania.fr>
date Thu, 13 Nov 2014 13:08:49 +0100
parents 73e5381d7baf
children
line wrap: on
line source

/*
 * Base64.h -- base64 encoding and decoding
 *
 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _BASE_64_H_
#define _BASE_64_H_

/**
 * @file Base64.h
 * @brief Base64 encoding and decoding
 */

#include <stdexcept>
#include <string>

/**
 * @class Base64
 * @brief Encode and decode Base64 data
 */
class Base64 {
public:
	/**
	 * Get the base 64 character from the 6-bits value.
	 *
	 * @param value the value
	 */
	static char lookup(int value) noexcept;

	/**
	 * Get the integer value from the base 64 character.
	 *
	 * @param ch the base64 character
	 */
	static int rlookup(char ch);

	/**
	 * Encode the input to the output. Requirements:
	 *	InputIt	must be InputIterator
	 *	OutputIt must be OutputIterator
	 *
	 * @param input the beginning
	 * @param end the end of the data
	 * @param output the output destination
	 * @return output
	 */
	template <typename InputIt, typename OutputIt>
	static OutputIt encode(InputIt input, InputIt end, OutputIt output)
	{
		while (input != end) {
			char inputbuf[3] = { 0, 0, 0 };
			int count;

			for (count = 0; count < 3 && input != end; ++count)
				inputbuf[count] = *input++;

			*output++ = lookup(inputbuf[0] >> 2 & 0x3f);
			*output++ = lookup((inputbuf[0] << 4 & 0x3f) | (inputbuf[1] >> 4 & 0x0f));
			*output++ = (count < 2) ? '=' : lookup((inputbuf[1] << 2 & 0x3c) | (inputbuf[2] >> 6 & 0x03));
			*output++ = (count < 3) ? '=' : lookup(inputbuf[2] & 0x3f);
		}

		return output;
	}

	/**
	 * Decode the input to the output. Requirements:
	 *	InputIt must be InputIterator
	 *	OutputIt must be OutputIterator
	 *
	 * @param input the beginning
	 * @param end the end of the data
	 * @param output the output destination
	 * @return output
	 * @throw std::invalid_argument on bad base64 string
	 */
	template <typename InputIt, typename OutputIt>
	static OutputIt decode(InputIt input, InputIt end, OutputIt output)
	{
		while (input != end) {
			char inputbuf[4] = { 0, 0, 0, 0 };
			int count;

			for (count = 0; count < 4 && input != end; ++count) {
				inputbuf[count] = (*input == '=') ? '=' : rlookup(*input);
				input++;
			}

			if (count != 4)
				throw std::invalid_argument("truncated string");

			*output++ = (inputbuf[0] << 2 & 0xfc) | (inputbuf[1] >> 4 & 0x03);

			if (inputbuf[2] != '=')
				*output++ = (inputbuf[1] << 4 & 0xf0) | (inputbuf[2] >> 2 & 0x0f);
			if (inputbuf[3] != '=')
				*output++ = (inputbuf[2] << 6 & 0xc0) | (inputbuf[3] & 0x3f);
		}

		return output;
	}

	/**
	 * Encode a string.
	 *
	 * @param input the input string
	 * @return the base64 formatted string
	 */
	static std::string encode(const std::string &input);

	/**
	 * Decode a string.
	 *
	 * @param input the base64 formatted string
	 * @return the original string
	 * @throw std::invalid_argument on bad base64 string
	 */
	static std::string decode(const std::string &input);
};

#endif // !_BASE_64_H_