From 59ef6b48ede3fbf8b6a787ea3f0153c176f13839 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Tue, 4 May 2021 12:59:10 -0400 Subject: [PATCH] Add some debug output, "debug" make target. Fix loading of dicts, trailer. --- Makefile | 3 +++ pdfio-common.c | 8 ++++++++ pdfio-dict.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++- pdfio-file.c | 5 ++++- pdfio-private.h | 5 +++++ pdfio-token.c | 2 ++ pdfio-value.c | 50 ++++++++++++++++++++++++++++++++++--------------- 7 files changed, 106 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 4082b37..dc57847 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,9 @@ all-shared: $(MAKE) DSONAME="libpdfio.so.1" -$(MAKEFLAGS) all; \ fi +debug: + $(MAKE) -$(MAKEFLAGS) COMMONFLAGS="$(COMMONFLAGS) -DDEBUG=1" clean all + # Clean everything clean: diff --git a/pdfio-common.c b/pdfio-common.c index 2a63712..2ddc303 100644 --- a/pdfio-common.c +++ b/pdfio-common.c @@ -30,6 +30,8 @@ static bool write_buffer(pdfio_file_t *pdf, const void *buffer, size_t bytes); void _pdfioFileClearTokens(pdfio_file_t *pdf)// I - PDF file { + PDFIO_DEBUG("_pdfioFileClearTokens(pdf=%p)\n", pdf); + while (pdf->num_tokens > 0) { pdf->num_tokens --; @@ -47,6 +49,8 @@ bool // O - `true` on sucess, `false` on EOF _pdfioFileConsume(pdfio_file_t *pdf, // I - PDF file 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) pdf->bufptr += bytes; 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 _pdfioFileFlush(pdfio_file_t *pdf) // I - PDF file { + PDFIO_DEBUG("_pdfioFileFlush(pdf=%p)\n", pdf); + if (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); 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]); pdf->tokens[pdf->num_tokens] = NULL; return (true); diff --git a/pdfio-dict.c b/pdfio-dict.c index a2e043c..5598071 100644 --- a/pdfio-dict.c +++ b/pdfio-dict.c @@ -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. // @@ -346,6 +390,8 @@ _pdfioDictRead(pdfio_file_t *pdf) // I - PDF file _pdfio_value_t value; // Dictionary value + PDFIO_DEBUG("_pdfioDictRead(pdf=%p)\n", pdf); + // Create a dictionary and start reading... dict = pdfioDictCreate(pdf); @@ -370,8 +416,10 @@ _pdfioDictRead(pdfio_file_t *pdf) // I - PDF file break; } - if (!_pdfioDictSetValue(dict, key, &value)) + if (!_pdfioDictSetValue(dict, pdfioStringCreate(pdf, key + 1), &value)) break; + + PDFIO_DEBUG("_pdfioDictRead: Set %s.\n", key); } // Dictionary is invalid - pdfioFileClose will free the memory, return NULL diff --git a/pdfio-file.c b/pdfio-file.c index 373cfc4..ca2b36c 100644 --- a/pdfio-file.c +++ b/pdfio-file.c @@ -564,12 +564,15 @@ load_xref(pdfio_file_t *pdf, // I - PDF file _pdfioFileError(pdf, "Unable to read trailer dictionary."); return (false); } - else + else if (trailer.type != PDFIO_VALTYPE_DICT) { _pdfioFileError(pdf, "Trailer is not a dictionary."); return (false); } + PDFIO_DEBUG("load_xref: Contents of trailer dictionary:\n"); + PDFIO_DEBUG_DICT(trailer.value.dict, "load_xref"); + if (!pdf->trailer) { // Save the trailer dictionary and grab the root (catalog) and info diff --git a/pdfio-private.h b/pdfio-private.h index dc8c0c7..ae2458a 100644 --- a/pdfio-private.h +++ b/pdfio-private.h @@ -48,8 +48,10 @@ # ifdef DEBUG # define PDFIO_DEBUG(...) fprintf(stderr, __VA_ARGS__) +# define PDFIO_DEBUG_DICT(dict,prefix) _pdfioDictDebug(dict, prefix) # else # define PDFIO_DEBUG(...) +# define PDFIO_DEBUG_DICT(dict,prefix) # 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 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 _pdfio_value_t *_pdfioDictGetValue(pdfio_dict_t *dict, const char *key) PDFIO_INTERNAL; extern pdfio_dict_t *_pdfioDictRead(pdfio_file_t *pdf) PDFIO_INTERNAL; diff --git a/pdfio-token.c b/pdfio-token.c index 8225615..1a55e08 100644 --- a/pdfio-token.c +++ b/pdfio-token.c @@ -379,6 +379,8 @@ _pdfioTokenRead( *bufptr = '\0'; + PDFIO_DEBUG("_pdfioTokenRead(pdf=%p, ...): Read '%s'.\n", pdf, buffer); + return (bufptr > buffer); } diff --git a/pdfio-value.c b/pdfio-value.c index 2ac9a48..be88f96 100644 --- a/pdfio-value.c +++ b/pdfio-value.c @@ -101,12 +101,44 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file _pdfio_value_t *v) // I - Value { char token[8192]; // Token buffer +#ifdef DEBUG + static const char * const valtypes[] = + { + "<>", // 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))) 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 // String @@ -221,26 +253,14 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file // null value 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 { _pdfioFileError(pdf, "Unexpected '%s' token seen.", token); return (NULL); } + PDFIO_DEBUG("_pdfioValueRead: Returning %s value.\n", valtypes[v->type]); + return (v); }