]> git.sesse.net Git - fjl/blobdiff - dehuff.c
Move some common input stuff around.
[fjl] / dehuff.c
index 0a9fbbcf1a4bcf1b2eb9f413eca7f1784503397e..8fd4c2072fce46cbe792295fc977a3e7f53b2deb 100644 (file)
--- a/dehuff.c
+++ b/dehuff.c
@@ -2,39 +2,11 @@
 #include <stdlib.h>
 #include <assert.h>
 
+#include "bytesource.h"
 #include "dehuff.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 <= 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];
-}
-
-void read_huffman_tables(huffman_tables_t* dst, raw_input_func_t* input_func, void* userdata)
+void read_huffman_tables(huffman_tables_t* dst, input_func_t* input_func, void* userdata)
 {
        size_t len = read_length(input_func, userdata);
        assert(len > 2);
@@ -158,7 +130,7 @@ unsigned read_huffman_symbol_slow_path(const struct huffman_table* table,
        unsigned code = read_bits(source, 1);
        int i = 0;
 
-       while (code > table->maxcode[i] || table->maxcode[i] == -1) {
+       while (table->maxcode[i] == -1 || code > (unsigned)table->maxcode[i]) {
                possibly_refill(source, 1);
                code = (code << 1) | read_bits(source, 1);
                ++i;