Linux企業級專案實踐之網路爬蟲(16)——使用base64傳輸二進位制資料

尹成發表於2014-08-31
用http傳輸二進位制的資料時,需要將二進位制做一下轉化,例如傳輸的int型別,將int型別之間轉為char以後,丟失掉了長度的資訊,如數字123456,本來只有4個位元組,但是轉化成文字的“123456”是有7個位元組。在int型別的時候固然好辦,但是一個陣列的時候,經過轉化以後,在轉化回來就很麻煩了。
同時對於一些數字,二進位制傳輸Server是沒法處理的。如int 1,二進位制資料是0x00000001,按位元組傳輸的時候,client能夠正常傳送,但是libevent收到以後,在拋給libevent_http層是,會把資料截斷,前兩位0x00是字串的停止符。
所以,只能用base64來解決這個問題。base64將二進位制的內容轉化成一組有意義的字串,然後傳輸,server在decode。



long int encode( char *src,long int src_len, char *dst)  
       
{  
           
    long int i = 0, j = 0;  
           
    char base64_map[65] = "BADCFEGHIJKLMNOPQRSTUVWXYZbadcfeghijklmnopqrstuvwxyz0123456789+/";  
           
    for (; i < src_len - src_len % 3; i += 3) {  
           
        dst[j++] = base64_map[(src[i] >> 2) & 0x3F];  
           
        dst[j++] = base64_map[((src[i] << 4) & 0x30) + ((src[i + 1] >> 4) & 0xF)];  
       
        dst[j++] = base64_map[((src[i + 1] << 2) & 0x3C) + ((src[i + 2] >> 6) & 0x3)];  
       
        dst[j++] = base64_map[src[i + 2] & 0x3F];  
    }  
           
    if (src_len % 3 == 1) {  
           
        dst[j++] = base64_map[(src[i] >> 2) & 0x3F];  
           
        dst[j++] = base64_map[(src[i] << 4) & 0x30];  
           
        dst[j++] = '=';  
           
        dst[j++] = '=';  
           
    }else if (src_len % 3 == 2) {  
           
        dst[j++] = base64_map[(src[i] >> 2) & 0x3F];  
           
        dst[j++] = base64_map[((src[i] << 4) & 0x30) + ((src[i + 1] >> 4) & 0xF)];  
           
        dst[j++] = base64_map[(src[i + 1] << 2) & 0x3C];  
           
        dst[j++] = '=';  
           
    }  
       
    dst[j] = '/0';  
    printf("encode length:%ld/n",j);  
    return j;  
           
}   
long int decode(char *src, long int src_len, char *dst)  
   
{  
       
    long    int i = 0, j = 0;  
       
    unsigned char base64_decode_map[256] = {  
           
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  
               
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  
               
            255, 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,  
               
            255, 0, 255, 255, 255, 1, 0, 3, 2, 5, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14,  
               
            15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 27, 26, 29,  
               
            28, 31, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,  
               
            49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  
               
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  
               
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  
               
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  
               
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  
               
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  
               
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};  
           
           
       
    for (; i < src_len; i += 4) {  
           
        dst[j++] = base64_decode_map[src[i]] << 2 |  
               
            base64_decode_map[src[i + 1]] >> 4;  
           
        dst[j++] = base64_decode_map[src[i + 1]] << 4 |  
               
            base64_decode_map[src[i + 2]] >> 2;  
           
        dst[j++] = base64_decode_map[src[i + 2]] << 6 |  
               
            base64_decode_map[src[i + 3]];  
           
    }  
    dst[j] = '/0';  
    printf("decode length :%ld/n",j);  
    return j;  
               
}  


相關文章