• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* test-gps.c
2  *
3  * Creates ExifEntries for the GPS ifd and initializes them.
4  * Checks for formats and component counts.
5  *
6  * Copyright 2020 Heiko Lewin <hlewin@gmx.de>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA  02110-1301  USA.
22  *
23  * SPDX-License-Identifier: LGPL-2.0-or-later
24  */
25 
26 #include <libexif/exif-utils.h>
27 #include <libexif/exif-data.h>
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdint.h>
33 
34 /*
35  * List of tags to test,
36  */
37 static const uint16_t test_tags [] = {
38  EXIF_TAG_GPS_VERSION_ID,
39  EXIF_TAG_GPS_LATITUDE_REF,
40  EXIF_TAG_GPS_LATITUDE,
41  EXIF_TAG_GPS_LONGITUDE_REF,
42  EXIF_TAG_GPS_LONGITUDE,
43  EXIF_TAG_GPS_ALTITUDE_REF,
44  EXIF_TAG_GPS_ALTITUDE,
45  EXIF_TAG_GPS_TIME_STAMP,
46  EXIF_TAG_GPS_SATELLITES,
47  EXIF_TAG_GPS_STATUS,
48  EXIF_TAG_GPS_MEASURE_MODE,
49  EXIF_TAG_GPS_DOP,
50  EXIF_TAG_GPS_SPEED_REF,
51  EXIF_TAG_GPS_SPEED,
52  EXIF_TAG_GPS_TRACK_REF,
53  EXIF_TAG_GPS_TRACK,
54  EXIF_TAG_GPS_IMG_DIRECTION_REF,
55  EXIF_TAG_GPS_IMG_DIRECTION,
56  EXIF_TAG_GPS_MAP_DATUM,
57  EXIF_TAG_GPS_DEST_LATITUDE_REF,
58  EXIF_TAG_GPS_DEST_LATITUDE,
59  EXIF_TAG_GPS_DEST_LONGITUDE_REF,
60  EXIF_TAG_GPS_DEST_LONGITUDE,
61  EXIF_TAG_GPS_DEST_BEARING_REF,
62  EXIF_TAG_GPS_DEST_BEARING,
63  EXIF_TAG_GPS_DEST_DISTANCE_REF,
64  EXIF_TAG_GPS_DEST_DISTANCE,
65  EXIF_TAG_GPS_PROCESSING_METHOD,
66  EXIF_TAG_GPS_AREA_INFORMATION,
67  EXIF_TAG_GPS_DATE_STAMP,
68  EXIF_TAG_GPS_DIFFERENTIAL,
69  EXIF_TAG_GPS_H_POSITIONING_ERROR,
70  0xFFFFu
71 };
72 
73 
74 /*
75  * Verify that the entry is properly initialized.
76  */
check_entry_format(ExifEntry * e)77 static int check_entry_format(ExifEntry *e)
78 {
79 	if(e->tag > EXIF_TAG_GPS_H_POSITIONING_ERROR) {
80 		/* unknown tags should get EXIF_FORMAT_UNDEFINED, no size and no data */
81 		if(e->format != EXIF_FORMAT_UNDEFINED || e->size || e->components || e->data) {
82 		    fprintf(stderr, "check_entry_format: Unknown tag not handled correctly (tag=%x)\n", e->tag);
83 		    return 1;
84 		}
85 		return 0;
86 	}
87 	switch(e->format) {
88 	case EXIF_FORMAT_UNDEFINED:
89 	case EXIF_FORMAT_ASCII:
90 		/* entries with ASCII or UNDEFINED format do not necessarily need to have the component count set.
91 		   only check here is, if component count is set, the size should match the count */
92 		if(e->size != e->components) {
93 			fprintf (stderr, "check_entry_format: Entry has bad component count or size (tag=%x)\n", e->tag);
94 			return 1;
95 		}
96 		break;
97 
98 	default:
99 		/* All other formats should have a nonzero component count. */
100 		if(!e->components) {
101 			fprintf (stderr, "check_entry_format: Entry should have component count set (tag=%x)\n", e->tag);
102 			return 1;
103 		}
104 		return 0;
105 	}
106 
107 	/* If a value is present the size should be set to the right value */
108 	if(e->data && e->size != e->components * exif_format_get_size(e->format)) {
109 		fprintf (stderr, "check_entry_format: Entry has bad size (tag=%x)\n", e->tag);
110 		return 1;
111 	}
112 	return 0;
113 }
114 
115 int
main(void)116 main (void)
117 {
118 	size_t i;
119 	ExifData *data = NULL;
120 	ExifEntry *e = NULL;
121 
122 	data = exif_data_new ();
123 	if (!data) {
124 		fprintf (stderr, "Error running exif_data_new()\n");
125 		goto ERROR_EXIT;
126 	}
127 
128 	/* Run tests */
129 	for (i=0; i < sizeof(test_tags)/sizeof(test_tags[0]); ++i) {
130 		e = exif_entry_new ();
131 		if (!e) {
132 			fprintf (stderr, "Error running exif_entry_new()\n");
133 			goto ERROR_EXIT;
134 		}
135 		exif_content_add_entry(data->ifd[EXIF_IFD_GPS], e);
136 		exif_entry_initialize (e, (ExifTag)test_tags[i]);
137 		if(check_entry_format(e)) goto ERROR_EXIT;
138 		exif_content_remove_entry (data->ifd[EXIF_IFD_GPS], e);
139 		exif_entry_unref (e);
140 	}
141 	exif_data_unref(data);
142 	return 0;
143 ERROR_EXIT:
144 	exif_entry_unref (e);
145 	exif_data_unref (data);
146 	exit(EXIT_FAILURE);
147 }
148 
149