• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* exif-mnote-data-olympus.c
2  *
3  * Copyright (c) 2002, 2003 Lutz Mueller <lutz@users.sourceforge.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA.
19  *
20  * SPDX-License-Identifier: LGPL-2.0-or-later
21  */
22 
23 #include <config.h>
24 #include "exif-mnote-data-olympus.h"
25 
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 
30 #include <libexif/exif-utils.h>
31 #include <libexif/exif-data.h>
32 
33 /* Uncomment this to fix a problem with Sanyo MakerNotes. It's probably best
34  * not to in most cases because it seems to only affect the thumbnail tag
35  * which is duplicated in IFD 1, and fixing the offset could actually cause
36  * problems with other software that expects the broken form.
37  */
38 /*#define EXIF_OVERCOME_SANYO_OFFSET_BUG */
39 
40 #define CHECKOVERFLOW(offset,datasize,structsize) (( (offset) >= (datasize)) || ((structsize) > (datasize)) || ((offset) > (datasize) - (structsize) ))
41 
42 static enum OlympusVersion
43 exif_mnote_data_olympus_identify_variant (const unsigned char *buf,
44 		unsigned int buf_size);
45 
46 
47 static void
exif_mnote_data_olympus_clear(ExifMnoteDataOlympus * n)48 exif_mnote_data_olympus_clear (ExifMnoteDataOlympus *n)
49 {
50 	ExifMnoteData *d = (ExifMnoteData *) n;
51 	unsigned int i;
52 
53 	if (!n) return;
54 
55 	if (n->entries) {
56 		for (i = 0; i < n->count; i++)
57 			if (n->entries[i].data) {
58 				exif_mem_free (d->mem, n->entries[i].data);
59 				n->entries[i].data = NULL;
60 			}
61 		exif_mem_free (d->mem, n->entries);
62 		n->entries = NULL;
63 		n->count = 0;
64 	}
65 }
66 
67 static void
exif_mnote_data_olympus_free(ExifMnoteData * n)68 exif_mnote_data_olympus_free (ExifMnoteData *n)
69 {
70 	if (!n) return;
71 
72 	exif_mnote_data_olympus_clear ((ExifMnoteDataOlympus *) n);
73 }
74 
75 static char *
exif_mnote_data_olympus_get_value(ExifMnoteData * d,unsigned int i,char * val,unsigned int maxlen)76 exif_mnote_data_olympus_get_value (ExifMnoteData *d, unsigned int i, char *val, unsigned int maxlen)
77 {
78 	ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
79 
80 	if (!d || !val) return NULL;
81 	if (i > n->count -1) return NULL;
82 /*
83 	exif_log (d->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
84 		  "Querying value for tag '%s'...",
85 		  mnote_olympus_tag_get_name (n->entries[i].tag));
86 */
87 	return mnote_olympus_entry_get_value (&n->entries[i], val, maxlen);
88 }
89 
90 
91 
92 
93 /**
94  * @brief save the MnoteData from ne to buf
95  *
96  * @param ne extract the data from this structure
97  * @param *buf write the mnoteData to this buffer (buffer will be allocated)
98  * @param buf_size the size of the buffer
99  */
100 static void
exif_mnote_data_olympus_save(ExifMnoteData * ne,unsigned char ** buf,unsigned int * buf_size)101 exif_mnote_data_olympus_save (ExifMnoteData *ne,
102 		unsigned char **buf, unsigned int *buf_size)
103 {
104 	ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) ne;
105 	size_t i, o, s, doff, base = 0, o2 = 6 + 2;
106 	size_t datao = 0;
107 	unsigned char *t;
108 	size_t ts;
109 
110 	if (!n || !buf || !buf_size) return;
111 
112 	/*
113 	 * Allocate enough memory for all entries and the number of entries.
114 	 */
115 	*buf_size = 6 + 2 + 2 + n->count * 12;
116 	switch (n->version) {
117 	case olympusV1:
118 	case sanyoV1:
119 	case epsonV1:
120 		*buf = exif_mem_alloc (ne->mem, *buf_size);
121 		if (!*buf) {
122 			EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataOlympus", *buf_size);
123 			return;
124 		}
125 
126 		/* Write the header and the number of entries. */
127 		strcpy ((char *)*buf, n->version==sanyoV1?"SANYO":
128 					(n->version==epsonV1?"EPSON":"OLYMP"));
129 		exif_set_short (*buf + 6, n->order, (ExifShort) 1);
130 		datao = n->offset;
131 		break;
132 
133 	case olympusV2:
134 		*buf_size += 8-6 + 4;
135 		*buf = exif_mem_alloc (ne->mem, *buf_size);
136 		if (!*buf) {
137 			EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataOlympus", *buf_size);
138 			return;
139 		}
140 
141 		/* Write the header and the number of entries. */
142 		strcpy ((char *)*buf, "OLYMPUS");
143 		exif_set_short (*buf + 8, n->order, (ExifShort) (
144 			(n->order == EXIF_BYTE_ORDER_INTEL) ?
145 			('I' << 8) | 'I' :
146 			('M' << 8) | 'M'));
147 		exif_set_short (*buf + 10, n->order, (ExifShort) 3);
148 		o2 += 4;
149 		break;
150 
151 	case nikonV1:
152 		base = MNOTE_NIKON1_TAG_BASE;
153 
154 		/* v1 has offsets based to main IFD, not makernote IFD */
155 		datao += n->offset + 10;
156 		/* subtract the size here, so the increment in the next case will not harm us */
157 		*buf_size -= 8 + 2;
158 		/* fall through */
159 	/* Fall through to nikonV2 handler */
160 	case nikonV2:
161 	/* Write out V0 files in V2 format */
162 	case nikonV0:
163 		*buf_size += 8 + 2;
164 		*buf_size += 4; /* Next IFD pointer */
165 		*buf = exif_mem_alloc (ne->mem, *buf_size);
166 		if (!*buf) {
167 			EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataOlympus", *buf_size);
168 			return;
169 		}
170 
171 		/* Write the header and the number of entries. */
172 		strcpy ((char *)*buf, "Nikon");
173 		(*buf)[6] = (unsigned char)n->version;
174 
175 		if (n->version != nikonV1) {
176 			exif_set_short (*buf + 10, n->order, (ExifShort) (
177 				(n->order == EXIF_BYTE_ORDER_INTEL) ?
178 				('I' << 8) | 'I' :
179 				('M' << 8) | 'M'));
180 			exif_set_short (*buf + 12, n->order, (ExifShort) 0x2A);
181 			exif_set_long (*buf + 14, n->order, (ExifShort) 8);
182 			o2 += 2 + 8;
183 		}
184 		datao -= 10;
185 		/* Reset next IFD pointer */
186 		exif_set_long (*buf + o2 + 2 + n->count * 12, n->order, 0);
187 		break;
188 
189 	default:
190 		return;
191 	}
192 
193 	exif_set_short (*buf + o2, n->order, (ExifShort) n->count);
194 	o2 += 2;
195 
196 	/* Save each entry */
197 	for (i = 0; i < n->count; i++) {
198 		o = o2 + i * 12;
199 		exif_set_short (*buf + o + 0, n->order,
200 				(ExifShort) (n->entries[i].tag - base));
201 		exif_set_short (*buf + o + 2, n->order,
202 				(ExifShort) n->entries[i].format);
203 		exif_set_long  (*buf + o + 4, n->order,
204 				n->entries[i].components);
205 		o += 8;
206 		s = exif_format_get_size (n->entries[i].format) *
207 						n->entries[i].components;
208 		if (s > 65536) {
209 			/* Corrupt data: EXIF data size is limited to the
210 			 * maximum size of a JPEG segment (64 kb).
211 			 */
212 			continue;
213 		}
214 		if (s > 4) {
215 			doff = *buf_size;
216 			ts = *buf_size + s;
217 			t = exif_mem_realloc (ne->mem, *buf,
218 						 sizeof (char) * ts);
219 			if (!t) {
220 				EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataOlympus", ts);
221 				return;
222 			}
223 			*buf = t;
224 			*buf_size = ts;
225 			exif_set_long (*buf + o, n->order, datao + doff);
226 		} else
227 			doff = o;
228 
229 		/* Write the data. */
230 		if (n->entries[i].data) {
231 			memcpy (*buf + doff, n->entries[i].data, s);
232 		} else {
233 			/* Most certainly damaged input file */
234 			memset (*buf + doff, 0, s);
235 		}
236 	}
237 }
238 
239 static void
exif_mnote_data_olympus_load(ExifMnoteData * en,const unsigned char * buf,unsigned int buf_size)240 exif_mnote_data_olympus_load (ExifMnoteData *en,
241 			      const unsigned char *buf, unsigned int buf_size)
242 {
243 	ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) en;
244 	ExifShort c;
245 	size_t i, tcount, o, o2, datao = 6, base = 0;
246 
247 	if (!n) return;
248 
249 	if (!buf || !buf_size) {
250 		exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
251 			  "ExifMnoteDataOlympus", "Short MakerNote");
252 		return;
253 	}
254 	o2 = 6 + n->offset; /* Start of interesting data */
255 	if (CHECKOVERFLOW(o2,buf_size,10)) {
256 		exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
257 			  "ExifMnoteDataOlympus", "Short MakerNote");
258 		return;
259 	}
260 
261 	/*
262 	 * Olympus headers start with "OLYMP" and need to have at least
263 	 * a size of 22 bytes (6 for 'OLYMP', 2 other bytes, 2 for the
264 	 * number of entries, and 12 for one entry.
265 	 *
266 	 * Sanyo format is identical and uses identical tags except that
267 	 * header starts with "SANYO".
268 	 *
269 	 * Epson format is identical and uses identical tags except that
270 	 * header starts with "EPSON".
271 	 *
272 	 * Nikon headers start with "Nikon" (6 bytes including '\0'),
273 	 * version number (1 or 2).
274 	 *
275 	 * Version 1 continues with 0, 1, 0, number_of_tags,
276 	 * or just with number_of_tags (models D1H, D1X...).
277 	 *
278 	 * Version 2 continues with an unknown byte (0 or 10),
279 	 * two unknown bytes (0), "MM" or "II", another byte 0 and
280 	 * lastly 0x2A.
281 	 */
282 	n->version = exif_mnote_data_olympus_identify_variant(buf+o2, buf_size-o2);
283 	switch (n->version) {
284 	case olympusV1:
285 	case sanyoV1:
286 	case epsonV1:
287 		exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
288 			"Parsing Olympus/Sanyo/Epson maker note v1...");
289 
290 		/* The number of entries is at position 8. */
291 		if (buf[o2 + 6] == 1)
292 			n->order = EXIF_BYTE_ORDER_INTEL;
293 		else if (buf[o2 + 6 + 1] == 1)
294 			n->order = EXIF_BYTE_ORDER_MOTOROLA;
295 		o2 += 8;
296 		c = exif_get_short (buf + o2, n->order);
297 		if ((!(c & 0xFF)) && (c > 0x500)) {
298 			if (n->order == EXIF_BYTE_ORDER_INTEL) {
299 				n->order = EXIF_BYTE_ORDER_MOTOROLA;
300 			} else {
301 				n->order = EXIF_BYTE_ORDER_INTEL;
302 			}
303 		}
304 		break;
305 
306 	case olympusV2:
307 		/* Olympus S760, S770 */
308 		datao = o2;
309 		o2 += 8;
310 		if (CHECKOVERFLOW(o2,buf_size,4)) return;
311 		exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
312 			"Parsing Olympus maker note v2 (0x%02x, %02x, %02x, %02x)...",
313 			buf[o2 + 0], buf[o2 + 1], buf[o2 + 2], buf[o2 + 3]);
314 
315 		if ((buf[o2] == 'I') && (buf[o2 + 1] == 'I'))
316 			n->order = EXIF_BYTE_ORDER_INTEL;
317 		else if ((buf[o2] == 'M') && (buf[o2 + 1] == 'M'))
318 			n->order = EXIF_BYTE_ORDER_MOTOROLA;
319 
320 		/* The number of entries is at position 8+4. */
321 		o2 += 4;
322 		break;
323 
324 	case nikonV1:
325 		o2 += 6;
326 		exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
327 			"Parsing Nikon maker note v1 (0x%02x, %02x, %02x, "
328 			"%02x)...",
329 			buf[o2 + 0], buf[o2 + 1], buf[o2 + 2], buf[o2 + 3]);
330 
331 		/* Skip version number */
332 		o2 += 1;
333 
334 		/* Skip an unknown byte (00 or 0A). */
335 		o2 += 1;
336 
337 		base = MNOTE_NIKON1_TAG_BASE;
338 		/* Fix endianness, if needed */
339 		c = exif_get_short (buf + o2, n->order);
340 		if ((!(c & 0xFF)) && (c > 0x500)) {
341 			if (n->order == EXIF_BYTE_ORDER_INTEL) {
342 				n->order = EXIF_BYTE_ORDER_MOTOROLA;
343 			} else {
344 				n->order = EXIF_BYTE_ORDER_INTEL;
345 			}
346 		}
347 		break;
348 
349 	case nikonV2:
350 		o2 += 6;
351 		if (CHECKOVERFLOW(o2,buf_size,12)) return;
352 		exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
353 			"Parsing Nikon maker note v2 (0x%02x, %02x, %02x, "
354 			"%02x, %02x, %02x, %02x, %02x)...",
355 			buf[o2 + 0], buf[o2 + 1], buf[o2 + 2], buf[o2 + 3],
356 			buf[o2 + 4], buf[o2 + 5], buf[o2 + 6], buf[o2 + 7]);
357 
358 		/* Skip version number */
359 		o2 += 1;
360 
361 		/* Skip an unknown byte (00 or 0A). */
362 		o2 += 1;
363 
364 		/* Skip 2 unknown bytes (00 00). */
365 		o2 += 2;
366 
367 		/*
368 		 * Byte order. From here the data offset
369 		 * gets calculated.
370 		 */
371 		datao = o2;
372 		if (!strncmp ((char *)&buf[o2], "II", 2))
373 			n->order = EXIF_BYTE_ORDER_INTEL;
374 		else if (!strncmp ((char *)&buf[o2], "MM", 2))
375 			n->order = EXIF_BYTE_ORDER_MOTOROLA;
376 		else {
377 			exif_log (en->log, EXIF_LOG_CODE_DEBUG,
378 				"ExifMnoteDataOlympus", "Unknown "
379 				"byte order '%c%c'", buf[o2],
380 				buf[o2 + 1]);
381 			return;
382 		}
383 		o2 += 2;
384 
385 		/* Skip 2 unknown bytes (00 2A). */
386 		o2 += 2;
387 
388 		/* Go to where the number of entries is. */
389 		o2 = datao + exif_get_long (buf + o2, n->order);
390 		break;
391 
392 	case nikonV0:
393 		exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
394 			"Parsing Nikon maker note v0 (0x%02x, %02x, %02x, "
395 			"%02x, %02x, %02x, %02x, %02x)...",
396 			buf[o2 + 0], buf[o2 + 1], buf[o2 + 2], buf[o2 + 3],
397 			buf[o2 + 4], buf[o2 + 5], buf[o2 + 6], buf[o2 + 7]);
398 		/* 00 1b is # of entries in Motorola order - the rest should also be in MM order */
399 		n->order = EXIF_BYTE_ORDER_MOTOROLA;
400 		break;
401 
402 	default:
403 		exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
404 			"Unknown Olympus variant %i.", n->version);
405 		return;
406 	}
407 
408 	/* Sanity check the offset */
409 	if (CHECKOVERFLOW(o2,buf_size,2)) {
410 		exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
411 			  "ExifMnoteOlympus", "Short MakerNote");
412 		return;
413 	}
414 
415 	/* Read the number of tags */
416 	c = exif_get_short (buf + o2, n->order);
417 	o2 += 2;
418 
419 	/* Just use an arbitrary max tag limit here to avoid needing to much memory or time. There are 150 named tags currently.
420 	 * The format allows specifying the same range of memory as often as it can, so this multiplies quickly. */
421 	if (c > 300) {
422 		exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifMnoteOlympus", "Too much tags (%d) in Olympus MakerNote", c);
423 		return;
424 	}
425 
426 	/* Remove any old entries */
427 	exif_mnote_data_olympus_clear (n);
428 
429 	/* Reserve enough space for all the possible MakerNote tags */
430 	n->entries = exif_mem_alloc (en->mem, sizeof (MnoteOlympusEntry) * c);
431 	if (!n->entries) {
432 		EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteOlympus", sizeof (MnoteOlympusEntry) * c);
433 		return;
434 	}
435 
436 	/* Parse all c entries, storing ones that are successfully parsed */
437 	tcount = 0;
438 	for (i = c, o = o2; i; --i, o += 12) {
439 		size_t s;
440 		memset(&n->entries[tcount], 0, sizeof(MnoteOlympusEntry));
441 		if (CHECKOVERFLOW(o, buf_size, 12)) {
442 			exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
443 				  "ExifMnoteOlympus", "Short MakerNote");
444 			break;
445 		}
446 
447 	    n->entries[tcount].tag        = exif_get_short (buf + o, n->order) + base;
448 	    n->entries[tcount].format     = exif_get_short (buf + o + 2, n->order);
449 	    n->entries[tcount].components = exif_get_long (buf + o + 4, n->order);
450 	    n->entries[tcount].order      = n->order;
451 
452 	    exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteOlympus",
453 		      "Loading entry 0x%x ('%s')...", n->entries[tcount].tag,
454 		      mnote_olympus_tag_get_name (n->entries[tcount].tag));
455 /*	    exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteOlympus",
456 			    "0x%x %d %ld*(%d)",
457 		    n->entries[tcount].tag,
458 		    n->entries[tcount].format,
459 		    n->entries[tcount].components,
460 		    (int)exif_format_get_size(n->entries[tcount].format)); */
461 
462 	    /* Check if we overflow the multiplication. Use buf_size as the max size for integer overflow detection,
463 	     * we will check the buffer sizes closer later. */
464 	    if (exif_format_get_size (n->entries[tcount].format) &&
465 		buf_size / exif_format_get_size (n->entries[tcount].format) < n->entries[tcount].components
466 	    ) {
467 		exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifMnoteOlympus", "Tag size overflow detected (%u * %lu)", exif_format_get_size (n->entries[tcount].format), n->entries[tcount].components);
468 		continue;
469 	    }
470 	    /*
471 	     * Size? If bigger than 4 bytes, the actual data is not
472 	     * in the entry but somewhere else (offset).
473 	     */
474 	    s = exif_format_get_size (n->entries[tcount].format) *
475 		   			 n->entries[tcount].components;
476 		n->entries[tcount].size = s;
477 		if (s) {
478 			size_t dataofs = o + 8;
479 			if (s > 4) {
480 				/* The data in this case is merely a pointer */
481 				dataofs = exif_get_long (buf + dataofs, n->order) + datao;
482 #ifdef EXIF_OVERCOME_SANYO_OFFSET_BUG
483 				/* Some Sanyo models (e.g. VPC-C5, C40) suffer from a bug when
484 				 * writing the offset for the MNOTE_OLYMPUS_TAG_THUMBNAILIMAGE
485 				 * tag in its MakerNote. The offset is actually the absolute
486 				 * position in the file instead of the position within the IFD.
487 				 */
488 			    if (dataofs > (buf_size - s) && n->version == sanyoV1) {
489 					/* fix pointer */
490 					dataofs -= datao + 6;
491 					exif_log (en->log, EXIF_LOG_CODE_DEBUG,
492 						  "ExifMnoteOlympus",
493 						  "Inconsistent thumbnail tag offset; attempting to recover");
494 			    }
495 #endif
496 			}
497 			if (CHECKOVERFLOW(dataofs, buf_size, s)) {
498 				exif_log (en->log, EXIF_LOG_CODE_DEBUG,
499 					  "ExifMnoteOlympus",
500 					  "Tag data past end of buffer (%u > %u)",
501 					  (unsigned)(dataofs + s), buf_size);
502 				continue;
503 			}
504 
505 			n->entries[tcount].data = exif_mem_alloc (en->mem, s);
506 			if (!n->entries[tcount].data) {
507 				EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteOlympus", s);
508 				continue;
509 			}
510 			memcpy (n->entries[tcount].data, buf + dataofs, s);
511 		}
512 
513 		/* Tag was successfully parsed */
514 		++tcount;
515 	}
516 	/* Store the count of successfully parsed tags */
517 	n->count = tcount;
518 }
519 
520 static unsigned int
exif_mnote_data_olympus_count(ExifMnoteData * n)521 exif_mnote_data_olympus_count (ExifMnoteData *n)
522 {
523 	return n ? ((ExifMnoteDataOlympus *) n)->count : 0;
524 }
525 
526 static unsigned int
exif_mnote_data_olympus_get_id(ExifMnoteData * d,unsigned int n)527 exif_mnote_data_olympus_get_id (ExifMnoteData *d, unsigned int n)
528 {
529 	ExifMnoteDataOlympus *note = (ExifMnoteDataOlympus *) d;
530 
531 	if (!note) return 0;
532 	if (note->count <= n) return 0;
533 	return note->entries[n].tag;
534 }
535 
536 static const char *
exif_mnote_data_olympus_get_name(ExifMnoteData * d,unsigned int i)537 exif_mnote_data_olympus_get_name (ExifMnoteData *d, unsigned int i)
538 {
539 	ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
540 
541 	if (!n) return NULL;
542 	if (i >= n->count) return NULL;
543 	return mnote_olympus_tag_get_name (n->entries[i].tag);
544 }
545 
546 static const char *
exif_mnote_data_olympus_get_title(ExifMnoteData * d,unsigned int i)547 exif_mnote_data_olympus_get_title (ExifMnoteData *d, unsigned int i)
548 {
549 	ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
550 
551 	if (!n) return NULL;
552 	if (i >= n->count) return NULL;
553         return mnote_olympus_tag_get_title (n->entries[i].tag);
554 }
555 
556 static const char *
exif_mnote_data_olympus_get_description(ExifMnoteData * d,unsigned int i)557 exif_mnote_data_olympus_get_description (ExifMnoteData *d, unsigned int i)
558 {
559 	ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
560 
561 	if (!n) return NULL;
562 	if (i >= n->count) return NULL;
563         return mnote_olympus_tag_get_description (n->entries[i].tag);
564 }
565 
566 static void
exif_mnote_data_olympus_set_byte_order(ExifMnoteData * d,ExifByteOrder o)567 exif_mnote_data_olympus_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
568 {
569 	ExifByteOrder o_orig;
570 	ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
571 	unsigned int i;
572 
573 	if (!n) return;
574 
575 	o_orig = n->order;
576 	n->order = o;
577 	for (i = 0; i < n->count; i++) {
578 		if (n->entries[i].components && (n->entries[i].size/n->entries[i].components < exif_format_get_size (n->entries[i].format)))
579 			continue;
580 		n->entries[i].order = o;
581 		exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
582 				n->entries[i].components, o_orig, o);
583 	}
584 }
585 
586 static void
exif_mnote_data_olympus_set_offset(ExifMnoteData * n,unsigned int o)587 exif_mnote_data_olympus_set_offset (ExifMnoteData *n, unsigned int o)
588 {
589 	if (n) ((ExifMnoteDataOlympus *) n)->offset = o;
590 }
591 
592 static enum OlympusVersion
exif_mnote_data_olympus_identify_variant(const unsigned char * buf,unsigned int buf_size)593 exif_mnote_data_olympus_identify_variant (const unsigned char *buf,
594 		unsigned int buf_size)
595 {
596 	/* Olympus, Nikon, Sanyo, Epson */
597 	if (buf_size >= 8) {
598 		/* Match the terminating NUL character, too */
599 		if (!memcmp (buf, "OLYMPUS", 8))
600 			   return olympusV2;
601 		else if (!memcmp (buf, "OLYMP", 6))
602 			   return olympusV1;
603 		else if (!memcmp (buf, "SANYO", 6))
604 			   return sanyoV1;
605 		else if (!memcmp (buf, "EPSON", 6))
606 			   return epsonV1;
607 		else if (!memcmp (buf, "Nikon", 6)) {
608 			switch (buf[6]) {
609 				case 1:  return nikonV1;
610 				case 2:  return nikonV2;
611 				default: return 0; /* Unrecognized Nikon variant */
612 			}
613 		}
614 	}
615 
616 	/* Another variant of Nikon */
617 	if ((buf_size >= 2) && (buf[0] == 0x00) && (buf[1] == 0x1b)) {
618 		return nikonV0;
619 	}
620 
621 	return unrecognized;
622 }
623 
624 int
exif_mnote_data_olympus_identify(const ExifData * ed,const ExifEntry * e)625 exif_mnote_data_olympus_identify (const ExifData *ed, const ExifEntry *e)
626 {
627 	int variant = exif_mnote_data_olympus_identify_variant(e->data, e->size);
628 
629 	if (variant == nikonV0) {
630 		/* This variant needs some extra checking with the Make */
631 		char value[5];
632 		ExifEntry *em = exif_data_get_entry (ed, EXIF_TAG_MAKE);
633 		variant = unrecognized;
634 
635 		if (em) {
636 			const char *v = exif_entry_get_value (em, value, sizeof(value));
637 			if (v && (!strncmp (v, "Nikon", sizeof(value)) ||
638 					  !strncmp (v, "NIKON", sizeof(value)) ))
639 				/* When saved, this variant will be written out like the
640 				 * alternative nikonV2 form above instead
641 				 */
642 				variant = nikonV0;
643 		}
644 	}
645 
646 	return variant;
647 }
648 
649 
650 ExifMnoteData *
exif_mnote_data_olympus_new(ExifMem * mem)651 exif_mnote_data_olympus_new (ExifMem *mem)
652 {
653 	ExifMnoteData *d;
654 
655 	if (!mem) return NULL;
656 
657 	d = exif_mem_alloc (mem, sizeof (ExifMnoteDataOlympus));
658 	if (!d) return NULL;
659 
660 	exif_mnote_data_construct (d, mem);
661 
662 	/* Set up function pointers */
663 	d->methods.free            = exif_mnote_data_olympus_free;
664 	d->methods.set_byte_order  = exif_mnote_data_olympus_set_byte_order;
665 	d->methods.set_offset      = exif_mnote_data_olympus_set_offset;
666 	d->methods.load            = exif_mnote_data_olympus_load;
667 	d->methods.save            = exif_mnote_data_olympus_save;
668 	d->methods.count           = exif_mnote_data_olympus_count;
669 	d->methods.get_id          = exif_mnote_data_olympus_get_id;
670 	d->methods.get_name        = exif_mnote_data_olympus_get_name;
671 	d->methods.get_title       = exif_mnote_data_olympus_get_title;
672 	d->methods.get_description = exif_mnote_data_olympus_get_description;
673 	d->methods.get_value       = exif_mnote_data_olympus_get_value;
674 
675 	return d;
676 }
677