Protect another g_memdup2 in exif.cc with ifdef
[geeqie.git] / src / exif.cc
1 /*
2  * Copyright (C) 2003, 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Authors: Eric Swalens, Quy Tonthat
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  *  The tags were added with information from the FREE document:
22  *     http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
23  *
24  *  For the official Exif Format, please refer to:
25  *     http://www.exif.org
26  *     http://www.exif.org/specifications.html (PDF spec sheets)
27  *
28  *  Notes:
29  *     Additional tag formats should be added to the proper
30  *     location in ExifKnownMarkersList[].
31  *
32  *     Human readable output (that needs additional processing of data to
33  *     be useable) can be defined by adding a key to ExifFormattedList[],
34  *     then handling that tag in the function exif_get_formatted_by_key().
35  *     The human readable formatted keys must begin with the character 'f'.
36  *
37  *  Unsupported at this time:
38  *     IFD1 (thumbnail)
39  *     MakerNote
40  *
41  *  TODO:
42  *     Convert data to useable form in the ??_as_text function for:
43  *        ComponentsConfiguration
44  *        UserComment (convert this to UTF-8?)
45  *     Add support for marker tag 0x0000
46  */
47
48 #include <config.h>
49
50 #ifndef HAVE_EXIV2
51
52 #include <stdio.h>
53 #include <string.h>
54 #include <fcntl.h>
55 #include <unistd.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <sys/mman.h>
59 #include <math.h>
60
61 #include <glib.h>
62 #include <glib/gprintf.h>
63
64 #include "intl.h"
65
66 #include "main.h"
67 #include "exif-int.h"
68 #include "jpeg-parser.h"
69
70 #include "format-raw.h"
71 #include "ui-fileops.h"
72
73
74 /*
75  *-----------------------------------------------------------------------------
76  * Tag formats
77  *-----------------------------------------------------------------------------
78  */
79
80 ExifFormatAttrib ExifFormatList[] = {
81         { EXIF_FORMAT_UNKNOWN,          1, "unknown",   "unknown" },
82         { EXIF_FORMAT_BYTE_UNSIGNED,    1, "ubyte",     "unsigned byte" },
83         { EXIF_FORMAT_STRING,           1, "string",    "string" },
84         { EXIF_FORMAT_SHORT_UNSIGNED,   2, "ushort",    "unsigned short" },
85         { EXIF_FORMAT_LONG_UNSIGNED,    4, "ulong",     "unsigned long" },
86         { EXIF_FORMAT_RATIONAL_UNSIGNED,8, "urational", "unsigned rational" },
87         { EXIF_FORMAT_BYTE,             1, "byte",      "byte" },
88         { EXIF_FORMAT_UNDEFINED,        1, "undefined", "undefined" },
89         { EXIF_FORMAT_SHORT,            2, "sshort",    "signed short" },
90         { EXIF_FORMAT_LONG,             4, "slong",     "signed long" },
91         { EXIF_FORMAT_RATIONAL,         8, "srational", "signed rational" },
92         { EXIF_FORMAT_FLOAT,            4, "float",     "float" },
93         { EXIF_FORMAT_DOUBLE,           8, "double",    "double" },
94         { (ExifFormatType)-1, 0, NULL, NULL }
95 };
96
97 /* tags that are special, or need special treatment */
98 #define TAG_EXIFOFFSET          0x8769
99 #define TAG_EXIFMAKERNOTE       0x927c
100 #define TAG_GPSOFFSET           0x8825
101
102
103 /*
104  *-----------------------------------------------------------------------------
105  * Data
106  *-----------------------------------------------------------------------------
107  */
108 static ExifTextList ExifCompressionList[] = {
109         { 1, "Uncompressed" },
110         { 2, "CCITT 1D" },
111         { 3, "T4/Group 3 Fax" },
112         { 4, "T6/Group 4 Fax" },
113         { 5, "LZW" },
114         { 6, "JPEG (old style)" },
115         { 7, "JPEG" },
116         { 8, "Adobe Deflate" },
117         { 9, "JBIG B&W" },
118         { 10, "JBIG Color" },
119         { 32766, "Next" },
120         { 32771, "CCIRLEW" },
121         { 32773, "PackBits" },
122         { 32809, "ThunderScan" },
123         { 32895, "IT8CTPAD" },
124         { 32896, "IT8LW" },
125         { 32897, "IT8MP" },
126         { 32898, "IT8BL" },
127         { 32908, "PixasFilm" },
128         { 32909, "PixasLog" },
129         { 32946, "Deflate" },
130         { 32947, "DCS" },
131         { 34661, "JBIG" },
132         { 34676, "SGILog" },
133         { 34677, "SGILog24" },
134         { 34712, "JPEF 2000" },
135         { 34713, "Nikon NEF Compressed" },
136         EXIF_TEXT_LIST_END
137 };
138
139 static ExifTextList ExifOrientationList[] = {
140         { EXIF_ORIENTATION_UNKNOWN,     N_("unknown") },
141         { EXIF_ORIENTATION_TOP_LEFT,    N_("top left") },
142         { EXIF_ORIENTATION_TOP_RIGHT,   N_("top right") },
143         { EXIF_ORIENTATION_BOTTOM_RIGHT,N_("bottom right") },
144         { EXIF_ORIENTATION_BOTTOM_LEFT, N_("bottom left") },
145         { EXIF_ORIENTATION_LEFT_TOP,    N_("left top") },
146         { EXIF_ORIENTATION_RIGHT_TOP,   N_("right top") },
147         { EXIF_ORIENTATION_RIGHT_BOTTOM,N_("right bottom") },
148         { EXIF_ORIENTATION_LEFT_BOTTOM, N_("left bottom") },
149         EXIF_TEXT_LIST_END
150 };
151
152 static ExifTextList ExifUnitList[] = {
153         { EXIF_UNIT_UNKNOWN,    N_("unknown") },
154         { EXIF_UNIT_NOUNIT,     "" },
155         { EXIF_UNIT_INCH,       N_("inch") },
156         { EXIF_UNIT_CENTIMETER, N_("centimeter") },
157         EXIF_TEXT_LIST_END
158 };
159
160 static ExifTextList ExifYCbCrPosList[] = {
161         { 1,    "center" },
162         { 2,    "datum" },
163         EXIF_TEXT_LIST_END
164 };
165
166 static ExifTextList ExifMeteringModeList[] = {
167         { 0,    N_("unknown") },
168         { 1,    N_("average") },
169         { 2,    N_("center weighted") },
170         { 3,    N_("spot") },
171         { 4,    N_("multi-spot") },
172         { 5,    N_("multi-segment") },
173         { 6,    N_("partial") },
174         { 255,  N_("other") },
175         EXIF_TEXT_LIST_END
176 };
177
178 static ExifTextList ExifExposureProgramList[] = {
179         { 0,    N_("not defined") },
180         { 1,    N_("manual") },
181         { 2,    N_("normal") },
182         { 3,    N_("aperture") },
183         { 4,    N_("shutter") },
184         { 5,    N_("creative") },
185         { 6,    N_("action") },
186         { 7,    N_("portrait") },
187         { 8,    N_("landscape") },
188         EXIF_TEXT_LIST_END
189 };
190
191 static ExifTextList ExifLightSourceList[] = {
192         { 0,    N_("unknown") },
193         { 1,    N_("daylight") },
194         { 2,    N_("fluorescent") },
195         { 3,    N_("tungsten (incandescent)") },
196         { 4,    N_("flash") },
197         { 9,    N_("fine weather") },
198         { 10,   N_("cloudy weather") },
199         { 11,   N_("shade") },
200         { 12,   N_("daylight fluorescent") },
201         { 13,   N_("day white fluorescent") },
202         { 14,   N_("cool white fluorescent") },
203         { 15,   N_("white fluorescent") },
204         { 17,   N_("standard light A") },
205         { 18,   N_("standard light B") },
206         { 19,   N_("standard light C") },
207         { 20,   N_("D55") },
208         { 21,   N_("D65") },
209         { 22,   N_("D75") },
210         { 23,   N_("D50") },
211         { 24,   N_("ISO studio tungsten") },
212         { 255,  N_("other") },
213         EXIF_TEXT_LIST_END
214 };
215
216 static ExifTextList ExifFlashList[] = {
217         { 0,    N_("no") },
218         { 1,    N_("yes") },
219         { 5,    N_("yes, not detected by strobe") },
220         { 7,    N_("yes, detected by strobe") },
221         EXIF_TEXT_LIST_END
222 };
223
224 static ExifTextList ExifColorSpaceList[] = {
225         { 1,    N_("sRGB") },
226         { 65535,N_("uncalibrated") },
227         EXIF_TEXT_LIST_END
228 };
229
230 static ExifTextList ExifSensorList[] = {
231         { 1,    N_("not defined") },
232         { 2,    N_("1 chip color area") },
233         { 2,    N_("2 chip color area") },
234         { 4,    N_("3 chip color area") },
235         { 5,    N_("color sequential area") },
236         { 7,    N_("trilinear") },
237         { 8,    N_("color sequential linear") },
238         EXIF_TEXT_LIST_END
239 };
240
241 static ExifTextList ExifSourceList[] = {
242         { 3,    N_("digital still camera") },
243         EXIF_TEXT_LIST_END
244 };
245
246 static ExifTextList ExifSceneList[] = {
247         { 1,    N_("direct photo") },
248         EXIF_TEXT_LIST_END
249 };
250
251 static ExifTextList ExifCustRenderList[] = {
252         { 0,    N_("normal") },
253         { 1,    N_("custom") },
254         EXIF_TEXT_LIST_END
255 };
256
257 static ExifTextList ExifExposureModeList[] = {
258         { 0,    N_("auto") },
259         { 1,    N_("manual") },
260         { 2,    N_("auto bracket") },
261         EXIF_TEXT_LIST_END
262 };
263
264 static ExifTextList ExifWhiteBalanceList[] = {
265         { 0,    N_("auto") },
266         { 1,    N_("manual") },
267         EXIF_TEXT_LIST_END
268 };
269
270 static ExifTextList ExifSceneCaptureList[] = {
271         { 0,    N_("standard") },
272         { 1,    N_("landscape") },
273         { 2,    N_("portrait") },
274         { 3,    N_("night scene") },
275         EXIF_TEXT_LIST_END
276 };
277
278 static ExifTextList ExifGainControlList[] = {
279         { 0,    N_("none") },
280         { 1,    N_("low gain up") },
281         { 2,    N_("high gain up") },
282         { 3,    N_("low gain down") },
283         { 4,    N_("high gain down") },
284         EXIF_TEXT_LIST_END
285 };
286
287 static ExifTextList ExifContrastList[] = {
288         { 0,    N_("normal") },
289         { 1,    N_("soft") },
290         { 2,    N_("hard") },
291         EXIF_TEXT_LIST_END
292 };
293
294 static ExifTextList ExifSaturationList[] = {
295         { 0,    N_("normal") },
296         { 1,    N_("low") },
297         { 2,    N_("high") },
298         EXIF_TEXT_LIST_END
299 };
300
301 static ExifTextList ExifSharpnessList[] = {
302         { 0,    N_("normal") },
303         { 1,    N_("soft") },
304         { 2,    N_("hard") },
305         EXIF_TEXT_LIST_END
306 };
307
308 static ExifTextList ExifSubjectRangeList[] = {
309         { 0,    N_("unknown") },
310         { 1,    N_("macro") },
311         { 2,    N_("close") },
312         { 3,    N_("distant") },
313         EXIF_TEXT_LIST_END
314 };
315
316 /*
317 Tag names should match to exiv2 keys, https://www.exiv2.org/metadata.html
318 Tags that don't match are not supported by exiv2 and should not be used anywhere in the code
319 */
320
321 ExifMarker ExifKnownMarkersList[] = {
322 { 0x0100, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.ImageWidth",        N_("Image Width"), NULL },
323 { 0x0101, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.ImageLength",       N_("Image Height"), NULL },
324 { 0x0102, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.BitsPerSample",     N_("Bits per Sample/Pixel"), NULL },
325 { 0x0103, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.Compression",       N_("Compression"), ExifCompressionList },
326 { 0x010e, EXIF_FORMAT_STRING, -1,               "Exif.Image.ImageDescription",  N_("Image description"), NULL },
327 { 0x010f, EXIF_FORMAT_STRING, -1,               "Exif.Image.Make",              N_("Camera make"), NULL },
328 { 0x0110, EXIF_FORMAT_STRING, -1,               "Exif.Image.Model",             N_("Camera model"), NULL },
329 { 0x0112, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.Orientation",       N_("Orientation"), ExifOrientationList },
330 { 0x011a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Image.XResolution",       N_("X resolution"), NULL },
331 { 0x011b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Image.YResolution",       N_("Y Resolution"), NULL },
332 { 0x0128, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.ResolutionUnit",    N_("Resolution units"), ExifUnitList },
333 { 0x0131, EXIF_FORMAT_STRING, -1,               "Exif.Image.Software",          N_("Firmware"), NULL },
334 { 0x0132, EXIF_FORMAT_STRING, 20,               "Exif.Image.DateTime",          N_("Date"), NULL },
335 { 0x013e, EXIF_FORMAT_RATIONAL_UNSIGNED, 2,     "Exif.Image.WhitePoint",        N_("White point"), NULL },
336 { 0x013f, EXIF_FORMAT_RATIONAL_UNSIGNED, 6,     "Exif.Image.PrimaryChromaticities",N_("Primary chromaticities"), NULL },
337 { 0x0211, EXIF_FORMAT_RATIONAL_UNSIGNED, 3,     "Exif.Image.YCbCrCoefficients", N_("YCbCy coefficients"), NULL },
338 { 0x0213, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.YCbCrPositioning",  N_("YCbCr positioning"), ExifYCbCrPosList },
339 { 0x0214, EXIF_FORMAT_RATIONAL_UNSIGNED, 6,     "Exif.Image.ReferenceBlackWhite",N_("Black white reference"), NULL },
340 { 0x8298, EXIF_FORMAT_STRING, -1,               "Exif.Image.Copyright",         N_("Copyright"), NULL },
341 { 0x8769, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.ExifTag",           N_("SubIFD Exif offset"), NULL },
342         /* subIFD follows */
343 { 0x829a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ExposureTime",      N_("Exposure time (seconds)"), NULL },
344 { 0x829d, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FNumber",           N_("FNumber"), NULL },
345 { 0x8822, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.ExposureProgram",   N_("Exposure program"), ExifExposureProgramList },
346 { 0x8824, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SpectralSensitivity",N_("Spectral Sensitivity"), NULL },
347 { 0x8827, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "Exif.Photo.ISOSpeedRatings",   N_("ISO sensitivity"), NULL },
348 { 0x8828, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.OECF",              N_("Optoelectric conversion factor"), NULL },
349 { 0x9000, EXIF_FORMAT_UNDEFINED, 4,             "Exif.Photo.ExifVersion",       N_("Exif version"), NULL },
350 { 0x9003, EXIF_FORMAT_STRING, 20,               "Exif.Photo.DateTimeOriginal",  N_("Date original"), NULL },
351 { 0x9004, EXIF_FORMAT_STRING, 20,               "Exif.Photo.DateTimeDigitized", N_("Date digitized"), NULL },
352 { 0x9101, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.ComponentsConfiguration",N_("Pixel format"), NULL },
353 { 0x9102, EXIF_FORMAT_RATIONAL_UNSIGNED,1,      "Exif.Photo.CompressedBitsPerPixel",N_("Compression ratio"), NULL },
354 { 0x9201, EXIF_FORMAT_RATIONAL, 1,              "Exif.Photo.ShutterSpeedValue", N_("Shutter speed"), NULL },
355 { 0x9202, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ApertureValue",     N_("Aperture"), NULL },
356 { 0x9203, EXIF_FORMAT_RATIONAL, 1,              "Exif.Photo.BrightnessValue",   N_("Brightness"), NULL },
357 { 0x9204, EXIF_FORMAT_RATIONAL, 1,              "Exif.Photo.ExposureBiasValue", N_("Exposure bias"), NULL },
358 { 0x9205, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.MaxApertureValue",  N_("Maximum aperture"), NULL },
359 { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.SubjectDistance",   N_("Subject distance"), NULL },
360 { 0x9207, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.MeteringMode",      N_("Metering mode"), ExifMeteringModeList },
361 { 0x9208, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.LightSource",       N_("Light source"), ExifLightSourceList },
362 { 0x9209, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Flash",             N_("Flash"), ExifFlashList },
363 { 0x920a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FocalLength",       N_("Focal length"), NULL },
364 { 0x9214, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "Exif.Photo.SubjectArea",       N_("Subject area"), NULL },
365 { 0x927c, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.MakerNote",         N_("MakerNote"), NULL },
366 { 0x9286, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.UserComment",       N_("UserComment"), NULL },
367 { 0x9290, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SubSecTime",        N_("Subsecond time"), NULL },
368 { 0x9291, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SubSecTimeOriginal",N_("Subsecond time original"), NULL },
369 { 0x9292, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SubSecTimeDigitized",N_("Subsecond time digitized"), NULL },
370 { 0xa000, EXIF_FORMAT_UNDEFINED, 4,             "Exif.Photo.FlashpixVersion",   N_("FlashPix version"), NULL },
371 { 0xa001, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.ColorSpace",        N_("Colorspace"), ExifColorSpaceList },
372         /* ExifImageWidth, ExifImageHeight can also be unsigned short */
373 { 0xa002, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Photo.PixelXDimension",   N_("Width"), NULL },
374 { 0xa003, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Photo.PixelYDimension",   N_("Height"), NULL },
375 { 0xa004, EXIF_FORMAT_STRING, -1,               "Exif.Photo.RelatedSoundFile",  N_("Audio data"), NULL },
376 { 0xa005, EXIF_FORMAT_LONG_UNSIGNED, 1,         "ExifInteroperabilityOffset",   N_("ExifR98 extension"), NULL },
377 { 0xa20b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FlashEnergy",       N_("Flash strength"), NULL },
378 { 0xa20c, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.SpatialFrequencyResponse",N_("Spatial frequency response"), NULL },
379 { 0xa20e, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FocalPlaneXResolution", N_("X Pixel density"), NULL },
380 { 0xa20f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FocalPlaneYResolution", N_("Y Pixel density"), NULL },
381 { 0xa210, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.FocalPlaneResolutionUnit", N_("Pixel density units"), ExifUnitList },
382 { 0x0214, EXIF_FORMAT_SHORT_UNSIGNED, 2,        "Exif.Photo.SubjectLocation",   N_("Subject location"), NULL },
383 { 0xa215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ExposureIndex",     N_("ISO sensitivity"), NULL },
384 { 0xa217, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "Exif.Photo.SensingMethod",     N_("Sensor type"), ExifSensorList },
385 { 0xa300, EXIF_FORMAT_UNDEFINED, 1,             "Exif.Photo.FileSource",        N_("Source type"), ExifSourceList },
386 { 0xa301, EXIF_FORMAT_UNDEFINED, 1,             "Exif.Photo.SceneType",         N_("Scene type"), ExifSceneList },
387 { 0xa302, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Image.CFAPattern",        N_("Color filter array pattern"), NULL },
388         /* tags a4xx were added for Exif 2.2 (not just these - some above, as well) */
389 { 0xa401, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.CustomRendered",    N_("Render process"), ExifCustRenderList },
390 { 0xa402, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.ExposureMode",      N_("Exposure mode"), ExifExposureModeList },
391 { 0xa403, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.WhiteBalance",      N_("White balance"), ExifWhiteBalanceList },
392 { 0xa404, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.DigitalZoomRatio",  N_("Digital zoom ratio"), NULL },
393 { 0xa405, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.FocalLengthIn35mmFilm",N_("Focal length (35mm)"), NULL },
394 { 0xa406, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.SceneCaptureType",  N_("Scene capture type"), ExifSceneCaptureList },
395 { 0xa407, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.GainControl",       N_("Gain control"), ExifGainControlList },
396 { 0xa408, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Contrast",          N_("Contrast"), ExifContrastList },
397 { 0xa409, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Saturation",        N_("Saturation"), ExifSaturationList },
398 { 0xa40a, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Sharpness",         N_("Sharpness"), ExifSharpnessList },
399 { 0xa40b, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.DeviceSettingDescription",N_("Device setting"), NULL },
400 { 0xa40c, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.SubjectDistanceRange",N_("Subject range"), ExifSubjectRangeList },
401 { 0xa420, EXIF_FORMAT_STRING, -1,               "Exif.Photo.ImageUniqueID",     N_("Image serial number"), NULL },
402         /* place known, but undocumented or lesser used tags here */
403 { 0x00fe, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.NewSubfileType",    NULL, NULL },
404 { 0x00ff, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "SubfileType",                  NULL, NULL },
405 { 0x012d, EXIF_FORMAT_SHORT_UNSIGNED, 3,        "Exif.Image.TransferFunction",  NULL, NULL },
406 { 0x013b, EXIF_FORMAT_STRING, -1,               "Exif.Image.Artist",            "Artist", NULL },
407 { 0x013d, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Predictor",            NULL, NULL },
408 { 0x0142, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "TileWidth",            NULL, NULL },
409 { 0x0143, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "TileLength",           NULL, NULL },
410 { 0x0144, EXIF_FORMAT_LONG_UNSIGNED, -1,        "TileOffsets",          NULL, NULL },
411 { 0x0145, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "TileByteCounts",       NULL, NULL },
412 { 0x014a, EXIF_FORMAT_LONG_UNSIGNED, -1,        "Exif.Image.SubIFDs",           NULL, NULL },
413 { 0x015b, EXIF_FORMAT_UNDEFINED, -1,            "JPEGTables",           NULL, NULL },
414 { 0x828d, EXIF_FORMAT_SHORT_UNSIGNED, 2,        "Exif.Image.CFARepeatPatternDim",       NULL, NULL },
415 { 0x828e, EXIF_FORMAT_BYTE_UNSIGNED, -1,        "Exif.Image.CFAPattern",                NULL, NULL },
416 { 0x828f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Image.BatteryLevel",              NULL, NULL },
417 { 0x83bb, EXIF_FORMAT_LONG_UNSIGNED, -1,        "IPTC/NAA",             NULL, NULL },
418 { 0x8773, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Image.InterColorProfile",         NULL, NULL },
419 { 0x8825, EXIF_FORMAT_LONG_UNSIGNED, 1,         "GPSInfo",              "SubIFD GPS offset", NULL },
420 { 0x8829, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Interlace",            NULL, NULL },
421 { 0x882a, EXIF_FORMAT_SHORT, 1,                 "TimeZoneOffset",       NULL, NULL },
422 { 0x882b, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "SelfTimerMode",        NULL, NULL },
423 { 0x920b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FlashEnergy",               NULL, NULL },
424 { 0x920c, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.SpatialFrequencyResponse", NULL, NULL },
425 { 0x920d, EXIF_FORMAT_UNDEFINED, -1,            "Noise",                NULL, NULL },
426 { 0x9211, EXIF_FORMAT_LONG_UNSIGNED, 1,         "ImageNumber",          NULL, NULL },
427 { 0x9212, EXIF_FORMAT_STRING, 1,                "SecurityClassification", NULL, NULL },
428 { 0x9213, EXIF_FORMAT_STRING, -1,               "ImageHistory",         NULL, NULL },
429 { 0x9215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ExposureIndex",     NULL, NULL },
430 { 0x9216, EXIF_FORMAT_BYTE_UNSIGNED, 4,         "TIFF/EPStandardID",    NULL, NULL },
431
432 EXIF_MARKER_LIST_END
433 };
434
435 ExifMarker ExifKnownGPSInfoMarkersList[] = {
436         /* The following do not work at the moment as the tag value 0x0000 has a
437          * special meaning. */
438         /* { 0x0000, EXIF_FORMAT_BYTE, -1, "Exif.GPSInfo.GPSVersionID", NULL, NULL }, */
439         { 0x0001, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLatitudeRef", NULL, NULL },
440         { 0x0002, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLatitude", NULL, NULL },
441         { 0x0003, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLongitudeRef", NULL, NULL },
442         { 0x0004, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLongitude", NULL, NULL },
443         { 0x0005, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitudeRef", NULL, NULL },
444         { 0x0006, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitude", NULL, NULL },
445         { 0x0007, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSTimeStamp", NULL, NULL },
446         { 0x0008, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSatellites", NULL, NULL },
447         { 0x0009, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSStatus", NULL, NULL },
448         { 0x000a, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMeasureMode", NULL, NULL },
449         { 0x000b, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDOP", NULL, NULL },
450         { 0x000c, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSpeedRef", NULL, NULL },
451         { 0x000d, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSSpeed", NULL, NULL },
452         { 0x000e, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSTrackRef", NULL, NULL },
453         { 0x000f, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSTrack", NULL, NULL },
454         { 0x0010, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSImgDirectionRef", NULL, NULL },
455         { 0x0011, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSImgDirection", NULL, NULL },
456         { 0x0012, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMapDatum", NULL, NULL },
457         { 0x0013, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLatitudeRef", NULL, NULL },
458         { 0x0014, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLatitude", NULL, NULL },
459         { 0x0015, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLongitudeRef", NULL, NULL },
460         { 0x0016, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLongitude", NULL, NULL },
461         { 0x0017, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestBearingRef", NULL, NULL },
462         { 0x0018, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestBearing", NULL, NULL },
463         { 0x0019, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestDistanceRef", NULL, NULL },
464         { 0x001a, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestDistance", NULL, NULL },
465         { 0x001b, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSProcessingMethod", NULL, NULL },
466         { 0x001c, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSAreaInformation", NULL, NULL },
467         { 0x001d, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSDateStamp", NULL, NULL },
468         { 0x001e, EXIF_FORMAT_SHORT, -1, "Exif.GPSInfo.GPSDifferential", NULL, NULL },
469
470         EXIF_MARKER_LIST_END
471 };
472
473 ExifMarker ExifUnknownMarkersList[] = {
474 { 0x0000, EXIF_FORMAT_UNKNOWN, 0,               "unknown",      NULL, NULL },
475 { 0x0000, EXIF_FORMAT_BYTE_UNSIGNED, -1,        "unknown",      NULL, NULL },
476 { 0x0000, EXIF_FORMAT_STRING, -1,               "unknown",      NULL, NULL },
477 { 0x0000, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "unknown",      NULL, NULL },
478 { 0x0000, EXIF_FORMAT_LONG_UNSIGNED, -1,        "unknown",      NULL, NULL },
479 { 0x0000, EXIF_FORMAT_RATIONAL_UNSIGNED, -1,    "unknown",      NULL, NULL },
480 { 0x0000, EXIF_FORMAT_BYTE, -1,                 "unknown",      NULL, NULL },
481 { 0x0000, EXIF_FORMAT_UNDEFINED, -1,            "unknown",      NULL, NULL },
482 { 0x0000, EXIF_FORMAT_SHORT, -1,                "unknown",      NULL, NULL },
483 { 0x0000, EXIF_FORMAT_LONG, -1,                 "unknown",      NULL, NULL },
484 { 0x0000, EXIF_FORMAT_RATIONAL, -1,             "unknown",      NULL, NULL },
485 { 0x0000, EXIF_FORMAT_FLOAT, -1,                "unknown",      NULL, NULL },
486 { 0x0000, EXIF_FORMAT_DOUBLE, -1,               "unknown",      NULL, NULL },
487 };
488
489 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list);
490
491 /*
492  *-----------------------------------------------------------------------------
493  * ExifItem
494  *-----------------------------------------------------------------------------
495  */
496
497 ExifItem *exif_item_new(ExifFormatType format, guint tag,
498                         guint elements, const ExifMarker *marker)
499 {
500         ExifItem *item;
501
502         item = g_new0(ExifItem, 1);
503         item->format = format;
504         item->tag = tag;
505         item->marker = marker;
506         item->elements = elements;
507
508         switch (format)
509                 {
510                 case EXIF_FORMAT_UNKNOWN:
511                         /* unknown, data is NULL */
512                         return item;
513                         break;
514                 case EXIF_FORMAT_BYTE_UNSIGNED:
515                         item->data_len = sizeof(gchar) * elements;
516                         break;
517                 case EXIF_FORMAT_STRING:
518                         item->data_len = sizeof(gchar) * elements;
519                         break;
520                 case EXIF_FORMAT_SHORT_UNSIGNED:
521                         item->data_len = sizeof(guint16) * elements;
522                         break;
523                 case EXIF_FORMAT_LONG_UNSIGNED:
524                         item->data_len = sizeof(guint32) * elements;
525                         break;
526                 case EXIF_FORMAT_RATIONAL_UNSIGNED:
527                         item->data_len = sizeof(ExifRational) * elements;
528                         break;
529                 case EXIF_FORMAT_BYTE:
530                         item->data_len = sizeof(gchar) * elements;
531                         break;
532                 case EXIF_FORMAT_UNDEFINED:
533                         item->data_len = sizeof(gchar) * elements;
534                         break;
535                 case EXIF_FORMAT_SHORT:
536                         item->data_len = sizeof(gint16) * elements;
537                         break;
538                 case EXIF_FORMAT_LONG:
539                         item->data_len = sizeof(gint32) * elements;
540                         break;
541                 case EXIF_FORMAT_RATIONAL:
542                         item->data_len = sizeof(ExifRational) * elements;
543                         break;
544                 case EXIF_FORMAT_FLOAT:
545                         item->data_len = sizeof(float) * elements;
546                         break;
547                 case EXIF_FORMAT_DOUBLE:
548                         item->data_len = sizeof(gdouble) * elements;
549                         break;
550                 }
551
552         item->data = g_malloc0(item->data_len);
553
554         return item;
555 }
556
557 static void exif_item_free(ExifItem *item)
558 {
559         if (!item) return;
560
561         g_free(item->data);
562         g_free(item);
563 }
564
565 gchar *exif_item_get_tag_name(ExifItem *item)
566 {
567         if (!item || !item->marker) return NULL;
568         return g_strdup(item->marker->key);
569 }
570
571 guint exif_item_get_tag_id(ExifItem *item)
572 {
573         if (!item) return 0;
574         return item->tag;
575 }
576
577 guint exif_item_get_elements(ExifItem *item)
578 {
579         if (!item) return 0;
580         return item->elements;
581 }
582
583 gchar *exif_item_get_data(ExifItem *item, guint *data_len)
584 {
585         if (data_len)
586                 *data_len = item->data_len;
587 #if GLIB_CHECK_VERSION(2,68,0)
588         return (gchar*)g_memdup2((gpointer)(item->data), item->data_len);
589 #else
590         return (gchar*)g_memdup((gpointer)(item->data), item->data_len);
591 #endif
592 }
593
594 guint exif_item_get_format_id(ExifItem *item)
595 {
596         if (!item) return EXIF_FORMAT_UNKNOWN;
597         return item->format;
598 }
599
600
601 gchar *exif_item_get_description(ExifItem *item)
602 {
603         if (!item || !item->marker) return NULL;
604         return g_strdup(_(item->marker->description));
605 }
606
607 const gchar *exif_item_get_format_name(ExifItem *item, gboolean brief)
608 {
609         if (!item || !item->marker) return NULL;
610         return (brief) ? ExifFormatList[item->format].short_name : ExifFormatList[item->format].description;
611 }
612
613 static GString *string_append_raw_bytes(GString *string, gpointer data, gint ne)
614 {
615         gint i;
616
617         for (i = 0 ; i < ne; i++)
618                 {
619                 guchar c = ((gchar *)data)[i];
620                 if (c < 32 || c > 127) c = '.';
621                 g_string_append_printf(string, "%c", c);
622                 }
623         string = g_string_append(string, " : ");
624         for (i = 0 ; i < ne; i++)
625                 {
626                 const gchar *spacer;
627                 if (i > 0)
628                         {
629                         if (i%8 == 0)
630                                 {
631                                 spacer = " - ";
632                                 }
633                         else
634                                 {
635                                 spacer = " ";
636                                 }
637                         }
638                 else
639                         {
640                         spacer = "";
641                         }
642                 g_string_append_printf(string, "%s%02x", spacer, ((gchar *)data)[i]);
643                 }
644
645         return string;
646 }
647
648
649 gchar *exif_text_list_find_value(ExifTextList *list, guint value)
650 {
651         gchar *result = NULL;
652         gint i;
653
654         i = 0;
655         while (!result && list[i].value >= 0)
656                 {
657                 if (value == (guint) list[i].value) result = g_strdup(_(list[i].description));
658                 i++;
659                 }
660         if (!result) result = g_strdup_printf("%d (%s)", value, _("unknown"));
661
662         return result;
663 }
664
665
666 /*
667  *-------------------------------------------------------------------
668  * byte order utils
669  *-------------------------------------------------------------------
670  */
671
672 /* note: the align_buf is used to avoid alignment issues (on sparc) */
673
674 guint16 exif_byte_get_int16(guchar *f, ExifByteOrder bo)
675 {
676         guint16 align_buf;
677
678         memcpy(&align_buf, f, sizeof(guint16));
679
680         if (bo == EXIF_BYTE_ORDER_INTEL)
681                 return GUINT16_FROM_LE(align_buf);
682         else
683                 return GUINT16_FROM_BE(align_buf);
684 }
685
686 guint32 exif_byte_get_int32(guchar *f, ExifByteOrder bo)
687 {
688         guint32 align_buf;
689
690         memcpy(&align_buf, f, sizeof(guint32));
691
692         if (bo == EXIF_BYTE_ORDER_INTEL)
693                 return GUINT32_FROM_LE(align_buf);
694         else
695                 return GUINT32_FROM_BE(align_buf);
696 }
697
698 void exif_byte_put_int16(guchar *f, guint16 n, ExifByteOrder bo)
699 {
700         guint16 align_buf;
701
702         if (bo == EXIF_BYTE_ORDER_INTEL)
703                 {
704                 align_buf = GUINT16_TO_LE(n);
705                 }
706         else
707                 {
708                 align_buf = GUINT16_TO_BE(n);
709                 }
710
711         memcpy(f, &align_buf, sizeof(guint16));
712 }
713
714 void exif_byte_put_int32(guchar *f, guint32 n, ExifByteOrder bo)
715 {
716         guint32 align_buf;
717
718         if (bo == EXIF_BYTE_ORDER_INTEL)
719                 {
720                 align_buf = GUINT32_TO_LE(n);
721                 }
722         else
723                 {
724                 align_buf = GUINT32_TO_BE(n);
725                 }
726
727         memcpy(f, &align_buf, sizeof(guint32));
728 }
729
730
731 /*
732  *-------------------------------------------------------------------
733  * IFD utils
734  *-------------------------------------------------------------------
735  */
736
737 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list)
738 {
739         gint i = 0;
740
741         if (!list) return NULL;
742
743         while (list[i].tag != 0 && list[i].tag != tag)
744                 {
745                 i++;
746                 }
747
748         return (list[i].tag == 0 ? NULL : &list[i]);
749 }
750
751 static void rational_from_data(ExifRational *r, gpointer src, ExifByteOrder bo)
752 {
753         r->num = exif_byte_get_int32((guchar *)src, bo);
754         r->den = exif_byte_get_int32((guchar *)src + sizeof(guint32), bo);
755 }
756
757 /* src_format and item->format must be compatible
758  * and not overrun src or item->data.
759  */
760 void exif_item_copy_data(ExifItem *item, gpointer src, guint len,
761                          ExifFormatType src_format, ExifByteOrder bo)
762 {
763         gint bs;
764         gint ne;
765         gpointer dest;
766         gint i;
767
768         bs = ExifFormatList[item->format].size;
769         ne = item->elements;
770         dest = item->data;
771
772         if (!dest ||
773             ExifFormatList[src_format].size * ne > len)
774                 {
775                 gchar *tag = exif_item_get_tag_name(item);
776                 log_printf("exif tag %s data size mismatch\n", tag);
777                 g_free(tag);
778                 return;
779                 }
780
781         switch (item->format)
782                 {
783                 case EXIF_FORMAT_UNKNOWN:
784                         break;
785                 case EXIF_FORMAT_BYTE_UNSIGNED:
786                 case EXIF_FORMAT_BYTE:
787                 case EXIF_FORMAT_UNDEFINED:
788                         memcpy(dest, src, len);
789                         break;
790                 case EXIF_FORMAT_STRING:
791                         memcpy(dest, src, len);
792                         /* string is NULL terminated, make sure this is true */
793                         if (((gchar *)dest)[len - 1] != '\0') ((gchar *)dest)[len - 1] = '\0';
794                         break;
795                 case EXIF_FORMAT_SHORT_UNSIGNED:
796                 case EXIF_FORMAT_SHORT:
797                         for (i = 0; i < ne; i++)
798                                 {
799                                 ((guint16 *)dest)[i] = exif_byte_get_int16((guchar *)src + i * bs, bo);
800                                 }
801                         break;
802                 case EXIF_FORMAT_LONG_UNSIGNED:
803                 case EXIF_FORMAT_LONG:
804                         if (src_format == EXIF_FORMAT_SHORT_UNSIGNED ||
805                             src_format == EXIF_FORMAT_SHORT)
806                                 {
807                                 /* a short fits into a long, so allow it */
808                                 gint ss;
809
810                                 ss = ExifFormatList[src_format].size;
811                                 for (i = 0; i < ne; i++)
812                                         {
813                                         ((gint32 *)dest)[i] =
814                                                 (gint32)exif_byte_get_int16((guchar *)src + i * ss, bo);
815                                         }
816                                 }
817                         else
818                                 {
819                                 for (i = 0; i < ne; i++)
820                                         {
821                                         ((gint32 *)dest)[i] =
822                                                 exif_byte_get_int32((guchar *)src + i * bs, bo);
823                                         }
824                                 }
825                         break;
826                 case EXIF_FORMAT_RATIONAL_UNSIGNED:
827                 case EXIF_FORMAT_RATIONAL:
828                         for (i = 0; i < ne; i++)
829                                 {
830                                 rational_from_data(&((ExifRational *)dest)[i], (guchar *)src + i * bs, bo);
831                                 }
832                         break;
833                 case EXIF_FORMAT_FLOAT:
834                         for (i = 0; i < ne; i++)
835                                 {
836                                 ((float *)dest)[i] = exif_byte_get_int32((guchar *)src + i * bs, bo);
837                                 }
838                         break;
839                 case EXIF_FORMAT_DOUBLE:
840                         for (i = 0; i < ne; i++)
841                                 {
842                                 ExifRational r;
843
844                                 rational_from_data(&r, (guchar *)src + i * bs, bo);
845                                 if (r.den) ((gdouble *)dest)[i] = (gdouble)r.num / r.den;
846                                 }
847                         break;
848                 }
849 }
850
851 static gint exif_parse_IFD_entry(ExifData *exif, guchar *tiff, guint offset,
852                                  guint size, ExifByteOrder bo,
853                                  gint level,
854                                  const ExifMarker *list)
855 {
856         guint tag;
857         guint format;
858         guint count;
859         guint data_val;
860         guint data_offset;
861         guint data_length;
862         const ExifMarker *marker;
863         ExifItem *item;
864
865         tag = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_TAG, bo);
866         format = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
867         count = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_COUNT, bo);
868         data_val = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_DATA, bo);
869
870         /* Check tag type. If it does not match, either the format is wrong,
871          * either it is a unknown tag; so it is not really an error.
872          */
873         marker = exif_marker_from_tag(tag, list);
874         if (!marker)
875                 {
876                 if (format >= EXIF_FORMAT_COUNT)
877                         {
878                         log_printf("warning: exif tag 0x%4x has invalid format %d\n", tag, format);
879                         return 0;
880                         }
881                 /* allow non recognized tags to be displayed */
882                 marker = &ExifUnknownMarkersList[format];
883                 }
884         if (marker->format != format)
885                 {
886                 /* Some cameras got mixed up signed/unsigned_rational
887                  * eg KODAK DC4800 on object_distance tag
888                  *
889                  * FIXME: what exactly is this test trying to do?
890                  * ok, so this test is to allow the case of swapped signed/unsigned mismatch to leak through?
891                  */
892                 if (!(marker->format == EXIF_FORMAT_RATIONAL_UNSIGNED && format == EXIF_FORMAT_RATIONAL) &&
893                     !(marker->format == EXIF_FORMAT_RATIONAL && format == EXIF_FORMAT_RATIONAL_UNSIGNED) &&
894                         /* short fits into a long so allow this mismatch
895                          * as well (some tags allowed to be unsigned short _or_ unsigned long)
896                          */
897                     !(marker->format == EXIF_FORMAT_LONG_UNSIGNED && format == EXIF_FORMAT_SHORT_UNSIGNED) )
898                         {
899                         if (format < EXIF_FORMAT_COUNT)
900                                 {
901                                 log_printf("warning: exif tag %s format mismatch, found %s exif spec requests %s\n",
902                                         marker->key, ExifFormatList[format].short_name,
903                                         ExifFormatList[marker->format].short_name);
904                                 }
905                         else
906                                 {
907                                 log_printf("warning: exif tag %s format mismatch, found unknown id %d exif spec requests %d (%s)\n",
908                                         marker->key, format, marker->format,
909                                         ExifFormatList[marker->format].short_name);
910                                 }
911                         return 0;
912                         }
913                 }
914
915         /* Where is the data, is it available?
916          */
917         if (marker->components > 0 && (guint) marker->components != count)
918                 {
919                 log_printf("warning: exif tag %s has %d elements, exif spec requests %d\n",
920                         marker->key, count, marker->components);
921                 }
922
923         data_length = ExifFormatList[marker->format].size * count;
924         if (data_length > 4)
925                 {
926                 data_offset = data_val;
927                 if (size < data_offset || size < data_offset + data_length)
928                         {
929                         log_printf("warning: exif tag %s data will overrun end of file, ignored.\n", marker->key);
930                         return -1;
931                         }
932                 }
933         else
934                 {
935                 data_offset = offset + EXIF_TIFD_OFFSET_DATA;
936                 }
937
938         item = exif_item_new(marker->format, tag, count, marker);
939         exif_item_copy_data(item, tiff + data_offset, data_length, (ExifFormatType)format, bo);
940         exif->items = g_list_prepend(exif->items, item);
941
942         if (list == ExifKnownMarkersList)
943                 {
944                 switch (item->tag)
945                         {
946                         case TAG_EXIFOFFSET:
947                                 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, list);
948                                 break;
949                         case TAG_GPSOFFSET:
950                                 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, ExifKnownGPSInfoMarkersList);
951                                 break;
952                         case TAG_EXIFMAKERNOTE:
953                                 format_exif_makernote_parse(exif, tiff, data_val, size, bo);
954                                 break;
955                         }
956                 }
957
958         return 0;
959 }
960
961 gint exif_parse_IFD_table(ExifData *exif,
962                           guchar *tiff, guint offset,
963                           guint size, ExifByteOrder bo,
964                           gint level,
965                           const ExifMarker *list)
966 {
967         guint count;
968         guint i;
969
970         /* limit damage from infinite loops */
971         if (level > EXIF_TIFF_MAX_LEVELS) return -1;
972
973         /* We should be able to read number of entries in IFD0) */
974         if (size < offset + 2) return -1;
975
976         count = exif_byte_get_int16(tiff + offset, bo);
977         offset += 2;
978
979         /* Entries and next IFD offset must be readable */
980         if (size < offset + count * EXIF_TIFD_SIZE + 4) return -1;
981
982         for (i = 0; i < count; i++)
983                 {
984                 exif_parse_IFD_entry(exif, tiff, offset + i * EXIF_TIFD_SIZE, size, bo, level, list);
985                 }
986
987         return 0;
988 }
989
990 /*
991  *-------------------------------------------------------------------
992  * file formats
993  *-------------------------------------------------------------------
994  */
995
996 gint exif_tiff_directory_offset(guchar *data, const guint len,
997                                 guint *offset, ExifByteOrder *bo)
998 {
999         if (len < 8) return FALSE;
1000
1001         if (memcmp(data, "II", 2) == 0)
1002                 {
1003                 *bo = EXIF_BYTE_ORDER_INTEL;
1004                 }
1005         else if (memcmp(data, "MM", 2) == 0)
1006                 {
1007                 *bo = EXIF_BYTE_ORDER_MOTOROLA;
1008                 }
1009         else
1010                 {
1011                 return FALSE;
1012                 }
1013
1014         if (exif_byte_get_int16(data + 2, *bo) != 0x002A)
1015                 {
1016                 return FALSE;
1017                 }
1018
1019         *offset = exif_byte_get_int32(data + 4, *bo);
1020
1021         return (*offset < len);
1022 }
1023
1024 gint exif_tiff_parse(ExifData *exif, guchar *tiff, guint size, ExifMarker *list)
1025 {
1026         ExifByteOrder bo;
1027         guint offset;
1028
1029         if (!exif_tiff_directory_offset(tiff, size, &offset, &bo)) return -1;
1030
1031         return exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, list);
1032 }
1033
1034
1035 /*
1036  *-------------------------------------------------------------------
1037  * jpeg marker utils
1038  *-------------------------------------------------------------------
1039  */
1040
1041 #define JPEG_MARKER             0xFF
1042 #define JPEG_MARKER_SOI         0xD8
1043 #define JPEG_MARKER_EOI         0xD9
1044 #define JPEG_MARKER_APP1        0xE1
1045 #define JPEG_MARKER_APP2        0xE2
1046
1047 /* jpeg container format:
1048      all data markers start with 0XFF
1049      2 byte long file start and end markers: 0xFFD8(SOI) and 0XFFD9(EOI)
1050      4 byte long data segment markers in format: 0xFFTTSSSSNNN...
1051        FF:   1 byte standard marker identifier
1052        TT:   1 byte data type
1053        SSSS: 2 bytes in Motorola byte alignment for length of the data.
1054              This value includes these 2 bytes in the count, making actual
1055              length of NN... == SSSS - 2.
1056        NNN.: the data in this segment
1057  */
1058 static ExifMarker jpeg_color_marker = { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", NULL, NULL };
1059
1060 void exif_add_jpeg_color_profile(ExifData *exif, guchar *cp_data, guint cp_length)
1061 {
1062         ExifItem *item = exif_item_new(jpeg_color_marker.format, jpeg_color_marker.tag, 1,
1063                                      &jpeg_color_marker);
1064         g_free(item->data);
1065         item->data = cp_data;
1066         item->elements = cp_length;
1067         item->data_len = cp_length;
1068         exif->items = g_list_prepend(exif->items, item);
1069
1070 }
1071
1072 static gint exif_jpeg_parse(ExifData *exif,
1073                             guchar *data, guint size,
1074                             ExifMarker *list)
1075 {
1076         guint seg_offset = 0;
1077         guint seg_length = 0;
1078         gint res = -1;
1079
1080         if (size < 4 ||
1081             memcmp(data, "\xFF\xD8", 2) != 0)
1082                 {
1083                 return -2;
1084                 }
1085
1086         if (jpeg_segment_find(data, size, JPEG_MARKER_APP1,
1087                                    "Exif\x00\x00", 6,
1088                                    &seg_offset, &seg_length))
1089                 {
1090                 res = exif_tiff_parse(exif, data + seg_offset + 6, seg_length - 6, list);
1091                 }
1092
1093         if (exif_jpeg_parse_color(exif, data, size))
1094                 {
1095                 res = 0;
1096                 }
1097
1098         return res;
1099 }
1100
1101 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1102 {
1103         ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1104         if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1105                 return (guchar *) exif_item_get_data(prof_item, data_len);
1106         return NULL;
1107 }
1108
1109
1110 gchar* exif_get_image_comment(FileData* UNUSED(fd))
1111 {
1112         log_printf("%s", _("Can't get image comment: not compiled with Exiv2.\n"));
1113         return g_strdup("");
1114 }
1115
1116 void exif_set_image_comment(FileData* UNUSED(fd), const gchar* UNUSED(comment))
1117 {
1118         log_printf("%s", _("Can't set image comment: not compiled with Exiv2.\n"));
1119 }
1120
1121
1122 /*
1123  *-------------------------------------------------------------------
1124  * misc
1125  *-------------------------------------------------------------------
1126  */
1127
1128
1129 ExifItem *exif_get_first_item(ExifData *exif)
1130 {
1131         if (exif->items)
1132                 {
1133                 ExifItem *ret = (ExifItem *)exif->items->data;
1134                 exif->current = exif->items->next;
1135                 return ret;
1136                 }
1137         exif->current = NULL;
1138         return NULL;
1139 }
1140
1141 ExifItem *exif_get_next_item(ExifData *exif)
1142 {
1143         if (exif->current)
1144                 {
1145                 ExifItem *ret = (ExifItem *)exif->current->data;
1146                 exif->current = exif->current->next;
1147                 return ret;
1148                 }
1149         return NULL;
1150 }
1151
1152 static gint map_file(const gchar *path, void **mapping, gint *size)
1153 {
1154         gint fd;
1155         struct stat fs;
1156
1157         fd = open(path, O_RDONLY);
1158         if (fd == -1)
1159                 {
1160                 perror(path);
1161                 return -1;
1162                 }
1163
1164         if (fstat(fd, &fs) == -1)
1165                 {
1166                 perror(path);
1167                 close(fd);
1168                 return -1;
1169                 }
1170
1171         *size = fs.st_size;
1172
1173         *mapping = mmap(0, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1174         if (*mapping == MAP_FAILED)
1175                 {
1176                 perror(path);
1177                 close(fd);
1178                 return -1;
1179                 }
1180
1181         close(fd);
1182         return 0;
1183 }
1184
1185 static gint unmap_file(gpointer mapping, gint size)
1186 {
1187         if (munmap(mapping, size) == -1)
1188                 {
1189                 perror("munmap");
1190                 return -1;
1191                 }
1192
1193         return 0;
1194 }
1195
1196 ExifData *exif_get_original(ExifData *processed)
1197 {
1198         return processed;
1199 }
1200
1201 void exif_free(ExifData *exif)
1202 {
1203         GList *work;
1204
1205         if (!exif) return;
1206
1207         work = exif->items;
1208         while (work)
1209                 {
1210                 ExifItem *item = static_cast<ExifItem *>(work->data);
1211                 work = work->next;
1212                 exif_item_free(item);
1213                 }
1214
1215         g_list_free(exif->items);
1216         g_free(exif->path);
1217         g_free(exif);
1218 }
1219
1220 ExifData *exif_read(gchar *path, gchar *UNUSED(sidecar_path), GHashTable *UNUSED(modified_xmp))
1221 {
1222         ExifData *exif;
1223         gpointer f;
1224         gint size, res;
1225         gchar *pathl;
1226
1227         if (!path) return NULL;
1228
1229         pathl = path_from_utf8(path);
1230         if (map_file(pathl, &f, &size) == -1)
1231                 {
1232                 g_free(pathl);
1233                 return NULL;
1234                 }
1235         g_free(pathl);
1236
1237         exif = g_new0(ExifData, 1);
1238         exif->path = g_strdup(path);
1239
1240         res = exif_jpeg_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1241         if (res == -2)
1242                 {
1243                 res = exif_tiff_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1244                 }
1245
1246         if (res != 0)
1247                 {
1248                 FormatRawExifType exif_type;
1249                 FormatRawExifParseFunc exif_parse_func;
1250                 guint32 offset = 0;
1251
1252                 exif_type = format_raw_exif_offset((guchar*)f, size, &offset, &exif_parse_func);
1253                 switch (exif_type)
1254                         {
1255                         case FORMAT_RAW_EXIF_NONE:
1256                         default:
1257                                 break;
1258                         case FORMAT_RAW_EXIF_TIFF:
1259                                 res = exif_tiff_parse(exif, (guchar *)f + offset, size - offset,
1260                                                       ExifKnownMarkersList);
1261                                 break;
1262                         case FORMAT_RAW_EXIF_JPEG:
1263                                 res = exif_jpeg_parse(exif, (guchar *)f + offset, size - offset,
1264                                                       ExifKnownMarkersList);
1265                                 break;
1266                         case FORMAT_RAW_EXIF_IFD_II:
1267                         case FORMAT_RAW_EXIF_IFD_MM:
1268                                 res = exif_parse_IFD_table(exif, (guchar *)f, offset, size - offset,
1269                                                            (exif_type == FORMAT_RAW_EXIF_IFD_II) ?
1270                                                                 EXIF_BYTE_ORDER_INTEL : EXIF_BYTE_ORDER_MOTOROLA,
1271                                                            0, ExifKnownMarkersList);
1272                                 break;
1273                         case FORMAT_RAW_EXIF_PROPRIETARY:
1274                                 if (exif_parse_func)
1275                                         {
1276                                         res = exif_parse_func((guchar *)f + offset, size - offset, exif);
1277                                         }
1278                                 break;
1279                         }
1280                 }
1281
1282         if (res != 0)
1283                 {
1284                 exif_free(exif);
1285                 exif = NULL;
1286                 }
1287
1288         unmap_file(f, size);
1289
1290         if (exif) exif->items = g_list_reverse(exif->items);
1291
1292         return exif;
1293 }
1294
1295 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
1296 {
1297         GList *work;
1298
1299         if (!key) return NULL;
1300
1301         work = exif->items;
1302         while (work)
1303                 {
1304                 ExifItem *item;
1305
1306                 item = (ExifItem*)(work->data);
1307                 work = work->next;
1308                 if (item->marker->key && strcmp(key, item->marker->key) == 0) return item;
1309                 }
1310         return NULL;
1311 }
1312
1313 #define EXIF_DATA_AS_TEXT_MAX_COUNT 16
1314
1315
1316 static gchar *exif_item_get_data_as_text_full(ExifItem *item, MetadataFormat format)
1317 {
1318         const ExifMarker *marker;
1319         gpointer data;
1320         GString *string;
1321         gchar *text;
1322         gint ne;
1323         gint i;
1324
1325         if (!item) return NULL;
1326
1327         marker = item->marker;
1328         if (!marker) return NULL;
1329
1330         data = item->data;
1331         ne = item->elements;
1332         if (ne > EXIF_DATA_AS_TEXT_MAX_COUNT) ne = EXIF_DATA_AS_TEXT_MAX_COUNT;
1333         string = g_string_new("");
1334         switch (item->format)
1335                 {
1336                 case EXIF_FORMAT_UNKNOWN:
1337                         break;
1338                 case EXIF_FORMAT_BYTE_UNSIGNED:
1339                 case EXIF_FORMAT_BYTE:
1340                 case EXIF_FORMAT_UNDEFINED:
1341                         if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1342                                 {
1343                                 gchar *result;
1344                                 guchar val;
1345
1346                                 if (item->format == EXIF_FORMAT_BYTE_UNSIGNED ||
1347                                     item->format == EXIF_FORMAT_UNDEFINED)
1348                                         {
1349                                         val = ((guchar *)data)[0];
1350                                         }
1351                                 else
1352                                         {
1353                                         val = (guchar)(((gchar *)data)[0]);
1354                                         }
1355
1356                                 result = exif_text_list_find_value(marker->list, (guint)val);
1357                                 string = g_string_append(string, result);
1358                                 g_free(result);
1359                                 }
1360                         else
1361                                 {
1362                                 string = string_append_raw_bytes(string, data, ne);
1363                                 }
1364                         break;
1365                 case EXIF_FORMAT_STRING:
1366                         if (item->data) string = g_string_append(string, (gchar *)(item->data));
1367                         break;
1368                 case EXIF_FORMAT_SHORT_UNSIGNED:
1369                         if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1370                                 {
1371                                 gchar *result;
1372
1373                                 result = exif_text_list_find_value(marker->list, ((guint16 *)data)[0]);
1374                                 string = g_string_append(string, result);
1375                                 g_free(result);
1376                                 }
1377                         else for (i = 0; i < ne; i++)
1378                                 {
1379                                 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1380                                                         ((guint16 *)data)[i]);
1381                                 }
1382                         break;
1383                 case EXIF_FORMAT_LONG_UNSIGNED:
1384                         for (i = 0; i < ne; i++)
1385                                 {
1386                                 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1387                                                         (gulong)((guint32 *)data)[i]);
1388                                 }
1389                         break;
1390                 case EXIF_FORMAT_RATIONAL_UNSIGNED:
1391                         for (i = 0; i < ne; i++)
1392                                 {
1393                                 ExifRational *r;
1394
1395                                 r = &((ExifRational *)data)[i];
1396                                 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1397                                                         (gulong)r->num, (gulong)r->den);
1398                                 }
1399                         break;
1400                 case EXIF_FORMAT_SHORT:
1401                         for (i = 0; i < ne; i++)
1402                                 {
1403                                 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1404                                                         ((gint16 *)data)[i]);
1405                                 }
1406                         break;
1407                 case EXIF_FORMAT_LONG:
1408                         for (i = 0; i < ne; i++)
1409                                 {
1410                                 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1411                                                         (glong)((gint32 *)data)[i]);
1412                                 }
1413                         break;
1414                 case EXIF_FORMAT_RATIONAL:
1415                         for (i = 0; i < ne; i++)
1416                                 {
1417                                 ExifRational *r;
1418
1419                                 r = &((ExifRational *)data)[i];
1420                                 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1421                                                         (glong)r->num, (glong)r->den);
1422                                 }
1423                         break;
1424                 case EXIF_FORMAT_FLOAT:
1425                         for (i = 0; i < ne; i++)
1426                                 {
1427                                 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1428                                                         ((float *)data)[i]);
1429                                 }
1430                         break;
1431                 case EXIF_FORMAT_DOUBLE:
1432                         for (i = 0; i < ne; i++)
1433                                 {
1434                                 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1435                                                         ((gdouble *)data)[i]);
1436                                 }
1437                         break;
1438                 }
1439
1440         if (item->elements > EXIF_DATA_AS_TEXT_MAX_COUNT &&
1441             item->format != EXIF_FORMAT_STRING)
1442                 {
1443                 g_string_append(string, " ...");
1444                 }
1445
1446         text = string->str;
1447         g_string_free(string, FALSE);
1448
1449         return text;
1450 }
1451
1452 gchar *exif_item_get_string(ExifItem *item, gint UNUSED(idx))
1453 {
1454         return exif_item_get_data_as_text_full(item, METADATA_PLAIN);
1455 }
1456
1457 gchar *exif_item_get_data_as_text(ExifItem *item)
1458 {
1459         return exif_item_get_data_as_text_full(item, METADATA_FORMATTED);
1460 }
1461
1462 gint exif_item_get_integer(ExifItem *item, gint *value)
1463 {
1464         if (!item) return FALSE;
1465         if (!item->elements) return FALSE;
1466
1467         switch (item->format)
1468                 {
1469                 case EXIF_FORMAT_SHORT:
1470                         *value = (gint)(((gint16 *)(item->data))[0]);
1471                         return TRUE;
1472                         break;
1473                 case EXIF_FORMAT_SHORT_UNSIGNED:
1474                         *value = (gint)(((guint16 *)(item->data))[0]);
1475                         return TRUE;
1476                         break;
1477                 case EXIF_FORMAT_LONG:
1478                         *value = (gint)(((gint32 *)(item->data))[0]);
1479                         return TRUE;
1480                         break;
1481                 case EXIF_FORMAT_LONG_UNSIGNED: /**< @FIXME overflow possible */
1482                         *value = (gint)(((guint32 *)(item->data))[0]);
1483                         return TRUE;
1484                 default:
1485                         /* all other type return FALSE */
1486                         break;
1487                 }
1488         return FALSE;
1489 }
1490
1491
1492 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
1493 {
1494         if (!item) return NULL;
1495         if (n >= item->elements) return NULL;
1496
1497         if (item->format == EXIF_FORMAT_RATIONAL ||
1498             item->format == EXIF_FORMAT_RATIONAL_UNSIGNED)
1499                 {
1500                 if (sign) *sign = (item->format == EXIF_FORMAT_RATIONAL);
1501                 return &((ExifRational *)(item->data))[n];
1502                 }
1503
1504         return NULL;
1505 }
1506
1507 gchar *exif_get_tag_description_by_key(const gchar *key)
1508 {
1509         gint i;
1510
1511         if (!key) return NULL;
1512
1513         i = 0;
1514         while (ExifKnownMarkersList[i].tag > 0)
1515                 {
1516                 if (strcmp(key, ExifKnownMarkersList[i].key) == 0) return g_strdup(_(ExifKnownMarkersList[i].description));
1517                 i++;
1518                 }
1519
1520         i = 0;
1521         while (ExifKnownGPSInfoMarkersList[i].tag > 0)
1522         {
1523            if (strcmp(key, ExifKnownGPSInfoMarkersList[i].key) == 0) return g_strdup(_(ExifKnownGPSInfoMarkersList[i].description));
1524            i++;
1525         }
1526
1527         return NULL;
1528 }
1529
1530 static void exif_write_item(FILE *f, ExifItem *item)
1531 {
1532         gchar *text;
1533
1534         text = exif_item_get_data_as_text(item);
1535         if (text)
1536                 {
1537                 gchar *tag = exif_item_get_tag_name(item);
1538                 g_fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
1539                         tag, text);
1540                 g_free(tag);
1541                 }
1542         g_free(text);
1543 }
1544
1545 void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
1546 {
1547         if (!f || !exif) return;
1548
1549         g_fprintf(f, " tag   format                             key value\n");
1550         g_fprintf(f, "----------------------------------------------------\n");
1551
1552         if (human_readable_list)
1553                 {
1554                 gint i;
1555
1556                 i = 0;
1557                 while (ExifFormattedList[i].key)
1558                         {
1559                         gchar *text;
1560
1561                         text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, NULL);
1562                         if (text)
1563                                 {
1564                                 g_fprintf(f, "     %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
1565                                 }
1566                         i++;
1567                         }
1568                 }
1569         else
1570                 {
1571                 GList *work;
1572
1573                 work = exif->items;
1574                 while (work)
1575                         {
1576                         ExifItem *item;
1577
1578                         item = (ExifItem*)(work->data);
1579                         work = work->next;
1580
1581                         exif_write_item(f, item);
1582                         }
1583                 }
1584         g_fprintf(f, "----------------------------------------------------\n");
1585 }
1586
1587 gboolean exif_write(ExifData *UNUSED(exif))
1588 {
1589         log_printf("Not compiled with EXIF write support\n");
1590         return FALSE;
1591 }
1592
1593 gboolean exif_write_sidecar(ExifData *UNUSED(exif), gchar *UNUSED(path))
1594 {
1595         log_printf("Not compiled with EXIF write support\n");
1596         return FALSE;
1597 }
1598
1599
1600 gint exif_update_metadata(ExifData *UNUSED(exif), const gchar *UNUSED(key), const GList *UNUSED(values))
1601 {
1602         return 0;
1603 }
1604
1605 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1606 {
1607         gchar *str;
1608         ExifItem *item;
1609
1610         if (!key) return NULL;
1611
1612         /* convert xmp key to exif key */
1613         if (strcmp(key, "Xmp.tiff.Orientation") == 0) key = "Exif.Image.Orientation";
1614
1615         if (format == METADATA_FORMATTED)
1616                 {
1617                 gchar *text;
1618                 gint key_valid;
1619                 text = exif_get_formatted_by_key(exif, key, &key_valid);
1620                 if (key_valid) return g_list_append(NULL, text);
1621                 }
1622
1623         item = exif_get_item(exif, key);
1624         if (!item) return NULL;
1625
1626         str = exif_item_get_data_as_text_full(item, format);
1627
1628         if (!str) return NULL;
1629
1630         return g_list_append(NULL, str);
1631 }
1632
1633 typedef struct _UnmapData UnmapData;
1634 struct _UnmapData
1635 {
1636         guchar *ptr;
1637         guchar *map_data;
1638         size_t map_len;
1639 };
1640
1641 static GList *exif_unmap_list = 0;
1642
1643 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint UNUSED(requested_width), gint UNUSED(requested_height))
1644 {
1645         guint offset;
1646         const gchar* path;
1647         struct stat st;
1648         guchar *map_data;
1649         size_t map_len;
1650         int fd;
1651
1652         if (!exif) return NULL;
1653         path = exif->path;
1654
1655         fd = open(path, O_RDONLY);
1656
1657
1658         if (fd == -1)
1659                 {
1660                 return 0;
1661                 }
1662
1663         if (fstat(fd, &st) == -1)
1664                 {
1665                 close(fd);
1666                 return 0;
1667                 }
1668         map_len = st.st_size;
1669         map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1670         close(fd);
1671
1672         if (map_data == MAP_FAILED)
1673                 {
1674                 return 0;
1675                 }
1676
1677         if (format_raw_img_exif_offsets(map_data, map_len, &offset, NULL) && offset)
1678                 {
1679                 UnmapData *ud;
1680
1681                 DEBUG_1("%s: offset %u", path, offset);
1682
1683                 *data_len = map_len - offset;
1684                 ud = g_new(UnmapData, 1);
1685                 ud->ptr = map_data + offset;
1686                 ud->map_data = map_data;
1687                 ud->map_len = map_len;
1688
1689                 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1690                 return ud->ptr;
1691                 }
1692
1693         munmap(map_data, map_len);
1694         return NULL;
1695
1696 }
1697
1698 void exif_free_preview(guchar *buf)
1699 {
1700         GList *work = exif_unmap_list;
1701
1702         while (work)
1703                 {
1704                 UnmapData *ud = (UnmapData *)work->data;
1705                 if (ud->ptr == buf)
1706                         {
1707                         exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1708                         g_free(ud);
1709                         return;
1710                         }
1711                 work = work->next;
1712                 }
1713         g_assert_not_reached();
1714 }
1715
1716 void exif_init(void)
1717 {
1718 }
1719 #else
1720 typedef int dummy_variable;
1721 #endif
1722 /* not HAVE_EXIV2 */
1723 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */