Debian Codegraph @

src/lib/tagrules/check_datetime.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 <assert.h>
12 #include <stdio.h>
13
14 #include "ctstring.h"
15
16 /** check if date / time values are within correct ranges
17 * @param year year
18 * @param month month
19 * @param day day
20 * @param hour hour
21 * @param min min
22 * @param sec sec
23 * @return 0 if success, otherwise -1
24 */
25 static int test_plausibility (const int * year, const int * month, const int * day, const int * hour, const int * min, const int * sec) {
26 #ifdef DEBUG
27 printf ("found: y=%d m=%d d=%d h=%d m=%d s=%d\n", *year, *month, *day, *hour, *min, *sec);
28 #endif
29 if (
30 1500 < *year &&
31 2100 > *year &&
32 0 < *month &&
33 13 > *month &&
34 0 < *day &&
35 32 > *day &&
36 0 <= *hour &&
37 24 > *hour &&
38 0 <= *min &&
39 60 > *min &&
40 0 <= *sec &&
41 60 > *sec
42 ) {
43 return 0;
44 } else {
45 return -1;
46 }
47 }
48
49
50 ret_t check_datetime(ctiff_t * ctif ) {
51 GET_EMPTY_RET(ret);
52 TIFP_CHECK( ctif, ret);
53 EXIST_TAG(ctif,TIFFTAG_DATETIME,ret);
54
55 /* find date-tag and fix it */
56 TIFFDataType datatype = TIFFGetRawTagType( ctif, ctif->ifd0, TIFFTAG_DATETIME );
57 if (datatype != TIFF_ASCII) {
58 ret= set_value_found_ret_formatted(&ret, "type:%s", TIFFTypeName(datatype));
59 ret.returncode = tagerror_unexpected_type_found;
60 return ret;
61 }
62 uint32 count=0;
63 char *datetime=NULL;
64 ret = TIFFGetFieldASCII(ctif, ctif->ifd0, TIFFTAG_DATETIME, &datetime, &count);
65 if (ret.returncode != is_valid) return ret;
66
67 // printf("DATETIME='%s'\n", datetime);
68 int day=0;
69 int month=0;
70 int year=0;
71 int hour=0;
72 int min=0;
73 int sec=0;
74 unsigned int r = 0;
75 for (uint32 i=0; i<count; i++) {
76 if ((datetime[i] == '\0') && (i != 19)) { /* \0 at 20th byte allowed */
77 r = i+1;
78 break;
79 }
80 }
81 #ifdef DEBUG
82 printf(" count=%u\n\n", count);
83 #endif
84 ret = set_expected_value_formatted(&ret, "date in form 'YYYY:MM:DD hh:mm:ss");
85 ret = set_value_found_ret_with_returncode(&ret, str(datetime), should_not_occur);
86 if (0 == r) {
87 if (6 == sscanf(datetime, "%04d:%02d:%02d%02d:%02d:%02d", &year, &month, &day, &hour, &min, &sec)) {
88 if (0 == test_plausibility(&year, &month, &day, &hour, &min, &sec)) {
89 ret.returncode= is_valid;
90 } else {
91 ret.returncode = tagerror_datetime_not_plausible;
92 }
93 } else {
94 ret.returncode = tagerror_datetime_wrong_format;
95 }
96 } else {
97 ret.returncode = tagerror_datetime_wrong_size;
98 }
99 free(datetime);
100 assert( ret.returncode != should_not_occur);
101 return ret;
102 }
103
104 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 smarttab expandtab :*/