pemencode.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <mp.h>
  13. #include <libsec.h>
  14. void
  15. usage(void)
  16. {
  17. fprint(2, "auth/pemdecode section [file]\n");
  18. exits("usage");
  19. }
  20. void
  21. main(int argc, char **argv)
  22. {
  23. char *buf, *cbuf;
  24. int fd;
  25. int32_t n, tot;
  26. int len;
  27. char *tag, *file;
  28. ARGBEGIN{
  29. default:
  30. usage();
  31. }ARGEND
  32. if(argc != 1 && argc != 2)
  33. usage();
  34. tag = argv[0];
  35. if(argc == 2)
  36. file = argv[1];
  37. else
  38. file = "#d/0";
  39. if((fd = open(file, OREAD)) < 0)
  40. sysfatal("open %s: %r", file);
  41. buf = nil;
  42. tot = 0;
  43. for(;;){
  44. buf = realloc(buf, tot+8192);
  45. if(buf == nil)
  46. sysfatal("realloc: %r");
  47. if((n = read(fd, buf+tot, 8192)) < 0)
  48. sysfatal("read: %r");
  49. if(n == 0)
  50. break;
  51. tot += n;
  52. }
  53. buf[tot] = 0;
  54. cbuf = malloc(2*tot);
  55. if(cbuf == nil)
  56. sysfatal("malloc: %r");
  57. len = enc64(cbuf, 2*tot, (unsigned char*)buf, tot);
  58. print("-----BEGIN %s-----\n", tag);
  59. while(len > 0){
  60. print("%.64s\n", cbuf);
  61. cbuf += 64;
  62. len -= 64;
  63. }
  64. print("-----END %s-----\n", tag);
  65. exits(0);
  66. }