From eaa62fa6d366727ab4f6c37f6c1be625560b5840 Mon Sep 17 00:00:00 2001 From: JaiLuthra1 Date: Tue, 28 Jan 2020 23:58:32 +0530 Subject: [PATCH] pdftops: added handling of empty input --- Makefile.am | 13 ++++++++++--- filter/pdf.cxx | 18 ++++++++++++++++++ filter/pdf.h | 1 + filter/pdftops.c | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/Makefile.am b/Makefile.am index 43f7a54e8..cc623cc68 100644 --- a/Makefile.am +++ b/Makefile.am @@ -789,10 +789,17 @@ texttotext_DEPENDENCIES = $(STRCASESTR) pdftops_SOURCES = \ filter/common.c \ filter/common.h \ - filter/pdftops.c + filter/pdftops.c \ + filter/pdf.cxx \ + filter/pdf.h EXTRA_pdftops_SOURCES = filter/strcasestr.c -pdftops_CFLAGS = $(CUPS_CFLAGS) -pdftops_LDADD = $(STRCASESTR) $(CUPS_LIBS) +pdftops_CFLAGS = \ + $(CUPS_CFLAGS) \ + $(LIBQPDF_CFLAGS) +pdftops_LDADD = \ + $(STRCASESTR) \ + $(CUPS_LIBS) \ + $(LIBQPDF_LIBS) pdftops_DEPENDENCIES = $(STRCASESTR) pdftoraster_SOURCES = \ diff --git a/filter/pdf.cxx b/filter/pdf.cxx index f12607f86..175df2151 100644 --- a/filter/pdf.cxx +++ b/filter/pdf.cxx @@ -84,6 +84,24 @@ extern "C" void pdf_free(pdf_t *pdf) delete pdf; } +/* + * 'pdf_pages()' - Count number of pages in file + * using QPDF. + * I - Filename to open + * O - Number of pages or -1 on error + */ +int pdf_pages(const char *filename) +{ + QPDF *pdf = new QPDF(); + try{ + pdf->processFile(filename); + }catch(...) { + return -1; + } + int pages = (pdf->getAllPages()).size(); + return pages; +} + /** * 'pdf_prepend_stream' - Prepend a stream to the contents of a specified diff --git a/filter/pdf.h b/filter/pdf.h index 18dcfa90b..eaaf1903f 100644 --- a/filter/pdf.h +++ b/filter/pdf.h @@ -45,6 +45,7 @@ void pdf_add_type1_font(pdf_t *doc, unsigned page, const char *name); void pdf_resize_page(pdf_t *doc, unsigned page, float width, float length, float *scale); void pdf_duplicate_page (pdf_t *doc, unsigned page, unsigned count); int pdf_fill_form(pdf_t *doc, opt_t *opt); +int pdf_pages(const char *filename); #ifdef __cplusplus } diff --git a/filter/pdftops.c b/filter/pdftops.c index 2ff74c6c4..8444e81f5 100644 --- a/filter/pdftops.c +++ b/filter/pdftops.c @@ -23,6 +23,7 @@ * Include necessary headers... */ +#include "pdf.h" #include #include #include @@ -224,6 +225,42 @@ void remove_options(char *options_str, const char **option_list) } +/* + * Check whether given file is empty + */ + +int is_empty(char *filename) +{ + FILE *fp = NULL; + fp = fopen(filename, "rb"); + if (fp == NULL) + { + fprintf(stderr, "ERROR: pdftops - cannot open print file \"%s\"\n", + filename); + exit(1); + } + else + { + char buf[1]; + rewind(fp); + if (fread(buf, 1, 1, fp) == 0) { + fclose(fp); + fprintf(stderr, "DEBUG: Input is empty, outputting empty file.\n"); + return 1; + } + fclose(fp); + int pages = pdf_pages(filename); + if (pages == 0) { + fprintf(stderr, "DEBUG: No pages left, outputting empty file.\n"); + return 1; + } + if (pages > 0) + return 0; + exit(1); + } +} + + /* * Before calling any command line utility, log its command line in CUPS' * debug mode @@ -381,6 +418,9 @@ main(int argc, /* I - Number of command-line args */ tempfile[0] = '\0'; } + if (is_empty(filename)) + return 0; + /* * Read out copy counts and collate setting passed over by pdftopdf */ -- 2.47.2