commit e2b211de570f3daa04b6aab28f3194d47ba036ae
parent 127957e9696a5c0249f738e453b59e7492e392b2
Author: Eyal Sawady <ecs@d2evs.net>
Date: Sat, 4 Sep 2021 10:39:55 +0000
compress::*: use caller allocation
Signed-off-by: Eyal Sawady <ecs@d2evs.net>
Diffstat:
2 files changed, 22 insertions(+), 27 deletions(-)
diff --git a/compress/flate/inflate.ha b/compress/flate/inflate.ha
@@ -19,7 +19,7 @@ const fixed_dist: huffman = huffman {
symbols = [0...],
};
-type huffman = struct {
+export type huffman = struct {
counts: [MAXBITS]u16,
symbols: [MAXCODES]u16,
};
@@ -34,8 +34,8 @@ type inflate_err = enum u8 {
TABLE,
};
-type decompressor = struct {
- stream: io::stream,
+export type decompressor = struct {
+ io::stream,
in: *io::stream,
bitbuf: u32,
cnt: u32,
@@ -77,12 +77,9 @@ type decompressor = struct {
};
// Creates a stream which decompresses Deflate (RFC 1951) data.
-export fn inflate(s: *io::stream) *io::stream = alloc(decompressor {
- stream = io::stream {
- reader = &read,
- closer = &close,
- ...
- },
+export fn inflate(s: *io::stream) decompressor = decompressor {
+ reader = &read,
+ closer = &close,
in = s,
bitbuf = 0,
cnt = 0,
@@ -95,7 +92,8 @@ export fn inflate(s: *io::stream) *io::stream = alloc(decompressor {
buf = [],
bufstart = [],
-}): *io::stream;
+ ...
+};
// Read [[want]] bits from the decompressor
fn bits(d: *decompressor, want: u32) (u32 | io::error) = {
@@ -411,8 +409,8 @@ fn close(s: *io::stream) void = {
let ins = bufio::fixed(in, io::mode::READ);
let outs = bufio::dynamic(io::mode::WRITE);
let s = inflate(ins);
- defer io::close(s);
- match (io::copy(outs, s)) {
+ defer io::close(&s);
+ match (io::copy(outs, &s)) {
size => void,
e: io::error => {
fmt::errorln(io::strerror(e))!;
diff --git a/compress/zlib/reader.ha b/compress/zlib/reader.ha
@@ -15,10 +15,10 @@ def FCHECK: u8 = 0b00011111;
def FDICT: u8 = 0b00100000;
def FLEVEL: u8 = 0b11000000;
-type reader = struct {
- stream: io::stream,
+export type reader = struct {
+ io::stream,
source: *io::stream,
- flate: *io::stream,
+ flate: flate::decompressor,
hash: adler32::state,
};
@@ -63,7 +63,7 @@ fn verifysum(s: *reader) (io::EOF | io::error) = {
fn read(s: *io::stream, buf: []u8) (size | io::EOF | io::error) = {
let s = s: *reader;
- match (io::read(s.flate, buf)?) {
+ match (io::read(&s.flate, buf)?) {
io::EOF => return verifysum(s),
z: size => buf = buf[..z],
};
@@ -72,11 +72,11 @@ fn read(s: *io::stream, buf: []u8) (size | io::EOF | io::error) = {
fn close(s: *io::stream) void = {
const s = s: *reader;
- io::close(s.flate);
+ io::close(&s.flate);
};
// Creates a stream which decompresses zlib (RFC 1950) data.
-export fn decompress(s: *io::stream) (*io::stream | io::error) = {
+export fn decompress(s: *io::stream) (reader | io::error) = {
let buf: [2]u8 = [0...];
for (let n = 0z; n < len(buf)) {
match (io::read(s, buf[n..])?) {
@@ -101,17 +101,14 @@ export fn decompress(s: *io::stream) (*io::stream | io::error) = {
// be tried
return wraperror(decompress_err::DICT);
};
- return alloc(reader {
- stream = io::stream {
- reader = &read,
- closer = &close,
- ...
- },
+ return reader {
+ reader = &read,
+ closer = &close,
source = s,
flate = flate::inflate(s),
hash = adler32::adler32(),
...
- }): *io::stream;
+ };
};
@test fn decompress() void = {
@@ -119,13 +116,13 @@ export fn decompress(s: *io::stream) (*io::stream | io::error) = {
let in = bufio::fixed(*vectors[i].1, io::mode::READ);
let out = bufio::dynamic(io::mode::WRITE);
let d = match (decompress(in)) {
- s: *io::stream => s,
+ s: reader => s,
e: io::error => {
fmt::errorln(io::strerror(e))!;
abort();
},
};
- match (io::copy(out, d)) {
+ match (io::copy(out, &d)) {
size => void,
e: io::error => {
fmt::errorfln("vector {}: {}", i, io::strerror(e))!;