わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

BASE64デコード

std::stringをBASE64でデコードします。

(BASE64の途中に改行コードが含まれていた場合も正しくデコードできます。)

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>



/*
	BASE64デコード
*/
std::string Base64Decode
(
	std::string strData
)
{
	/*
		変換テーブル
	*/
	const static unsigned char baTable[ 256 ] = {
		  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfe, 0xff, 0xff
		, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
		, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f
		, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
		, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e
		, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff
		, 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28
		, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
		, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
		, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
		, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
		, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
		, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
		, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
		, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
		, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
	};

	std::string strRet;
	std::string::iterator ite    = strData.begin();
	std::string::iterator iteEnd = strData.end();

	while( ite != iteEnd ) {

		unsigned long dwValue = 0;
		int nBitCount = 0;

		// 4バイト単位で処理する
		int nCount = 4;
		while( nCount-- ) {

			// 1バイト取得
			dwValue <<= 6;
			if ( ite != iteEnd ) {
				unsigned char bChar = baTable[ *ite++ ];
				if ( 64 > bChar ) {
					dwValue |= bChar;
					nBitCount += 6;
				}
				else if ( 0xfe == bChar ) {

					// 改行コードは完全に無視する
					nCount++;
					dwValue >>= 6;
				}
			}
		}

		unsigned char* baTmp = (unsigned char*)&dwValue;

		// 並べ替え
		unsigned char baWrite[ 3 ] = { baTmp[ 2 ], baTmp[ 1 ], baTmp[ 0 ] };

		// 最大3バイトの書き込み
		int nByte = nBitCount / 8;
		if ( 0 < nByte ) {
			strRet.append( (char*)baWrite, nByte );
		}
	}

	// BASE64デコード結果を返す
	return( strRet );
}



/*
	BASE64デコード
*/
int _tmain
(
	  int argc
	, _TCHAR* argv[]
)
{
	/*
		std::wcoutのロケールを設定
		 これを設定するだけで、std::wcoutで日本語が表示される
		 ようになります。
	*/
	std::wcout.imbue( std::locale( "", std::locale::ctype ) );

	// 変換対象の文字列
	std::string strText = "QkFTRTY0gvCDZoNSgVuDaIK1gtyCtYK9gUI=";

	// BASE64デコードを行います。
	std::string strResult = Base64Decode( strText );

	// 変換結果を表示
	std::cout << strResult.c_str() << std::endl;

	// 正常終了
	return( 0 );
}

実行結果

BASE64をデコードしました。






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。