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)
#include <sys/types.h>
#include <arpa/inet.h>
+#include "input.h"
+
// Optimize for 64 bits. We might want to replace this for 32-bit machines
// (benchmark later).
typedef uint64_t 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,
-#include <stdio.h>
-#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include <sys/types.h>
-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
#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)
{
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#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];
+}
+
--- /dev/null
+#ifndef _INPUT_H
+#define _INPUT_H 1
+
+#include <stdint.h>
+#include <sys/types.h>
+
+// 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) */