shithub: jbig2

Download patch

ref: 90fe198243e4d573baf7ba4839099a17bd6c266d
parent: 5a0a14decb8c3810e122396dcb7c6b4f5ee4f4f7
author: Sebastian Rasmussen <[email protected]>
date: Sun Apr 22 12:11:32 EDT 2018

jbig2dec: Return error code from get_next_word().

Previously Jbig2WordStream's get_next_word() callback
sometimes returned error codes. This contradicted the
declaration of the callback. However indicating errors
and the number of bytes returned upon successful calls
is a good idea. The declaration and all implementations
of the callback have been adjusted accordingly.

This means that jbig2dec now compiles without warnings.

--- a/jbig2.c
+++ b/jbig2.c
@@ -387,21 +387,37 @@
     size_t size;
 } Jbig2WordStreamBuf;
 
-static void
+static int
 jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word)
 {
     Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self;
     const byte *data = z->data;
+    uint32_t val = 0;
+    int ret = 0;
 
-    *word = 0;
-    if (offset + 4 < z->size)
-        *word = (data[offset] << 24) | (data[offset + 1] << 16) | (data[offset + 2] << 8) | data[offset + 3];
-    else if (offset <= z->size) {
-        size_t i;
+    if (self == NULL || word == NULL)
+        return -1;
+    if (offset >= z->size)
+        return 0;
 
-        for (i = 0; i < z->size - offset; i++)
-            *word |= data[offset + i] << ((3 - i) << 3);
+    if (offset < z->size) {
+        val |= data[offset] << 24;
+        ret++;
     }
+    if (offset + 1 < z->size) {
+        val |= data[offset + 1] << 16;
+        ret++;
+    }
+    if (offset + 2 < z->size) {
+        val |= data[offset + 2] << 8;
+        ret++;
+    }
+    if (offset + 3 < z->size) {
+        val |= data[offset + 3];
+        ret++;
+    }
+    *word = val;
+    return ret;
 }
 
 Jbig2WordStream *
--- a/jbig2_arith.c
+++ b/jbig2_arith.c
@@ -332,21 +332,42 @@
 
 #ifdef TEST
 
-static uint32_t
-test_get_word(Jbig2WordStream *self, int offset, uint32_t *word)
+const byte test_stream[] = {
+    0x84, 0xC7, 0x3B, 0xFC, 0xE1, 0xA1, 0x43, 0x04, 0x02, 0x20, 0x00, 0x00,
+    0x41, 0x0D, 0xBB, 0x86, 0xF4, 0x31, 0x7F, 0xFF, 0x88, 0xFF, 0x37, 0x47,
+    0x1A, 0xDB, 0x6A, 0xDF, 0xFF, 0xAC,
+    0x00, 0x00
+};
+
+static int
+test_get_word(Jbig2WordStream *self, size_t offset, uint32_t *word)
 {
-    byte stream[] = {
-        0x84, 0xC7, 0x3B, 0xFC, 0xE1, 0xA1, 0x43, 0x04, 0x02, 0x20, 0x00, 0x00,
-        0x41, 0x0D, 0xBB, 0x86, 0xF4, 0x31, 0x7F, 0xFF, 0x88, 0xFF, 0x37, 0x47,
-        0x1A, 0xDB, 0x6A, 0xDF, 0xFF, 0xAC,
-        0x00, 0x00
-    };
-    if (offset >= sizeof(stream))
-        *word = 0;
-    else
-        *word = (stream[offset] << 24) | (stream[offset + 1] << 16) |
-                (stream[offset + 2] << 8) | stream[offset + 3];
-    return 0;
+    uint32_t val = 0;
+    int ret = 0;
+
+    if (self == NULL || word == NULL)
+        return -1;
+    if (offset >= sizeof (test_stream))
+        return -1;
+
+    if (offset < sizeof(test_stream)) {
+        val |= test_stream[offset] << 24;
+        ret++;
+    }
+    if (offset + 1 < sizeof(test_stream)) {
+        val |= test_stream[offset + 1] << 16;
+        ret++;
+    }
+    if (offset + 2 < sizeof(test_stream)) {
+        val |= test_stream[offset + 2] << 8;
+        ret++;
+    }
+    if (offset + 3 < sizeof(test_stream)) {
+        val |= test_stream[offset + 3];
+        ret++;
+    }
+    *word = val;
+    return ret;
 }
 
 int
--- a/jbig2_huffman.c
+++ b/jbig2_huffman.c
@@ -684,13 +684,34 @@
 const byte test_tabindex[] = { 4, 2, 2, 1 };
 
 static int
-test_get_word(Jbig2WordStream *self, int offset, uint32_t *word)
+test_get_word(Jbig2WordStream *self, size_t offset, uint32_t *word)
 {
-    /* assume test_stream[] is at least 4 bytes */
-    if (offset + 3 > sizeof(test_stream))
+    uint32_t val = 0;
+    int ret = 0;
+
+    if (self == NULL || word == NULL)
         return -1;
-    *word = ((test_stream[offset] << 24) | (test_stream[offset + 1] << 16) | (test_stream[offset + 2] << 8) | (test_stream[offset + 3]));
-    return 0;
+    if (offset >= sizeof (test_stream))
+        return 0;
+
+    if (offset < sizeof(test_stream)) {
+        val |= test_stream[offset] << 24;
+        ret++;
+    }
+    if (offset + 1 < sizeof(test_stream)) {
+        val |= test_stream[offset + 1] << 16;
+        ret++;
+    }
+    if (offset + 2 < sizeof(test_stream)) {
+        val |= test_stream[offset + 2] << 8;
+        ret++;
+    }
+    if (offset + 3 < sizeof(test_stream)) {
+        val |= test_stream[offset + 3];
+        ret++;
+    }
+    *word = val;
+    return ret;
 }
 
 int
@@ -1957,31 +1978,35 @@
 } test_stream_t;
 
 static int
-test_get_word(Jbig2WordStream *self, int offset, uint32_t *word)
+test_get_word(Jbig2WordStream *self, size_t offset, uint32_t *word)
 {
-    uint32_t val = 0;
     test_stream_t *st = (test_stream_t *) self;
+    uint32_t val = 0;
+    int ret = 0;
 
-    if (st != NULL) {
-        if (st->h != NULL) {
-            if (offset >= st->h->input_len)
-                return -1;
-            if (offset < st->h->input_len) {
-                val |= (st->h->input[offset] << 24);
-            }
-            if (offset + 1 < st->h->input_len) {
-                val |= (st->h->input[offset + 1] << 16);
-            }
-            if (offset + 2 < st->h->input_len) {
-                val |= (st->h->input[offset + 2] << 8);
-            }
-            if (offset + 3 < st->h->input_len) {
-                val |= st->h->input[offset + 3];
-            }
-        }
+    if (st == NULL || st->h == NULL || word == NULL)
+        return -1;
+    if (offset >= st->h->input_len)
+        return 0;
+
+    if (offset < st->h->input_len) {
+        val |= (st->h->input[offset] << 24);
+        ret++;
     }
+    if (offset + 1 < st->h->input_len) {
+        val |= (st->h->input[offset + 1] << 16);
+        ret++;
+    }
+    if (offset + 2 < st->h->input_len) {
+        val |= (st->h->input[offset + 2] << 8);
+        ret++;
+    }
+    if (offset + 3 < st->h->input_len) {
+        val |= st->h->input[offset + 3];
+        ret++;
+    }
     *word = val;
-    return 0;
+    return ret;
 }
 
 int
--- a/jbig2_priv.h
+++ b/jbig2_priv.h
@@ -126,7 +126,7 @@
 typedef struct _Jbig2WordStream Jbig2WordStream;
 
 struct _Jbig2WordStream {
-    void (*get_next_word)(Jbig2WordStream *self, size_t offset, uint32_t *word);
+    int (*get_next_word)(Jbig2WordStream *self, size_t offset, uint32_t *word);
 };
 
 Jbig2WordStream *jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size);