Add some debug output, "debug" make target.

Fix loading of dicts, trailer.
This commit is contained in:
Michael R Sweet 2021-05-04 12:59:10 -04:00
parent 516c6b9ace
commit 59ef6b48ed
No known key found for this signature in database
GPG Key ID: 999559A027815955
7 changed files with 106 additions and 17 deletions

View File

@ -66,6 +66,9 @@ all-shared:
$(MAKE) DSONAME="libpdfio.so.1" -$(MAKEFLAGS) all; \ $(MAKE) DSONAME="libpdfio.so.1" -$(MAKEFLAGS) all; \
fi fi
debug:
$(MAKE) -$(MAKEFLAGS) COMMONFLAGS="$(COMMONFLAGS) -DDEBUG=1" clean all
# Clean everything # Clean everything
clean: clean:

View File

@ -30,6 +30,8 @@ static bool write_buffer(pdfio_file_t *pdf, const void *buffer, size_t bytes);
void void
_pdfioFileClearTokens(pdfio_file_t *pdf)// I - PDF file _pdfioFileClearTokens(pdfio_file_t *pdf)// I - PDF file
{ {
PDFIO_DEBUG("_pdfioFileClearTokens(pdf=%p)\n", pdf);
while (pdf->num_tokens > 0) while (pdf->num_tokens > 0)
{ {
pdf->num_tokens --; pdf->num_tokens --;
@ -47,6 +49,8 @@ bool // O - `true` on sucess, `false` on EOF
_pdfioFileConsume(pdfio_file_t *pdf, // I - PDF file _pdfioFileConsume(pdfio_file_t *pdf, // I - PDF file
size_t bytes) // I - Bytes to consume size_t bytes) // I - Bytes to consume
{ {
PDFIO_DEBUG("_pdfioFileConsume(pdf=%p, bytes=%u)\n", pdf, (unsigned)bytes);
if ((size_t)(pdf->bufend - pdf->bufptr) > bytes) if ((size_t)(pdf->bufend - pdf->bufptr) > bytes)
pdf->bufptr += bytes; pdf->bufptr += bytes;
else if (_pdfioFileSeek(pdf, (off_t)bytes, SEEK_CUR) < 0) else if (_pdfioFileSeek(pdf, (off_t)bytes, SEEK_CUR) < 0)
@ -105,6 +109,8 @@ _pdfioFileError(pdfio_file_t *pdf, // I - PDF file
bool // O - `true` on success, `false` on failure bool // O - `true` on success, `false` on failure
_pdfioFileFlush(pdfio_file_t *pdf) // I - PDF file _pdfioFileFlush(pdfio_file_t *pdf) // I - PDF file
{ {
PDFIO_DEBUG("_pdfioFileFlush(pdf=%p)\n", pdf);
if (pdf->bufptr > pdf->buffer) if (pdf->bufptr > pdf->buffer)
{ {
if (!write_buffer(pdf, pdf->buffer, (size_t)(pdf->bufptr - pdf->buffer))) if (!write_buffer(pdf, pdf->buffer, (size_t)(pdf->bufptr - pdf->buffer)))
@ -155,6 +161,8 @@ _pdfioFileGetToken(pdfio_file_t *pdf, // I - PDF file
strncpy(buffer, pdf->tokens[pdf->num_tokens], bufsize - 1); strncpy(buffer, pdf->tokens[pdf->num_tokens], bufsize - 1);
buffer[bufsize - 1] = '\0'; buffer[bufsize - 1] = '\0';
PDFIO_DEBUG("_pdfioFileGetToken(pdf=%p, buffer=%p, bufsize=%u): Popping '%s' from stack.\n", pdf, buffer, (unsigned)bufsize, buffer);
free(pdf->tokens[pdf->num_tokens]); free(pdf->tokens[pdf->num_tokens]);
pdf->tokens[pdf->num_tokens] = NULL; pdf->tokens[pdf->num_tokens] = NULL;
return (true); return (true);

View File

@ -107,6 +107,50 @@ pdfioDictCreate(pdfio_file_t *pdf) // I - PDF file
} }
#ifdef DEBUG
//
// '_pdfioDictDebug()' - Dump a dictionary to stderr.
//
void
_pdfioDictDebug(pdfio_dict_t *dict, // I - Dictionary
const char *prefix) // I - Prefix for each line
{
size_t i; // Looping var
_pdfio_pair_t *pair; // Current pair
for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++)
{
switch (pair->value.type)
{
case PDFIO_VALTYPE_INDIRECT :
PDFIO_DEBUG("%s: /%s %u %u R\n", prefix, pair->key, (unsigned)pair->value.value.indirect.number, pair->value.value.indirect.generation);
break;
case PDFIO_VALTYPE_NUMBER :
PDFIO_DEBUG("%s: /%s %g\n", prefix, pair->key, pair->value.value.number);
break;
case PDFIO_VALTYPE_BOOLEAN :
PDFIO_DEBUG("%s: /%s %s\n", prefix, pair->key, pair->value.value.boolean ? "true" : "false");
break;
case PDFIO_VALTYPE_NULL :
PDFIO_DEBUG("%s: /%s null\n", prefix, pair->key);
break;
case PDFIO_VALTYPE_ARRAY :
PDFIO_DEBUG("%s: /%s [...]\n", prefix, pair->key);
break;
case PDFIO_VALTYPE_DICT :
PDFIO_DEBUG("%s: /%s <<...>>\n", prefix, pair->key);
break;
default :
PDFIO_DEBUG("%s: /%s ...\n", prefix, pair->key);
break;
}
}
}
#endif // DEBUG
// //
// '_pdfioDictDelete()' - Free the memory used by a dictionary. // '_pdfioDictDelete()' - Free the memory used by a dictionary.
// //
@ -346,6 +390,8 @@ _pdfioDictRead(pdfio_file_t *pdf) // I - PDF file
_pdfio_value_t value; // Dictionary value _pdfio_value_t value; // Dictionary value
PDFIO_DEBUG("_pdfioDictRead(pdf=%p)\n", pdf);
// Create a dictionary and start reading... // Create a dictionary and start reading...
dict = pdfioDictCreate(pdf); dict = pdfioDictCreate(pdf);
@ -370,8 +416,10 @@ _pdfioDictRead(pdfio_file_t *pdf) // I - PDF file
break; break;
} }
if (!_pdfioDictSetValue(dict, key, &value)) if (!_pdfioDictSetValue(dict, pdfioStringCreate(pdf, key + 1), &value))
break; break;
PDFIO_DEBUG("_pdfioDictRead: Set %s.\n", key);
} }
// Dictionary is invalid - pdfioFileClose will free the memory, return NULL // Dictionary is invalid - pdfioFileClose will free the memory, return NULL

View File

@ -564,12 +564,15 @@ load_xref(pdfio_file_t *pdf, // I - PDF file
_pdfioFileError(pdf, "Unable to read trailer dictionary."); _pdfioFileError(pdf, "Unable to read trailer dictionary.");
return (false); return (false);
} }
else else if (trailer.type != PDFIO_VALTYPE_DICT)
{ {
_pdfioFileError(pdf, "Trailer is not a dictionary."); _pdfioFileError(pdf, "Trailer is not a dictionary.");
return (false); return (false);
} }
PDFIO_DEBUG("load_xref: Contents of trailer dictionary:\n");
PDFIO_DEBUG_DICT(trailer.value.dict, "load_xref");
if (!pdf->trailer) if (!pdf->trailer)
{ {
// Save the trailer dictionary and grab the root (catalog) and info // Save the trailer dictionary and grab the root (catalog) and info

View File

@ -48,8 +48,10 @@
# ifdef DEBUG # ifdef DEBUG
# define PDFIO_DEBUG(...) fprintf(stderr, __VA_ARGS__) # define PDFIO_DEBUG(...) fprintf(stderr, __VA_ARGS__)
# define PDFIO_DEBUG_DICT(dict,prefix) _pdfioDictDebug(dict, prefix)
# else # else
# define PDFIO_DEBUG(...) # define PDFIO_DEBUG(...)
# define PDFIO_DEBUG_DICT(dict,prefix)
# endif // DEBUG # endif // DEBUG
@ -186,6 +188,9 @@ extern _pdfio_value_t *_pdfioArrayGetValue(pdfio_array_t *a, size_t n) PDFIO_INT
extern pdfio_array_t *_pdfioArrayRead(pdfio_file_t *pdf) PDFIO_INTERNAL; extern pdfio_array_t *_pdfioArrayRead(pdfio_file_t *pdf) PDFIO_INTERNAL;
extern bool _pdfioArrayWrite(pdfio_array_t *a) PDFIO_INTERNAL; extern bool _pdfioArrayWrite(pdfio_array_t *a) PDFIO_INTERNAL;
# ifdef DEBUG
extern void _pdfioDictDebug(pdfio_dict_t *dict, const char *prefix) PDFIO_INTERNAL;
# endif // DEBUG
extern void _pdfioDictDelete(pdfio_dict_t *dict) PDFIO_INTERNAL; extern void _pdfioDictDelete(pdfio_dict_t *dict) PDFIO_INTERNAL;
extern _pdfio_value_t *_pdfioDictGetValue(pdfio_dict_t *dict, const char *key) PDFIO_INTERNAL; extern _pdfio_value_t *_pdfioDictGetValue(pdfio_dict_t *dict, const char *key) PDFIO_INTERNAL;
extern pdfio_dict_t *_pdfioDictRead(pdfio_file_t *pdf) PDFIO_INTERNAL; extern pdfio_dict_t *_pdfioDictRead(pdfio_file_t *pdf) PDFIO_INTERNAL;

View File

@ -379,6 +379,8 @@ _pdfioTokenRead(
*bufptr = '\0'; *bufptr = '\0';
PDFIO_DEBUG("_pdfioTokenRead(pdf=%p, ...): Read '%s'.\n", pdf, buffer);
return (bufptr > buffer); return (bufptr > buffer);
} }

View File

@ -101,12 +101,44 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file
_pdfio_value_t *v) // I - Value _pdfio_value_t *v) // I - Value
{ {
char token[8192]; // Token buffer char token[8192]; // Token buffer
#ifdef DEBUG
static const char * const valtypes[] =
{
"<<none>>", // No value, not set
"array", // Array
"hex-string", // Binary data
"boolean", // Boolean
"date", // Date/time
"dict", // Dictionary
"indirect", // Indirect object (N G obj)
"name", // Name
"null", // Null object
"number", // Number (integer or real)
"string" // String
};
#endif // DEBUG
PDFIO_DEBUG("_pdfioValueRead(pdf=%p, v=%p)\n", pdf, v);
if (!_pdfioFileGetToken(pdf, token, sizeof(token))) if (!_pdfioFileGetToken(pdf, token, sizeof(token)))
return (NULL); return (NULL);
if (token[0] == '(') if (!strcmp(token, "["))
{
// Start of array
v->type = PDFIO_VALTYPE_ARRAY;
if ((v->value.array = _pdfioArrayRead(pdf)) == NULL)
return (NULL);
}
else if (!strcmp(token, "<<"))
{
// Start of dictionary
v->type = PDFIO_VALTYPE_DICT;
if ((v->value.dict = _pdfioDictRead(pdf)) == NULL)
return (NULL);
}
else if (token[0] == '(')
{ {
// TODO: Add date value support // TODO: Add date value support
// String // String
@ -221,26 +253,14 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file
// null value // null value
v->type = PDFIO_VALTYPE_NULL; v->type = PDFIO_VALTYPE_NULL;
} }
else if (!strcmp(token, "["))
{
// Start of array
v->type = PDFIO_VALTYPE_ARRAY;
if ((v->value.array = _pdfioArrayRead(pdf)) == NULL)
return (NULL);
}
else if (!strcmp(token, "<<"))
{
// Start of dictionary
v->type = PDFIO_VALTYPE_DICT;
if ((v->value.dict = _pdfioDictRead(pdf)) == NULL)
return (NULL);
}
else else
{ {
_pdfioFileError(pdf, "Unexpected '%s' token seen.", token); _pdfioFileError(pdf, "Unexpected '%s' token seen.", token);
return (NULL); return (NULL);
} }
PDFIO_DEBUG("_pdfioValueRead: Returning %s value.\n", valtypes[v->type]);
return (v); return (v);
} }