base64.c 6.6 KB

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