~apreiml/hare-utils

c1b38787b9088cf2f6f531f4ac5daece2fa1dac2 — Armin Preiml 1 year, 6 months ago 352e8ab
pemdump: add binary option
1 files changed, 29 insertions(+), 4 deletions(-)

M pemdump.ha
M pemdump.ha => pemdump.ha +29 -4
@@ 2,27 2,48 @@ use fmt;
use errors;
use encoding::pem;
use encoding::hex;
use getopt;
use os;
use io;
use bufio;

export fn main() void = {
	const cmd = getopt::parse(os::args,
		"pemdump",
		('b', "binary output"),
	);
	defer getopt::finish(&cmd);

	let binary = false;
	for (let i = 0z; i < len(cmd.opts); i += 1) {
		const opt = cmd.opts[i];
		switch (opt.0) {
		case 'b' =>
			binary = true;
		};
	};

	let d = pem::newdecoder(os::stdin);

	for (true) {
	for (let i = 0z; true; i += 1) {
		let dec = match (pem::next(&d)) {
		case io::EOF =>
			return;
		case let e: io::error =>
			fmt::fatal(pem::strerror(e));
		case let e: (str, pem::pemdecoder) =>
			fmt::println(e.0)!;
			if (!binary) {
				fmt::println(e.0)!;
			};
			yield e.1;
		};

		if (i > 0 && binary) {
			fmt::fatal("More than one cert");
		};

		let buf = bufio::dynamic(io::mode::WRITE);

		io::write(&buf, [0xff, 0xaa])!;
		match (io::copy(&buf, &dec)) {
		case errors::invalid =>
			fmt::fatal("invalid PEM format");


@@ 32,7 53,11 @@ export fn main() void = {
			yield;
		};

		hex::dump(os::stdout, bufio::buffer(&buf))!;
		if (binary) {
			io::writeall(os::stdout, bufio::buffer(&buf))!;
		} else {
			hex::dump(os::stdout, bufio::buffer(&buf))!;
		};
	};
};