📄 base64.cpp
字号:
// $Archive:: /SafeTP/base64.cpp $// $Date: 1999/07/14 13:16:38 $// $Revision: 1.5 $// Description: Base64 low-level tranformation header (conforms to RFC 2228)// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt#include <string.h>#include "base64.h"#ifdef __BORLANDC__# pragma warn -amb // "ambiguous operators need parentheses", for lines with | and <<#endif // __BORLANDC__#if !defined(SAFETP_BIG_ENDIAN) && !defined(SAFETP_LITTLE_ENDIAN) #error This file needs either SAFETP_BIG_ENDIAN or SAFETP_LITTLE_ENDIAN defined#endifstatic char const encodeTable[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";int base64enc(const unsigned char* input, unsigned char* output, int inputlen) { // handle special cases if (inputlen < 0) return BASE64_ERROR; // the only possible error for encode if (inputlen == 0) { *output = '\0'; return 0; } // empty string special-case int numchunks = int((inputlen + 2) / 3); // + 2 is for round-up: number of chunks to take int outputlen = numchunks * 4; // total buffer finished length (not including null) int lastchunksize = inputlen - ((numchunks - 1) * 3); const unsigned char *inputpos = input + inputlen; unsigned char* outputpos = output + outputlen; *outputpos = '\0'; // do null-termination now unsigned long chunk; if (lastchunksize == 1) { inputpos -= 1; outputpos -= 4; chunk = inputpos[0] << 4; outputpos[3] = '='; outputpos[2] = '='; outputpos[1] = encodeTable[chunk & 0x3F]; outputpos[0] = encodeTable[(chunk >> 6) & 0x3F]; } else if (lastchunksize == 2) { inputpos -= 2; outputpos -= 4; chunk = inputpos[0] << 10 | inputpos[1] << 2; outputpos[3] = '='; outputpos[2] = encodeTable[chunk & 0x3F]; outputpos[1] = encodeTable[(chunk >> 6) & 0x3F]; outputpos[0] = encodeTable[(chunk >> 12) & 0x3F]; } while (outputpos != output) { inputpos -= 3; outputpos -= 4; chunk = inputpos[0] << 16 | inputpos[1] << 8 | inputpos[2]; outputpos[3] = encodeTable[chunk & 0x3F]; outputpos[2] = encodeTable[(chunk >> 6) & 0x3F]; outputpos[1] = encodeTable[(chunk >> 12) & 0x3F]; outputpos[0] = encodeTable[(chunk >> 18) & 0x3F]; } return outputlen; // returns size of output }// "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";// 0123456789012345678901234567890123456789012345678901234567890123static unsigned char const decodeTable[256] = { '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0', '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0', '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\x3E','\0','\0','\0','\x3F', '\x34','\x35','\x36','\x37','\x38','\x39','\x3A','\x3B','\x3C','\x3D','\0','\0','\0','\0','\0','\0', '\0','\x0','\x1','\x2','\x3','\x4','\x5','\x6','\x7','\x8','\x9','\xA','\xB','\xC','\xD','\xE', '\xF','\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\0','\0','\0','\0','\0', '\0','\x1A','\x1B','\x1C','\x1D','\x1E','\x1F','\x20','\x21','\x22','\x23','\x24','\x25','\x26','\x27','\x28', '\x29','\x2A','\x2B','\x2C','\x2D','\x2E','\x2F','\x30','\x31','\x32','\x33','\0','\0','\0','\0','\0', '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0', '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0', '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0', '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0', '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0', '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0', '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0', '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0' };static inline int isspace(unsigned char c) { return (c == ' ' || c == '\xD' || c == '\xA' || c == '\x9' || c == '\xC'); // space CR LF tab new page }int base64dec(const unsigned char* input, unsigned char* output) { // returns size of output or BASE64_ERROR if translation fails (damaged base64 encoding) const unsigned char *inputpos=input; unsigned char *outputpos=output; unsigned long dest; #ifdef SAFETP_LITTLE_ENDIAN unsigned char* pdest = (unsigned char*)&dest; #else // for big endian (and sizeof(long) > 4), we need a pointer to // the byte representing the 256^3 digit of sigificance; this // is the 4th byte from the right (right being addresses furthest // from 0) unsigned char* pdest = ((unsigned char*)(&dest + 1)) - 4; #endif unsigned char tmp; int lengthadjust=0; while(1) { while (*inputpos && isspace(*inputpos)) inputpos++; // skip whitespace if (!*inputpos) break; // end of string if (lengthadjust < 0) { *output = '\0'; return BASE64_ERROR; } // stuff after == tmp = decodeTable[*inputpos]; if (!tmp && *inputpos != 'A') { *output = '\0'; return BASE64_ERROR; } // illegal character dest = tmp; inputpos++; while (*inputpos && isspace(*inputpos)) inputpos++; // skip whitespace if (!*inputpos) { *output = '\0'; return BASE64_ERROR; } // unexpected end of string tmp = decodeTable[*inputpos]; if (!tmp && *inputpos != 'A') { *output = '\0'; return BASE64_ERROR; } // illegal character dest = (dest << 6) | tmp; inputpos++; while (*inputpos && isspace(*inputpos)) inputpos++; // skip whitespace if (!*inputpos) { *output = '\0'; return BASE64_ERROR; } // unexpected end of string tmp = decodeTable[*inputpos]; if (!tmp && *inputpos != 'A') { // illegal character or first of two '=' if (*inputpos == '=') lengthadjust--; else { *output = '\0'; return BASE64_ERROR; } } dest = (dest << 6) | tmp; inputpos++; while (*inputpos && isspace(*inputpos)) inputpos++; // skip whitespace if (!*inputpos) { *output = '\0'; return BASE64_ERROR; } // unexpected end of string tmp = decodeTable[*inputpos]; if (!tmp && *inputpos != 'A') { // illegal character or second '=' if (*inputpos == '=') lengthadjust--; else { *output = '\0'; return BASE64_ERROR; } } else if (lengthadjust < 0) { *output = '\0'; return BASE64_ERROR; } // first '=' not followed by second dest = (dest << 6) | tmp; inputpos++;# ifdef SAFETP_LITTLE_ENDIAN // little endian outputpos[0] = pdest[2]; // first byte in MSB outputpos[1] = pdest[1]; outputpos[2] = pdest[0]; // last byte in LSB# else // big endian outputpos[0] = pdest[1]; // first byte in MSB outputpos[1] = pdest[2]; outputpos[2] = pdest[3]; // last byte in LSB# endif // !SAFETP_LITTLE_ENDIAN outputpos += 3; } int length = outputpos - output + lengthadjust; *outputpos = '\0'; return length; }//--------------------------------------------------------------------------// This is the working code for non-overlapping buffers//--------------------------------------------------------------------------/*int base64enc(const unsigned char* input, unsigned char* output, int inputlen) { // returns size of output const unsigned char *inputpos=input; unsigned char *outputpos=output; const unsigned char *inputend=input+inputlen; if (inputend == input) return 0; // empty buffer while(1) { if (inputpos == inputend) break; else if (inputpos + 1 == inputend) { // handle 8 bit left-over unsigned long chunk = inputpos[0] << 4; outputpos[3] = '='; outputpos[2] = '='; outputpos[1] = encodeTable[chunk & 0x3F]; outputpos[0] = encodeTable[(chunk >> 6) & 0x3F]; inputpos = inputpos + 1; outputpos = outputpos + 4; break; } else if (inputpos + 2 == inputend) { // handle 16 bit left-over unsigned long chunk = inputpos[0] << 10 | inputpos[1] << 2; outputpos[3] = '='; outputpos[2] = encodeTable[chunk & 0x3F]; outputpos[1] = encodeTable[(chunk >> 6) & 0x3F]; outputpos[0] = encodeTable[(chunk >> 12) & 0x3F]; inputpos = inputpos + 2; outputpos = outputpos + 4; break; } else { // we have at least one 24 bit chunk left unsigned long chunk = inputpos[0] << 16 | inputpos[1] << 8 | inputpos[2]; outputpos[3] = encodeTable[chunk & 0x3F]; outputpos[2] = encodeTable[(chunk >> 6) & 0x3F]; outputpos[1] = encodeTable[(chunk >> 12) & 0x3F]; outputpos[0] = encodeTable[(chunk >> 18) & 0x3F]; inputpos = inputpos + 3; outputpos = outputpos + 4; } } *outputpos = '\0'; return outputpos-output; }*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -