base64.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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. *
  24. * Test harnesses down the bottom - compile with -DTEST_ENCODE for
  25. * a program that will read in raw data from stdin and write out
  26. * a base64-encoded version to stdout, and the length returned by the
  27. * encoding function to stderr. Compile with -DTEST_DECODE for a program that
  28. * will go the other way.
  29. *
  30. * This code will break if int is smaller than 32 bits
  31. */
  32. #include "setup.h"
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #define _MPRINTF_REPLACE /* use our functions only */
  36. #include <curl/mprintf.h>
  37. #include "urldata.h" /* for the SessionHandle definition */
  38. #include "easyif.h" /* for Curl_convert_... prototypes */
  39. #include "warnless.h"
  40. #include "curl_base64.h"
  41. #include "curl_memory.h"
  42. /* include memdebug.h last */
  43. #include "memdebug.h"
  44. /* ---- Base64 Encoding/Decoding Table --- */
  45. static const char table64[]=
  46. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  47. static void decodeQuantum(unsigned char *dest, const char *src)
  48. {
  49. const char *s, *p;
  50. unsigned long i, v, x = 0;
  51. for(i = 0, s = src; i < 4; i++, s++) {
  52. v = 0;
  53. p = table64;
  54. while(*p && (*p != *s)) {
  55. v++;
  56. p++;
  57. }
  58. if(*p == *s)
  59. x = (x << 6) + v;
  60. else if(*s == '=')
  61. x = (x << 6);
  62. }
  63. dest[2] = curlx_ultouc(x);
  64. x >>= 8;
  65. dest[1] = curlx_ultouc(x);
  66. x >>= 8;
  67. dest[0] = curlx_ultouc(x);
  68. }
  69. /*
  70. * Curl_base64_decode()
  71. *
  72. * Given a base64 string at src, decode it and return an allocated memory in
  73. * the *outptr. Returns the length of the decoded data.
  74. */
  75. size_t Curl_base64_decode(const char *src, unsigned char **outptr)
  76. {
  77. size_t length = 0;
  78. size_t equalsTerm = 0;
  79. size_t i;
  80. size_t numQuantums;
  81. unsigned char lastQuantum[3];
  82. size_t rawlen = 0;
  83. unsigned char *newstr;
  84. *outptr = NULL;
  85. while((src[length] != '=') && src[length])
  86. length++;
  87. /* A maximum of two = padding characters is allowed */
  88. if(src[length] == '=') {
  89. equalsTerm++;
  90. if(src[length+equalsTerm] == '=')
  91. equalsTerm++;
  92. }
  93. numQuantums = (length + equalsTerm) / 4;
  94. /* Don't allocate a buffer if the decoded length is 0 */
  95. if(numQuantums == 0)
  96. return 0;
  97. rawlen = (numQuantums * 3) - equalsTerm;
  98. /* The buffer must be large enough to make room for the last quantum
  99. (which may be partially thrown out) and the zero terminator. */
  100. newstr = malloc(rawlen+4);
  101. if(!newstr)
  102. return 0;
  103. *outptr = newstr;
  104. /* Decode all but the last quantum (which may not decode to a
  105. multiple of 3 bytes) */
  106. for(i = 0; i < numQuantums - 1; i++) {
  107. decodeQuantum(newstr, src);
  108. newstr += 3; src += 4;
  109. }
  110. /* This final decode may actually read slightly past the end of the buffer
  111. if the input string is missing pad bytes. This will almost always be
  112. harmless. */
  113. decodeQuantum(lastQuantum, src);
  114. for(i = 0; i < 3 - equalsTerm; i++)
  115. newstr[i] = lastQuantum[i];
  116. newstr[i] = '\0'; /* zero terminate */
  117. return rawlen;
  118. }
  119. /*
  120. * Curl_base64_encode()
  121. *
  122. * Returns the length of the newly created base64 string. The third argument
  123. * is a pointer to an allocated area holding the base64 data. If something
  124. * went wrong, 0 is returned.
  125. *
  126. */
  127. size_t Curl_base64_encode(struct SessionHandle *data,
  128. const char *inputbuff, size_t insize,
  129. char **outptr)
  130. {
  131. unsigned char ibuf[3];
  132. unsigned char obuf[4];
  133. int i;
  134. int inputparts;
  135. char *output;
  136. char *base64data;
  137. #ifdef CURL_DOES_CONVERSIONS
  138. char *convbuf = NULL;
  139. #endif
  140. const char *indata = inputbuff;
  141. *outptr = NULL; /* set to NULL in case of failure before we reach the end */
  142. if(0 == insize)
  143. insize = strlen(indata);
  144. base64data = output = malloc(insize*4/3+4);
  145. if(NULL == output)
  146. return 0;
  147. #ifdef CURL_DOES_CONVERSIONS
  148. /*
  149. * The base64 data needs to be created using the network encoding
  150. * not the host encoding. And we can't change the actual input
  151. * so we copy it to a buffer, translate it, and use that instead.
  152. */
  153. if(data) {
  154. convbuf = malloc(insize);
  155. if(!convbuf) {
  156. free(output);
  157. return 0;
  158. }
  159. memcpy(convbuf, indata, insize);
  160. if(CURLE_OK != Curl_convert_to_network(data, convbuf, insize)) {
  161. free(convbuf);
  162. free(output);
  163. return 0;
  164. }
  165. indata = convbuf; /* switch to the converted buffer */
  166. }
  167. #else
  168. (void)data;
  169. #endif
  170. while(insize > 0) {
  171. for (i = inputparts = 0; i < 3; i++) {
  172. if(insize > 0) {
  173. inputparts++;
  174. ibuf[i] = (unsigned char) *indata;
  175. indata++;
  176. insize--;
  177. }
  178. else
  179. ibuf[i] = 0;
  180. }
  181. obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
  182. obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
  183. ((ibuf[1] & 0xF0) >> 4));
  184. obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
  185. ((ibuf[2] & 0xC0) >> 6));
  186. obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
  187. switch(inputparts) {
  188. case 1: /* only one byte read */
  189. snprintf(output, 5, "%c%c==",
  190. table64[obuf[0]],
  191. table64[obuf[1]]);
  192. break;
  193. case 2: /* two bytes read */
  194. snprintf(output, 5, "%c%c%c=",
  195. table64[obuf[0]],
  196. table64[obuf[1]],
  197. table64[obuf[2]]);
  198. break;
  199. default:
  200. snprintf(output, 5, "%c%c%c%c",
  201. table64[obuf[0]],
  202. table64[obuf[1]],
  203. table64[obuf[2]],
  204. table64[obuf[3]] );
  205. break;
  206. }
  207. output += 4;
  208. }
  209. *output=0;
  210. *outptr = base64data; /* make it return the actual data memory */
  211. #ifdef CURL_DOES_CONVERSIONS
  212. if(data)
  213. free(convbuf);
  214. #endif
  215. return strlen(base64data); /* return the length of the new data */
  216. }
  217. /* ---- End of Base64 Encoding ---- */