base64.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 table64[]=
  35. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  36. static void decodeQuantum(unsigned char *dest, const char *src)
  37. {
  38. const char *s, *p;
  39. unsigned long i, v, x = 0;
  40. for(i = 0, s = src; i < 4; i++, s++) {
  41. v = 0;
  42. p = table64;
  43. while(*p && (*p != *s)) {
  44. v++;
  45. p++;
  46. }
  47. if(*p == *s)
  48. x = (x << 6) + v;
  49. else if(*s == '=')
  50. x = (x << 6);
  51. }
  52. dest[2] = curlx_ultouc(x & 0xFFUL);
  53. x >>= 8;
  54. dest[1] = curlx_ultouc(x & 0xFFUL);
  55. x >>= 8;
  56. dest[0] = curlx_ultouc(x & 0xFFUL);
  57. }
  58. /*
  59. * Curl_base64_decode()
  60. *
  61. * Given a base64 NUL-terminated string at src, decode it and return a
  62. * pointer in *outptr to a newly allocated memory area holding decoded
  63. * data. Size of decoded data is returned in variable pointed by outlen.
  64. *
  65. * Returns CURLE_OK on success, otherwise specific error code. Function
  66. * output shall not be considered valid unless CURLE_OK is returned.
  67. *
  68. * When decoded data length is 0, returns NULL in *outptr.
  69. *
  70. * @unittest: 1302
  71. */
  72. CURLcode Curl_base64_decode(const char *src,
  73. unsigned char **outptr, size_t *outlen)
  74. {
  75. size_t length = 0;
  76. size_t equalsTerm = 0;
  77. size_t i;
  78. size_t numQuantums;
  79. unsigned char lastQuantum[3];
  80. size_t rawlen = 0;
  81. unsigned char *newstr;
  82. *outptr = NULL;
  83. *outlen = 0;
  84. while((src[length] != '=') && src[length])
  85. length++;
  86. /* A maximum of two = padding characters is allowed */
  87. if(src[length] == '=') {
  88. equalsTerm++;
  89. if(src[length+equalsTerm] == '=')
  90. equalsTerm++;
  91. }
  92. numQuantums = (length + equalsTerm) / 4;
  93. /* Don't allocate a buffer if the decoded length is 0 */
  94. if(numQuantums == 0)
  95. return CURLE_OK;
  96. rawlen = (numQuantums * 3) - equalsTerm;
  97. /* The buffer must be large enough to make room for the last quantum
  98. (which may be partially thrown out) and the zero terminator. */
  99. newstr = malloc(rawlen+4);
  100. if(!newstr)
  101. return CURLE_OUT_OF_MEMORY;
  102. *outptr = newstr;
  103. /* Decode all but the last quantum (which may not decode to a
  104. multiple of 3 bytes) */
  105. for(i = 0; i < numQuantums - 1; i++) {
  106. decodeQuantum(newstr, src);
  107. newstr += 3; src += 4;
  108. }
  109. /* This final decode may actually read slightly past the end of the buffer
  110. if the input string is missing pad bytes. This will almost always be
  111. harmless. */
  112. decodeQuantum(lastQuantum, src);
  113. for(i = 0; i < 3 - equalsTerm; i++)
  114. newstr[i] = lastQuantum[i];
  115. newstr[i] = '\0'; /* zero terminate */
  116. *outlen = rawlen; /* return size of decoded data */
  117. return CURLE_OK;
  118. }
  119. /*
  120. * Curl_base64_encode()
  121. *
  122. * Given a pointer to an input buffer and an input size, encode it and
  123. * return a pointer in *outptr to a newly allocated memory area holding
  124. * encoded data. Size of encoded data is returned in variable pointed by
  125. * outlen.
  126. *
  127. * Input length of 0 indicates input buffer holds a NUL-terminated string.
  128. *
  129. * Returns CURLE_OK on success, otherwise specific error code. Function
  130. * output shall not be considered valid unless CURLE_OK is returned.
  131. *
  132. * When encoded data length is 0, returns NULL in *outptr.
  133. *
  134. * @unittest: 1302
  135. */
  136. CURLcode Curl_base64_encode(struct SessionHandle *data,
  137. const char *inputbuff, size_t insize,
  138. char **outptr, size_t *outlen)
  139. {
  140. CURLcode error;
  141. unsigned char ibuf[3];
  142. unsigned char obuf[4];
  143. int i;
  144. int inputparts;
  145. char *output;
  146. char *base64data;
  147. char *convbuf = NULL;
  148. const char *indata = inputbuff;
  149. *outptr = NULL;
  150. *outlen = 0;
  151. if(0 == insize)
  152. insize = strlen(indata);
  153. base64data = output = malloc(insize*4/3+4);
  154. if(NULL == output)
  155. return CURLE_OUT_OF_MEMORY;
  156. /*
  157. * The base64 data needs to be created using the network encoding
  158. * not the host encoding. And we can't change the actual input
  159. * so we copy it to a buffer, translate it, and use that instead.
  160. */
  161. error = Curl_convert_clone(data, indata, insize, &convbuf);
  162. if(error) {
  163. free(output);
  164. return error;
  165. }
  166. if(convbuf)
  167. indata = (char *)convbuf;
  168. while(insize > 0) {
  169. for(i = inputparts = 0; i < 3; i++) {
  170. if(insize > 0) {
  171. inputparts++;
  172. ibuf[i] = (unsigned char) *indata;
  173. indata++;
  174. insize--;
  175. }
  176. else
  177. ibuf[i] = 0;
  178. }
  179. obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
  180. obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
  181. ((ibuf[1] & 0xF0) >> 4));
  182. obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
  183. ((ibuf[2] & 0xC0) >> 6));
  184. obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
  185. switch(inputparts) {
  186. case 1: /* only one byte read */
  187. snprintf(output, 5, "%c%c==",
  188. table64[obuf[0]],
  189. table64[obuf[1]]);
  190. break;
  191. case 2: /* two bytes read */
  192. snprintf(output, 5, "%c%c%c=",
  193. table64[obuf[0]],
  194. table64[obuf[1]],
  195. table64[obuf[2]]);
  196. break;
  197. default:
  198. snprintf(output, 5, "%c%c%c%c",
  199. table64[obuf[0]],
  200. table64[obuf[1]],
  201. table64[obuf[2]],
  202. table64[obuf[3]] );
  203. break;
  204. }
  205. output += 4;
  206. }
  207. *output = '\0';
  208. *outptr = base64data; /* return pointer to new data, allocated memory */
  209. if(convbuf)
  210. free(convbuf);
  211. *outlen = strlen(base64data); /* return the length of the new data */
  212. return CURLE_OK;
  213. }
  214. /* ---- End of Base64 Encoding ---- */