base64.c 7.1 KB

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