base64.c 7.9 KB

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