xref: /plan9/sys/src/cmd/auth/pemencode.c (revision fb7f0c934c48abaed6040d054ef636408c3c522d)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <mp.h>
5 #include <libsec.h>
6 
7 void
usage(void)8 usage(void)
9 {
10 	fprint(2, "auth/pemdecode section [file]\n");
11 	exits("usage");
12 }
13 
14 void
main(int argc,char ** argv)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 = "#d/0";
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 }
65