• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** \file test-parse-from-data.c
2  * \brief Completely parse all files given on the command line.
3  *
4  * Copyright (C) 2007 Hans Ulrich Niedermann <gp@n-dimensional.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA  02110-1301  USA.
20  *
21  * SPDX-License-Identifier: LGPL-2.0-or-later
22  */
23 
24 #include "libexif/exif-data.h"
25 #include "libexif/exif-system.h"
26 
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 
34 
35 static unsigned entry_count;
36 
37 /** Callback function handling an ExifEntry. */
content_foreach_func(ExifEntry * entry,void * UNUSED (callback_data))38 static void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data))
39 {
40   char buf[2000];
41   exif_entry_get_value(entry, buf, sizeof(buf));
42   printf("    Entry %u: %s (%s)\n"
43 	 "      Size, Comps: %d, %d\n"
44 	 "      Value: %s\n",
45 	 entry_count,
46 	 exif_tag_get_name(entry->tag),
47 	 exif_format_get_name(entry->format),
48 	 entry->size,
49 	 (int)(entry->components),
50 	 exif_entry_get_value(entry, buf, sizeof(buf)));
51   ++entry_count;
52 }
53 
54 
55 /** Callback function handling an ExifContent (corresponds 1:1 to an IFD). */
data_foreach_func(ExifContent * content,void * callback_data)56 static void data_foreach_func(ExifContent *content, void *callback_data)
57 {
58   static unsigned content_count;
59   entry_count = 0;
60   printf("  Content %u: ifd=%d\n", content_count, exif_content_get_ifd(content));
61   exif_content_foreach_entry(content, content_foreach_func, callback_data);
62   ++content_count;
63 }
64 
dump_makernote(ExifData * d)65 static void dump_makernote(ExifData *d) {
66   ExifMnoteData *mn = exif_data_get_mnote_data(d);
67   if (mn) {
68     char buf[2000];
69     int i;
70     int num = exif_mnote_data_count(mn);
71     printf("  MakerNote\n");
72     for (i=0; i < num; ++i) {
73       if (exif_mnote_data_get_value(mn, i, buf, sizeof(buf))) {
74 	const char *name = exif_mnote_data_get_name(mn, i);
75 	unsigned int id = exif_mnote_data_get_id(mn, i);
76 	if (!name)
77 	    name = "(unknown)";
78 	printf("    Entry %u: %u, %s\n"
79 	       "      Size: %u\n"
80 	       "      Value: %s\n", i, id, name, (unsigned)strlen(buf), buf);
81       }
82     }
83   }
84 }
85 
86 /** Run EXIF parsing test on the given file. */
test_parse(const char * filename,void * callback_data,int swap)87 static void test_parse(const char *filename, void *callback_data, int swap)
88 {
89   ExifData *d;
90   int fd;
91   unsigned char *data;
92   struct stat stbuf;
93 
94   /* Skip over path to display only the file name */
95   const char *fn = strrchr(filename, '/');
96   if (fn)
97     ++fn;
98   else
99     fn = filename;
100   printf("File %s\n", fn);
101 
102   d = exif_data_new_from_file(filename);
103   exif_data_unref(d);
104 
105   fd = open(filename,O_RDONLY);
106   if (fd == -1) {
107     perror(filename);
108     return;
109   }
110   if (-1 == fstat(fd, &stbuf)) {
111     perror(filename);
112     return;
113   }
114   data = malloc(stbuf.st_size);
115   if (!data) {
116     fprintf (stderr, "Failed to allocate %ld bytes for reading %s\n", (long)stbuf.st_size, filename);
117     return;
118   }
119   if (-1 == read(fd, data, stbuf.st_size)) {
120     perror ("read");
121     free(data);
122     close(fd);
123     return;
124   }
125   close(fd);
126 
127   d = exif_data_new_from_data(data, stbuf.st_size);
128   free(data);
129   if (!d) {
130       fprintf (stderr, "Could not load data from '%s'!\n", filename);
131       return;
132   }
133   printf("Byte order: %s\n",
134           exif_byte_order_get_name(exif_data_get_byte_order(d)));
135 
136   if (swap) {
137       ExifByteOrder order = EXIF_BYTE_ORDER_INTEL;
138       if (exif_data_get_byte_order(d) == order) {
139           order = EXIF_BYTE_ORDER_MOTOROLA;
140       }
141       /* This switches the byte order of the entire EXIF data structure,
142        * including the MakerNote */
143       exif_data_set_byte_order(d, order);
144       printf("New byte order: %s\n",
145               exif_byte_order_get_name(exif_data_get_byte_order(d)));
146   }
147 
148   exif_data_foreach_content(d, data_foreach_func, callback_data);
149 
150   dump_makernote(d);
151 
152   exif_data_unref(d);
153 }
154 
155 
156 /** Callback function prototype for string parsing. */
157 typedef void (*test_parse_func) (const char *filename, void *callback_data, int swap);
158 
159 
160 /** Split string at whitespace and call callback with each substring. */
split_ws_string(const char * string,test_parse_func func,void * callback_data)161 static void split_ws_string(const char *string, test_parse_func func, void *callback_data)
162 {
163   const char *start = string;
164   const char *p = start;
165   for (;;) {
166     if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == '\0' ) {
167       size_t len = p-start;
168       if (len > 0) {
169 	/* emulate strndup */
170 	char *str = malloc(1+len);
171 	if (str) {
172 	  memcpy(str, start, len);
173 	  str[len] = '\0';
174 	  func(str, callback_data, 0);
175 	  free(str);
176 	  start = p+1;
177 	}
178       } else {
179 	start = p+1;
180       }
181     }
182     if (*p == '\0') {
183       break;
184     }
185     p++;
186   }
187 }
188 
189 
190 /** Main program. */
main(const int argc,const char * argv[])191 int main(const int argc, const char *argv[])
192 {
193   int i;
194   void *callback_data = NULL;
195   int swap = 0;
196   int first = 1;
197 
198   if (argc > 1 && !strcmp(argv[1], "--swap-byte-order")) {
199       swap = 1;
200       ++first;
201   }
202 
203   if (argc > first) {
204     for (i=first; i<argc; i++) {
205       test_parse(argv[i], callback_data, swap);
206     }
207   } else {
208     /* If no command-line argument is found, get the file names from
209        the environment. */
210     const char *envar = getenv("TEST_IMAGES");
211     if (envar) {
212       split_ws_string(envar, test_parse, callback_data);
213     }
214   }
215 
216   return 0;
217 }
218