From bed2510078b46c35b95941e59327b1008e4bd176 Mon Sep 17 00:00:00 2001 From: "sgunderson@bigfoot.com" <> Date: Sun, 31 May 2009 20:15:06 +0200 Subject: [PATCH] Let the bit source alert about EOF as soon as we have started eating into padding. Finally fixes EOF issues in driver. --- bitsource.c | 23 +++++++++++++---------- bitsource.h | 2 ++ driver.c | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/bitsource.c b/bitsource.c index 7a4ea4a..b1593aa 100644 --- a/bitsource.c +++ b/bitsource.c @@ -45,17 +45,20 @@ void possibly_refill_slow_path(struct bit_source* source, unsigned num_bits) fprintf(stderr, "Input function returned error\n"); exit(1); } - if (bytes_read == 0 && source->padding_bytes_available > 0) { - unsigned padding_to_add = source->padding_bytes_available; - if (padding_to_add > bytes_to_read) { - padding_to_add = bytes_to_read; + if (bytes_read == 0) { + source->source_eof = true; + if (source->padding_bytes_available > 0) { + unsigned padding_to_add = source->padding_bytes_available; + if (padding_to_add > bytes_to_read) { + padding_to_add = bytes_to_read; + } + memset(source->bytes + source->bytes_available, 0, padding_to_add); + source->padding_bytes_available -= padding_to_add; + source->bytes_available += padding_to_add; + } else { + fprintf(stderr, "Premature EOF\n"); + exit(1); } - memset(source->bytes + source->bytes_available, 0, padding_to_add); - source->padding_bytes_available -= padding_to_add; - source->bytes_available += padding_to_add; - } else if (bytes_read == 0) { - fprintf(stderr, "Premature EOF\n"); - exit(1); } else { source->bytes_available += bytes_read; } diff --git a/bitsource.h b/bitsource.h index ae29b4c..60744b3 100644 --- a/bitsource.h +++ b/bitsource.h @@ -2,6 +2,7 @@ #define _BITSOURCE_H 1 #include +#include #include #include #include @@ -45,6 +46,7 @@ struct bit_source { // Data source. input_func_t* input_func; void* userdata; + bool source_eof; }; void init_bit_source(struct bit_source* source, input_func_t* input_func, diff --git a/driver.c b/driver.c index aa803d6..35934da 100644 --- a/driver.c +++ b/driver.c @@ -151,7 +151,7 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab unsigned mcu_x = 0, mcu_y = 0; - for ( ;; ) { + while (!bits.source_eof) { for (unsigned c = 0; c < num_components; ++c) { unsigned cn = component_num[c]; unsigned stride = image->num_blocks_horizontal * image->hsample[cn] * DCTSIZE; -- 2.39.2