算法 Base64编解码

头文件声明(Base64.h):

#pragma once

#include "stdafx.h"
#include <windows.h>
 
#ifdef  __cplusplus
extern "C" {
#endif
 
/*
功能:将二进制数据转换成BASE64编码字符串
参数说明:
  pInputBuffer:要编码的二进制数据
  nInputCount:数据长度
  pOutputBuffer:存储转换后的BASE64编码字符串,为NULL返回转换后字符串长度
返回值:
   -1:参数错误
  >=0:有效编码长度(字符数),不包括字符串结束符。
*/
INT BASE64_Encode( const BYTE* pInputBuffer, INT nInputCount, TCHAR* pOutputBuffer );
 
/*
功能:将BASE64编码字符串解码转换为二进制数据
参数说明:
  pInputBuffer:BASE64编码字符串
  nInputCount:编码长度(字符数),应该为4的倍数。
  pOutputBuffer:存储转换后的二进制数据,为NULL返回转换后字符串长度
返回值:
   -1:参数错误
   -2:数据错误
  >=0:转换后的字节数
*/
INT BASE64_Decode( const TCHAR* pInputBuffer, INT nInputCount, BYTE* pOutputBuffer );
 
#ifdef __cplusplus
}
#endif

源码实现(Base64.cpp):

#include "stdafx.h"
#include "BASE64_API.h"
 
static const CHAR* ENCODE_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
INT BASE64_Encode( const BYTE* pInputBuffer, INT nInputCount, TCHAR* pOutputBuffer )
{
  INT i;
  BYTE b0, b1, b2;
 
  if( (pInputBuffer == NULL) || (nInputCount < 0) )
  {
    return -1;  // 参数错误
  }
 
  if( pOutputBuffer != NULL )
  {
    for( i = nInputCount; i > 0; i -= 3 )
    {
      if( i >= 3 )
      {  // 将3字节数据转换成4个ASCII字符
        b0 = *pInputBuffer++;
        b1 = *pInputBuffer++;
        b2 = *pInputBuffer++;
 
        *pOutputBuffer++ = ENCODE_TABLE[b0 >> 2];
        *pOutputBuffer++ = ENCODE_TABLE[((b0 << 4) | (b1 >> 4)) & 0x3F];
        *pOutputBuffer++ = ENCODE_TABLE[((b1 << 2) | (b2 >> 6)) & 0x3F];
        *pOutputBuffer++ = ENCODE_TABLE[b2 & 0x3F];
      }
      else
      {
        b0 = *pInputBuffer++;
        if( i == 2 )b1 = *pInputBuffer++; else b1 = 0;
 
        *pOutputBuffer++ = ENCODE_TABLE[b0 >> 2];
        *pOutputBuffer++ = ENCODE_TABLE[((b0 << 4) | (b1 >> 4)) & 0x3F];
        *pOutputBuffer++ = (i == 1) ? TEXT('=') : ENCODE_TABLE[(b1 << 2) & 0x3F];
        *pOutputBuffer++ = TEXT('=');
      }
    } // End for i
 
    *pOutputBuffer++ = TEXT('/0');  // 添加字符串结束标记
  }
 
  return ((nInputCount + 2) / 3) * 4;  // 返回有效字符个数
}
 
#define B64_EOLN   0xF0  // 换行/n
#define B64_CR     0xF1  // 回车/r
#define B64_EOF    0xF2  // 连字符-
#define B64_WS     0xE0  // 跳格或者空格(/t、space)
#define B64_ERROR  0xFF  // 错误字符
#define B64_NOT_BASE64(a)  (((a)|0x13) == 0xF3)
 
static const BYTE DECODE_TABLE[128] = {
  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE0,0xF0,0xFF,0xFF,0xF1,0xFF,0xFF,
  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  0xE0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x3E,0xFF,0xF2,0xFF,0x3F,
  0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0xFF,0xFF,0xFF,0x00,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
};
 
INT BASE64_Decode( const TCHAR* pInputBuffer, INT nInputCount, BYTE* pOutputBuffer )
{
  INT i, j;
  BYTE b[4];
  TCHAR ch;
 
  if( (pInputBuffer == NULL) || (nInputCount < 0) )
  {
    return -1;  // 参数错误
  }
 
  // 去除头部空白字符
  while( nInputCount > 0 )
  {
    ch = *pInputBuffer;
    if( (ch < 0) || (ch >= 0x80) )
    {
      return -2;  // 数据错误,不在ASCII字符编码范围内
    }
    else
    {
      if( DECODE_TABLE[ch] == B64_WS )
      {
        pInputBuffer++;
        nInputCount--;
      }
      else
      {
        break;
      }
    }
  }
 
  // 去除尾部的空白字符、回车换行字符、连字符
  while( nInputCount >= 4 )
  {
    ch = pInputBuffer[nInputCount - 1];
    if( (ch < 0) || (ch >= 0x80) )
    {
      return -2;  // 数据错误,不在ASCII字符编码范围内
    }
    else
    {
      if( B64_NOT_BASE64(DECODE_TABLE[ch]) )
      {
        nInputCount--;
      }
      else
      {
        break;
      }
    }
  }
 
  // 字符串长度必须为4的倍数
  if( (nInputCount % 4) != 0 )
  {
    return -2;  // 数据错误
  }
 
  if( pOutputBuffer != NULL )
  {
    for( i = 0; i < nInputCount; i += 4 )
    {
      for( j = 0; j < 4; j++ )
      {
        ch = *pInputBuffer++;
        if( (ch < 0) || (ch >= 0x80) )
        {
          return -2;  // 数据错误,不在ASCII字符编码范围内
        }
        else
        {
          if( ch == '=' )  // 发现BASE64编码中的填充字符
          {
            break;
          }
          else
          {
            b[j] = DECODE_TABLE[ch];
            if( b[j] & 0x80 )
            {
              return -2;  // 数据错误,无效的Base64编码字符
            }
          }          
        }
      } // End for j
 
      if( j == 4 )
      {
        *pOutputBuffer++ = (b[0] << 2) | (b[1] >> 4);
        *pOutputBuffer++ = (b[1] << 4) | (b[2] >> 2 );
        *pOutputBuffer++ = (b[2] << 6) | b[3];
      }
      else if( j == 3 )
      {  // 有1个填充字节
        *pOutputBuffer++ = (b[0] << 2) | (b[1] >> 4);
        *pOutputBuffer++ = (b[1] << 4) | (b[2] >> 2 );
 
        return (i >> 2) * 3 + 2;
      }
      else if( j == 2 )
      {  // 有2个填充字节
        *pOutputBuffer++ = (b[0] << 2) | (b[1] >> 4);
 
        return (i >> 2) * 3 + 1;
      }
      else
      {
        return -2;  // 数据错误,无效的Base64编码字符
      }      
    }  // End for i
  }
 
  return (nInputCount >> 2) * 3;
}

猜你喜欢

转载自blog.csdn.net/u012156872/article/details/105900879