⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 base64decoder.cpp

📁 .net 方面的开发说明资料。
💻 CPP
字号:
// =============================================================
// BASE64 Decoder
//
// Purpose: Decodes a BASE64 text stream to a byte array.
//          If out is NULL, the function returns the minimum
//          size of the target buffer.
//
// This file is part of Eplug
//
// Copyright (c) 2002 - 2003 Pylon Software
// =============================================================

#include <windows.h>

// --------------------------------------------------------

static BYTE base64[] =
{ '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', '+', '/' };

// --------------------------------------------------------

inline BYTE
EncodeByte(BYTE a) {
	return base64[a];
}

inline BYTE
DecodeByte(BYTE a) {
	if ((a >= 'A') && (a <= 'Z'))
		return a - 'A';

	if ((a >= 'a') && (a <= 'z'))
		return 26 + a - 'a';

	if ((a >= '0') && (a <= '9'))
		return 52 + a - '0';

	if (a == '+')
		return 62;

	if ((a == '\\') || (a == '/'))
		return 63;

	return 0xFF;
}

inline BYTE
DecodeReadByte(const char *in, int length, int &enter_count) {
	while ((in[enter_count] == 0x0D) || (in[enter_count] == 0x0A))
		++enter_count;

	return in[enter_count++];
}

// --------------------------------------------------------

int
DecodeFileBase64(unsigned char *out, const char *in) {
	// Step 1 : find the size of the source file

	int source_size = strlen(in);
	int j = 0;
	int i = 0;
	BYTE value[4];

	// Step 2: decode data stream
	// The encoded output stream must be represented in lines of no more
	// than 76 characters each.

	if (out == NULL) {
		while (j < source_size) {
			// read next 4 values

			value[0] = DecodeReadByte(in, source_size, j);
			value[1] = DecodeReadByte(in, source_size, j);
			value[2] = DecodeReadByte(in, source_size, j);
			value[3] = DecodeReadByte(in, source_size, j);

			// decode values

			if (value[2] == '=') {
				return i + 1;
			} else if (value[3] == '=') {
				return i + 2;
			} else {
				i += 3;
			}
		}
	} else {
		while (j < source_size) {
			// read next 4 values

			value[0] = DecodeReadByte(in, source_size, j);
			value[1] = DecodeReadByte(in, source_size, j);
			value[2] = DecodeReadByte(in, source_size, j);
			value[3] = DecodeReadByte(in, source_size, j);

			// decode values

			if (value[2] == '=') {
				BYTE v1 = DecodeByte(value[0]);
				BYTE v2 = DecodeByte(value[1]);

				DWORD chain = (v1 << 18) | (v2 << 12);

				out[i++] = ((BYTE *)&chain)[2];

				return i;
			} else if (value[3] == '=') {
				BYTE v1 = DecodeByte(value[0]);
				BYTE v2 = DecodeByte(value[1]);
				BYTE v3 = DecodeByte(value[2]);

				DWORD chain = (v1 << 18) | (v2 << 12) | (v3 << 6);

				out[i++] = ((BYTE *)&chain)[2];
				out[i++] = ((BYTE *)&chain)[1];

				return i;
			} else {
				// decode seperate values

				BYTE v1 = DecodeByte(value[0]);
				BYTE v2 = DecodeByte(value[1]);
				BYTE v3 = DecodeByte(value[2]);
				BYTE v4 = DecodeByte(value[3]);

				// chain the 4 6-bit pairs to 3 8-bit pairs

				DWORD chain = (v1 << 18) | (v2 << 12) | (v3 << 6) | (v4);

				// write the 3 bytes out

				out[i++] = ((BYTE *)&chain)[2];
				out[i++] = ((BYTE *)&chain)[1];
				out[i++] = ((BYTE *)&chain)[0];
			}
		}
	}

	return i;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -