From: Steinar H. Gunderson Date: Sat, 3 Jan 2009 10:26:04 +0000 (+0100) Subject: Move some common input stuff around. X-Git-Url: https://git.sesse.net/?p=fjl;a=commitdiff_plain;h=352c52d38c212a2aa93a9970731c9d7a2f8d4dac;ds=sidebyside Move some common input stuff around. --- diff --git a/Makefile b/Makefile index 299b1e3..a30f390 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ BITSOURCE_TEST_OBJS=bitsource.o bitsource_test.o bitsource_test: $(BITSOURCE_TEST_OBJS) $(CC) $(LDFLAGS) -o $@ $(BITSOURCE_TEST_OBJS) -DEHUFF_TEST_OBJS=dehuff.o bitsource.o dehuff_test.o +DEHUFF_TEST_OBJS=dehuff.o input.o bitsource.o dehuff_test.o dehuff_test: $(DEHUFF_TEST_OBJS) $(CC) $(LDFLAGS) -o $@ $(DEHUFF_TEST_OBJS) diff --git a/bitsource.h b/bitsource.h index e5bdd28..6cabbf0 100644 --- a/bitsource.h +++ b/bitsource.h @@ -7,6 +7,8 @@ #include #include +#include "input.h" + // Optimize for 64 bits. We might want to replace this for 32-bit machines // (benchmark later). typedef uint64_t bitreservoir_t; @@ -21,11 +23,6 @@ static const unsigned BITRESERVOIR_SIZE = 8 * sizeof(bitreservoir_t); static const unsigned BITRESERVOIR_FILL_SIZE = 8 * sizeof(bitreservoir_fill_t); static const unsigned BYTERESERVOIR_SIZE = 4096; -// A function to read bytes from some input source. The bytes should be -// already unstuffed (and thus without markers). -// A return value of -1 indicates error, a return value of 0 indicates EOF. -typedef ssize_t (input_func_t)(void*, uint8_t*, size_t); - // A data source for efficient reading of bit-level data. struct bit_source { // Short-term bit reservoir; holds up to 64 bits. When it's empty, diff --git a/bytesource.c b/bytesource.c index 25cea22..4ebe79c 100644 --- a/bytesource.c +++ b/bytesource.c @@ -1,5 +1,3 @@ -#include -#include #include #include #include diff --git a/bytesource.h b/bytesource.h index fe17259..4e9e307 100644 --- a/bytesource.h +++ b/bytesource.h @@ -4,11 +4,9 @@ #include #include -static const unsigned BYTESOURCE_CHUNK_SIZE = 4096; +#include "input.h" -// Same as input_func_t, although it expects raw bytes, ie. with markers -// and all. -typedef ssize_t (raw_input_func_t)(void*, uint8_t*, size_t); +static const unsigned BYTESOURCE_CHUNK_SIZE = 4096; // A data source taking in a byte stream and returning unstuffed bytes until // there's a marker. When there's a marker, it artificially returns EOF until diff --git a/dehuff.c b/dehuff.c index 9b296a7..8fd4c20 100644 --- a/dehuff.c +++ b/dehuff.c @@ -4,36 +4,7 @@ #include "bytesource.h" #include "dehuff.h" - -void reliable_read(raw_input_func_t* input_func, void* userdata, uint8_t* buf, size_t len) -{ - while (len > 0) { - ssize_t bytes_read = input_func(userdata, buf, len); - assert(bytes_read <= (ssize_t)len); - - // TODO: We need better error handling here. setjmp()/longjmp() - // should hopefully do the trick, but we need to take care for - // suspension. - if (bytes_read == (ssize_t)-1) { - fprintf(stderr, "Input function returned error\n"); - exit(1); - } - if (bytes_read == 0) { - fprintf(stderr, "Premature EOF\n"); - exit(1); - } - - buf += bytes_read; - len -= bytes_read; - } -} - -uint16_t read_length(raw_input_func_t* input_func, void* userdata) -{ - uint8_t buf[2]; - reliable_read(input_func, userdata, buf, 2); - return (buf[0] << 8) | buf[1]; -} +#include "input.h" void read_huffman_tables(huffman_tables_t* dst, input_func_t* input_func, void* userdata) { diff --git a/input.c b/input.c new file mode 100644 index 0000000..2d80457 --- /dev/null +++ b/input.c @@ -0,0 +1,36 @@ +#include +#include +#include + +#include "input.h" + +void reliable_read(raw_input_func_t* input_func, void* userdata, uint8_t* buf, size_t len) +{ + while (len > 0) { + ssize_t bytes_read = input_func(userdata, buf, len); + assert(bytes_read <= (ssize_t)len); + + // TODO: We need better error handling here. setjmp()/longjmp() + // should hopefully do the trick, but we need to take care for + // suspension. + if (bytes_read == (ssize_t)-1) { + fprintf(stderr, "Input function returned error\n"); + exit(1); + } + if (bytes_read == 0) { + fprintf(stderr, "Premature EOF\n"); + exit(1); + } + + buf += bytes_read; + len -= bytes_read; + } +} + +uint16_t read_length(raw_input_func_t* input_func, void* userdata) +{ + uint8_t buf[2]; + reliable_read(input_func, userdata, buf, 2); + return (buf[0] << 8) | buf[1]; +} + diff --git a/input.h b/input.h new file mode 100644 index 0000000..be60c5a --- /dev/null +++ b/input.h @@ -0,0 +1,19 @@ +#ifndef _INPUT_H +#define _INPUT_H 1 + +#include +#include + +// A function to read bytes from some input source. The bytes should be +// already unstuffed (and thus without markers). +// A return value of -1 indicates error, a return value of 0 indicates EOF. +typedef ssize_t (input_func_t)(void*, uint8_t*, size_t); + +// Same as input_func_t, although it expects raw bytes, ie. with markers +// and all. +typedef ssize_t (raw_input_func_t)(void*, uint8_t*, size_t); + +void reliable_read(raw_input_func_t* input_func, void* userdata, uint8_t* buf, size_t len); +uint16_t read_length(raw_input_func_t* input_func, void* userdata); + +#endif /* !defined(_INPUT_H) */