1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <libexif/exif-data.h>
16 #include <libexif/exif-loader.h>
17 #include <stddef.h>
18 #include <stdlib.h>
19 
20 /* Extract all MakerNote tags */
mnote_dump(ExifData * data)21 static void mnote_dump(ExifData *data) {
22     ExifMnoteData *mn = exif_data_get_mnote_data(data);
23     if (mn) {
24         int num = exif_mnote_data_count(mn);
25 
26         /* Loop through all MakerNote tags */
27         for (int i=0; i < num; ++i) {
28             char buf[1024];
29             exif_mnote_data_get_value(mn, i, buf, sizeof(buf));
30         }
31     }
32 }
33 
dump_value(ExifEntry * entry,void * user_data)34 static void dump_value(ExifEntry *entry, void *user_data) {
35   char buf[1024];
36   exif_entry_get_value(entry, buf, sizeof(buf));
37 }
38 
data_func(ExifContent * content,void * user_data)39 static void data_func(ExifContent *content, void *user_data) {
40   exif_content_foreach_entry(content, dump_value, NULL);
41 }
42 
43 /* This is like exif_data_dump but without writing to stdout */
data_dump(ExifData * data)44 static void data_dump(ExifData *data) {
45   exif_data_foreach_content(data, data_func, NULL);
46 }
47 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)48 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
49 
50   // Parse tags using (ultimately) exif_data_load_data()
51   auto image = exif_data_new_from_data(data, size);
52   if (image) {
53     // Exercise the EXIF tag manipulation code
54     exif_data_get_mnote_data(image);
55     data_dump(image);
56     mnote_dump(image);
57     unsigned char *buf;
58     unsigned int sz;
59     exif_data_save_data(image, &buf, &sz);
60     free(buf);
61     exif_data_fix(image);
62     exif_data_unref(image);
63   }
64 
65   return 0;
66 }
67