00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00036 #include <string.h>
00037
00038 #include "wbxml.h"
00039
00040
00041
00042 static const unsigned char pr2six[256] =
00043 {
00044
00045 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00046 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00047 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
00048 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
00049 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
00050 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
00051 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
00052 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
00053 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00054 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00055 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00056 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00057 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00058 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00059 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00060 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
00061 };
00062
00064 static const char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00065
00066
00067
00068
00069
00070
00071
00072 WBXML_DECLARE(WB_UTINY *) wbxml_base64_encode(const WB_UTINY *buffer, WB_LONG len)
00073 {
00074 WB_LONG i = 0;
00075 WB_UTINY *p = NULL, *result = NULL;
00076
00077 if ((buffer == NULL) || (len <= 0))
00078 return NULL;
00079
00080
00081 if ((result = (WB_UTINY *) wbxml_malloc(((len + 2) / 3 * 4) + 1 + 1)) == NULL)
00082 return NULL;
00083
00084 p = result;
00085 for (i = 0; i < len - 2; i += 3) {
00086 *p++ = basis_64[(buffer[i] >> 2) & 0x3F];
00087 *p++ = basis_64[((buffer[i] & 0x3) << 4) |
00088 ((int) (buffer[i + 1] & 0xF0) >> 4)];
00089 *p++ = basis_64[((buffer[i + 1] & 0xF) << 2) |
00090 ((int) (buffer[i + 2] & 0xC0) >> 6)];
00091 *p++ = basis_64[buffer[i + 2] & 0x3F];
00092 }
00093 if (i < len) {
00094 *p++ = basis_64[(buffer[i] >> 2) & 0x3F];
00095 if (i == (len - 1)) {
00096 *p++ = basis_64[((buffer[i] & 0x3) << 4)];
00097 *p++ = '=';
00098 }
00099 else {
00100 *p++ = basis_64[((buffer[i] & 0x3) << 4) |
00101 ((int) (buffer[i + 1] & 0xF0) >> 4)];
00102 *p++ = basis_64[((buffer[i + 1] & 0xF) << 2)];
00103 }
00104 *p++ = '=';
00105 }
00106
00107 *p++ = '\0';
00108
00109 return result;
00110 }
00111
00112
00113
00114 WBXML_DECLARE(WB_LONG) wbxml_base64_decode(const WB_UTINY *buffer, WB_UTINY **result)
00115 {
00116 WB_LONG nbytesdecoded = 0, nprbytes = 0;
00117 const WB_UTINY *bufin = NULL;
00118 WB_UTINY *bufout = NULL;
00119
00120 if ((buffer == NULL) || (result == NULL))
00121 return 0;
00122
00123
00124 *result = NULL;
00125
00126 bufin = buffer;
00127 while (pr2six[*(bufin++)] <= 63);
00128
00129 nprbytes = (bufin - buffer) - 1;
00130 nbytesdecoded = ((nprbytes + 3) / 4) * 3;
00131
00132
00133 if ((*result = (WB_UTINY*) wbxml_malloc(nbytesdecoded + 1)) == NULL)
00134 return 0;
00135
00136 bufout = *result;
00137 bufin = buffer;
00138
00139 while (nprbytes > 4)
00140 {
00141 *(bufout++) = (WB_UTINY) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
00142 *(bufout++) = (WB_UTINY) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
00143 *(bufout++) = (WB_UTINY) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
00144 bufin += 4;
00145 nprbytes -= 4;
00146 }
00147
00148
00149 if (nprbytes > 1) {
00150 *(bufout++) = (WB_UTINY) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
00151 }
00152 if (nprbytes > 2) {
00153 *(bufout++) = (WB_UTINY) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
00154 }
00155 if (nprbytes > 3) {
00156 *(bufout++) = (WB_UTINY) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
00157 }
00158
00159 nbytesdecoded -= (4 - nprbytes) & 3;
00160
00161 return nbytesdecoded;
00162 }