Refactor ZLIB structs.

This commit is contained in:
Jeroen van Rijn
2021-06-23 16:32:47 +02:00
parent bb3ffdbdfb
commit 8663c64e47
6 changed files with 176 additions and 133 deletions
+7 -7
View File
@@ -12,7 +12,6 @@ package gzip
A small GZIP implementation as an example.
*/
import "core:compress/gzip"
import "core:bytes"
import "core:os"
@@ -31,7 +30,7 @@ TEST: []u8 = {
main :: proc() {
// Set up output buffer.
buf: bytes.Buffer;
buf := bytes.Buffer{};
stdout :: proc(s: string) {
os.write_string(os.stdout, s);
@@ -44,26 +43,27 @@ main :: proc() {
if len(args) < 2 {
stderr("No input file specified.\n");
err := gzip.load(TEST, &buf);
if err != nil {
err := load(TEST, &buf);
if err == nil {
stdout("Displaying test vector: ");
stdout(bytes.buffer_to_string(&buf));
stdout("\n");
}
bytes.buffer_destroy(&buf);
os.exit(0);
}
// The rest are all files.
args = args[1:];
err: gzip.Error;
err: Error;
for file in args {
if file == "-" {
// Read from stdin
s := os.stream_from_handle(os.stdin);
err = gzip.load(s, &buf);
err = load(s, &buf);
} else {
err = gzip.load(file, &buf);
err = load(file, &buf);
}
if err != nil {
if err != E_General.File_Not_Found {
+6 -4
View File
@@ -281,8 +281,10 @@ load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := con
/*
We should have arrived at the ZLIB payload.
*/
code_buffer := compress.Code_Buffer{};
cb := &code_buffer;
zlib_error := zlib.inflate_raw(&ctx);
zlib_error := zlib.inflate_raw(&ctx, &code_buffer);
// fmt.printf("ZLIB returned: %v\n", zlib_error);
@@ -293,16 +295,16 @@ load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := con
/*
Read CRC32 using the ctx bit reader because zlib may leave bytes in there.
*/
compress.discard_to_next_byte_lsb(&ctx);
compress.discard_to_next_byte_lsb(cb);
payload_crc_b: [4]u8;
payload_len_b: [4]u8;
for _, i in payload_crc_b {
payload_crc_b[i] = u8(compress.read_bits_lsb(&ctx, 8));
payload_crc_b[i] = u8(compress.read_bits_lsb(&ctx, cb, 8));
}
payload_crc := transmute(u32le)payload_crc_b;
for _, i in payload_len_b {
payload_len_b[i] = u8(compress.read_bits_lsb(&ctx, 8));
payload_len_b[i] = u8(compress.read_bits_lsb(&ctx, cb, 8));
}
payload_len := int(transmute(u32le)payload_len_b);