base64.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, 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, x = 0;
  45. for(i = 0, s = src; i < 4; i++, s++) {
  46. unsigned long 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 numQuantums;
  94. size_t rawlen = 0;
  95. unsigned char *pos;
  96. unsigned char *newstr;
  97. *outptr = NULL;
  98. *outlen = 0;
  99. srclen = strlen(src);
  100. /* Check the length of the input string is valid */
  101. if(!srclen || srclen % 4)
  102. return CURLE_BAD_CONTENT_ENCODING;
  103. /* Find the position of any = padding characters */
  104. while((src[length] != '=') && src[length])
  105. length++;
  106. /* A maximum of two = padding characters is allowed */
  107. if(src[length] == '=') {
  108. padding++;
  109. if(src[length + 1] == '=')
  110. padding++;
  111. }
  112. /* Check the = padding characters weren't part way through the input */
  113. if(length + padding != srclen)
  114. return CURLE_BAD_CONTENT_ENCODING;
  115. /* Calculate the number of quantums */
  116. numQuantums = srclen / 4;
  117. /* Calculate the size of the decoded string */
  118. rawlen = (numQuantums * 3) - padding;
  119. /* Allocate our buffer including room for a zero terminator */
  120. newstr = malloc(rawlen + 1);
  121. if(!newstr)
  122. return CURLE_OUT_OF_MEMORY;
  123. pos = newstr;
  124. /* Decode the quantums */
  125. for(i = 0; i < numQuantums; i++) {
  126. size_t result = decodeQuantum(pos, src);
  127. if(!result) {
  128. Curl_safefree(newstr);
  129. return CURLE_BAD_CONTENT_ENCODING;
  130. }
  131. pos += result;
  132. src += 4;
  133. }
  134. /* Zero terminate */
  135. *pos = '\0';
  136. /* Return the decoded data */
  137. *outptr = newstr;
  138. *outlen = rawlen;
  139. return CURLE_OK;
  140. }
  141. static CURLcode base64_encode(const char *table64,
  142. struct SessionHandle *data,
  143. const char *inputbuff, size_t insize,
  144. char **outptr, size_t *outlen)
  145. {
  146. CURLcode error;
  147. unsigned char ibuf[3];
  148. unsigned char obuf[4];
  149. int i;
  150. int inputparts;
  151. char *output;
  152. char *base64data;
  153. char *convbuf = NULL;
  154. const char *indata = inputbuff;
  155. *outptr = NULL;
  156. *outlen = 0;
  157. if(0 == insize)
  158. insize = strlen(indata);
  159. base64data = output = malloc(insize*4/3+4);
  160. if(NULL == output)
  161. return CURLE_OUT_OF_MEMORY;
  162. /*
  163. * The base64 data needs to be created using the network encoding
  164. * not the host encoding. And we can't change the actual input
  165. * so we copy it to a buffer, translate it, and use that instead.
  166. */
  167. error = Curl_convert_clone(data, indata, insize, &convbuf);
  168. if(error) {
  169. free(output);
  170. return error;
  171. }
  172. if(convbuf)
  173. indata = (char *)convbuf;
  174. while(insize > 0) {
  175. for(i = inputparts = 0; i < 3; i++) {
  176. if(insize > 0) {
  177. inputparts++;
  178. ibuf[i] = (unsigned char) *indata;
  179. indata++;
  180. insize--;
  181. }
  182. else
  183. ibuf[i] = 0;
  184. }
  185. obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
  186. obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
  187. ((ibuf[1] & 0xF0) >> 4));
  188. obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
  189. ((ibuf[2] & 0xC0) >> 6));
  190. obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
  191. switch(inputparts) {
  192. case 1: /* only one byte read */
  193. snprintf(output, 5, "%c%c==",
  194. table64[obuf[0]],
  195. table64[obuf[1]]);
  196. break;
  197. case 2: /* two bytes read */
  198. snprintf(output, 5, "%c%c%c=",
  199. table64[obuf[0]],
  200. table64[obuf[1]],
  201. table64[obuf[2]]);
  202. break;
  203. default:
  204. snprintf(output, 5, "%c%c%c%c",
  205. table64[obuf[0]],
  206. table64[obuf[1]],
  207. table64[obuf[2]],
  208. table64[obuf[3]] );
  209. break;
  210. }
  211. output += 4;
  212. }
  213. *output = '\0';
  214. *outptr = base64data; /* return pointer to new data, allocated memory */
  215. if(convbuf)
  216. free(convbuf);
  217. *outlen = strlen(base64data); /* return the length of the new data */
  218. return CURLE_OK;
  219. }
  220. /*
  221. * Curl_base64_encode()
  222. *
  223. * Given a pointer to an input buffer and an input size, encode it and
  224. * return a pointer in *outptr to a newly allocated memory area holding
  225. * encoded data. Size of encoded data is returned in variable pointed by
  226. * outlen.
  227. *
  228. * Input length of 0 indicates input buffer holds a NUL-terminated string.
  229. *
  230. * Returns CURLE_OK on success, otherwise specific error code. Function
  231. * output shall not be considered valid unless CURLE_OK is returned.
  232. *
  233. * When encoded data length is 0, returns NULL in *outptr.
  234. *
  235. * @unittest: 1302
  236. */
  237. CURLcode Curl_base64_encode(struct SessionHandle *data,
  238. const char *inputbuff, size_t insize,
  239. char **outptr, size_t *outlen)
  240. {
  241. return base64_encode(base64, data, inputbuff, insize, outptr, outlen);
  242. }
  243. /*
  244. * Curl_base64url_encode()
  245. *
  246. * Given a pointer to an input buffer and an input size, encode it and
  247. * return a pointer in *outptr to a newly allocated memory area holding
  248. * encoded data. Size of encoded data is returned in variable pointed by
  249. * outlen.
  250. *
  251. * Input length of 0 indicates input buffer holds a NUL-terminated string.
  252. *
  253. * Returns CURLE_OK on success, otherwise specific error code. Function
  254. * output shall not be considered valid unless CURLE_OK is returned.
  255. *
  256. * When encoded data length is 0, returns NULL in *outptr.
  257. *
  258. * @unittest: 1302
  259. */
  260. CURLcode Curl_base64url_encode(struct SessionHandle *data,
  261. const char *inputbuff, size_t insize,
  262. char **outptr, size_t *outlen)
  263. {
  264. return base64_encode(base64url, data, inputbuff, insize, outptr, outlen);
  265. }
  266. /* ---- End of Base64 Encoding ---- */