Debian Codegraph @

src/lib/ifdrules/check_all_offsets_within_filesize.c

raw
1 /* 'checkit_tiff' is a conformance checker for baseline TIFFs
2 *
3 * author: Andreas Romeyke, 2015-2022
4 * licensed under conditions of libtiff
5 * (see http://libtiff.maptools.org/misc.html)
6 *
7 */
8
9 #include <stdio.h>
10
11 #include "check.h"
12 #include "check_helper.h"
13 #include "ctstring.h"
14
15 static ret_t check_specific_offsets_within_filesize(ctiff_t * ctif, cifd_t * ifd) {
16 GET_EMPTY_RET(ret);
17 TIFP_CHECK( ctif, ret);
18 size_t filesize = ctif->streamlen;
19 uint16 count = get_ifd_count( ifd);
20 for (int tagidx = 0; tagidx< count; tagidx++) {
21 ifd_entry_t ifd_entry = TIFFGetRawTagIFDListEntry(ctif, ifd, tagidx);
22 uint32 tag = TIFFGetRawTagListEntry(ctif, ifd, tagidx);
23 if (ifd_entry.value_or_offset == is_offset || tag == TIFFTAG_EXIFIFD) {
24 uint32 offset = ifd_entry.data32offset;
25 uint32 streamlen = 0;
26 if (tag_is_an_IFD(&ifd_entry) || tag == TIFFTAG_EXIFIFD) {
27 cifd_t tmpifd = {0};
28 get_IFD_at_pos(ctif, offset, &tmpifd);
29 streamlen = 12*get_ifd_count(&tmpifd) + 2 + 4;
30 } else {
31 streamlen = get_streamlen_at_offset(&ifd_entry);
32 }
33 /* end else if tag == TIFFTAG_EXIFIFD */
34 char msg_if_subifd[8192] = {0};
35 if (ifd->tag_if_subifd < 0) {
36 sprintf( msg_if_subifd, "of sub IFD (tag=%u)", - ifd->tag_if_subifd);
37 }
38 if ((size_t) offset > filesize) {
39 ret = set_value_found_ret_formatted_with_returncode(&ret, ifderror_offset_out_of_filesize, "tag %u %s pointing to 0x%08x (%lu) exceeding filesize %lu bytes", tag, msg_if_subifd, offset, offset, filesize);
40 return ret;
41 }
42 if (((size_t) offset + streamlen) > filesize) {
43 ret = set_value_found_ret_formatted_with_returncode(&ret, ifderror_offset_out_of_filesize, "tag %u %s pointing to 0x%08x (%lu) with stream of len %u, exceeding filesize %lu bytes", tag, msg_if_subifd, offset, offset, streamlen, filesize);
44 return ret;
45 }
46 }
47 }
48 ret.returncode=is_valid;
49 return ret;
50 }
51
52 /* check if offsets are within filesize */
53 ret_t check_all_offsets_within_filesize(ctiff_t * ctif) {
54 GET_EMPTY_RET(ret);
55 TIFP_CHECK( ctif, ret);
56 ret = check_specific_offsets_within_filesize(ctif, ctif->ifd0);
57 if (ret.returncode != is_valid) {
58 return ret;
59 }
60 uint16 count = get_ifd0_count( ctif);
61 for (int tagidx = 0; tagidx< count; tagidx++) {
62 ifd_entry_t ifd_entry = TIFFGetRawTagIFDListEntry(ctif, ctif->ifd0, tagidx);
63 uint32 tag = TIFFGetRawTagListEntry(ctif, ctif->ifd0, tagidx);
64 if (
65 tag_is_an_IFD(&ifd_entry)
66 || (tag == TIFFTAG_EXIFIFD)
67 ) {
68
69 //printf("DEBUG: found IFD type for tag %u\n", tag);
70 uint32 offset = ifd_entry.data32offset;
71 cifd_t ifd = {0};
72 ifd.tag_if_subifd = -tag;
73 get_IFD_at_pos(ctif, offset, &ifd);
74 ret = check_specific_offsets_within_filesize( ctif, &ifd);
75 if (ret.returncode!=is_valid) { return ret; }
76 }
77 }
78 ret.returncode=is_valid;
79 return ret;
80 }
81
82
83 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 smarttab expandtab :*/