~apreiml/hare-utils

e404f8e81f4d1abd9c4dad22e86a22036e075d55 — Armin Preiml 1 year, 11 months ago ed9927d
pemdump
2 files changed, 41 insertions(+), 1 deletions(-)

M Makefile
A pemdump.ha
M Makefile => Makefile +3 -1
@@ 1,8 1,10 @@

all: bin u8dump
all: bin u8dump pemdump

bin:
	mkdir -p bin

u8dump: u8dump.ha bin
	hare build -o bin/u8dump u8dump.ha
pemdump: pemdump.ha bin
	hare build -o bin/pemdump pemdump.ha

A pemdump.ha => pemdump.ha +38 -0
@@ 0,0 1,38 @@
use fmt;
use errors;
use encoding::pem;
use encoding::hex;
use os;
use io;
use bufio;

export fn main() void = {
	let d = pem::newdecoder(os::stdin);

	for (true) {
		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)!;
			yield e.1;
		};

		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");
		case let e: io::error =>
			fmt::fatal(io::strerror(e));
		case size =>
			yield;
		};

		hex::dump(os::stdout, bufio::buffer(&buf))!;
	};
};