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