00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "curl_setup.h"
00026 #include "urldata.h"
00027 #include "warnless.h"
00028 #include "curl_base64.h"
00029 #include "non-ascii.h"
00030
00031
00032 #include "curl_printf.h"
00033 #include "curl_memory.h"
00034 #include "memdebug.h"
00035
00036
00037 static const char base64[]=
00038 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00039
00040
00041
00042 static const char base64url[]=
00043 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
00044
00045 static size_t decodeQuantum(unsigned char *dest, const char *src)
00046 {
00047 size_t padding = 0;
00048 const char *s, *p;
00049 unsigned long i, x = 0;
00050
00051 for(i = 0, s = src; i < 4; i++, s++) {
00052 unsigned long v = 0;
00053
00054 if(*s == '=') {
00055 x = (x << 6);
00056 padding++;
00057 }
00058 else {
00059 p = base64;
00060
00061 while(*p && (*p != *s)) {
00062 v++;
00063 p++;
00064 }
00065
00066 if(*p == *s)
00067 x = (x << 6) + v;
00068 else
00069 return 0;
00070 }
00071 }
00072
00073 if(padding < 1)
00074 dest[2] = curlx_ultouc(x & 0xFFUL);
00075
00076 x >>= 8;
00077 if(padding < 2)
00078 dest[1] = curlx_ultouc(x & 0xFFUL);
00079
00080 x >>= 8;
00081 dest[0] = curlx_ultouc(x & 0xFFUL);
00082
00083 return 3 - padding;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 CURLcode Curl_base64_decode(const char *src,
00101 unsigned char **outptr, size_t *outlen)
00102 {
00103 size_t srclen = 0;
00104 size_t length = 0;
00105 size_t padding = 0;
00106 size_t i;
00107 size_t numQuantums;
00108 size_t rawlen = 0;
00109 unsigned char *pos;
00110 unsigned char *newstr;
00111
00112 *outptr = NULL;
00113 *outlen = 0;
00114 srclen = strlen(src);
00115
00116
00117 if(!srclen || srclen % 4)
00118 return CURLE_BAD_CONTENT_ENCODING;
00119
00120
00121 while((src[length] != '=') && src[length])
00122 length++;
00123
00124
00125 if(src[length] == '=') {
00126 padding++;
00127 if(src[length + 1] == '=')
00128 padding++;
00129 }
00130
00131
00132 if(length + padding != srclen)
00133 return CURLE_BAD_CONTENT_ENCODING;
00134
00135
00136 numQuantums = srclen / 4;
00137
00138
00139 rawlen = (numQuantums * 3) - padding;
00140
00141
00142 newstr = malloc(rawlen + 1);
00143 if(!newstr)
00144 return CURLE_OUT_OF_MEMORY;
00145
00146 pos = newstr;
00147
00148
00149 for(i = 0; i < numQuantums; i++) {
00150 size_t result = decodeQuantum(pos, src);
00151 if(!result) {
00152 free(newstr);
00153
00154 return CURLE_BAD_CONTENT_ENCODING;
00155 }
00156
00157 pos += result;
00158 src += 4;
00159 }
00160
00161
00162 *pos = '\0';
00163
00164
00165 *outptr = newstr;
00166 *outlen = rawlen;
00167
00168 return CURLE_OK;
00169 }
00170
00171 static CURLcode base64_encode(const char *table64,
00172 struct Curl_easy *data,
00173 const char *inputbuff, size_t insize,
00174 char **outptr, size_t *outlen)
00175 {
00176 CURLcode result;
00177 unsigned char ibuf[3];
00178 unsigned char obuf[4];
00179 int i;
00180 int inputparts;
00181 char *output;
00182 char *base64data;
00183 char *convbuf = NULL;
00184
00185 const char *indata = inputbuff;
00186
00187 *outptr = NULL;
00188 *outlen = 0;
00189
00190 if(!insize)
00191 insize = strlen(indata);
00192
00193 #if SIZEOF_SIZE_T == 4
00194 if(insize > UINT_MAX/4)
00195 return CURLE_OUT_OF_MEMORY;
00196 #endif
00197
00198 base64data = output = malloc(insize * 4 / 3 + 4);
00199 if(!output)
00200 return CURLE_OUT_OF_MEMORY;
00201
00202
00203
00204
00205
00206
00207 result = Curl_convert_clone(data, indata, insize, &convbuf);
00208 if(result) {
00209 free(output);
00210 return result;
00211 }
00212
00213 if(convbuf)
00214 indata = (char *)convbuf;
00215
00216 while(insize > 0) {
00217 for(i = inputparts = 0; i < 3; i++) {
00218 if(insize > 0) {
00219 inputparts++;
00220 ibuf[i] = (unsigned char) *indata;
00221 indata++;
00222 insize--;
00223 }
00224 else
00225 ibuf[i] = 0;
00226 }
00227
00228 obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
00229 obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
00230 ((ibuf[1] & 0xF0) >> 4));
00231 obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
00232 ((ibuf[2] & 0xC0) >> 6));
00233 obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
00234
00235 switch(inputparts) {
00236 case 1:
00237 snprintf(output, 5, "%c%c==",
00238 table64[obuf[0]],
00239 table64[obuf[1]]);
00240 break;
00241
00242 case 2:
00243 snprintf(output, 5, "%c%c%c=",
00244 table64[obuf[0]],
00245 table64[obuf[1]],
00246 table64[obuf[2]]);
00247 break;
00248
00249 default:
00250 snprintf(output, 5, "%c%c%c%c",
00251 table64[obuf[0]],
00252 table64[obuf[1]],
00253 table64[obuf[2]],
00254 table64[obuf[3]]);
00255 break;
00256 }
00257 output += 4;
00258 }
00259
00260
00261 *output = '\0';
00262
00263
00264 *outptr = base64data;
00265
00266 free(convbuf);
00267
00268
00269 *outlen = strlen(base64data);
00270
00271 return CURLE_OK;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 CURLcode Curl_base64_encode(struct Curl_easy *data,
00292 const char *inputbuff, size_t insize,
00293 char **outptr, size_t *outlen)
00294 {
00295 return base64_encode(base64, data, inputbuff, insize, outptr, outlen);
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 CURLcode Curl_base64url_encode(struct Curl_easy *data,
00316 const char *inputbuff, size_t insize,
00317 char **outptr, size_t *outlen)
00318 {
00319 return base64_encode(base64url, data, inputbuff, insize, outptr, outlen);
00320 }