[cups] Can't detect file type / Not a PDF file

Till Kamppeter till.kamppeter at gmail.com
Thu May 2 08:40:37 PDT 2019


On 02/05/2019 16:59, Henrik Morsing wrote:
> Hi,
> 
> I don't really do C++ programming but:
> 
> parse_doc_type(FILE *fp)
> {
>   char buf[5];
> 
>   /* get the first few bytes of the file */
>   rewind(fp);
> /* skip until PDF/PS start header */
> while (fgets(buf,sizeof(buf),fp) != 0) {
>    if (strncmp(buf,"%PDF",4) == 0) return GS_DOC_TYPE_PDF;
>    if (strncmp(buf,"%!",2) == 0) return GS_DOC_TYPE_PS;
>   }
>   return GS_DOC_TYPE_UNKNOWN;
> }
> 
> 
> the comment alone says "get the first few bytes of the file"? And 
> buf[5], is that five bytes? In which case that wouldn't get to the %!.

The rewind(fp) moves the read pointer into the file to the beginning, so 
that the following reading of the file is starting at the beginning of 
the file.

The reading is done in a while() loop, meaning that it is repeated until 
either one of the magic strings is found (and returned from the 
function) or the file ends. There is no limitation to the first few 
bytes. This loop would go until the end of the file.

Each read cycle reads a maximum of 5 bytes and if a line ends withing 
the 5 bytes it is read only to the end of the line and not further, 
making the next read starting in the beginning of the line and so a 
magic string in the beginning of the line should always be found as it 
never can fall onto the boundary of two reads.

For me all looks OK and I did a short test and it works for me.

As you seem to be able to reproduce the bug I recommend you to add some 
additiional code for debugging:

----------
int n;
while ((n = fgets(buf,sizeof(buf),fp)) != 0) {
    fprintf(stderr, "DEBUG: |%d|%s|\n", n, buf);
    if (strncmp(buf,"%PDF",4) == 0) return GS_DOC_TYPE_PDF;
    if (strncmp(buf,"%!",2) == 0) return GS_DOC_TYPE_PS;
}
return GS_DOC_TYPE_UNKNOWN;
----------

    Till


More information about the cups mailing list