base64.c 7.1 KB

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