• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** \file test-parse.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 
31 static unsigned entry_count;
32 
33 /** Callback function handling an ExifEntry. */
content_foreach_func(ExifEntry * entry,void * UNUSED (callback_data))34 static void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data))
35 {
36   char buf[2000];
37   exif_entry_get_value(entry, buf, sizeof(buf));
38   printf("    Entry %u: %s (%s)\n"
39 	 "      Size, Comps: %d, %d\n"
40 	 "      Value: %s\n",
41 	 entry_count,
42 	 exif_tag_get_name(entry->tag),
43 	 exif_format_get_name(entry->format),
44 	 entry->size,
45 	 (int)(entry->components),
46 	 exif_entry_get_value(entry, buf, sizeof(buf)));
47   ++entry_count;
48 }
49 
50 
51 /** Callback function handling an ExifContent (corresponds 1:1 to an IFD). */
data_foreach_func(ExifContent * content,void * callback_data)52 static void data_foreach_func(ExifContent *content, void *callback_data)
53 {
54   static unsigned content_count;
55   entry_count = 0;
56   printf("  Content %u: ifd=%d\n", content_count, exif_content_get_ifd(content));
57   exif_content_foreach_entry(content, content_foreach_func, callback_data);
58   ++content_count;
59 }
60 
dump_makernote(ExifData * d)61 static void dump_makernote(ExifData *d) {
62   ExifMnoteData *mn = exif_data_get_mnote_data(d);
63   if (mn) {
64     char buf[2000];
65     int i;
66     int num = exif_mnote_data_count(mn);
67     printf("  MakerNote\n");
68     for (i=0; i < num; ++i) {
69       if (exif_mnote_data_get_value(mn, i, buf, sizeof(buf))) {
70 	const char *name = exif_mnote_data_get_name(mn, i);
71 	unsigned int id = exif_mnote_data_get_id(mn, i);
72 	if (!name)
73 	    name = "(unknown)";
74 	printf("    Entry %u: %u, %s\n"
75 	       "      Size: %u\n"
76 	       "      Value: %s\n", i, id, name, (unsigned)strlen(buf), buf);
77       }
78     }
79   }
80 }
81 
82 /** Run EXIF parsing test on the given file. */
test_parse(const char * filename,void * callback_data,int swap)83 static void test_parse(const char *filename, void *callback_data, int swap)
84 {
85   ExifData *d;
86 
87   /* Skip over path to display only the file name */
88   const char *fn = strrchr(filename, '/');
89   if (fn)
90     ++fn;
91   else
92     fn = filename;
93   printf("File %s\n", fn);
94 
95   d = exif_data_new_from_file(filename);
96   if (!d) {
97       fprintf (stderr, "Could not load data from '%s'!\n", filename);
98       return;
99   }
100   printf("Byte order: %s\n",
101           exif_byte_order_get_name(exif_data_get_byte_order(d)));
102 
103   if (swap) {
104       ExifByteOrder order = EXIF_BYTE_ORDER_INTEL;
105       if (exif_data_get_byte_order(d) == order) {
106           order = EXIF_BYTE_ORDER_MOTOROLA;
107       }
108       /* This switches the byte order of the entire EXIF data structure,
109        * including the MakerNote */
110       exif_data_set_byte_order(d, order);
111       printf("New byte order: %s\n",
112               exif_byte_order_get_name(exif_data_get_byte_order(d)));
113   }
114 
115   exif_data_foreach_content(d, data_foreach_func, callback_data);
116 
117   dump_makernote(d);
118 
119   exif_data_unref(d);
120 }
121 
122 
123 /** Callback function prototype for string parsing. */
124 typedef void (*test_parse_func) (const char *filename, void *callback_data, int swap);
125 
126 
127 /** Split string at whitespace and call callback with each substring. */
split_ws_string(const char * string,test_parse_func func,void * callback_data)128 static void split_ws_string(const char *string, test_parse_func func, void *callback_data)
129 {
130   const char *start = string;
131   const char *p = start;
132   for (;;) {
133     if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == '\0' ) {
134       size_t len = p-start;
135       if (len > 0) {
136 	/* emulate strndup */
137 	char *str = malloc(1+len);
138 	if (str) {
139 	  memcpy(str, start, len);
140 	  str[len] = '\0';
141 	  func(str, callback_data, 0);
142 	  free(str);
143 	  start = p+1;
144 	}
145       } else {
146 	start = p+1;
147       }
148     }
149     if (*p == '\0') {
150       break;
151     }
152     p++;
153   }
154 }
155 
156 
157 /** Main program. */
main(const int argc,const char * argv[])158 int main(const int argc, const char *argv[])
159 {
160   int i;
161   void *callback_data = NULL;
162   int swap = 0;
163   int first = 1;
164 
165   if (argc > 1 && !strcmp(argv[1], "--swap-byte-order")) {
166       swap = 1;
167       ++first;
168   }
169 
170   if (argc > first) {
171     for (i=first; i<argc; i++) {
172       test_parse(argv[i], callback_data, swap);
173     }
174   } else {
175     /* If no command-line argument is found, get the file names from
176        the environment. */
177     const char *envar = getenv("TEST_IMAGES");
178     if (envar) {
179       split_ws_string(envar, test_parse, callback_data);
180     }
181   }
182 
183   return 0;
184 }
185