shithub: jbig2

Download patch

ref: 466be6852e589edac29f9c3dc063df2200daecd4
parent: dc80279ae9ffb4353fb7ebc0d8e32868b63a26ef
author: Sebastian Rasmussen <sebras@gmail.com>
date: Tue Jun 26 22:13:04 EDT 2018

jbig2dec: Print segment number in messages when available.

--- a/jbig2.c
+++ b/jbig2.c
@@ -301,7 +301,7 @@
                 segments = jbig2_renew(ctx, ctx->segments, Jbig2Segment *, (ctx->n_segments_max <<= 2));
                 if (segments == NULL) {
                     ctx->state = JBIG2_FILE_EOF;
-                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "could not reallocate space for more segments");
+                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "could not reallocate space for more segments");
                 }
                 ctx->segments = segments;
             }
@@ -330,7 +330,7 @@
             }
             if (code < 0) {
                 ctx->state = JBIG2_FILE_EOF;
-                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to decode; treating as end of file");
+                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode; treating as end of file");
             }
             break;
         case JBIG2_FILE_EOF:
--- a/jbig2_generic.c
+++ b/jbig2_generic.c
@@ -101,7 +101,7 @@
 
                 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
                 if (code)
-                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template0 optimized");
+                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template0 optimized");
                 result |= bit << (7 - x_minor);
                 CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit | ((line_m1 >> (7 - x_minor)) & 0x10) | ((line_m2 >> (7 - x_minor)) & 0x800);
             }
@@ -163,7 +163,7 @@
             CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
             bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
             if (code)
-                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template0 unoptimized");
+                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template0 unoptimized");
             jbig2_image_set_pixel(image, x, y, bit);
         }
     }
@@ -206,7 +206,7 @@
             CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 12;
             bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
             if (code)
-                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template1 unoptimized");
+                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template1 unoptimized");
             jbig2_image_set_pixel(image, x, y, bit);
         }
     }
@@ -262,7 +262,7 @@
 
                 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
                 if (code)
-                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template1 optimized");
+                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template1 optimized");
                 result |= bit << (7 - x_minor);
                 CONTEXT = ((CONTEXT & 0xefb) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x8) | ((line_m2 >> (8 - x_minor)) & 0x200);
             }
@@ -312,7 +312,7 @@
             CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 9;
             bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
             if (code)
-                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template2 unoptimized");
+                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template2 unoptimized");
             jbig2_image_set_pixel(image, x, y, bit);
         }
     }
@@ -369,7 +369,7 @@
 
                 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
                 if (code)
-                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template2 optimized");
+                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template2 optimized");
                 result |= bit << (7 - x_minor);
                 CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80);
             }
@@ -429,7 +429,7 @@
 
                 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
                 if (code)
-                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template3 optimized");
+                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template3 optimized");
                 result |= bit << (7 - x_minor);
                 CONTEXT = ((CONTEXT & 0x1f7) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x10);
             }
@@ -478,7 +478,7 @@
             CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9;
             bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
             if (code)
-                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template3 unoptimized");
+                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template3 unoptimized");
             jbig2_image_set_pixel(image, x, y, bit);
         }
     }
@@ -522,7 +522,7 @@
     for (y = 0; y < GBH; y++) {
         LTP ^= jbig2_arith_decode(as, &GB_stats[0x9B25], &code);
         if (code)
-            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template0 TPGDON1");
+            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
         if (!LTP) {
             for (x = 0; x < GBW; x++) {
                 CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
@@ -543,7 +543,7 @@
                 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
                 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
                 if (code)
-                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template0 TPGDON2");
+                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
                 jbig2_image_set_pixel(image, x, y, bit);
             }
         } else {
@@ -574,7 +574,7 @@
     for (y = 0; y < GBH; y++) {
         LTP ^= jbig2_arith_decode(as, &GB_stats[0x0795], &code);
         if (code)
-            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template1 TPGDON1");
+            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON1");
         if (!LTP) {
             for (x = 0; x < GBW; x++) {
                 CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
@@ -592,7 +592,7 @@
                 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 12;
                 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
                 if (code)
-                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template1 TPGDON2");
+                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON2");
                 jbig2_image_set_pixel(image, x, y, bit);
             }
         } else {
@@ -623,7 +623,7 @@
     for (y = 0; y < GBH; y++) {
         LTP ^= jbig2_arith_decode(as, &GB_stats[0xE5], &code);
         if (code)
-            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template2 TPGDON1");
+            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON1");
         if (!LTP) {
             for (x = 0; x < GBW; x++) {
                 CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
@@ -638,7 +638,7 @@
                 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 9;
                 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
                 if (code)
-                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template2 TPGDON2");
+                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON2");
                 jbig2_image_set_pixel(image, x, y, bit);
             }
         } else {
@@ -669,7 +669,7 @@
     for (y = 0; y < GBH; y++) {
         LTP ^= jbig2_arith_decode(as, &GB_stats[0x0195], &code);
         if (code)
-            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template3 TPGDON1");
+            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON1");
         if (!LTP) {
             for (x = 0; x < GBW; x++) {
                 CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
@@ -684,7 +684,7 @@
                 CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9;
                 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
                 if (code)
-                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling generic template3 TPGDON2");
+                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON2");
                 jbig2_image_set_pixel(image, x, y, bit);
             }
         } else {
--- a/jbig2_mmr.c
+++ b/jbig2_mmr.c
@@ -35,7 +35,9 @@
 #include "jbig2_priv.h"
 #include "jbig2_arith.h"
 #include "jbig2_generic.h"
+#include "jbig2_image.h"
 #include "jbig2_mmr.h"
+#include "jbig2_segment.h"
 
 typedef struct {
     uint32_t width;
@@ -1033,7 +1035,7 @@
         memset(dst, 0, rowstride);
         code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst);
         if (code < 0)
-            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to decode mmr line");
+            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode mmr line");
         ref = dst;
         dst += rowstride;
     }
--- a/jbig2_refinement.c
+++ b/jbig2_refinement.c
@@ -83,7 +83,7 @@
             CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy + params->grat[3]) << 12;
             bit = jbig2_arith_decode(as, &GR_stats[CONTEXT], &code);
             if (code)
-                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling refinement template0");
+                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling refinement template0");
             jbig2_image_set_pixel(image, x, y, bit);
         }
     }
@@ -96,11 +96,11 @@
         snprintf(name, 32, "refin-%d.pbm", count);
         code = jbig2_image_write_pbm_file(ref, name);
         if (code < 0)
-            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed write refinement input");
+            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed write refinement input");
         snprintf(name, 32, "refout-%d.pbm", count);
         code = jbig2_image_write_pbm_file(image, name);
         if (code < 0)
-            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed write refinement output");
+            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed write refinement output");
         count++;
     }
 #endif
@@ -138,7 +138,7 @@
             CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
             bit = jbig2_arith_decode(as, &GR_stats[CONTEXT], &code);
             if (code)
-                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling refinement template0");
+                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling refinement template0");
             jbig2_image_set_pixel(image, x, y, bit);
         }
     }
@@ -151,11 +151,11 @@
         snprintf(name, 32, "refin-%d.pbm", count);
         code = jbig2_image_write_pbm_file(ref, name);
         if (code < 0)
-            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to write refinement input");
+            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write refinement input");
         snprintf(name, 32, "refout-%d.pbm", count);
         code = jbig2_image_write_pbm_file(image, name);
         if (code < 0)
-            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to write refinement output");
+            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write refinement output");
         count++;
     }
 #endif
@@ -216,7 +216,7 @@
 
                 bit = jbig2_arith_decode(as, &GR_stats[CONTEXT], &code);
                 if (code)
-                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode arithmetic code when handling refinement template1");
+                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling refinement template1");
                 result |= bit << (7 - x_minor);
                 CONTEXT = ((CONTEXT & 0x0d6) << 1) | bit |
                           ((line_m1 >> (9 - x_minor)) & 0x002) |
@@ -503,7 +503,7 @@
         stats_size = params.GRTEMPLATE ? 1 << 10 : 1 << 13;
         GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
         if (GR_stats == NULL) {
-            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate GR-stats in jbig2_refinement_region");
+            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GR-stats in jbig2_refinement_region");
             goto cleanup;
         }
         memset(GR_stats, 0, stats_size);
@@ -510,19 +510,19 @@
 
         ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
         if (ws == NULL) {
-            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to allocate ws in jbig2_refinement_region");
+            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate ws in jbig2_refinement_region");
             goto cleanup;
         }
 
         as = jbig2_arith_new(ctx, ws);
         if (as == NULL) {
-            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to allocate as in jbig2_refinement_region");
+            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate as in jbig2_refinement_region");
             goto cleanup;
         }
 
         code = jbig2_decode_refinement_region(ctx, segment, &params, as, image, GR_stats);
         if (code < 0) {
-            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to decode refinement region");
+            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode refinement region");
             goto cleanup;
         }
 
@@ -535,7 +535,7 @@
                         "composing %dx%d decoded refinement region onto page at (%d, %d)", rsi.width, rsi.height, rsi.x, rsi.y);
             code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
             if (code < 0) {
-                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "unable to add refinement region to page");
+                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add refinement region to page");
                 goto cleanup;
             }
         }
--- a/jbig2_segment.c
+++ b/jbig2_segment.c
@@ -94,7 +94,7 @@
 
         referred_to_segments = jbig2_new(ctx, uint32_t, referred_to_segment_count * referred_to_segment_size);
         if (referred_to_segments == NULL) {
-            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "could not allocate referred_to_segments in jbig2_parse_segment_header");
+            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, result->number, "could not allocate referred_to_segments in jbig2_parse_segment_header");
             return NULL;
         }
 
--- a/jbig2_symbol_dict.c
+++ b/jbig2_symbol_dict.c
@@ -1035,7 +1035,7 @@
 
         GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
         if (GB_stats == NULL) {
-            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate GB_stats in jbig2_symbol_dictionary");
+            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GB_stats in jbig2_symbol_dictionary");
             goto cleanup;
         }
         memset(GB_stats, 0, stats_size);
@@ -1043,7 +1043,7 @@
         stats_size = params.SDRTEMPLATE ? 1 << 10 : 1 << 13;
         GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
         if (GR_stats == NULL) {
-            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate GR_stats in jbig2_symbol_dictionary");
+            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GR_stats in jbig2_symbol_dictionary");
             jbig2_free(ctx->allocator, GB_stats);
             goto cleanup;
         }