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