b2d3e94e8b5675ccc2e717a975400219f8dd84b4
[platform/upstream/libexif.git] / test / test-fuzzer.c
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
30 #include "libexif/exif-data.h"
31 #include "libexif/exif-system.h"
32
33 /** Callback function handling an ExifEntry. */
34 void content_foreach_func(ExifEntry *entry, void *callback_data);
35 void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data))
36 {
37         char buf[2001];
38
39         /* ensure \0 */
40         buf[sizeof(buf)-1] = 0;
41         buf[sizeof(buf)-2] = 0;
42         exif_entry_get_value(entry, buf, sizeof(buf)-1);
43         printf("    Entry %p: %s (%s)\n"
44                  "      Size, Comps: %d, %d\n"
45                  "      Value: %s\n", 
46                  entry,
47                  exif_tag_get_name(entry->tag),
48                  exif_format_get_name(entry->format),
49                  entry->size,
50                  (int)(entry->components),
51                  exif_entry_get_value(entry, buf, sizeof(buf)-1));
52         if (buf[sizeof(buf)-2] != 0) abort();
53 }
54
55
56 /** Callback function handling an ExifContent (corresponds 1:1 to an IFD). */
57 void data_foreach_func(ExifContent *content, void *callback_data);
58 void data_foreach_func(ExifContent *content, void *callback_data)
59 {
60         printf("  Content %p: ifd=%d\n", content, exif_content_get_ifd(content));
61         exif_content_foreach_entry(content, content_foreach_func, callback_data);
62 }
63 static int
64 test_exif_data (ExifData *d)
65 {
66         unsigned int i, c;
67         char v[1024], *p;
68         ExifMnoteData *md;
69
70         fprintf (stdout, "Byte order: %s\n",
71                 exif_byte_order_get_name (exif_data_get_byte_order (d)));
72
73         md = exif_data_get_mnote_data (d);
74         if (!md) {
75                 fprintf (stderr, "Could not parse maker note!\n");
76                 return 1;
77         }
78
79         exif_mnote_data_ref (md);
80         exif_mnote_data_unref (md);
81
82         c = exif_mnote_data_count (md);
83         for (i = 0; i < c; i++) {
84                 const char *name = exif_mnote_data_get_name (md, i);
85                 if (!name) break;
86                 fprintf (stdout, "Dumping entry number %i...\n", i);
87                 fprintf (stdout, "  Name: '%s'\n",
88                                 exif_mnote_data_get_name (md, i));
89                 fprintf (stdout, "  Title: '%s'\n",
90                                 exif_mnote_data_get_title (md, i));
91                 fprintf (stdout, "  Description: '%s'\n",
92                                 exif_mnote_data_get_description (md, i));
93                 p = exif_mnote_data_get_value (md, i, v, sizeof (v));
94                 if (p) fprintf (stdout, "  Value: '%s'\n", v);
95         }
96
97         return 0;
98 }
99
100
101
102 /** Run EXIF parsing test on the given file. */
103 static void test_parse(const char *filename, void *callback_data)
104 {
105         ExifData *d;
106         unsigned int buf_size;
107         unsigned char *buf;
108
109         d = exif_data_new_from_file(filename);
110         exif_data_foreach_content(d, data_foreach_func, callback_data);
111         test_exif_data (d);
112
113         buf = NULL;
114         exif_data_save_data (d, &buf, &buf_size);
115         free (buf);
116
117         exif_data_set_byte_order(d, EXIF_BYTE_ORDER_INTEL);
118
119         buf = NULL;
120         exif_data_save_data (d, &buf, &buf_size);
121         free (buf);
122
123
124         exif_data_unref(d);
125 }
126
127
128 /** Main program. */
129 int main(const int argc, const char *argv[])
130 {
131         int i;
132         void *callback_data = NULL;
133
134         for (i=1; i<argc; i++) {
135                 test_parse(argv[i], callback_data);
136         }
137         return 0;
138 }