• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
25 
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 
31 #include "libexif/exif-data.h"
32 #include "libexif/exif-loader.h"
33 #include "libexif/exif-system.h"
34 
35 #undef USE_LOG
36 
37 #ifdef USE_LOG
38 static void
logfunc(ExifLog * log,ExifLogCode code,const char * domain,const char * format,va_list args,void * data)39 logfunc(ExifLog *log, ExifLogCode code, const char *domain, const char *format, va_list args, void *data)
40 {
41 	fprintf( stderr, "test-fuzzer: code=%d domain=%s ", code, domain);
42 	vfprintf (stderr, format, args);
43 	fprintf (stderr, "\n");
44 }
45 #endif
46 
47 /** Callback function handling an ExifEntry. */
48 void content_foreach_func(ExifEntry *entry, void *callback_data);
content_foreach_func(ExifEntry * entry,void * UNUSED (callback_data))49 void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data))
50 {
51 	char buf[2001];
52 
53 	/* ensure \0 */
54 	buf[sizeof(buf)-1] = 0;
55 	buf[sizeof(buf)-2] = 0;
56 	exif_tag_get_name(entry->tag);
57 	exif_format_get_name(entry->format);
58 	exif_entry_get_value(entry, buf, sizeof(buf)-1);
59 	if (buf[sizeof(buf)-2] != 0) abort();
60 }
61 
62 
63 /** Callback function handling an ExifContent (corresponds 1:1 to an IFD). */
64 void data_foreach_func(ExifContent *content, void *callback_data);
data_foreach_func(ExifContent * content,void * callback_data)65 void data_foreach_func(ExifContent *content, void *callback_data)
66 {
67 	printf("  Content %p: ifd=%d\n", (void *)content, exif_content_get_ifd(content));
68 	exif_content_foreach_entry(content, content_foreach_func, callback_data);
69 }
70 static int
test_exif_data(ExifData * d)71 test_exif_data (ExifData *d)
72 {
73 	unsigned int i, c;
74 	char v[1024];
75 	ExifMnoteData *md;
76 
77 	fprintf (stdout, "Byte order: %s\n",
78 		exif_byte_order_get_name (exif_data_get_byte_order (d)));
79 
80 	md = exif_data_get_mnote_data (d);
81 	if (!md) {
82 		fprintf (stderr, "Could not parse maker note!\n");
83 		return 1;
84 	}
85 
86 	exif_mnote_data_ref (md);
87 	exif_mnote_data_unref (md);
88 
89 	c = exif_mnote_data_count (md);
90 	for (i = 0; i < c; i++) {
91 		const char *name = exif_mnote_data_get_name (md, i);
92 		if (!name) continue;
93 		exif_mnote_data_get_name (md, i);
94 		exif_mnote_data_get_title (md, i);
95 		exif_mnote_data_get_description (md, i);
96 		exif_mnote_data_get_value (md, i, v, sizeof (v));
97 	}
98 
99 	return 0;
100 }
101 
102 
103 
104 /** Run EXIF parsing test on the given file. */
test_parse(const char * filename,void * callback_data)105 static void test_parse(const char *filename, void *callback_data)
106 {
107 	ExifData	*d;
108 	ExifLoader	*loader = exif_loader_new();
109 	unsigned int	buf_size;
110 	unsigned char	*buf;
111 	FILE		*f;
112 	struct		stat stbuf;
113 #ifdef USE_LOG
114 	ExifLog		*log = exif_log_new ();
115 
116 	exif_log_set_func(log, logfunc, NULL);
117 #endif
118 
119 	/* try the exif loader */
120 	d = exif_data_new_from_file(filename);
121 #ifdef USE_LOG
122 	exif_data_log (d, log);
123 #endif
124 	exif_data_foreach_content(d, data_foreach_func, callback_data);
125 	test_exif_data (d);
126 
127 	buf = NULL;
128 	exif_data_save_data (d, &buf, &buf_size);
129 	free (buf);
130 
131 	exif_data_set_byte_order(d, EXIF_BYTE_ORDER_INTEL);
132 
133 	buf = NULL;
134 	exif_data_save_data (d, &buf, &buf_size);
135 	free (buf);
136 
137 	exif_data_unref(d);
138 
139 	/* try the exif data writer ... different than the loader */
140 	if (-1 == stat(filename,&stbuf))
141 		perror("stat");
142 	f = fopen(filename,"r");
143 	if (!f) return;
144 
145 	buf = malloc(stbuf.st_size);
146 	fread (buf, stbuf.st_size, 1, f);
147 	fclose(f);
148 
149 	exif_loader_write(loader, buf, stbuf.st_size);
150 	free (buf);
151 
152 	d = exif_loader_get_data(loader);
153 	exif_data_foreach_content(d, data_foreach_func, callback_data);
154 	test_exif_data (d);
155 	exif_loader_unref(loader);
156 	exif_data_unref(d);
157 }
158 
159 
160 /** Main program. */
main(const int argc,const char * argv[])161 int main(const int argc, const char *argv[])
162 {
163 	int	i;
164 	void	*callback_data = NULL;
165 	for (i=1; i<argc; i++) {
166 		test_parse(argv[i], callback_data);
167 	}
168 	return 0;
169 }
170