Linux企业级项目实践之网络爬虫(16)——使用base64传输二进制数据

用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;  
               
}  


posted on 2014-08-31 23:02  三少爷的剑123  阅读(393)  评论(0编辑  收藏  举报

导航