From 4689a6b341843e18f712025aac049c2938a0ed21 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sat, 26 Jun 2021 22:25:55 +0200 Subject: [PATCH] Refactor compress.Context struct. --- core/compress/common.odin | 24 ++++++++++++++---------- core/compress/gzip/gzip.odin | 1 - core/compress/zlib/zlib.odin | 5 +++-- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/core/compress/common.odin b/core/compress/common.odin index 47b80a995..d086c71f8 100644 --- a/core/compress/common.odin +++ b/core/compress/common.odin @@ -127,28 +127,30 @@ Deflate_Error :: enum { // General I/O context for ZLIB, LZW, etc. -Context :: struct { +Context :: struct #packed { input_data: []u8, input: io.Stream, output: ^bytes.Buffer, bytes_written: i64, - /* - If we know the data size, we can optimize the reads and writes. - */ - size_packed: i64, - size_unpacked: i64, - code_buffer: u64, num_bits: u64, + /* + If we know the data size, we can optimize the reads and writes. + */ + size_packed: i64, + size_unpacked: i64, + /* Flags: - `input_fully_in_memory` tells us whether we're EOF when `input_data` is empty. - `input_refills_from_stream` tells us we can then possibly refill from the stream. + `input_fully_in_memory` + true = This tells us we read input from `input_data` exclusively. [] = EOF. + false = Try to refill `input_data` from the `input` stream. */ input_fully_in_memory: b8, - input_refills_from_stream: b8, + + padding: [1]u8, } @@ -162,6 +164,8 @@ Context :: struct { This simplifies end-of-stream handling where bits may be left in the bit buffer. */ +// TODO: Make these return compress.Error errors. + @(optimization_mode="speed") read_slice :: #force_inline proc(z: ^Context, size: int) -> (res: []u8, err: io.Error) { #no_bounds_check { diff --git a/core/compress/gzip/gzip.odin b/core/compress/gzip/gzip.odin index 38cb77b20..ac3940763 100644 --- a/core/compress/gzip/gzip.odin +++ b/core/compress/gzip/gzip.odin @@ -113,7 +113,6 @@ load_from_slice :: proc(slice: []u8, buf: ^bytes.Buffer, known_gzip_size := -1, input = stream, input_data = slice, input_fully_in_memory = true, - input_refills_from_stream = true, output = buf, }; diff --git a/core/compress/zlib/zlib.odin b/core/compress/zlib/zlib.odin index aaa549e7b..fcdb42c44 100644 --- a/core/compress/zlib/zlib.odin +++ b/core/compress/zlib/zlib.odin @@ -430,8 +430,7 @@ inflate_from_stream :: proc(using ctx: ^Context, raw := false, expected_output_s - read - size - ctx.output must be an io.Stream backed by an implementation that supports: - - write + ctx.output must be a bytes.Buffer for now. We'll add a separate implementation that writes to a stream. raw determines whether the ZLIB header is processed, or we're inflating a raw DEFLATE stream. @@ -499,6 +498,8 @@ inflate_from_stream :: proc(using ctx: ^Context, raw := false, expected_output_s return nil; } +// TODO: Check alignment of reserve/resize. + @(optimization_mode="speed") inflate_from_stream_raw :: proc(z: ^Context, expected_output_size := -1, allocator := context.allocator) -> (err: Error) #no_bounds_check { expected_output_size := expected_output_size;