base64.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2007, 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 "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])))
  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, -1 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;
  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. return 0;
  151. }
  152. memcpy(convbuf, indata, insize);
  153. if(CURLE_OK != Curl_convert_to_network(data, convbuf, insize)) {
  154. free(convbuf);
  155. return 0;
  156. }
  157. indata = convbuf; /* switch to the converted buffer */
  158. }
  159. #else
  160. (void)data;
  161. #endif
  162. while(insize > 0) {
  163. for (i = inputparts = 0; i < 3; i++) {
  164. if(insize > 0) {
  165. inputparts++;
  166. ibuf[i] = *indata;
  167. indata++;
  168. insize--;
  169. }
  170. else
  171. ibuf[i] = 0;
  172. }
  173. obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
  174. obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
  175. ((ibuf[1] & 0xF0) >> 4));
  176. obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
  177. ((ibuf[2] & 0xC0) >> 6));
  178. obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
  179. switch(inputparts) {
  180. case 1: /* only one byte read */
  181. snprintf(output, 5, "%c%c==",
  182. table64[obuf[0]],
  183. table64[obuf[1]]);
  184. break;
  185. case 2: /* two bytes read */
  186. snprintf(output, 5, "%c%c%c=",
  187. table64[obuf[0]],
  188. table64[obuf[1]],
  189. table64[obuf[2]]);
  190. break;
  191. default:
  192. snprintf(output, 5, "%c%c%c%c",
  193. table64[obuf[0]],
  194. table64[obuf[1]],
  195. table64[obuf[2]],
  196. table64[obuf[3]] );
  197. break;
  198. }
  199. output += 4;
  200. }
  201. *output=0;
  202. *outptr = base64data; /* make it return the actual data memory */
  203. #ifdef CURL_DOES_CONVERSIONS
  204. if(data)
  205. free(convbuf);
  206. #endif
  207. return strlen(base64data); /* return the length of the new data */
  208. }
  209. /* ---- End of Base64 Encoding ---- */
  210. /************* TEST HARNESS STUFF ****************/
  211. #ifdef TEST_ENCODE
  212. /* encoding test harness. Read in standard input and write out the length
  213. * returned by Curl_base64_encode, followed by the base64'd data itself
  214. */
  215. #include <stdio.h>
  216. #define TEST_NEED_SUCK
  217. void *suck(int *);
  218. int main(int argc, char **argv, char **envp)
  219. {
  220. char *base64;
  221. size_t base64Len;
  222. unsigned char *data;
  223. int dataLen;
  224. struct SessionHandle *handle = NULL;
  225. #ifdef CURL_DOES_CONVERSIONS
  226. /* get a Curl handle so Curl_base64_encode can translate properly */
  227. handle = curl_easy_init();
  228. if(handle == NULL) {
  229. fprintf(stderr, "Error: curl_easy_init failed\n");
  230. return 0;
  231. }
  232. #endif
  233. data = (unsigned char *)suck(&dataLen);
  234. base64Len = Curl_base64_encode(handle, data, dataLen, &base64);
  235. fprintf(stderr, "%d\n", base64Len);
  236. fprintf(stdout, "%s\n", base64);
  237. free(base64); free(data);
  238. #ifdef CURL_DOES_CONVERSIONS
  239. curl_easy_cleanup(handle);
  240. #endif
  241. return 0;
  242. }
  243. #endif
  244. #ifdef TEST_DECODE
  245. /* decoding test harness. Read in a base64 string from stdin and write out the
  246. * length returned by Curl_base64_decode, followed by the decoded data itself
  247. *
  248. * gcc -DTEST_DECODE base64.c -o base64 mprintf.o memdebug.o
  249. */
  250. #include <stdio.h>
  251. #define TEST_NEED_SUCK
  252. void *suck(int *);
  253. int main(int argc, char **argv, char **envp)
  254. {
  255. char *base64;
  256. int base64Len;
  257. unsigned char *data;
  258. int dataLen;
  259. int i, j;
  260. #ifdef CURL_DOES_CONVERSIONS
  261. /* get a Curl handle so main can translate properly */
  262. struct SessionHandle *handle = curl_easy_init();
  263. if(handle == NULL) {
  264. fprintf(stderr, "Error: curl_easy_init failed\n");
  265. return 0;
  266. }
  267. #endif
  268. base64 = (char *)suck(&base64Len);
  269. dataLen = Curl_base64_decode(base64, &data);
  270. fprintf(stderr, "%d\n", dataLen);
  271. for(i=0; i < dataLen; i+=0x10) {
  272. printf("0x%02x: ", i);
  273. for(j=0; j < 0x10; j++)
  274. if((j+i) < dataLen)
  275. printf("%02x ", data[i+j]);
  276. else
  277. printf(" ");
  278. printf(" | ");
  279. for(j=0; j < 0x10; j++)
  280. if((j+i) < dataLen) {
  281. #ifdef CURL_DOES_CONVERSIONS
  282. if(CURLE_OK !=
  283. Curl_convert_from_network(handle, &data[i+j], (size_t)1))
  284. data[i+j] = '.';
  285. #endif /* CURL_DOES_CONVERSIONS */
  286. printf("%c", ISGRAPH(data[i+j])?data[i+j]:'.');
  287. } else
  288. break;
  289. puts("");
  290. }
  291. #ifdef CURL_DOES_CONVERSIONS
  292. curl_easy_cleanup(handle);
  293. #endif
  294. free(base64); free(data);
  295. return 0;
  296. }
  297. #endif
  298. #ifdef TEST_NEED_SUCK
  299. /* this function 'sucks' in as much as possible from stdin */
  300. void *suck(int *lenptr)
  301. {
  302. int cursize = 8192;
  303. unsigned char *buf = NULL;
  304. int lastread;
  305. int len = 0;
  306. do {
  307. cursize *= 2;
  308. buf = (unsigned char *)realloc(buf, cursize);
  309. memset(buf + len, 0, cursize - len);
  310. lastread = fread(buf + len, 1, cursize - len, stdin);
  311. len += lastread;
  312. } while(!feof(stdin));
  313. lenptr[0] = len;
  314. return (void *)buf;
  315. }
  316. #endif