test-base64.c 831 B

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