diff --git a/pdfio-common.c b/pdfio-common.c index 02f96d7..55e247e 100644 --- a/pdfio-common.c +++ b/pdfio-common.c @@ -80,6 +80,8 @@ _pdfioFileError(pdfio_file_t *pdf, // I - PDF file vsnprintf(buffer, sizeof(buffer), format, ap); va_end(ap); + PDFIO_DEBUG("_pdfioFileError: %s\n", buffer); + return ((pdf->error_cb)(pdf, buffer, pdf->error_data)); } diff --git a/pdfio-file.c b/pdfio-file.c index e24ab73..47ff890 100644 --- a/pdfio-file.c +++ b/pdfio-file.c @@ -1154,11 +1154,15 @@ load_xref(pdfio_file_t *pdf, // I - PDF file return (false); } - if (!_pdfioFileGets(pdf, line, sizeof(line))) + do { - _pdfioFileError(pdf, "Unable to read start of xref table."); - return (false); + if (!_pdfioFileGets(pdf, line, sizeof(line))) + { + _pdfioFileError(pdf, "Unable to read start of xref table."); + return (false); + } } + while (!line[0]); PDFIO_DEBUG("load_xref: xref_offset=%lu, line='%s'\n", (unsigned long)xref_offset, line); @@ -1169,7 +1173,8 @@ load_xref(pdfio_file_t *pdf, // I - PDF file size_t i; // Looping var pdfio_array_t *index_array; // Index array size_t index_n, // Current element in array - index_count; // Number of values in index array + index_count, // Number of values in index array + count; // Number of objects in current pairing pdfio_array_t *w_array; // W array size_t w[3]; // Size of each cross-reference field size_t w_2, // Offset to second field @@ -1265,12 +1270,23 @@ load_xref(pdfio_file_t *pdf, // I - PDF file return (false); } - for (index_n = 0; index_n < index_count; index_n ++) + for (index_n = 0; index_n < index_count; index_n += 2) { - number = (intmax_t)pdfioArrayGetNumber(index_array, index_n); - - while (pdfioStreamRead(st, buffer, w_total) > 0) + if (index_count == 1) + { + number = 0; + count = 999999999; + } + else { + number = (intmax_t)pdfioArrayGetNumber(index_array, index_n); + count = (size_t)pdfioArrayGetNumber(index_array, index_n + 1); + } + + while (count > 0 && pdfioStreamRead(st, buffer, w_total) > 0) + { + count --; + PDFIO_DEBUG("load_xref: number=%u %02X%02X%02X%02X%02X\n", (unsigned)number, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]); // Check whether this is an object definition... @@ -1371,6 +1387,8 @@ load_xref(pdfio_file_t *pdf, // I - PDF file { if (!strcmp(line, "trailer")) break; + else if (!line[0]) + continue; if (sscanf(line, "%jd%jd", &number, &num_objects) != 2) { diff --git a/pdfio-stream.c b/pdfio-stream.c index 7193d9d..96492e3 100644 --- a/pdfio-stream.c +++ b/pdfio-stream.c @@ -385,6 +385,7 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object int predictor = (int)pdfioDictGetNumber(params, "Predictor"); // Predictory value, if any int status; // ZLIB status + ssize_t rbytes; // Bytes read PDFIO_DEBUG("_pdfioStreamOpen: FlateDecode - BitsPerComponent=%d, Colors=%d, Columns=%d, Predictor=%d\n", bpc, colors, columns, predictor); @@ -450,8 +451,28 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object else st->predictor = _PDFIO_PREDICTOR_NONE; + if (sizeof(st->cbuffer) > st->remaining) + rbytes = _pdfioFileRead(st->pdf, st->cbuffer, st->remaining); + else + rbytes = _pdfioFileRead(st->pdf, st->cbuffer, sizeof(st->cbuffer)); + + if (rbytes <= 0) + { + _pdfioFileError(st->pdf, "Unable to read bytes for stream."); + free(st->prbuffer); + free(st->psbuffer); + free(st); + return (NULL); + } + st->flate.next_in = (Bytef *)st->cbuffer; - st->flate.avail_in = (uInt)_pdfioFileRead(st->pdf, st->cbuffer, sizeof(st->cbuffer)); + st->flate.avail_in = (uInt)rbytes; + + if (st->cbuffer[0] == 0x0a) + { + st->flate.next_in ++; // Skip newline + st->flate.avail_in --; + } PDFIO_DEBUG("_pdfioStreamOpen: avail_in=%u, cbuffer=<%02X%02X%02X%02X%02X%02X%02X%02X...>\n", st->flate.avail_in, st->cbuffer[0], st->cbuffer[1], st->cbuffer[2], st->cbuffer[3], st->cbuffer[4], st->cbuffer[5], st->cbuffer[6], st->cbuffer[7]); @@ -989,7 +1010,11 @@ stream_read(pdfio_stream_t *st, // I - Stream } if (st->flate.avail_out > 0) - return (-1); // Early end of stream + { + // Early end of stream + PDFIO_DEBUG("stream_read: Early EOF (remaining=%u, avail_in=%d, avail_out=%d, data_type=%d, next_in=<%02X%02X%02X%02X...>).\n", (unsigned)st->remaining, st->flate.avail_in, st->flate.avail_out, st->flate.data_type, st->flate.next_in[0], st->flate.next_in[1], st->flate.next_in[2], st->flate.next_in[3]); + return (-1); + } // Apply predictor for this line PDFIO_DEBUG("stream_read: Line %02X %02X %02X %02X %02X.\n", sptr[-1], sptr[0], sptr[0], sptr[2], sptr[3]);