Implement object loading.

This commit is contained in:
Michael R Sweet 2021-05-04 17:04:09 -04:00
parent 7733fbeb10
commit 7afdfc725c
No known key found for this signature in database
GPG Key ID: 999559A027815955
3 changed files with 76 additions and 6 deletions

View File

@ -197,10 +197,11 @@ pdfioFileCreateObject(
pdf->objs[pdf->num_objs ++] = obj; pdf->objs[pdf->num_objs ++] = obj;
// Initialize the object... // Initialize the object...
obj->pdf = pdf; obj->pdf = pdf;
obj->number = pdf->num_objs; obj->number = pdf->num_objs;
obj->dict = dict; obj->offset = _pdfioFileTell(pdf);
obj->offset = _pdfioFileTell(pdf); obj->value.type = PDFIO_VALTYPE_DICT;
obj->value.value.dict = dict;
// Don't write anything just yet... // Don't write anything just yet...
return (obj); return (obj);

View File

@ -122,3 +122,72 @@ pdfioObjOpenStream(pdfio_obj_t *obj) // I - Object
return (NULL); return (NULL);
} }
//
// '()' - Load an object dictionary/value.
//
bool // O - `true` on success, `false` otherwise
_pdfioObjLoad(pdfio_obj_t *obj) // I - Object
{
char line[1024], // Line from file
*ptr; // Pointer into line
// Seek to the start of the object and read its header...
if (_pdfioFileSeek(obj->pdf, obj->offset, SEEK_SET) != obj->offset)
{
_pdfioFileError(obj->pdf, "Unable to seek to object %lu.", (unsigned long)obj->number);
return (false);
}
if (!_pdfioFileGets(obj->pdf, line, sizeof(line)))
{
_pdfioFileError(obj->pdf, "Unable to read header for object %lu.", (unsigned long)obj->number);
return (false);
}
if (strtoimax(line, &ptr, 10) != (intmax_t)obj->number)
{
_pdfioFileError(obj->pdf, "Bad header for object %lu.", (unsigned long)obj->number);
return (false);
}
if (strtol(ptr, &ptr, 10) != (long)obj->generation)
{
_pdfioFileError(obj->pdf, "Bad header for object %lu.", (unsigned long)obj->number);
return (false);
}
while (isspace(*ptr & 255))
ptr ++;
if (strcmp(ptr, "obj"))
{
_pdfioFileError(obj->pdf, "Bad header for object %lu.", (unsigned long)obj->number);
return (false);
}
// Then grab the object value...
if (!_pdfioValueRead(obj->pdf, &obj->value))
{
_pdfioFileError(obj->pdf, "Unable to read value for object %lu.", (unsigned long)obj->number);
return (false);
}
// Now see if there is an associated stream...
if (!_pdfioFileGets(obj->pdf, line, sizeof(line)))
{
_pdfioFileError(obj->pdf, "Early end-of-file for object %lu.", (unsigned long)obj->number);
return (false);
}
if (!strcmp(line, "stream"))
{
// Yes, save its location...
obj->stream_offset = _pdfioFileTell(obj->pdf);
}
return (true);
}

View File

@ -161,7 +161,7 @@ struct _pdfio_obj_s // Object
length_offset, // Offset to /Length in object dict length_offset, // Offset to /Length in object dict
stream_offset; // Offset to start of stream in file stream_offset; // Offset to start of stream in file
size_t stream_length; // Length of stream, if any size_t stream_length; // Length of stream, if any
pdfio_dict_t *dict; // Dictionary _pdfio_value_t value; // Dictionary/number/etc. value
pdfio_stream_t *stream; // Open stream, if any pdfio_stream_t *stream; // Open stream, if any
}; };
@ -214,8 +214,8 @@ extern off_t _pdfioFileSeek(pdfio_file_t *pdf, off_t offset, int whence) PDFIO_
extern off_t _pdfioFileTell(pdfio_file_t *pdf) PDFIO_INTERNAL; extern off_t _pdfioFileTell(pdfio_file_t *pdf) PDFIO_INTERNAL;
extern bool _pdfioFileWrite(pdfio_file_t *pdf, const void *buffer, size_t bytes) PDFIO_INTERNAL; extern bool _pdfioFileWrite(pdfio_file_t *pdf, const void *buffer, size_t bytes) PDFIO_INTERNAL;
extern pdfio_obj_t *_pdfioObjAdd(pdfio_file_t *pdf, size_t number, unsigned short generation, off_t offset) PDFIO_INTERNAL;
extern void _pdfioObjDelete(pdfio_obj_t *obj) PDFIO_INTERNAL; extern void _pdfioObjDelete(pdfio_obj_t *obj) PDFIO_INTERNAL;
extern bool _pdfioObjLoad(pdfio_obj_t *obj) PDFIO_INTERNAL;
extern void _pdfioStreamDelete(pdfio_stream_t *st) PDFIO_INTERNAL; extern void _pdfioStreamDelete(pdfio_stream_t *st) PDFIO_INTERNAL;