base64.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* Base64 encoding/decoding */
  23. #include "curl_setup.h"
  24. #include "urldata.h" /* for the Curl_easy definition */
  25. #include "warnless.h"
  26. #include "curl_base64.h"
  27. #include "non-ascii.h"
  28. /* The last 3 #include files should be in this order */
  29. #include "curl_printf.h"
  30. #include "curl_memory.h"
  31. #include "memdebug.h"
  32. /* ---- Base64 Encoding/Decoding Table --- */
  33. static const char base64[]=
  34. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  35. /* The Base 64 encoding with an URL and filename safe alphabet, RFC 4648
  36. section 5 */
  37. static const char base64url[]=
  38. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  39. static size_t decodeQuantum(unsigned char *dest, const char *src)
  40. {
  41. size_t padding = 0;
  42. const char *s, *p;
  43. unsigned long i, x = 0;
  44. for(i = 0, s = src; i < 4; i++, s++) {
  45. unsigned long v = 0;
  46. if(*s == '=') {
  47. x = (x << 6);
  48. padding++;
  49. }
  50. else {
  51. p = base64;
  52. while(*p && (*p != *s)) {
  53. v++;
  54. p++;
  55. }
  56. if(*p == *s)
  57. x = (x << 6) + v;
  58. else
  59. return 0;
  60. }
  61. }
  62. if(padding < 1)
  63. dest[2] = curlx_ultouc(x & 0xFFUL);
  64. x >>= 8;
  65. if(padding < 2)
  66. dest[1] = curlx_ultouc(x & 0xFFUL);
  67. x >>= 8;
  68. dest[0] = curlx_ultouc(x & 0xFFUL);
  69. return 3 - padding;
  70. }
  71. /*
  72. * Curl_base64_decode()
  73. *
  74. * Given a base64 NUL-terminated string at src, decode it and return a
  75. * pointer in *outptr to a newly allocated memory area holding decoded
  76. * data. Size of decoded data is returned in variable pointed by outlen.
  77. *
  78. * Returns CURLE_OK on success, otherwise specific error code. Function
  79. * output shall not be considered valid unless CURLE_OK is returned.
  80. *
  81. * When decoded data length is 0, returns NULL in *outptr.
  82. *
  83. * @unittest: 1302
  84. */
  85. CURLcode Curl_base64_decode(const char *src,
  86. unsigned char **outptr, size_t *outlen)
  87. {
  88. size_t srclen = 0;
  89. size_t length = 0;
  90. size_t padding = 0;
  91. size_t i;
  92. size_t numQuantums;
  93. size_t rawlen = 0;
  94. unsigned char *pos;
  95. unsigned char *newstr;
  96. *outptr = NULL;
  97. *outlen = 0;
  98. srclen = strlen(src);
  99. /* Check the length of the input string is valid */
  100. if(!srclen || srclen % 4)
  101. return CURLE_BAD_CONTENT_ENCODING;
  102. /* Find the position of any = padding characters */
  103. while((src[length] != '=') && src[length])
  104. length++;
  105. /* A maximum of two = padding characters is allowed */
  106. if(src[length] == '=') {
  107. padding++;
  108. if(src[length + 1] == '=')
  109. padding++;
  110. }
  111. /* Check the = padding characters weren't part way through the input */
  112. if(length + padding != srclen)
  113. return CURLE_BAD_CONTENT_ENCODING;
  114. /* Calculate the number of quantums */
  115. numQuantums = srclen / 4;
  116. /* Calculate the size of the decoded string */
  117. rawlen = (numQuantums * 3) - padding;
  118. /* Allocate our buffer including room for a zero terminator */
  119. newstr = malloc(rawlen + 1);
  120. if(!newstr)
  121. return CURLE_OUT_OF_MEMORY;
  122. pos = newstr;
  123. /* Decode the quantums */
  124. for(i = 0; i < numQuantums; i++) {
  125. size_t result = decodeQuantum(pos, src);
  126. if(!result) {
  127. free(newstr);
  128. return CURLE_BAD_CONTENT_ENCODING;
  129. }
  130. pos += result;
  131. src += 4;
  132. }
  133. /* Zero terminate */
  134. *pos = '\0';
  135. /* Return the decoded data */
  136. *outptr = newstr;
  137. *outlen = rawlen;
  138. return CURLE_OK;
  139. }
  140. static CURLcode base64_encode(const char *table64,
  141. struct Curl_easy *data,
  142. const char *inputbuff, size_t insize,
  143. char **outptr, size_t *outlen)
  144. {
  145. CURLcode result;
  146. unsigned char ibuf[3];
  147. unsigned char obuf[4];
  148. int i;
  149. int inputparts;
  150. char *output;
  151. char *base64data;
  152. char *convbuf = NULL;
  153. const char *indata = inputbuff;
  154. *outptr = NULL;
  155. *outlen = 0;
  156. if(!insize)
  157. insize = strlen(indata);
  158. #if SIZEOF_SIZE_T == 4
  159. if(insize > UINT_MAX/4)
  160. return CURLE_OUT_OF_MEMORY;
  161. #endif
  162. base64data = output = malloc(insize * 4 / 3 + 4);
  163. if(!output)
  164. return CURLE_OUT_OF_MEMORY;
  165. /*
  166. * The base64 data needs to be created using the network encoding
  167. * not the host encoding. And we can't change the actual input
  168. * so we copy it to a buffer, translate it, and use that instead.
  169. */
  170. result = Curl_convert_clone(data, indata, insize, &convbuf);
  171. if(result) {
  172. free(output);
  173. return result;
  174. }
  175. if(convbuf)
  176. indata = (char *)convbuf;
  177. while(insize > 0) {
  178. for(i = inputparts = 0; i < 3; i++) {
  179. if(insize > 0) {
  180. inputparts++;
  181. ibuf[i] = (unsigned char) *indata;
  182. indata++;
  183. insize--;
  184. }
  185. else
  186. ibuf[i] = 0;
  187. }
  188. obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
  189. obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
  190. ((ibuf[1] & 0xF0) >> 4));
  191. obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
  192. ((ibuf[2] & 0xC0) >> 6));
  193. obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
  194. switch(inputparts) {
  195. case 1: /* only one byte read */
  196. snprintf(output, 5, "%c%c==",
  197. table64[obuf[0]],
  198. table64[obuf[1]]);
  199. break;
  200. case 2: /* two bytes read */
  201. snprintf(output, 5, "%c%c%c=",
  202. table64[obuf[0]],
  203. table64[obuf[1]],
  204. table64[obuf[2]]);
  205. break;
  206. default:
  207. snprintf(output, 5, "%c%c%c%c",
  208. table64[obuf[0]],
  209. table64[obuf[1]],
  210. table64[obuf[2]],
  211. table64[obuf[3]]);
  212. break;
  213. }
  214. output += 4;
  215. }
  216. /* Zero terminate */
  217. *output = '\0';
  218. /* Return the pointer to the new data (allocated memory) */
  219. *outptr = base64data;
  220. free(convbuf);
  221. /* Return the length of the new data */
  222. *outlen = strlen(base64data);
  223. return CURLE_OK;
  224. }
  225. /*
  226. * Curl_base64_encode()
  227. *
  228. * Given a pointer to an input buffer and an input size, encode it and
  229. * return a pointer in *outptr to a newly allocated memory area holding
  230. * encoded data. Size of encoded data is returned in variable pointed by
  231. * outlen.
  232. *
  233. * Input length of 0 indicates input buffer holds a NUL-terminated string.
  234. *
  235. * Returns CURLE_OK on success, otherwise specific error code. Function
  236. * output shall not be considered valid unless CURLE_OK is returned.
  237. *
  238. * When encoded data length is 0, returns NULL in *outptr.
  239. *
  240. * @unittest: 1302
  241. */
  242. CURLcode Curl_base64_encode(struct Curl_easy *data,
  243. const char *inputbuff, size_t insize,
  244. char **outptr, size_t *outlen)
  245. {
  246. return base64_encode(base64, data, inputbuff, insize, outptr, outlen);
  247. }
  248. /*
  249. * Curl_base64url_encode()
  250. *
  251. * Given a pointer to an input buffer and an input size, encode it and
  252. * return a pointer in *outptr to a newly allocated memory area holding
  253. * encoded data. Size of encoded data is returned in variable pointed by
  254. * outlen.
  255. *
  256. * Input length of 0 indicates input buffer holds a NUL-terminated string.
  257. *
  258. * Returns CURLE_OK on success, otherwise specific error code. Function
  259. * output shall not be considered valid unless CURLE_OK is returned.
  260. *
  261. * When encoded data length is 0, returns NULL in *outptr.
  262. *
  263. * @unittest: 1302
  264. */
  265. CURLcode Curl_base64url_encode(struct Curl_easy *data,
  266. const char *inputbuff, size_t insize,
  267. char **outptr, size_t *outlen)
  268. {
  269. return base64_encode(base64url, data, inputbuff, insize, outptr, outlen);
  270. }