base64.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2008, 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 *inp, size_t insize, char **outptr)
  124. {
  125. unsigned char ibuf[3];
  126. unsigned char obuf[4];
  127. int i;
  128. int inputparts;
  129. char *output;
  130. char *base64data;
  131. #ifdef CURL_DOES_CONVERSIONS
  132. char *convbuf = NULL;
  133. #endif
  134. const char *indata = inp;
  135. *outptr = NULL; /* set to NULL in case of failure before we reach the end */
  136. if(0 == insize)
  137. insize = strlen(indata);
  138. base64data = output = malloc(insize*4/3+4);
  139. if(NULL == output)
  140. return 0;
  141. #ifdef CURL_DOES_CONVERSIONS
  142. /*
  143. * The base64 data needs to be created using the network encoding
  144. * not the host encoding. And we can't change the actual input
  145. * so we copy it to a buffer, translate it, and use that instead.
  146. */
  147. if(data) {
  148. convbuf = malloc(insize);
  149. if(!convbuf) {
  150. free(output);
  151. return 0;
  152. }
  153. memcpy(convbuf, indata, insize);
  154. if(CURLE_OK != Curl_convert_to_network(data, convbuf, insize)) {
  155. free(convbuf);
  156. free(output);
  157. return 0;
  158. }
  159. indata = convbuf; /* switch to the converted buffer */
  160. }
  161. #else
  162. (void)data;
  163. #endif
  164. while(insize > 0) {
  165. for (i = inputparts = 0; i < 3; i++) {
  166. if(insize > 0) {
  167. inputparts++;
  168. ibuf[i] = *indata;
  169. indata++;
  170. insize--;
  171. }
  172. else
  173. ibuf[i] = 0;
  174. }
  175. obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
  176. obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
  177. ((ibuf[1] & 0xF0) >> 4));
  178. obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
  179. ((ibuf[2] & 0xC0) >> 6));
  180. obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
  181. switch(inputparts) {
  182. case 1: /* only one byte read */
  183. snprintf(output, 5, "%c%c==",
  184. table64[obuf[0]],
  185. table64[obuf[1]]);
  186. break;
  187. case 2: /* two bytes read */
  188. snprintf(output, 5, "%c%c%c=",
  189. table64[obuf[0]],
  190. table64[obuf[1]],
  191. table64[obuf[2]]);
  192. break;
  193. default:
  194. snprintf(output, 5, "%c%c%c%c",
  195. table64[obuf[0]],
  196. table64[obuf[1]],
  197. table64[obuf[2]],
  198. table64[obuf[3]] );
  199. break;
  200. }
  201. output += 4;
  202. }
  203. *output=0;
  204. *outptr = base64data; /* make it return the actual data memory */
  205. #ifdef CURL_DOES_CONVERSIONS
  206. if(data)
  207. free(convbuf);
  208. #endif
  209. return strlen(base64data); /* return the length of the new data */
  210. }
  211. /* ---- End of Base64 Encoding ---- */