plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

pemencode.c (1004B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <bio.h>
      4 #include <mp.h>
      5 #include <libsec.h>
      6 
      7 void
      8 usage(void)
      9 {
     10 	fprint(2, "auth/pemdecode section [file]\n");
     11 	exits("usage");
     12 }
     13 
     14 void
     15 main(int argc, char **argv)
     16 {
     17 	char *buf, *cbuf;
     18 	int fd;
     19 	long n, tot;
     20 	int len;
     21 	char *tag, *file;
     22 
     23 	ARGBEGIN{
     24 	default:
     25 		usage();
     26 	}ARGEND
     27 
     28 	if(argc != 1 && argc != 2)
     29 		usage();
     30 
     31 	tag = argv[0];
     32 	if(argc == 2)
     33 		file = argv[1];
     34 	else
     35 		file = "/dev/stdin";
     36 
     37 	if((fd = open(file, OREAD)) < 0)
     38 		sysfatal("open %s: %r", file);
     39 	buf = nil;
     40 	tot = 0;
     41 	for(;;){
     42 		buf = realloc(buf, tot+8192);
     43 		if(buf == nil)
     44 			sysfatal("realloc: %r");
     45 		if((n = read(fd, buf+tot, 8192)) < 0)
     46 			sysfatal("read: %r");
     47 		if(n == 0)
     48 			break;
     49 		tot += n;
     50 	}
     51 	buf[tot] = 0;
     52 	cbuf = malloc(2*tot);
     53 	if(cbuf == nil)
     54 		sysfatal("malloc: %r");
     55 	len = enc64(cbuf, 2*tot, (uchar*)buf, tot);
     56 	print("-----BEGIN %s-----\n", tag);
     57 	while(len > 0){
     58 		print("%.64s\n", cbuf);
     59 		cbuf += 64;
     60 		len -= 64;
     61 	}
     62 	print("-----END %s-----\n", tag);
     63 	exits(0);
     64 }