为什么我的函数从base64解码不起作用?

时间:2017-08-17 15:24:21

标签: c++ vector encoding base64 decode

我有这段代码:

static char Base64Digits[] =
 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

std::string ToBase64Simple( const unsigned char* pSrc, int nLenSrc)
{
   std::string pDst;
   int nLenOut= 0;
   int count = 0;
   while ( nLenSrc > 0 ) {
      unsigned char s1= pSrc[0]; 
      unsigned char s2= 0; if (nLenSrc>1) s2=pSrc[1]; 
      unsigned char s3= 0; if (nLenSrc>2) s3=pSrc[2];

      unsigned int n;
      n =  s1;    
      n <<= 8;   
      n |= s2;   
      n <<= 8;   
      n |= s3;  

      unsigned char m4= n & 0x3f;  n >>= 6;
      unsigned char m3= n & 0x3f;  n >>= 6;
      unsigned char m2= n & 0x3f;  n >>= 6;
      unsigned char m1= n & 0x3f;  

      unsigned char b1 = Base64Digits[m1];
      unsigned char b2 = Base64Digits[m2];
      unsigned char b3 = Base64Digits[m3];
      unsigned char b4 = Base64Digits[m4];

      pDst.push_back(b1);
      pDst.push_back(b2);
      if ( nLenSrc >= 3 ) { 
         pDst.push_back(b3);
         pDst.push_back(b4);
      }
      if ( nLenSrc == 2 ) {
         pDst.push_back(b3);
         pDst.push_back('=');
      }
      if ( nLenSrc == 1 ) { 
         pDst.push_back('=');
         pDst.push_back('=');
      }
      pSrc    += 3;
      nLenSrc -= 3;
      nLenOut += 4;
   }
   return pDst;
}

static unsigned char LookupDigits[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //gap: ctrl chars
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //gap: ctrl chars
0,0,0,0,0,0,0,0,0,0,0,           //gap: spc,!"#$%'()*
62,                   // +
 0, 0, 0,             // gap ,-.
63,                   // /
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // 0-9
 0, 0, 0,             // gap: :;<
99,                   //  = (end padding)
 0, 0, 0,             // gap: >?@
 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,
17,18,19,20,21,22,23,24,25, // A-Z
 0, 0, 0, 0, 0, 0,    // gap: [\]^_`
26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,
43,44,45,46,47,48,49,50,51, // a-z    
 0, 0, 0, 0,          // gap: {|}~ (and the rest...)
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
};

int FromBase64Simple( std::string pSrc, std::vector<unsigned char> vec)
{
   for( int j=0; j<pSrc.size(); j+=4 ) {
      unsigned char s1= LookupDigits[pSrc.at(j)];
      unsigned char s2= LookupDigits[pSrc.at(j+1)];
      unsigned char s3= LookupDigits[pSrc.at(j+2)];
      unsigned char s4= LookupDigits[pSrc.at(j+3)];

      unsigned char d1= ((s1 & 0x3f) << 2) | ((s2 & 0x30) >> 4);
      unsigned char d2= ((s2 & 0x0f) << 4) | ((s3 & 0x3c) >> 2);
      unsigned char d3= ((s3 & 0x03) << 6) | ((s4 & 0x3f) >> 0);

      vec.push_back(d1);  
      if (s3==99) break;     
      vec.push_back(d2);  
      if (s4==99) break;  
      vec.push_back(d3);  
   }
   return 0;
}

我在网上找到并修改它以使用std :: strings和unsigned char vector而不是指针。

编码工作完美:我一直用在线编码器和解码器检查它。解码每次都返回一个空向量。这是为什么?

我使用此代码调用它:

std::vector<unsigned char> byteVec;
byteVec.push_back('a');
std::string str = ToBase64Simple(&byteVec[0], byteVec.size());
std::cout<<str<<std::endl;
std::vector<unsigned char> vec;
FromBase64Simple(str, vec);
std::cout<<vec.size()<<std::endl;

a正确转换为YQ ==,但不能回复。

2 个答案:

答案 0 :(得分:2)

您需要通过引用传递vec

int FromBase64Simple( std::string pSrc, std::vector<unsigned char> &vec)

否则,您正在使用本地副本,并且没有任何内容返回给调用者。

答案 1 :(得分:1)

结果vecror通过复制传递给解码函数,因此在函数中修改它,但不在调用者站点中修改实际参数。 将其作为对向量的引用传递:std::vector<unsigned char>&