- 论坛徽章:
- 0
|
- /***********************************************************************
- Mady By ZwelL
- 2004.8.16
- zwell@sohu.com
- http://www.donews.net/zwell
- Usage: base.exe <-e|-d> <string>
- ***********************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- const int BASE64_MAXLINE = 76;
- const char EOL[] = "\r\n";
- const char BASE64_TAB[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- const char DeBase64Tab[] =
- {
- 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' - '9'
- 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, //'A' - 'Z'
- 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, //'a' - 'z'
- };
- char *
- enBase64(const char *szInANSI)
- {
- int nInLen = strlen(szInANSI);
- char *szOutBase64;
- char *returnstr;
- //Set up the parameters prior to the main encoding loop
- int nInPos = 0;
- int nOutPos = 0;
- int nLineLen = 0;
- int c1, c2, c3;
- int i;
- szOutBase64 = (char *)malloc(nInLen * 4 / 3 + 1);
- returnstr = szOutBase64;
- memset(szOutBase64, 0, nInLen * 4 / 3 + 1);
- //Input Parameter validation
- if ((szInANSI == NULL) || (nInLen == 0) || (szOutBase64 == NULL))
- return 0;
- //Get three characters at a time from the input buffer and encode them
- for (i = 0; i < nInLen / 3; ++i) {
- //Get the next 2 characters
- c1 = szInANSI[nInPos++] & 0xFF;
- c2 = szInANSI[nInPos++] & 0xFF;
- c3 = szInANSI[nInPos++] & 0xFF;
- //Encode into the 4 6 bit characters
- szOutBase64[nOutPos++] = BASE64_TAB[c1 >> 2];
- szOutBase64[nOutPos++] = BASE64_TAB[((c1 << 4) | (c2 >> 4)) & 0x3F];
- szOutBase64[nOutPos++] = BASE64_TAB[((c2 << 2) | (c3 >> 6)) & 0x3F];
- szOutBase64[nOutPos++] = BASE64_TAB[c3 & 0x3F];
- nLineLen += 4;
- //Handle the case where we have gone over the max line boundary
- if (nLineLen > BASE64_MAXLINE - 4) {
- szOutBase64[nOutPos++] = EOL[0];
- szOutBase64[nOutPos++] = EOL[1];
- nLineLen = 0;
- }
- }
- //Encode the remaining one or two characters in the input buffer
- switch (nInLen % 3) {
- case 0: {
- szOutBase64[nOutPos++] = EOL[0];
- szOutBase64[nOutPos++] = EOL[1];
- break;
- }
- case 1: {
- c1 = szInANSI[nInPos] & 0xFF;
- szOutBase64[nOutPos++] = BASE64_TAB[(c1 & 0xFC) >> 2];
- szOutBase64[nOutPos++] = BASE64_TAB[((c1 & 0x03) << 4)];
- szOutBase64[nOutPos++] = '=';
- szOutBase64[nOutPos++] = '=';
- szOutBase64[nOutPos++] = EOL[0];
- szOutBase64[nOutPos++] = EOL[1];
- break;
- }
- case 2: {
- c1 = szInANSI[nInPos++] & 0xFF;
- c2 = szInANSI[nInPos] & 0xFF;
- szOutBase64[nOutPos++] = BASE64_TAB[(c1 & 0xFC) >> 2];
- szOutBase64[nOutPos++] = BASE64_TAB[((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4)];
- szOutBase64[nOutPos++] = BASE64_TAB[((c2 & 0x0F) << 2)];
- szOutBase64[nOutPos++] = '=';
- szOutBase64[nOutPos++] = EOL[0];
- szOutBase64[nOutPos++] = EOL[1];
- break;
- }
- default:
- {
- return 0;
- }
- }
- szOutBase64[nOutPos] = 0;
- return returnstr;
- }
- char *
- deBase64(const char *pSrc)
- {
- int nDstLen;
- //Output string 's length
- int nValue;
- //decode used length
- int i;
- int nSrcLen;
- unsigned char *pDst;
- char *ptmpDst;
- i = 0;
- nDstLen = 0;
- nSrcLen = strlen(pSrc);
- pDst = (unsigned char *)malloc(nSrcLen * 3 / 4 + 1);
- ptmpDst = (char *)pDst;
- memset(pDst, 0, nSrcLen * 3 / 4 + 1);
- //Get four characters at a time from the input buffer and decode them
- while (i < nSrcLen) {
- if (*pSrc != '\r' && *pSrc != '\n') {
- nValue = DeBase64Tab[*pSrc++] << 18;
- nValue += DeBase64Tab[*pSrc++] << 12;
- *pDst++ = (nValue & 0x00ff0000) >> 16;
- nDstLen++;
- if (*pSrc != '=') {
- nValue += DeBase64Tab[*pSrc++] << 6;
- *pDst++ = (nValue & 0x0000ff00) >> 8;
- nDstLen++;
- if (*pSrc != '=') {
- nValue += DeBase64Tab[*pSrc++];
- *pDst++ = nValue & 0x000000ff;
- nDstLen++;
- }
- }
- i += 4;
- } else
- //CR LF not care
- {
- pSrc++;
- i++;
- }
- }
- //the end of the output string
- * pDst = '\0';
- return ptmpDst;
- }
- void
- Usage(char *exename)
- {
- printf("Usage:%s -e|-d string\r\n", exename);
- }
- int
- main(int argc, char **argv)
- {
- if (argc < 3) {
- Usage(argv[0]);
- return -1;
- }
- if (!strcmp(argv[1], "-e")) {
- printf(enBase64(argv[2]));
- printf("\n");
- } else if (!strcmp(argv[1], "-d")) {
- printf(deBase64(argv[2]));
- printf("\n");
- } else {
- Usage(argv[0]);
- return -1;
- }
- return 0;
- }
复制代码 |
|