shithub: jbig2

Download patch

ref: d5db893a4dfe988b1ecea14595f3c7605ee166ea
parent: 2dea640032a72f9756cdccd54352c10b18348f56
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Mon May 26 14:52:22 EDT 2008

Work around broken streams created by Xerox WorkCentre products, where
the final segment length is written as -1. Ghostscript bug 689841.


git-svn-id: http://svn.ghostscript.com/jbig2dec/trunk@468 ded80894-8fb9-0310-811b-c03f3676ab4d

--- a/jbig2_page.c
+++ b/jbig2_page.c
@@ -177,7 +177,7 @@
     if (end_row < page.end_row) {
 	jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
 	    "end of stripe segment with non-positive end row advance"
-	    "(new end row %d vs current end row %d)",
+	    " (new end row %d vs current end row %d)",
 	    end_row, page.end_row);
     } else {
 	jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
@@ -200,6 +200,24 @@
 int
 jbig2_complete_page (Jbig2Ctx *ctx)
 {
+
+    /* check for unfinished segments */
+    if (ctx->segment_index != ctx->n_segments) {
+      Jbig2Segment *segment = ctx->segments[ctx->segment_index];
+      int code = 0;
+      /* Some versions of Xerox Workcentre generate PDF files
+         with the segment data length field of the last segment
+         set to -1. Try to cope with this here. */
+      if ((segment->data_length & 0xffffffff) == 0xffffffff) {
+        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
+          "File has an invalid segment data length!"
+          " Trying to decode using the available data.");
+        segment->data_length = ctx->buf_wr_ix - ctx->buf_rd_ix;
+        code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
+        ctx->buf_rd_ix += segment->data_length;
+        ctx->segment_index++;
+      }
+    }
     ctx->pages[ctx->current_page].state = JBIG2_PAGE_COMPLETE;
 
     return 0;