From e0419bc9f012f4dfaddbee3d2b5c75e6f68d6ebb Mon Sep 17 00:00:00 2001 From: JaiLuthra1 Date: Tue, 4 Feb 2020 16:51:58 +0530 Subject: [PATCH] foomatic-rip: Added handling of empty input --- filter/foomatic-rip/foomaticrip.c | 4 ++++ filter/foomatic-rip/pdf.c | 16 ++++++++++++---- filter/foomatic-rip/postscript.c | 8 ++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/filter/foomatic-rip/foomaticrip.c b/filter/foomatic-rip/foomaticrip.c index 2c6cdb661..a1a4e9ea0 100644 --- a/filter/foomatic-rip/foomaticrip.c +++ b/filter/foomatic-rip/foomaticrip.c @@ -576,6 +576,10 @@ int print_file(const char *filename, int convert) } n = fread_or_die(buf, 1, sizeof(buf) - 1, file); + if (!n){ + _log("Input is empty, outputting empty file.\n"); + return 1; + } buf[n] = '\0'; type = guess_file_type(buf, n, &startpos); /* We do not use any JCL preceeded to the inputr data, as it is simply diff --git a/filter/foomatic-rip/pdf.c b/filter/foomatic-rip/pdf.c index c79b56450..cc6f2ca06 100644 --- a/filter/foomatic-rip/pdf.c +++ b/filter/foomatic-rip/pdf.c @@ -42,7 +42,7 @@ static int wait_for_renderer(); static int pdf_count_pages(const char *filename) { char gscommand[CMDLINE_MAX]; - char output[31] = ""; + char output[63] = ""; int pagecount; size_t bytes; @@ -55,11 +55,14 @@ static int pdf_count_pages(const char *filename) if (!pd) rip_die(EXIT_STARVED, "Failed to execute ghostscript to determine number of input pages!\n"); - bytes = fread_or_die(output, 1, 31, pd); + bytes = fread_or_die(output, 1, 63, pd); pclose(pd); if (bytes <= 0 || sscanf(output, "PageCount: %d", &pagecount) < 1) - pagecount = -1; + { + if(bytes <= 0 || sscanf(output, " **** Error: Too many pages in Page tree.\nPageCount: %d", &pagecount) < 1) + pagecount = -1; + } return pagecount; } @@ -242,8 +245,13 @@ static int print_pdf_file(const char *filename) page_count = pdf_count_pages(filename); - if (page_count <= 0) + if (page_count < 0) rip_die(EXIT_JOBERR, "Unable to determine number of pages, page count: %d\n", page_count); + if (page_count == 0) + { + _log("No pages left, outputting empty file.\n"); + return 1; + } _log("File contains %d pages\n", page_count); optionset_copy_values(optionset("header"), optionset("currentpage")); diff --git a/filter/foomatic-rip/postscript.c b/filter/foomatic-rip/postscript.c index f0ddf0180..8b6f0ad59 100644 --- a/filter/foomatic-rip/postscript.c +++ b/filter/foomatic-rip/postscript.c @@ -322,6 +322,7 @@ void _print_ps(stream_t *stream) pid_t rendererpid = 0; FILE *rendererhandle = NULL; + int empty = 1; int retval; dstr_t *tmp = create_dstr(); @@ -1009,6 +1010,7 @@ void _print_ps(stream_t *stream) /* No renderer running, start it */ dstrcpy(tmp, psheader->data); dstrcat(tmp, psfifo->data); + empty = 0; get_renderer_handle(tmp, &rendererhandle, &rendererpid); /* psfifo is sent out, flush it */ dstrclear(psfifo); @@ -1073,6 +1075,12 @@ void _print_ps(stream_t *stream) } while ((maxlines == 0 || linect < maxlines) && more_stuff != 0); + if (empty) + { + _log("No pages left, outputting empty file.\n"); + return; + } + /* Some buffer still containing data? Send it out to the renderer */ if (more_stuff || inheader || !isempty(psfifo->data)) { /* Flush psfifo and send the remaining data to the renderer, this -- 2.47.2