base64.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 /* Base64 encoding/decoding */
00024 
00025 #include "curl_setup.h"
00026 #include "urldata.h" /* for the Curl_easy definition */
00027 #include "warnless.h"
00028 #include "curl_base64.h"
00029 #include "non-ascii.h"
00030 
00031 /* The last 3 #include files should be in this order */
00032 #include "curl_printf.h"
00033 #include "curl_memory.h"
00034 #include "memdebug.h"
00035 
00036 /* ---- Base64 Encoding/Decoding Table --- */
00037 static const char base64[]=
00038   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00039 
00040 /* The Base 64 encoding with an URL and filename safe alphabet, RFC 4648
00041    section 5 */
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  * Curl_base64_decode()
00088  *
00089  * Given a base64 NUL-terminated string at src, decode it and return a
00090  * pointer in *outptr to a newly allocated memory area holding decoded
00091  * data. Size of decoded data is returned in variable pointed by outlen.
00092  *
00093  * Returns CURLE_OK on success, otherwise specific error code. Function
00094  * output shall not be considered valid unless CURLE_OK is returned.
00095  *
00096  * When decoded data length is 0, returns NULL in *outptr.
00097  *
00098  * @unittest: 1302
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   /* Check the length of the input string is valid */
00117   if(!srclen || srclen % 4)
00118     return CURLE_BAD_CONTENT_ENCODING;
00119 
00120   /* Find the position of any = padding characters */
00121   while((src[length] != '=') && src[length])
00122     length++;
00123 
00124   /* A maximum of two = padding characters is allowed */
00125   if(src[length] == '=') {
00126     padding++;
00127     if(src[length + 1] == '=')
00128       padding++;
00129   }
00130 
00131   /* Check the = padding characters weren't part way through the input */
00132   if(length + padding != srclen)
00133     return CURLE_BAD_CONTENT_ENCODING;
00134 
00135   /* Calculate the number of quantums */
00136   numQuantums = srclen / 4;
00137 
00138   /* Calculate the size of the decoded string */
00139   rawlen = (numQuantums * 3) - padding;
00140 
00141   /* Allocate our buffer including room for a zero terminator */
00142   newstr = malloc(rawlen + 1);
00143   if(!newstr)
00144     return CURLE_OUT_OF_MEMORY;
00145 
00146   pos = newstr;
00147 
00148   /* Decode the quantums */
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   /* Zero terminate */
00162   *pos = '\0';
00163 
00164   /* Return the decoded data */
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    * The base64 data needs to be created using the network encoding
00204    * not the host encoding.  And we can't change the actual input
00205    * so we copy it to a buffer, translate it, and use that instead.
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: /* only one byte read */
00237       snprintf(output, 5, "%c%c==",
00238                table64[obuf[0]],
00239                table64[obuf[1]]);
00240       break;
00241 
00242     case 2: /* two bytes read */
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   /* Zero terminate */
00261   *output = '\0';
00262 
00263   /* Return the pointer to the new data (allocated memory) */
00264   *outptr = base64data;
00265 
00266   free(convbuf);
00267 
00268   /* Return the length of the new data */
00269   *outlen = strlen(base64data);
00270 
00271   return CURLE_OK;
00272 }
00273 
00274 /*
00275  * Curl_base64_encode()
00276  *
00277  * Given a pointer to an input buffer and an input size, encode it and
00278  * return a pointer in *outptr to a newly allocated memory area holding
00279  * encoded data. Size of encoded data is returned in variable pointed by
00280  * outlen.
00281  *
00282  * Input length of 0 indicates input buffer holds a NUL-terminated string.
00283  *
00284  * Returns CURLE_OK on success, otherwise specific error code. Function
00285  * output shall not be considered valid unless CURLE_OK is returned.
00286  *
00287  * When encoded data length is 0, returns NULL in *outptr.
00288  *
00289  * @unittest: 1302
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  * Curl_base64url_encode()
00300  *
00301  * Given a pointer to an input buffer and an input size, encode it and
00302  * return a pointer in *outptr to a newly allocated memory area holding
00303  * encoded data. Size of encoded data is returned in variable pointed by
00304  * outlen.
00305  *
00306  * Input length of 0 indicates input buffer holds a NUL-terminated string.
00307  *
00308  * Returns CURLE_OK on success, otherwise specific error code. Function
00309  * output shall not be considered valid unless CURLE_OK is returned.
00310  *
00311  * When encoded data length is 0, returns NULL in *outptr.
00312  *
00313  * @unittest: 1302
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 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:01