... | ... |
@@ -350,6 +350,10 @@ static int find_length(struct pdf_struct *pdf, |
350 | 350 |
length = atoi(q); |
351 | 351 |
} |
352 | 352 |
} |
353 |
+ /* limit length */ |
|
354 |
+ if (start - pdf->map + length+5 > pdf->size) { |
|
355 |
+ length = pdf->size - (start - pdf->map)-5; |
|
356 |
+ } |
|
353 | 357 |
return length; |
354 | 358 |
} |
355 | 359 |
|
... | ... |
@@ -421,7 +425,7 @@ static int pdf_extract_obj(struct pdf_struct *pdf, struct pdf_obj *obj) |
421 | 421 |
length = size; |
422 | 422 |
|
423 | 423 |
if (obj->flags & (1 << OBJ_FILTER_AH)) { |
424 |
- ascii_decoded = cli_malloc(size/2 + 1); |
|
424 |
+ ascii_decoded = cli_malloc(length/2 + 1); |
|
425 | 425 |
if (!ascii_decoded) { |
426 | 426 |
cli_errmsg("Cannot allocate memory for asciidecode\n"); |
427 | 427 |
rc = CL_EMEM; |
... | ... |
@@ -431,7 +435,7 @@ static int pdf_extract_obj(struct pdf_struct *pdf, struct pdf_obj *obj) |
431 | 431 |
length, |
432 | 432 |
ascii_decoded); |
433 | 433 |
} else if (obj->flags & (1 << OBJ_FILTER_A85)) { |
434 |
- ascii_decoded = cli_malloc(size*5); |
|
434 |
+ ascii_decoded = cli_malloc(length*5); |
|
435 | 435 |
if (!ascii_decoded) { |
436 | 436 |
cli_errmsg("Cannot allocate memory for asciidecode\n"); |
437 | 437 |
rc = CL_EMEM; |
... | ... |
@@ -731,7 +735,7 @@ int cli_pdf(const char *dir, cli_ctx *ctx, off_t offset) |
731 | 731 |
|
732 | 732 |
/* Check PDF version */ |
733 | 733 |
if (!pdfver) { |
734 |
- cli_errmsg("cli_pdf: mmap() failed\n"); |
|
734 |
+ cli_errmsg("cli_pdf: mmap() failed (1)\n"); |
|
735 | 735 |
return CL_EMAP; |
736 | 736 |
} |
737 | 737 |
/* offset is 0 when coming from filetype2 */ |
... | ... |
@@ -760,7 +764,7 @@ int cli_pdf(const char *dir, cli_ctx *ctx, off_t offset) |
760 | 760 |
bytesleft = map->len - map_off; |
761 | 761 |
eofmap = fmap_need_off_once(map, map_off, bytesleft); |
762 | 762 |
if (!eofmap) { |
763 |
- cli_errmsg("cli_pdf: mmap() failed\n"); |
|
763 |
+ cli_errmsg("cli_pdf: mmap() failed (2)\n"); |
|
764 | 764 |
return CL_EMAP; |
765 | 765 |
} |
766 | 766 |
eof = eofmap + bytesleft; |
... | ... |
@@ -793,11 +797,12 @@ int cli_pdf(const char *dir, cli_ctx *ctx, off_t offset) |
793 | 793 |
pdf.flags |= 1 << BAD_PDF_TRAILER; |
794 | 794 |
} |
795 | 795 |
} |
796 |
+ size -= offset; |
|
796 | 797 |
|
797 | 798 |
pdf.size = size; |
798 | 799 |
pdf.map = fmap_need_off_once(map, offset, size); |
799 | 800 |
if (!pdf.map) { |
800 |
- cli_errmsg("cli_pdf: mmap() failed\n"); |
|
801 |
+ cli_errmsg("cli_pdf: mmap() failed (3)\n"); |
|
801 | 802 |
return CL_EMAP; |
802 | 803 |
} |
803 | 804 |
// parse PDF and find obj offsets |
... | ... |
@@ -1432,6 +1437,7 @@ static int asciihexdecode(const char *buf, off_t len, unsigned char *output) |
1432 | 1432 |
if (buf[i] == '>') |
1433 | 1433 |
break; |
1434 | 1434 |
cli_hex2str_to(buf+i, output+j++, 2); |
1435 |
+ i++; |
|
1435 | 1436 |
} |
1436 | 1437 |
return j; |
1437 | 1438 |
} |