EXIF library (libexif) Internals  0.6.24
test-parse-from-data.c
Go to the documentation of this file.
1 
23 #include "libexif/exif-data.h"
24 #include "libexif/exif-system.h"
25 
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32 
33 
34 static unsigned entry_count;
35 
37 static void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data))
38 {
39  char buf[2000];
40  exif_entry_get_value(entry, buf, sizeof(buf));
41  printf(" Entry %u: %s (%s)\n"
42  " Size, Comps: %d, %d\n"
43  " Value: %s\n",
45  exif_tag_get_name(entry->tag),
47  entry->size,
48  (int)(entry->components),
49  exif_entry_get_value(entry, buf, sizeof(buf)));
50  ++entry_count;
51 }
52 
53 
55 static void data_foreach_func(ExifContent *content, void *callback_data)
56 {
57  static unsigned content_count;
58  entry_count = 0;
59  printf(" Content %u: ifd=%d\n", content_count, exif_content_get_ifd(content));
60  exif_content_foreach_entry(content, content_foreach_func, callback_data);
61  ++content_count;
62 }
63 
64 static void dump_makernote(ExifData *d) {
66  if (mn) {
67  char buf[2000];
68  int i;
69  int num = exif_mnote_data_count(mn);
70  printf(" MakerNote\n");
71  for (i=0; i < num; ++i) {
72  if (exif_mnote_data_get_value(mn, i, buf, sizeof(buf))) {
73  const char *name = exif_mnote_data_get_name(mn, i);
74  unsigned int id = exif_mnote_data_get_id(mn, i);
75  if (!name)
76  name = "(unknown)";
77  printf(" Entry %u: %u, %s\n"
78  " Size: %u\n"
79  " Value: %s\n", i, id, name, (unsigned)strlen(buf), buf);
80  }
81  }
82  }
83 }
84 
86 static void test_parse(const char *filename, void *callback_data, int swap)
87 {
88  ExifData *d;
89  int fd;
90  unsigned char *data;
91  struct stat stbuf;
92 
93  /* Skip over path to display only the file name */
94  const char *fn = strrchr(filename, '/');
95  if (fn)
96  ++fn;
97  else
98  fn = filename;
99  printf("File %s\n", fn);
100 
101  d = exif_data_new_from_file(filename);
102  fd = open(filename,O_RDONLY);
103  if (fd == -1) {
104  perror(filename);
105  return;
106  }
107  if (-1 == fstat(fd, &stbuf)) {
108  perror(filename);
109  return;
110  }
111  data = malloc(stbuf.st_size);
112  if (!data) {
113  fprintf (stderr, "Failed to allocate %ld bytes for reading %s\n", stbuf.st_size, filename);
114  return;
115  }
116  if (-1 == read(fd, data, stbuf.st_size)) {
117  perror ("read");
118  free(data);
119  close(fd);
120  return;
121  }
122  close(fd);
123 
124  d = exif_data_new_from_data(data, stbuf.st_size);
125  if (!d) {
126  fprintf (stderr, "Could not load data from '%s'!\n", filename);
127  free(data);
128  return;
129  }
130  printf("Byte order: %s\n",
132 
133  if (swap) {
135  if (exif_data_get_byte_order(d) == order) {
136  order = EXIF_BYTE_ORDER_MOTOROLA;
137  }
138  /* This switches the byte order of the entire EXIF data structure,
139  * including the MakerNote */
140  exif_data_set_byte_order(d, order);
141  printf("New byte order: %s\n",
143  }
144 
145  exif_data_foreach_content(d, data_foreach_func, callback_data);
146 
147  dump_makernote(d);
148 
149  exif_data_unref(d);
150 }
151 
152 
154 typedef void (*test_parse_func) (const char *filename, void *callback_data, int swap);
155 
156 
158 static void split_ws_string(const char *string, test_parse_func func, void *callback_data)
159 {
160  const char *start = string;
161  const char *p = start;
162  for (;;) {
163  if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == '\0' ) {
164  size_t len = p-start;
165  if (len > 0) {
166  /* emulate strndup */
167  char *str = malloc(1+len);
168  if (str) {
169  memcpy(str, start, len);
170  str[len] = '\0';
171  func(str, callback_data, 0);
172  free(str);
173  start = p+1;
174  }
175  } else {
176  start = p+1;
177  }
178  }
179  if (*p == '\0') {
180  break;
181  }
182  p++;
183  }
184 }
185 
186 
188 int main(const int argc, const char *argv[])
189 {
190  int i;
191  void *callback_data = NULL;
192  int swap = 0;
193  int first = 1;
194 
195  if (argc > 1 && !strcmp(argv[1], "--swap-byte-order")) {
196  swap = 1;
197  ++first;
198  }
199 
200  if (argc > first) {
201  for (i=first; i<argc; i++) {
202  test_parse(argv[i], callback_data, swap);
203  }
204  } else {
205  /* If no command-line argument is found, get the file names from
206  the environment. */
207  const char *envar = getenv("TEST_IMAGES");
208  if (envar) {
209  split_ws_string(envar, test_parse, callback_data);
210  }
211  }
212 
213  return 0;
214 }
const char * exif_byte_order_get_name(ExifByteOrder order)
Return a short, localized, textual name for the given byte order.
ExifByteOrder
Which byte order to use.
@ EXIF_BYTE_ORDER_INTEL
Little-endian byte order.
@ EXIF_BYTE_ORDER_MOTOROLA
Big-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
ExifData * exif_data_new_from_data(const unsigned char *data, unsigned int size)
Allocate a new ExifData and load EXIF data from a memory buffer.
Definition: exif-data.c:154
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_foreach_content(ExifData *data, ExifDataForeachContentFunc func, void *user_data)
Execute a function on each IFD in turn.
Definition: exif-data.c:1182
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
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.
unsigned int exif_mnote_data_get_id(ExifMnoteData *d, unsigned int n)
Return the MakerNote tag number for the tag at the specified index within the MakerNote.
unsigned int exif_mnote_data_count(ExifMnoteData *d)
Return the number of tags in the MakerNote.
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
const char * string
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
unsigned long components
Number of elements in the array, if this is an array entry.
Definition: exif-entry.h:52
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
unsigned int size
Number of bytes in the buffer at data.
Definition: exif-entry.h:61
void(* test_parse_func)(const char *filename, void *callback_data, int swap)
static unsigned entry_count
static void data_foreach_func(ExifContent *content, void *callback_data)
static void test_parse(const char *filename, void *callback_data, int swap)
static void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data))
static void dump_makernote(ExifData *d)
static void split_ws_string(const char *string, test_parse_func func, void *callback_data)
int main(const int argc, const char *argv[])

libexif Generated by doxygen