base64.c 8.3 KB

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