base64.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* Base64 encoding/decoding */
  25. #include "../curl_setup.h"
  26. #include <curl/curl.h>
  27. #include "warnless.h"
  28. #include "base64.h"
  29. /* The last 2 #include files should be in this order */
  30. #ifdef BUILDING_LIBCURL
  31. #include "../curl_memory.h"
  32. #endif
  33. #include "../memdebug.h"
  34. /* ---- Base64 Encoding/Decoding Table --- */
  35. const char Curl_base64encdec[]=
  36. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  37. /* The Base 64 encoding with a URL and filename safe alphabet, RFC 4648
  38. section 5 */
  39. static const char base64url[]=
  40. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  41. static const unsigned char decodetable[] =
  42. { 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255,
  43. 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  44. 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 26, 27, 28,
  45. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  46. 48, 49, 50, 51 };
  47. /*
  48. * curlx_base64_decode()
  49. *
  50. * Given a base64 null-terminated string at src, decode it and return a
  51. * pointer in *outptr to a newly allocated memory area holding decoded data.
  52. * Size of decoded data is returned in variable pointed by outlen.
  53. *
  54. * Returns CURLE_OK on success, otherwise specific error code. Function
  55. * output shall not be considered valid unless CURLE_OK is returned.
  56. *
  57. * When decoded data length is 0, returns NULL in *outptr.
  58. *
  59. * @unittest: 1302
  60. */
  61. CURLcode curlx_base64_decode(const char *src,
  62. unsigned char **outptr, size_t *outlen)
  63. {
  64. size_t srclen = 0;
  65. size_t padding = 0;
  66. size_t i;
  67. size_t numQuantums;
  68. size_t fullQuantums;
  69. size_t rawlen = 0;
  70. unsigned char *pos;
  71. unsigned char *newstr;
  72. unsigned char lookup[256];
  73. *outptr = NULL;
  74. *outlen = 0;
  75. srclen = strlen(src);
  76. /* Check the length of the input string is valid */
  77. if(!srclen || srclen % 4)
  78. return CURLE_BAD_CONTENT_ENCODING;
  79. /* srclen is at least 4 here */
  80. while(src[srclen - 1 - padding] == '=') {
  81. /* count padding characters */
  82. padding++;
  83. /* A maximum of two = padding characters is allowed */
  84. if(padding > 2)
  85. return CURLE_BAD_CONTENT_ENCODING;
  86. }
  87. /* Calculate the number of quantums */
  88. numQuantums = srclen / 4;
  89. fullQuantums = numQuantums - (padding ? 1 : 0);
  90. /* Calculate the size of the decoded string */
  91. rawlen = (numQuantums * 3) - padding;
  92. /* Allocate our buffer including room for a null-terminator */
  93. newstr = malloc(rawlen + 1);
  94. if(!newstr)
  95. return CURLE_OUT_OF_MEMORY;
  96. pos = newstr;
  97. memset(lookup, 0xff, sizeof(lookup));
  98. memcpy(&lookup['+'], decodetable, sizeof(decodetable));
  99. /* Decode the complete quantums first */
  100. for(i = 0; i < fullQuantums; i++) {
  101. unsigned char val;
  102. unsigned int x = 0;
  103. int j;
  104. for(j = 0; j < 4; j++) {
  105. val = lookup[(unsigned char)*src++];
  106. if(val == 0xff) /* bad symbol */
  107. goto bad;
  108. x = (x << 6) | val;
  109. }
  110. pos[2] = x & 0xff;
  111. pos[1] = (x >> 8) & 0xff;
  112. pos[0] = (x >> 16) & 0xff;
  113. pos += 3;
  114. }
  115. if(padding) {
  116. /* this means either 8 or 16 bits output */
  117. unsigned char val;
  118. unsigned int x = 0;
  119. int j;
  120. size_t padc = 0;
  121. for(j = 0; j < 4; j++) {
  122. if(*src == '=') {
  123. x <<= 6;
  124. src++;
  125. if(++padc > padding)
  126. /* this is a badly placed '=' symbol! */
  127. goto bad;
  128. }
  129. else {
  130. val = lookup[(unsigned char)*src++];
  131. if(val == 0xff) /* bad symbol */
  132. goto bad;
  133. x = (x << 6) | val;
  134. }
  135. }
  136. if(padding == 1)
  137. pos[1] = (x >> 8) & 0xff;
  138. pos[0] = (x >> 16) & 0xff;
  139. pos += 3 - padding;
  140. }
  141. /* Zero terminate */
  142. *pos = '\0';
  143. /* Return the decoded data */
  144. *outptr = newstr;
  145. *outlen = rawlen;
  146. return CURLE_OK;
  147. bad:
  148. free(newstr);
  149. return CURLE_BAD_CONTENT_ENCODING;
  150. }
  151. static CURLcode base64_encode(const char *table64,
  152. unsigned char padbyte,
  153. const char *inputbuff, size_t insize,
  154. char **outptr, size_t *outlen)
  155. {
  156. char *output;
  157. char *base64data;
  158. const unsigned char *in = (const unsigned char *)inputbuff;
  159. *outptr = NULL;
  160. *outlen = 0;
  161. if(!insize)
  162. insize = strlen(inputbuff);
  163. #if SIZEOF_SIZE_T == 4
  164. if(insize > UINT_MAX/4)
  165. return CURLE_OUT_OF_MEMORY;
  166. #endif
  167. base64data = output = malloc((insize + 2) / 3 * 4 + 1);
  168. if(!output)
  169. return CURLE_OUT_OF_MEMORY;
  170. while(insize >= 3) {
  171. *output++ = table64[ in[0] >> 2 ];
  172. *output++ = table64[ ((in[0] & 0x03) << 4) | (in[1] >> 4) ];
  173. *output++ = table64[ ((in[1] & 0x0F) << 2) | ((in[2] & 0xC0) >> 6) ];
  174. *output++ = table64[ in[2] & 0x3F ];
  175. insize -= 3;
  176. in += 3;
  177. }
  178. if(insize) {
  179. /* this is only one or two bytes now */
  180. *output++ = table64[ in[0] >> 2 ];
  181. if(insize == 1) {
  182. *output++ = table64[ ((in[0] & 0x03) << 4) ];
  183. if(padbyte) {
  184. *output++ = padbyte;
  185. *output++ = padbyte;
  186. }
  187. }
  188. else {
  189. /* insize == 2 */
  190. *output++ = table64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xF0) >> 4) ];
  191. *output++ = table64[ ((in[1] & 0x0F) << 2) ];
  192. if(padbyte)
  193. *output++ = padbyte;
  194. }
  195. }
  196. /* Zero terminate */
  197. *output = '\0';
  198. /* Return the pointer to the new data (allocated memory) */
  199. *outptr = base64data;
  200. /* Return the length of the new data */
  201. *outlen = (size_t)(output - base64data);
  202. return CURLE_OK;
  203. }
  204. /*
  205. * curlx_base64_encode()
  206. *
  207. * Given a pointer to an input buffer and an input size, encode it and
  208. * return a pointer in *outptr to a newly allocated memory area holding
  209. * encoded data. Size of encoded data is returned in variable pointed by
  210. * outlen.
  211. *
  212. * Input length of 0 indicates input buffer holds a null-terminated string.
  213. *
  214. * Returns CURLE_OK on success, otherwise specific error code. Function
  215. * output shall not be considered valid unless CURLE_OK is returned.
  216. *
  217. * @unittest: 1302
  218. */
  219. CURLcode curlx_base64_encode(const char *inputbuff, size_t insize,
  220. char **outptr, size_t *outlen)
  221. {
  222. return base64_encode(Curl_base64encdec, '=',
  223. inputbuff, insize, outptr, outlen);
  224. }
  225. /*
  226. * curlx_base64url_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 null-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. * @unittest: 1302
  239. */
  240. CURLcode curlx_base64url_encode(const char *inputbuff, size_t insize,
  241. char **outptr, size_t *outlen)
  242. {
  243. return base64_encode(base64url, 0, inputbuff, insize, outptr, outlen);
  244. }