]> git.sesse.net Git - fjl/commitdiff
Move some common input stuff around.
authorSteinar H. Gunderson <sesse@debian.org>
Sat, 3 Jan 2009 10:26:04 +0000 (11:26 +0100)
committerSteinar H. Gunderson <sesse@debian.org>
Sat, 3 Jan 2009 10:26:04 +0000 (11:26 +0100)
Makefile
bitsource.h
bytesource.c
bytesource.h
dehuff.c
input.c [new file with mode: 0644]
input.h [new file with mode: 0644]

index 299b1e3ab88ea845ab9a76237467f981ec68ff71..a30f39098a04a9bbaaee13a6b376d68351fe0ad5 100644 (file)
--- 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)
 
 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)
 
 dehuff_test: $(DEHUFF_TEST_OBJS)
        $(CC) $(LDFLAGS) -o $@ $(DEHUFF_TEST_OBJS)
 
index e5bdd282ae104de0c9017f651efa6a7d60dd3751..6cabbf004245139d6f3f5a8f5f5fac5cfdb646e6 100644 (file)
@@ -7,6 +7,8 @@
 #include <sys/types.h>
 #include <arpa/inet.h>
 
 #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;
 // 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;
 
 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,
 // 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,
index 25cea227e399cce151228489642004e6ad49fe7d..4ebe79c8d3e5afc34b3fed144ac28a5f6fef7bcb 100644 (file)
@@ -1,5 +1,3 @@
-#include <stdio.h>
-#include <stdlib.h>
 #include <stdbool.h>
 #include <string.h>
 #include <assert.h>
 #include <stdbool.h>
 #include <string.h>
 #include <assert.h>
index fe1725981710746ed542d99fb473c4dd2c8c5660..4e9e307bbc138325bb2d5a181bf4d9acbf1fb5c6 100644 (file)
@@ -4,11 +4,9 @@
 #include <stdint.h>
 #include <sys/types.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
 
 // 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
index 9b296a798075bf6f6f01e2bd439c8c4b7da1bba2..8fd4c2072fce46cbe792295fc977a3e7f53b2deb 100644 (file)
--- a/dehuff.c
+++ b/dehuff.c
@@ -4,36 +4,7 @@
 
 #include "bytesource.h"
 #include "dehuff.h"
 
 #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)
 {
 
 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 (file)
index 0000000..2d80457
--- /dev/null
+++ b/input.c
@@ -0,0 +1,36 @@
+#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];
+}
+
diff --git a/input.h b/input.h
new file mode 100644 (file)
index 0000000..be60c5a
--- /dev/null
+++ b/input.h
@@ -0,0 +1,19 @@
+#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) */