Refactor compress.Context struct.

This commit is contained in:
Jeroen van Rijn
2021-06-26 22:25:55 +02:00
parent 30a5808460
commit 4689a6b341
3 changed files with 17 additions and 13 deletions
+14 -10
View File
@@ -127,28 +127,30 @@ Deflate_Error :: enum {
// General I/O context for ZLIB, LZW, etc. // General I/O context for ZLIB, LZW, etc.
Context :: struct { Context :: struct #packed {
input_data: []u8, input_data: []u8,
input: io.Stream, input: io.Stream,
output: ^bytes.Buffer, output: ^bytes.Buffer,
bytes_written: i64, 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, code_buffer: u64,
num_bits: u64, num_bits: u64,
/*
If we know the data size, we can optimize the reads and writes.
*/
size_packed: i64,
size_unpacked: i64,
/* /*
Flags: Flags:
`input_fully_in_memory` tells us whether we're EOF when `input_data` is empty. `input_fully_in_memory`
`input_refills_from_stream` tells us we can then possibly refill from the stream. 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_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. 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") @(optimization_mode="speed")
read_slice :: #force_inline proc(z: ^Context, size: int) -> (res: []u8, err: io.Error) { read_slice :: #force_inline proc(z: ^Context, size: int) -> (res: []u8, err: io.Error) {
#no_bounds_check { #no_bounds_check {
-1
View File
@@ -113,7 +113,6 @@ load_from_slice :: proc(slice: []u8, buf: ^bytes.Buffer, known_gzip_size := -1,
input = stream, input = stream,
input_data = slice, input_data = slice,
input_fully_in_memory = true, input_fully_in_memory = true,
input_refills_from_stream = true,
output = buf, output = buf,
}; };
+3 -2
View File
@@ -430,8 +430,7 @@ inflate_from_stream :: proc(using ctx: ^Context, raw := false, expected_output_s
- read - read
- size - size
ctx.output must be an io.Stream backed by an implementation that supports: ctx.output must be a bytes.Buffer for now. We'll add a separate implementation that writes to a stream.
- write
raw determines whether the ZLIB header is processed, or we're inflating a raw raw determines whether the ZLIB header is processed, or we're inflating a raw
DEFLATE stream. DEFLATE stream.
@@ -499,6 +498,8 @@ inflate_from_stream :: proc(using ctx: ^Context, raw := false, expected_output_s
return nil; return nil;
} }
// TODO: Check alignment of reserve/resize.
@(optimization_mode="speed") @(optimization_mode="speed")
inflate_from_stream_raw :: proc(z: ^Context, expected_output_size := -1, allocator := context.allocator) -> (err: Error) #no_bounds_check { 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; expected_output_size := expected_output_size;