Debian Codegraph @

src/lib/ifdrules/check_tagorder.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 "check.h"
10 #include "check_helper.h"
11 #include <errno.h>
12 #include <stdio.h>
13
14 #include "ctstring.h"
15
16 ret_t check_tagorder(ctiff_t * ctif) {
17 GET_EMPTY_RET(ret);
18 TIFP_CHECK( ctif, ret);
19 if (has_sorted_tags != ctif->ifd0->tagorder) {
20 uint32 offset = get_ifd0_pos(ctif);
21 uint16 count = get_ifd0_count(ctif);
22
23 /* read count of tags (2 Bytes) */
24 /* replace i/o operatrions with in-memory-operations */
25 uint8 * ifdentries = NULL;
26 ifdentries = malloc ( sizeof(uint8) * 12 * count);
27 if (NULL == ifdentries) {
28 ret.returncode = could_not_allocate_memory;
29 return ret;
30 }
31 if (ct_seek(ctif, offset+2, SEEK_SET) != (offset+2)) {
32 ret = set_value_found_ret_formatted_with_returncode(&ret, tiff_seek_error_header, "%u bytes, errorcode=%i", offset+2, errno);
33 free(ifdentries);
34 return ret;
35 }
36 if (ct_read8(ctif, ifdentries, (12 * (size_t) count)) != 12 * count ) {
37 ret = set_value_found_ret_formatted_with_returncode(&ret, tiff_read_error_header, "%i bytes, errorcode=%i", 12*count, errno);
38 free(ifdentries);
39 return ret;
40 } else {
41 const uint8 * e = ifdentries;
42 uint16 lasttag = 0;
43 for (int i = 0; i<count; i++) {
44 uint8 lo = *e;
45 e++;
46 uint8 hi = *e;
47 uint16 tag = ((uint16) hi << 8) + lo;
48 e++;
49 if (is_byteswapped(ctif))
50 tag = TIFFSwabShort(tag);
51 if (i>0 && lasttag >= tag) {
52 // printf("tag idx=%i, tag=%u (0x%04x) (0x%02x) (0x%02x)\n", i, tag, tag, hi, lo);
53 free( ifdentries );
54 // FIXME: tif_fails?
55 ret = set_value_found_ret_formatted_with_returncode( &ret, ifderror_tags_not_in_ascending_order, "previous tag:%u (%s) , actual tag:%u (%s) at pos %i of %i\n", lasttag, TIFFTagName(lasttag), tag, TIFFTagName(tag), i, count);
56 ctif->ifd0->tagorder = has_unsorted_tags;
57 return ret;
58 }
59 lasttag = tag;
60 e+=10;
61 }
62 /* loop each tag until end or given tag found */
63 free( ifdentries );
64 ctif->ifd0->tagorder = has_sorted_tags;
65 }
66 }
67 ret.returncode=is_valid;
68 return ret;
69 }
70
71
72 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 smarttab expandtab :*/