EXIF library (libexif) Internals  0.6.24
test-fuzzer.c
Go to the documentation of this file.
1 
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
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 
48 void content_foreach_func(ExifEntry *entry, void *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);
58  exif_entry_get_value(entry, buf, sizeof(buf)-1);
59  if (buf[sizeof(buf)-2] != 0) abort();
60 }
61 
62 
64 void 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
72 {
73  unsigned int i, c;
74  char v[1024];
75  ExifMnoteData *md;
76 
77  fprintf (stdout, "Byte order: %s\n",
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 
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;
96  exif_mnote_data_get_value (md, i, v, sizeof (v));
97  }
98 
99  return 0;
100 }
101 
102 
103 
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 
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 
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 }
const char * exif_byte_order_get_name(ExifByteOrder order)
Return a short, localized, textual name for the given byte order.
@ EXIF_BYTE_ORDER_INTEL
Little-endian byte order.
ExifIfd exif_content_get_ifd(ExifContent *c)
Return the IFD number in which the given ExifContent is found.
Definition: exif-content.c:240
void exif_content_foreach_entry(ExifContent *content, ExifContentForeachEntryFunc func, void *data)
Executes function on each EXIF tag in this IFD in turn.
Definition: exif-content.c:216
void exif_data_set_byte_order(ExifData *data, ExifByteOrder order)
Set the byte order to use for this EXIF data.
Definition: exif-data.c:1217
void exif_data_unref(ExifData *data)
Definition: exif-data.c:1098
ExifByteOrder exif_data_get_byte_order(ExifData *data)
Return the byte order in use by this EXIF structure.
Definition: exif-data.c:1173
ExifMnoteData * exif_data_get_mnote_data(ExifData *d)
Return the MakerNote data out of the EXIF data.
Definition: exif-data.c:90
ExifData * exif_data_new_from_file(const char *path)
Allocate a new ExifData and load EXIF data from a JPEG file.
Definition: exif-data.c:1075
void exif_data_save_data(ExifData *data, unsigned char **d, unsigned int *ds)
Store raw EXIF data representing the ExifData structure into a memory buffer.
Definition: exif-data.c:1030
void exif_data_foreach_content(ExifData *data, ExifDataForeachContentFunc func, void *user_data)
Execute a function on each IFD in turn.
Definition: exif-data.c:1182
void exif_data_log(ExifData *data, ExifLog *log)
Set the log message object for all IFDs.
Definition: exif-data.c:1233
Defines the ExifData type and the associated functions.
const char * exif_entry_get_value(ExifEntry *e, char *val, unsigned int maxlen)
Return a localized textual representation of the value of the EXIF entry.
Definition: exif-entry.c:846
const char * exif_format_get_name(ExifFormat format)
Return a textual representation of the given EXIF data type.
Definition: exif-format.c:53
ExifFormat format
Definition: exif-format.c:33
ExifLoader * exif_loader_new(void)
Allocate a new ExifLoader.
Definition: exif-loader.c:352
void exif_loader_unref(ExifLoader *loader)
Decrease the refcount of the ExifLoader.
Definition: exif-loader.c:404
ExifData * exif_loader_get_data(ExifLoader *loader)
Create an ExifData from the data in the loader.
Definition: exif-loader.c:426
unsigned char exif_loader_write(ExifLoader *eld, unsigned char *buf, unsigned int len)
Load a buffer into the ExifLoader from a memory buffer.
Definition: exif-loader.c:163
Defines the ExifLoader type.
ExifLogCode code
Definition: exif-log.c:39
void exif_log_set_func(ExifLog *log, ExifLogFunc func, void *data)
Register log callback function.
Definition: exif-log.c:123
ExifLog * exif_log_new(void)
Create a new logging instance.
Definition: exif-log.c:86
ExifLogCode
Definition: exif-log.h:54
void exif_mnote_data_ref(ExifMnoteData *d)
char * exif_mnote_data_get_value(ExifMnoteData *d, unsigned int n, char *val, unsigned int maxlen)
Return a textual representation of the value of the MakerNote entry.
const char * exif_mnote_data_get_name(ExifMnoteData *d, unsigned int n)
Returns textual name of the given MakerNote tag.
const char * exif_mnote_data_get_description(ExifMnoteData *d, unsigned int n)
Returns verbose textual description of the given MakerNote tag.
const char * exif_mnote_data_get_title(ExifMnoteData *d, unsigned int n)
Returns textual title of the given MakerNote tag.
unsigned int exif_mnote_data_count(ExifMnoteData *d)
Return the number of tags in the MakerNote.
void exif_mnote_data_unref(ExifMnoteData *d)
System specific definitions, not for installation!
#define UNUSED(param)
Definition: exif-system.h:29
const char * exif_tag_get_name(ExifTag tag)
Definition: exif-tag.c:1147
const char * name
Represents the entire EXIF data found in an image.
Definition: exif-data.h:47
Data found in one EXIF tag.
Definition: exif-entry.h:43
ExifFormat format
Type of data in this entry.
Definition: exif-entry.h:48
ExifTag tag
EXIF tag for this entry.
Definition: exif-entry.h:45
void data_foreach_func(ExifContent *content, void *callback_data)
Definition: test-fuzzer.c:65
static int test_exif_data(ExifData *d)
Definition: test-fuzzer.c:71
int main(const int argc, const char *argv[])
Definition: test-fuzzer.c:161
static void test_parse(const char *filename, void *callback_data)
Definition: test-fuzzer.c:105
void content_foreach_func(ExifEntry *entry, void *callback_data)
Completely parse all files given on the command line.

libexif Generated by doxygen