Fix _pdfioFileRead/Seek to fix test suite-reported error loading an object.

This commit is contained in:
Michael R Sweet 2021-06-10 10:58:07 -04:00
parent d5ceed0694
commit 466cb473d1
No known key found for this signature in database
GPG Key ID: BE67C75EC81F3244
4 changed files with 29 additions and 4 deletions

View File

@ -141,6 +141,8 @@ _pdfioFileGets(pdfio_file_t *pdf, // I - PDF file
*bufend = buffer + bufsize - 1; // Pointer to end of buffer *bufend = buffer + bufsize - 1; // Pointer to end of buffer
PDFIO_DEBUG("_pdfioFileGets(pdf=%p, buffer=%p, bufsize=%lu) bufpos=%ld, buffer=%p, bufptr=%p, bufend=%p\n", pdf, buffer, (unsigned long)bufsize, (long)pdf->bufpos, pdf->buffer, pdf->bufptr, pdf->bufend);
while (!eol) while (!eol)
{ {
// If there are characters ready in the buffer, use them... // If there are characters ready in the buffer, use them...
@ -180,6 +182,8 @@ _pdfioFileGets(pdfio_file_t *pdf, // I - PDF file
*bufptr = '\0'; *bufptr = '\0';
PDFIO_DEBUG("_pdfioFileGets: Returning %s, '%s'\n", eol ? "true" : "false", buffer);
return (eol); return (eol);
} }
@ -310,6 +314,13 @@ _pdfioFileRead(pdfio_file_t *pdf, // I - PDF file
// Nothing buffered... // Nothing buffered...
if (bytes > 1024) if (bytes > 1024)
{ {
// Advance current position in file as needed...
if (pdf->bufend)
{
pdf->bufpos += pdf->bufend - pdf->buffer;
pdf->bufptr = pdf->bufend = NULL;
}
// Read directly from the file... // Read directly from the file...
if ((rbytes = read_buffer(pdf, bufptr, bytes)) > 0) if ((rbytes = read_buffer(pdf, bufptr, bytes)) > 0)
{ {
@ -356,6 +367,8 @@ _pdfioFileSeek(pdfio_file_t *pdf, // I - PDF file
{ {
// Yes, seek within existing buffer... // Yes, seek within existing buffer...
pdf->bufptr = pdf->buffer + offset - pdf->bufpos; pdf->bufptr = pdf->buffer + offset - pdf->bufpos;
PDFIO_DEBUG("_pdfioFileSeek: Seek within buffer, bufpos=%ld.\n", (long)pdf->bufpos);
PDFIO_DEBUG("_pdfioFileSeek: buffer=%p, bufptr=%p, bufend=%p\n", pdf->buffer, pdf->bufptr, pdf->bufend);
return (offset); return (offset);
} }
@ -381,6 +394,9 @@ _pdfioFileSeek(pdfio_file_t *pdf, // I - PDF file
return (-1); return (-1);
} }
PDFIO_DEBUG("_pdfioFileSeek: Reset bufpos=%ld.\n", (long)pdf->bufpos);
PDFIO_DEBUG("_pdfioFileSeek: buffer=%p, bufptr=%p, bufend=%p\n", pdf->buffer, pdf->bufptr, pdf->bufend);
pdf->bufpos = offset; pdf->bufpos = offset;
return (offset); return (offset);

View File

@ -360,6 +360,8 @@ _pdfioObjLoad(pdfio_obj_t *obj) // I - Object
return (false); return (false);
} }
PDFIO_DEBUG("_pdfioObjLoad: Header is '%s'.\n", line);
if (strtoimax(line, &ptr, 10) != (intmax_t)obj->number) if (strtoimax(line, &ptr, 10) != (intmax_t)obj->number)
{ {
_pdfioFileError(obj->pdf, "Bad header for object %lu.", (unsigned long)obj->number); _pdfioFileError(obj->pdf, "Bad header for object %lu.", (unsigned long)obj->number);
@ -403,8 +405,13 @@ _pdfioObjLoad(pdfio_obj_t *obj) // I - Object
{ {
// Yes, save its location... // Yes, save its location...
obj->stream_offset = _pdfioFileTell(obj->pdf); obj->stream_offset = _pdfioFileTell(obj->pdf);
PDFIO_DEBUG("_pdfioObjLoad: stream_offset=%lu.\n", (unsigned long)obj->stream_offset);
} }
PDFIO_DEBUG("_pdfioObjLoad: ");
PDFIO_DEBUG_VALUE(&obj->value);
PDFIO_DEBUG("\n");
return (true); return (true);
} }

View File

@ -216,7 +216,6 @@ _pdfioStreamCreate(
if (predictor >= 10) if (predictor >= 10)
st->pbsize ++; // Add PNG predictor byte st->pbsize ++; // Add PNG predictor byte
fprintf(stderr, "colors=%d, bpc=%d, pbpixel=%u\n", colors, bpc, (unsigned)st->pbpixel);
if ((st->prbuffer = calloc(1, st->pbsize - 1)) == NULL || (st->psbuffer = calloc(1, st->pbsize)) == NULL) if ((st->prbuffer = calloc(1, st->pbsize - 1)) == NULL || (st->psbuffer = calloc(1, st->pbsize)) == NULL)
{ {
_pdfioFileError(st->pdf, "Unable to allocate %lu bytes for Predictor buffers.", (unsigned long)st->pbsize); _pdfioFileError(st->pdf, "Unable to allocate %lu bytes for Predictor buffers.", (unsigned long)st->pbsize);

View File

@ -274,7 +274,7 @@ do_unit_tests(void)
return (1); return (1);
// Write a page with test images... // Write a page with test images...
first_image = pdfioFileGetNumObjs(outpdf); first_image = pdfioFileGetNumObjs(outpdf) + 1;
if (write_images(outpdf, 7, helvetica)) if (write_images(outpdf, 7, helvetica))
return (1); return (1);
@ -328,7 +328,10 @@ do_unit_tests(void)
// Verify the images // Verify the images
for (i = 0; i < 7; i ++) for (i = 0; i < 7; i ++)
verify_image(pdf, first_image + (size_t)i); {
if (verify_image(pdf, first_image + (size_t)i))
return (1);
}
// Close the new PDF file... // Close the new PDF file...
fputs("pdfioFileClose(\"testpdfio-out.pdf\"): ", stdout); fputs("pdfioFileClose(\"testpdfio-out.pdf\"): ", stdout);