Add pdfioObjGetLength function.

This commit is contained in:
Michael R Sweet 2021-05-08 19:16:27 -04:00
parent f8f048b87a
commit 4e4c47ab33
No known key found for this signature in database
GPG Key ID: 999559A027815955
3 changed files with 42 additions and 25 deletions

View File

@ -91,6 +91,44 @@ pdfioObjGetGeneration(pdfio_obj_t *obj) // I - Object
}
//
// 'pdfioObjGetLength()' - Get the length of the object's (data) stream.
//
size_t // O - Length in bytes or `0` for none
pdfioObjGetLength(pdfio_obj_t *obj) // I - Object
{
size_t length; // Length of stream
pdfio_obj_t *lenobj; // Length object
// Range check input...
if (!obj || !obj->stream_offset || obj->value.type != PDFIO_VALTYPE_DICT)
return (0);
// Try getting the length, directly or indirectly
if ((length = (size_t)pdfioDictGetNumber(obj->value.value.dict, "Length")) > 0)
return (length);
if ((lenobj = pdfioDictGetObject(obj->value.value.dict, "Length")) == NULL)
{
_pdfioFileError(obj->pdf, "Unable to get length of stream.");
return (0);
}
if (lenobj->value.type == PDFIO_VALTYPE_NONE)
_pdfioObjLoad(lenobj);
if (lenobj->value.type != PDFIO_VALTYPE_NUMBER || lenobj->value.value.number <= 0.0f)
{
_pdfioFileError(obj->pdf, "Unable to get length of stream.");
return (0);
}
return ((size_t)lenobj->value.value.number);
}
//
// 'pdfioObjGetNumber()' - Get the object's number.
//

View File

@ -150,7 +150,6 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
pdfio_stream_t *st; // Stream
pdfio_dict_t *dict = pdfioObjGetDict(obj);
// Object dictionary
size_t length; // Length of stream
// Allocate a new stream object...
@ -165,33 +164,12 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
_pdfioFileSeek(st->pdf, obj->stream_offset, SEEK_SET);
if ((length = (size_t)pdfioDictGetNumber(dict, "Length")) == 0)
if ((st->remaining = pdfioObjGetLength(obj)) == 0)
{
// Length must be an indirect reference...
pdfio_obj_t *lenobj; // Length object
if ((lenobj = pdfioDictGetObject(dict, "Length")) == NULL)
{
_pdfioFileError(obj->pdf, "Unable to get length of stream.");
free(st);
return (NULL);
}
if (lenobj->value.type == PDFIO_VALTYPE_NONE)
_pdfioObjLoad(lenobj);
if (lenobj->value.type != PDFIO_VALTYPE_NUMBER || lenobj->value.value.number <= 0.0f)
{
_pdfioFileError(obj->pdf, "Unable to get length of stream.");
free(st);
return (NULL);
}
length = (size_t)lenobj->value.value.number;
}
st->remaining = length;
if (decode)
{
// Try to decode/decompress the contents of this object...

View File

@ -164,6 +164,7 @@ extern bool pdfioObjClose(pdfio_obj_t *obj) PDFIO_PUBLIC;
extern pdfio_stream_t *pdfioObjCreateStream(pdfio_obj_t *obj, pdfio_filter_t compression) PDFIO_PUBLIC;
extern pdfio_dict_t *pdfioObjGetDict(pdfio_obj_t *obj) PDFIO_PUBLIC;
extern unsigned short pdfioObjGetGeneration(pdfio_obj_t *obj) PDFIO_PUBLIC;
extern size_t pdfioObjGetLength(pdfio_obj_t *obj) PDFIO_PUBLIC;
extern size_t pdfioObjGetNumber(pdfio_obj_t *obj) PDFIO_PUBLIC;
extern const char *pdfioObjGetType(pdfio_obj_t *obj) PDFIO_PUBLIC;
extern pdfio_stream_t *pdfioObjOpenStream(pdfio_obj_t *obj, bool decode) PDFIO_PUBLIC;