base64.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2008, 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 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 "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((unsigned char *)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. char *indata = (char *)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 = (char*)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 = (char*)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 ---- */
  212. /************* TEST HARNESS STUFF ****************/
  213. #ifdef TEST_ENCODE
  214. /* encoding test harness. Read in standard input and write out the length
  215. * returned by Curl_base64_encode, followed by the base64'd data itself
  216. */
  217. #include <stdio.h>
  218. #define TEST_NEED_SUCK
  219. void *suck(int *);
  220. int main(int argc, argv_item_t argv[], char **envp)
  221. {
  222. char *base64;
  223. size_t base64Len;
  224. unsigned char *data;
  225. int dataLen;
  226. struct SessionHandle *handle = NULL;
  227. #ifdef CURL_DOES_CONVERSIONS
  228. /* get a Curl handle so Curl_base64_encode can translate properly */
  229. handle = curl_easy_init();
  230. if(handle == NULL) {
  231. fprintf(stderr, "Error: curl_easy_init failed\n");
  232. return 1;
  233. }
  234. #endif
  235. data = (unsigned char *)suck(&dataLen);
  236. base64Len = Curl_base64_encode(handle, data, dataLen, &base64);
  237. fprintf(stderr, "%d\n", base64Len);
  238. fprintf(stdout, "%s\n", base64);
  239. free(base64); free(data);
  240. #ifdef CURL_DOES_CONVERSIONS
  241. curl_easy_cleanup(handle);
  242. #endif
  243. return 0;
  244. }
  245. #endif
  246. #ifdef TEST_DECODE
  247. /* decoding test harness. Read in a base64 string from stdin and write out the
  248. * length returned by Curl_base64_decode, followed by the decoded data itself
  249. *
  250. * gcc -DTEST_DECODE base64.c -o base64 mprintf.o memdebug.o
  251. */
  252. #include <stdio.h>
  253. #define TEST_NEED_SUCK
  254. void *suck(int *);
  255. int main(int argc, argv_item_t argv[], char **envp)
  256. {
  257. char *base64;
  258. int base64Len;
  259. unsigned char *data;
  260. int dataLen;
  261. int i, j;
  262. #ifdef CURL_DOES_CONVERSIONS
  263. /* get a Curl handle so main can translate properly */
  264. struct SessionHandle *handle = curl_easy_init();
  265. if(handle == NULL) {
  266. fprintf(stderr, "Error: curl_easy_init failed\n");
  267. return 1;
  268. }
  269. #endif
  270. base64 = (char *)suck(&base64Len);
  271. dataLen = Curl_base64_decode(base64, &data);
  272. fprintf(stderr, "%d\n", dataLen);
  273. for(i=0; i < dataLen; i+=0x10) {
  274. printf("0x%02x: ", i);
  275. for(j=0; j < 0x10; j++)
  276. if((j+i) < dataLen)
  277. printf("%02x ", data[i+j]);
  278. else
  279. printf(" ");
  280. printf(" | ");
  281. for(j=0; j < 0x10; j++)
  282. if((j+i) < dataLen) {
  283. #ifdef CURL_DOES_CONVERSIONS
  284. if(CURLE_OK !=
  285. Curl_convert_from_network(handle, &data[i+j], (size_t)1))
  286. data[i+j] = '.';
  287. #endif /* CURL_DOES_CONVERSIONS */
  288. printf("%c", ISGRAPH(data[i+j])?data[i+j]:'.');
  289. } else
  290. break;
  291. puts("");
  292. }
  293. #ifdef CURL_DOES_CONVERSIONS
  294. curl_easy_cleanup(handle);
  295. #endif
  296. free(base64); free(data);
  297. return 0;
  298. }
  299. #endif
  300. #ifdef TEST_NEED_SUCK
  301. /* this function 'sucks' in as much as possible from stdin */
  302. void *suck(int *lenptr)
  303. {
  304. int cursize = 8192;
  305. unsigned char *buf = NULL;
  306. int lastread;
  307. int len = 0;
  308. do {
  309. cursize *= 2;
  310. buf = (unsigned char *)realloc(buf, cursize);
  311. memset(buf + len, 0, cursize - len);
  312. lastread = fread(buf + len, 1, cursize - len, stdin);
  313. len += lastread;
  314. } while(!feof(stdin));
  315. lenptr[0] = len;
  316. return (void *)buf;
  317. }
  318. #endif