1 /**file test-fuzzer.c
2 * from test-parse.c and test-mnote.c
3 *
4 * \brief Completely parse all files given on the command line.
5 *
6 * Copyright (C) 2007 Hans Ulrich Niedermann <gp@n-dimensional.de>
7 * Copyright 2002 Lutz Mueller <lutz@users.sourceforge.net>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA.
23 *
24 * SPDX-License-Identifier: LGPL-2.0-or-later
25 */
26
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/stat.h>
31
32 #include "libexif/exif-data.h"
33 #include "libexif/exif-loader.h"
34 #include "libexif/exif-system.h"
35
36 #undef USE_LOG
37
38 #ifdef USE_LOG
39 static void
logfunc(ExifLog * log,ExifLogCode code,const char * domain,const char * format,va_list args,void * data)40 logfunc(ExifLog *log, ExifLogCode code, const char *domain, const char *format, va_list args, void *data)
41 {
42 fprintf( stderr, "test-fuzzer: code=%d domain=%s ", code, domain);
43 vfprintf (stderr, format, args);
44 fprintf (stderr, "\n");
45 }
46 #endif
47
48 /** Callback function handling an ExifEntry. */
49 void content_foreach_func(ExifEntry *entry, void *callback_data);
content_foreach_func(ExifEntry * entry,void * UNUSED (callback_data))50 void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data))
51 {
52 char buf[2001];
53
54 /* ensure \0 */
55 buf[sizeof(buf)-1] = 0;
56 buf[sizeof(buf)-2] = 0;
57 exif_tag_get_name(entry->tag);
58 exif_format_get_name(entry->format);
59 exif_entry_get_value(entry, buf, sizeof(buf)-1);
60 if (buf[sizeof(buf)-2] != 0) abort();
61 }
62
63
64 /** Callback function handling an ExifContent (corresponds 1:1 to an IFD). */
65 void data_foreach_func(ExifContent *content, void *callback_data);
data_foreach_func(ExifContent * content,void * callback_data)66 void data_foreach_func(ExifContent *content, void *callback_data)
67 {
68 printf(" Content %p: ifd=%d\n", (void *)content, exif_content_get_ifd(content));
69 exif_content_foreach_entry(content, content_foreach_func, callback_data);
70 }
71 static int
test_exif_data(ExifData * d)72 test_exif_data (ExifData *d)
73 {
74 unsigned int i, c;
75 char v[1024];
76 ExifMnoteData *md;
77
78 fprintf (stdout, "Byte order: %s\n",
79 exif_byte_order_get_name (exif_data_get_byte_order (d)));
80
81 md = exif_data_get_mnote_data (d);
82 if (!md) {
83 fprintf (stderr, "Could not parse maker note!\n");
84 return 1;
85 }
86
87 exif_mnote_data_ref (md);
88 exif_mnote_data_unref (md);
89
90 c = exif_mnote_data_count (md);
91 for (i = 0; i < c; i++) {
92 const char *name = exif_mnote_data_get_name (md, i);
93 if (!name) continue;
94 exif_mnote_data_get_name (md, i);
95 exif_mnote_data_get_title (md, i);
96 exif_mnote_data_get_description (md, i);
97 exif_mnote_data_get_value (md, i, v, sizeof (v));
98 }
99
100 return 0;
101 }
102
103
104
105 /** Run EXIF parsing test on the given file. */
test_parse(const char * filename,void * callback_data)106 static void test_parse(const char *filename, void *callback_data)
107 {
108 ExifData *d;
109 ExifLoader *loader = exif_loader_new();
110 unsigned int buf_size;
111 unsigned char *buf;
112 FILE *f;
113 struct stat stbuf;
114 #ifdef USE_LOG
115 ExifLog *log = exif_log_new ();
116
117 exif_log_set_func(log, logfunc, NULL);
118 #endif
119
120 /* try the exif loader */
121 d = exif_data_new_from_file(filename);
122 #ifdef USE_LOG
123 exif_data_log (d, log);
124 #endif
125 exif_data_foreach_content(d, data_foreach_func, callback_data);
126 test_exif_data (d);
127
128 buf = NULL;
129 exif_data_save_data (d, &buf, &buf_size);
130 free (buf);
131
132 exif_data_set_byte_order(d, EXIF_BYTE_ORDER_INTEL);
133
134 buf = NULL;
135 exif_data_save_data (d, &buf, &buf_size);
136 free (buf);
137
138 exif_data_unref(d);
139
140 /* try the exif data writer ... different than the loader */
141 if (-1 == stat(filename,&stbuf))
142 perror("stat");
143 f = fopen(filename,"r");
144 if (!f) {
145 fprintf(stderr, "Error opening %s\n", filename);
146 return;
147 }
148
149 buf = malloc(stbuf.st_size);
150 if (fread (buf, stbuf.st_size, 1, f) != 1) {
151 fprintf(stderr, "Error reading %s\n", filename);
152 return;
153 }
154 fclose(f);
155
156 exif_loader_write(loader, buf, stbuf.st_size);
157 free (buf);
158
159 d = exif_loader_get_data(loader);
160 exif_data_foreach_content(d, data_foreach_func, callback_data);
161 test_exif_data (d);
162 exif_loader_unref(loader);
163 exif_data_unref(d);
164 }
165
166
167 /** Main program. */
main(const int argc,const char * argv[])168 int main(const int argc, const char *argv[])
169 {
170 int i;
171 void *callback_data = NULL;
172 for (i=1; i<argc; i++) {
173 test_parse(argv[i], callback_data);
174 }
175 return 0;
176 }
177