From 63a7a2cdbd17ce507ddaf0adb5f06cba8c6191c0 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Mon, 24 Jun 2024 11:46:15 -0400 Subject: [PATCH] Add unit tests for new pdfioFileGetCatalog API (Issue #67) Fix pdfioDictGetString to convert (formerly) encrypted binary strings to regular strings. --- pdfio-dict.c | 18 ++++++++++ testpdfio.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/pdfio-dict.c b/pdfio-dict.c index 423ada1..7fe5291 100644 --- a/pdfio-dict.c +++ b/pdfio-dict.c @@ -426,9 +426,27 @@ pdfioDictGetString(pdfio_dict_t *dict, // I - Dictionary if (value && value->type == PDFIO_VALTYPE_STRING) + { return (value->value.string); + } + else if (value && value->type == PDFIO_VALTYPE_BINARY && value->value.binary.datalen < 4096) + { + // Convert binary string to regular string... + char temp[4096]; // Temporary string + + memcpy(temp, value->value.binary.data, value->value.binary.datalen); + temp[value->value.binary.datalen] = '\0'; + + free(value->value.binary.data); + value->type = PDFIO_VALTYPE_STRING; + value->value.string = pdfioStringCreate(dict->pdf, temp); + + return (value->value.string); + } else + { return (NULL); + } } diff --git a/testpdfio.c b/testpdfio.c index 3a4cc0c..d37d388 100644 --- a/testpdfio.c +++ b/testpdfio.c @@ -1308,6 +1308,7 @@ read_unit_file(const char *filename, // I - File to read bool is_output) // I - File written with output callback? { pdfio_file_t *pdf; // PDF file + pdfio_dict_t *catalog; // Catalog dictionary size_t i; // Looping var const char *s; // String bool error = false; // Error callback data @@ -1320,6 +1321,83 @@ read_unit_file(const char *filename, // I - File to read else return (1); + // Get the root object/catalog dictionary + fputs("pdfioFileGetCatalog: ", stdout); + if ((catalog = pdfioFileGetCatalog(pdf)) != NULL) + { + puts("PASS"); + } + else + { + puts("FAIL (got NULL, expected dictionary)"); + return (1); + } + + // Verify some catalog values... + fputs("pdfioDictGetName(PageLayout): ", stdout); + if ((s = pdfioDictGetName(catalog, "PageLayout")) != NULL && !strcmp(s, "SinglePage")) + { + puts("PASS"); + } + else if (s) + { + printf("FAIL (got '%s', expected 'SinglePage')\n", s); + return (1); + } + else + { + puts("FAIL (got NULL, expected 'SinglePage')"); + return (1); + } + + fputs("pdfioDictGetName(PageLayout): ", stdout); + if ((s = pdfioDictGetName(catalog, "PageLayout")) != NULL && !strcmp(s, "SinglePage")) + { + puts("PASS"); + } + else if (s) + { + printf("FAIL (got '%s', expected 'SinglePage')\n", s); + return (1); + } + else + { + puts("FAIL (got NULL, expected 'SinglePage')"); + return (1); + } + + fputs("pdfioDictGetName(PageMode): ", stdout); + if ((s = pdfioDictGetName(catalog, "PageMode")) != NULL && !strcmp(s, "UseThumbs")) + { + puts("PASS"); + } + else if (s) + { + printf("FAIL (got '%s', expected 'UseThumbs')\n", s); + return (1); + } + else + { + puts("FAIL (got NULL, expected 'UseThumbs')"); + return (1); + } + + fputs("pdfioDictGetString(Lang): ", stdout); + if ((s = pdfioDictGetString(catalog, "Lang")) != NULL && !strcmp(s, "en")) + { + puts("PASS"); + } + else if (s) + { + printf("FAIL (got '%s', expected 'en')\n", s); + return (1); + } + else + { + puts("FAIL (got NULL, expected 'en')"); + return (1); + } + // Verify metadata... fputs("pdfioFileGetAuthor: ", stdout); if ((s = pdfioFileGetAuthor(pdf)) != NULL && !strcmp(s, "Michael R Sweet")) @@ -3256,8 +3334,26 @@ write_unit_file( *gray_jpg, // gray.jpg image *helvetica, // Helvetica font *page; // Page from test PDF file + pdfio_dict_t *catalog; // Catalog dictionary + // Get the root object/catalog dictionary + fputs("pdfioFileGetCatalog: ", stdout); + if ((catalog = pdfioFileGetCatalog(outpdf)) != NULL) + { + puts("PASS"); + } + else + { + puts("FAIL (got NULL, expected dictionary)"); + return (1); + } + + // Set some catalog values... + pdfioDictSetName(catalog, "PageLayout", "SinglePage"); + pdfioDictSetName(catalog, "PageMode", "UseThumbs"); + pdfioDictSetString(catalog, "Lang", "en"); + // Set info values... fputs("pdfioFileGet/SetAuthor: ", stdout); pdfioFileSetAuthor(outpdf, "Michael R Sweet");