Copy Highlighter-hljs
| void base64_encode_s(const unsigned char *str, long inlen, std::string& outstr, long* lpBufLen) |
| { |
| long len; |
| long str_len; |
| |
| |
| int i,j; |
| |
| const unsigned char *base64_table = (const unsigned char *)("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); |
| |
| |
| str_len= inlen; |
| if(str_len % 3 == 0) |
| len=str_len/3*4; |
| else |
| len=(str_len/3+1)*4; |
| |
| if (NULL != lpBufLen) { |
| *lpBufLen = len; |
| } |
| |
| |
| outstr.resize(sizeof(unsigned char)*len+1); |
| outstr[len] = '\0'; |
| |
| |
| for(i=0,j=0;i<len-2;j+=3,i+=4) |
| { |
| outstr[i] = base64_table[str[j]>>2]; |
| outstr[i+1] = base64_table[(str[j]&0x3)<<4 | (str[j+1]>>4)]; |
| outstr[i+2] = base64_table[(str[j+1]&0xf)<<2 | (str[j+2]>>6)]; |
| outstr[i+3] = base64_table[str[j+2]&0x3f]; |
| } |
| |
| switch(str_len % 3) |
| { |
| case 1: |
| outstr[i-2]='='; |
| outstr[i-1]='='; |
| break; |
| case 2: |
| outstr[i-1]='='; |
| break; |
| } |
| |
| return; |
| } |
| |
| |
| void base64_decode_s(const unsigned char *code, std::string& outstr, long* lpBufLen) |
| { |
| |
| int table[]={0,0,0,0,0,0,0,0,0,0,0,0, |
| 0,0,0,0,0,0,0,0,0,0,0,0, |
| 0,0,0,0,0,0,0,0,0,0,0,0, |
| 0,0,0,0,0,0,0,62,0,0,0, |
| 63,52,53,54,55,56,57,58, |
| 59,60,61,0,0,0,0,0,0,0,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,0,0,0,0,0,0,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 |
| }; |
| long len; |
| long str_len; |
| |
| int i,j; |
| |
| |
| len = strlen((const char*)code); |
| |
| if(strstr((const char*)code,"==")) |
| str_len=len/4*3-2; |
| else if(strstr((const char*)code,"=")) |
| str_len=len/4*3-1; |
| else |
| str_len=len/4*3; |
| |
| if (NULL != lpBufLen) { |
| *lpBufLen = str_len; |
| } |
| |
| |
| outstr.resize(sizeof(unsigned char)*len+1); |
| |
| |
| |
| |
| for(i=0,j=0;i < len-2;j+=3,i+=4) |
| { |
| outstr[j]=((unsigned char)table[code[i]])<<2 | (((unsigned char)table[code[i+1]])>>4); |
| outstr[j+1]=(((unsigned char)table[code[i+1]])<<4) | (((unsigned char)table[code[i+2]])>>2); |
| outstr[j+2]=(((unsigned char)table[code[i+2]])<<6) | ((unsigned char)table[code[i+3]]); |
| } |
| |
| return; |
| } |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步