test-base64.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Base64 encoding/decoding (RFC1341) - test program
  3. * Copyright (c) 2005, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/os.h"
  16. #include "utils/base64.h"
  17. int main(int argc, char *argv[])
  18. {
  19. FILE *f;
  20. size_t len, elen;
  21. unsigned char *buf, *e;
  22. if (argc != 4) {
  23. printf("Usage: base64 <encode|decode> <in file> <out file>\n");
  24. return -1;
  25. }
  26. buf = (unsigned char *) os_readfile(argv[2], &len);
  27. if (buf == NULL)
  28. return -1;
  29. if (strcmp(argv[1], "encode") == 0)
  30. e = base64_encode(buf, len, &elen);
  31. else
  32. e = base64_decode(buf, len, &elen);
  33. if (e == NULL)
  34. return -2;
  35. f = fopen(argv[3], "w");
  36. if (f == NULL)
  37. return -3;
  38. fwrite(e, 1, elen, f);
  39. fclose(f);
  40. free(e);
  41. return 0;
  42. }