2017-08-17 86 views
-3

我有這樣的代碼:爲什麼我的函數從base64解碼失敗?

static char Base64Digits[] = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/"; 

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: >[email protected] 
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 ::字符串和無符號的字符向量,而不是指針。

編碼完美:我一直在使用在線編碼器和解碼器進行檢查。解碼每次都會返回一個空向量。爲什麼是這樣?

我打電話使用此代碼是:

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; 

正確轉化爲YQ ==,但不回。

回答

2

您需要通過引用vec傳遞:

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

否則你和一個本地副本的工作並沒有什麼回來給調用者。

1

結果vecror通過複製傳遞給解碼函數,所以它在函數中被修改,但在調用者站點中沒有實際參數。 將其作爲參考向量傳遞:std::vector<unsigned char>&