base64.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2001, Andrew Francis and Daniel Stenberg
  9. *
  10. * In order to be useful for every potential user, curl and libcurl are
  11. * dual-licensed under the MPL and the MIT/X-derivate licenses.
  12. *
  13. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  14. * copies of the Software, and permit persons to whom the Software is
  15. * furnished to do so, under the terms of the MPL or the MIT/X-derivate
  16. * licenses. You may pick one of these licenses.
  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 "base64.h"
  39. #ifdef MALLOCDEBUG
  40. #include "memdebug.h"
  41. #endif
  42. static void decodeQuantum(unsigned char *dest, char *src)
  43. {
  44. unsigned int x = 0;
  45. int i;
  46. for(i = 0; i < 4; i++) {
  47. if(src[i] >= 'A' && src[i] <= 'Z')
  48. x = (x << 6) + (unsigned int)(src[i] - 'A' + 0);
  49. else if(src[i] >= 'a' && src[i] <= 'z')
  50. x = (x << 6) + (unsigned int)(src[i] - 'a' + 26);
  51. else if(src[i] >= '0' && src[i] <= '9')
  52. x = (x << 6) + (unsigned int)(src[i] - '0' + 52);
  53. else if(src[i] == '+')
  54. x = (x << 6) + 62;
  55. else if(src[i] == '/')
  56. x = (x << 6) + 63;
  57. }
  58. dest[2] = (unsigned char)(x & 255); x >>= 8;
  59. dest[1] = (unsigned char)(x & 255); x >>= 8;
  60. dest[0] = (unsigned char)(x & 255); x >>= 8;
  61. }
  62. /* base64Decode
  63. * Given a base64 string at src, decode it into the memory pointed
  64. * to by dest. If rawLength points to a valid address (ie not NULL),
  65. * store the length of the decoded data to it.
  66. */
  67. static void base64Decode(unsigned char *dest, char *src, int *rawLength)
  68. {
  69. int length = 0;
  70. int equalsTerm = 0;
  71. int i;
  72. unsigned char lastQuantum[3];
  73. while((src[length] != '=') && src[length])
  74. length++;
  75. while(src[length+equalsTerm] == '=')
  76. equalsTerm++;
  77. if(rawLength)
  78. *rawLength = (length * 3 / 4) - equalsTerm;
  79. for(i = 0; i < length/4 - 1; i++) {
  80. decodeQuantum(dest, src);
  81. dest += 3; src += 4;
  82. }
  83. decodeQuantum(lastQuantum, src);
  84. for(i = 0; i < 3 - equalsTerm; i++) dest[i] = lastQuantum[i];
  85. }
  86. /* ---- Base64 Encoding --- */
  87. static char table64[]=
  88. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  89. /*
  90. * Curl_base64_encode()
  91. *
  92. * Returns the length of the newly created base64 string. The third argument
  93. * is a pointer to an allocated area holding the base64 data. If something
  94. * went wrong, -1 is returned.
  95. *
  96. */
  97. int Curl_base64_encode(const void *inp, int insize, char **outptr)
  98. {
  99. unsigned char ibuf[3];
  100. unsigned char obuf[4];
  101. int i;
  102. int inputparts;
  103. char *output;
  104. char *base64data;
  105. char *indata = (char *)inp;
  106. if(0 == insize)
  107. insize = strlen(indata);
  108. base64data = output = (char*)malloc(insize*4/3+4);
  109. if(NULL == output)
  110. return -1;
  111. while(insize > 0) {
  112. for (i = inputparts = 0; i < 3; i++) {
  113. if(*indata) {
  114. inputparts++;
  115. ibuf[i] = *indata;
  116. indata++;
  117. insize--;
  118. }
  119. else
  120. ibuf[i] = 0;
  121. }
  122. obuf [0] = (ibuf [0] & 0xFC) >> 2;
  123. obuf [1] = ((ibuf [0] & 0x03) << 4) | ((ibuf [1] & 0xF0) >> 4);
  124. obuf [2] = ((ibuf [1] & 0x0F) << 2) | ((ibuf [2] & 0xC0) >> 6);
  125. obuf [3] = ibuf [2] & 0x3F;
  126. switch(inputparts) {
  127. case 1: /* only one byte read */
  128. sprintf(output, "%c%c==",
  129. table64[obuf[0]],
  130. table64[obuf[1]]);
  131. break;
  132. case 2: /* two bytes read */
  133. sprintf(output, "%c%c%c=",
  134. table64[obuf[0]],
  135. table64[obuf[1]],
  136. table64[obuf[2]]);
  137. break;
  138. default:
  139. sprintf(output, "%c%c%c%c",
  140. table64[obuf[0]],
  141. table64[obuf[1]],
  142. table64[obuf[2]],
  143. table64[obuf[3]] );
  144. break;
  145. }
  146. output += 4;
  147. }
  148. *output=0;
  149. *outptr = base64data; /* make it return the actual data memory */
  150. return strlen(base64data); /* return the length of the new data */
  151. }
  152. /* ---- End of Base64 Encoding ---- */
  153. int Curl_base64_decode(const char *str, void *data)
  154. {
  155. int ret;
  156. base64Decode((unsigned char *)data, (char *)str, &ret);
  157. return ret;
  158. }
  159. /************* TEST HARNESS STUFF ****************/
  160. #ifdef TEST_ENCODE
  161. /* encoding test harness. Read in standard input and write out the length
  162. * returned by Curl_base64_encode, followed by the base64'd data itself
  163. */
  164. #include <stdio.h>
  165. #define TEST_NEED_SUCK
  166. void *suck(int *);
  167. int main(int argc, char **argv, char **envp) {
  168. char *base64;
  169. int base64Len;
  170. unsigned char *data;
  171. int dataLen;
  172. data = (unsigned char *)suck(&dataLen);
  173. base64Len = Curl_base64_encode(data, dataLen, &base64);
  174. fprintf(stderr, "%d\n", base64Len);
  175. fprintf(stdout, "%s", base64);
  176. free(base64); free(data);
  177. return 0;
  178. }
  179. #endif
  180. #ifdef TEST_DECODE
  181. /* decoding test harness. Read in a base64 string from stdin and write out the
  182. * length returned by Curl_base64_decode, followed by the decoded data itself
  183. */
  184. #include <stdio.h>
  185. #define TEST_NEED_SUCK
  186. void *suck(int *);
  187. int main(int argc, char **argv, char **envp) {
  188. char *base64;
  189. int base64Len;
  190. unsigned char *data;
  191. int dataLen;
  192. base64 = (char *)suck(&base64Len);
  193. data = (unsigned char *)malloc(base64Len * 3/4 + 8);
  194. dataLen = Curl_base64_decode(base64, data);
  195. fprintf(stderr, "%d\n", dataLen);
  196. fwrite(data,1,dataLen,stdout);
  197. free(base64); free(data);
  198. return 0;
  199. }
  200. #endif
  201. #ifdef TEST_NEED_SUCK
  202. /* this function 'sucks' in as much as possible from stdin */
  203. void *suck(int *lenptr) {
  204. int cursize = 8192;
  205. unsigned char *buf = NULL;
  206. int lastread;
  207. int len = 0;
  208. do {
  209. cursize *= 2;
  210. buf = (unsigned char *)realloc(buf, cursize);
  211. memset(buf + len, 0, cursize - len);
  212. lastread = fread(buf + len, 1, cursize - len, stdin);
  213. len += lastread;
  214. } while(!feof(stdin));
  215. lenptr[0] = len;
  216. return (void *)buf;
  217. }
  218. #endif
  219. /*
  220. * local variables:
  221. * eval: (load-file "../curl-mode.el")
  222. * end:
  223. * vim600: fdm=marker
  224. * vim: et sw=2 ts=2 sts=2 tw=78
  225. */