支援中文的BASE64 編解碼

來源:互聯網
上載者:User
支援中文的BASE64 編解碼

(原創)昨天為了測試我的終端,自己寫了個類比伺服器,用到BASE64 編解碼,已用了GoAhead的代碼,發現不支援中文,自己改造一下,解決了問題。現在把代碼發上來給大家借鑒。



/*


* base64.c -- Base64 Mime encoding


*


* Copyright (c) GoAhead Software Inc., 1995-2000. All Rights Reserved.


*


* See the file "license.txt" for usage and redistribution license requirements


*


* $Id: base64.c,v 1.1 2005/08/12 06:50:14 archfree Exp $


*/

/******************************** Description *********************************/

/*


* The base64 command encodes and decodes a string in mime base64 format


*/


/******************************** Local Data **********************************/


/*


* Mapping of ANSI chars to base64 Mime encoding alphabet (see below)


*/



#if 0


static char map64[] = {


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,


-1, 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, -1, -1, -1, -1, -1,


-1, 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, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


};


#endif


static char map64[] = {


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,


52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1,


-1, 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, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1, -1, -1,


-1, -1,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,


-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};

static char alphabet64[] = {


'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',


'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',


'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',


'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',


'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',


'o', 'p', 'q', 'r', 's', 't', 'u', 'v',


'w', 'x', 'y', 'z', '0', '1', '2', '3',


'4', '5', '6', '7', '8', '9', '+', '/',


};

/*********************************** Code *************************************/


/*


* Decode a buffer from "string" and into "outbuf"


*/

int websDecode64(char *outbuf, char *string, int outlen)


{


unsigned long shiftbuf;


char *cp, *op;


int c, i, j, shift;

op = outbuf;


*op = '/0';


cp = string;


while (*cp && *cp != '=') {


/*


* Map 4 (6bit) input bytes and store in a single long (shiftbuf)


*/


shiftbuf = 0;


shift = 18;


for (i = 0; i
c = map64[*cp & 0xff];


if (c == -1) {


#if zwhtemp


error(E_L, E_LOG, T("Bad string: %s at %c index %d"), string,


c, i);


#endif


return -1;


}


shiftbuf = shiftbuf | (c
shift -= 6;


}


/*


* Interpret as 3 normal 8 bit bytes (fill in reverse order).


* Check for potential buffer overflow before filling.


*/


--i;


if ((op + i) >= &outbuf[outlen]) {


//gstrcpy(outbuf, T("String too big"));]


//strcpy(outbuf, T("String too big"));


strcpy(outbuf, "String too big");

return -1;


}


for (j = 0; j
*op++ = (char) ((shiftbuf >> (8 * (2 - j))) & 0xff);


}


*op = '/0';


}


return 0;


}



/******************************************************************************/


/*


* Encode a buffer from "string" into "outbuf"


*/

void websEncode64(char *outbuf, char *string, int outlen)


{


unsigned long shiftbuf;


char *cp, *op;


int x, i, j, shift;

op = outbuf;


*op = '/0';


cp = string;


while (*cp) {


/*


* Take three characters and create a 24 bit number in shiftbuf


*/


shiftbuf = 0;


for (j = 2; j >= 0 && *cp; j--, cp++) {


shiftbuf |= ((*cp & 0xff)
}


/*


* Now convert shiftbuf to 4 base64 letters. The i,j magic calculates


* how many letters need to be output.


*/


shift = 18;


for (i = ++j; i
x = (shiftbuf >> shift) & 0x3f;


*op++ = alphabet64[(shiftbuf >> shift) & 0x3f];


shift -= 6;


}


/*


* Pad at the end with '='


*/


while (j-- > 0) {


*op++ = '=';


}


*op = '/0';


}


}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.