~sircmpwn/hare-compress

cc2603261312bd740562e97ff446453ff6cd2fd6 — Drew DeVault 1 year, 5 months ago bb5978b
compress::zlib: introduce decompress_error

Matches the similar inflate_error function.

Signed-off-by: Drew DeVault <sir@cmpwn.com>
1 files changed, 38 insertions(+), 12 deletions(-)

M compress/zlib/reader.ha
M compress/zlib/reader.ha => compress/zlib/reader.ha +38 -12
@@ 28,26 28,33 @@ export type reader = struct {
	hash: adler32::state,
};

type decompress_err = enum {
// List of zlib error conditions.
export type decompress_err = enum {
	// Invalid zlib header
	HEADER,
	// Invalid zlib checksum
	CHECKSUM,
	// Unexpected EOF
	EOF,
	// Invalid dictionary
	DICT,
};

fn opaque_strerror(
	data: *errors::opaque_data
) const str = switch (*(data: *decompress_err)) {
case decompress_err::HEADER =>
	yield "Invalid zlib header";
case decompress_err::CHECKSUM =>
	yield "Invalid zlib checksum";
case decompress_err::EOF =>
	yield "Unexpected EOF";
case decompress_err::DICT =>
	yield "Invalid dictionary";
case =>
	abort();
) const str = {
	switch (*(data: *decompress_err)) {
	case decompress_err::HEADER =>
		return "Invalid zlib header";
	case decompress_err::CHECKSUM =>
		return "Invalid zlib checksum";
	case decompress_err::EOF =>
		return "Unexpected EOF";
	case decompress_err::DICT =>
		return "Invalid dictionary";
	case =>
		abort();
	};
};

fn wraperror(err: decompress_err) errors::opaque = {


@@ 58,6 65,25 @@ fn wraperror(err: decompress_err) errors::opaque = {
	return wrapped;
};

// Returns the zlib-specific error for a given [[io::error]], assuming it was a
// decompress error that caused the issue. If not, it returns the
// [[flate::inflate_err]] if appropriate; otherwise returns the original I/O
// error.
export fn decompress_error(
	err: io::error,
) (decompress_err | flate::inflate_err | io::error) = {
	match (err) {
	case let op: errors::opaque =>
		if (op.strerror != &opaque_strerror) {
			return flate::inflate_error(err);
		};
		const err = &op.data: *decompress_err;
		return *err;
	case let err: io::error =>
		return err;
	};
};

fn verifysum(s: *reader) (io::EOF | io::error) = {
	let hash: [adler32::SIZE]u8 = [0...];