Bug fix: Run-time errors when removing a toolbar icon
[geeqie.git] / src / jpeg-parser.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: Vladimir Nadvornik
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
22 #include "main.h"
23 #include "jpeg-parser.h"
24
25 gboolean jpeg_segment_find(const guchar *data, guint size,
26                             guchar app_marker, const gchar *magic, guint magic_len,
27                             guint *seg_offset, guint *seg_length)
28 {
29         guchar marker = 0;
30         guint offset = 0;
31         guint length = 0;
32
33         while (marker != JPEG_MARKER_EOI)
34                 {
35                 offset += length;
36                 length = 2;
37
38                 if (offset + 2 >= size ||
39                     data[offset] != JPEG_MARKER) return FALSE;
40
41                 marker = data[offset + 1];
42                 if (marker != JPEG_MARKER_SOI &&
43                     marker != JPEG_MARKER_EOI)
44                         {
45                         if (offset + 4 >= size) return FALSE;
46                         length += (static_cast<guint>(data[offset + 2]) << 8) + data[offset + 3];
47
48                         if (marker == app_marker &&
49                             offset + length < size &&
50                             length >= 4 + magic_len &&
51                             memcmp(data + offset + 4, magic, magic_len) == 0)
52                                 {
53                                 *seg_offset = offset + 4;
54                                 *seg_length = length - 4;
55                                 return TRUE;
56                                 }
57                         }
58                 }
59         return FALSE;
60 }
61
62
63 enum TiffByteOrder {
64         TIFF_BYTE_ORDER_INTEL,
65         TIFF_BYTE_ORDER_MOTOROLA
66 };
67
68 #define TIFF_TIFD_OFFSET_TAG 0
69 #define TIFF_TIFD_OFFSET_FORMAT 2
70 #define TIFF_TIFD_OFFSET_COUNT 4
71 #define TIFF_TIFD_OFFSET_DATA 8
72 #define TIFF_TIFD_SIZE 12
73
74
75
76 guint16 tiff_byte_get_int16(const guchar *f, TiffByteOrder bo)
77 {
78         guint16 align_buf;
79
80         memcpy(&align_buf, f, sizeof(guint16));
81
82         if (bo == TIFF_BYTE_ORDER_INTEL)
83                 return GUINT16_FROM_LE(align_buf);
84         else
85                 return GUINT16_FROM_BE(align_buf);
86 }
87
88 guint32 tiff_byte_get_int32(const guchar *f, TiffByteOrder bo)
89 {
90         guint32 align_buf;
91
92         memcpy(&align_buf, f, sizeof(guint32));
93
94         if (bo == TIFF_BYTE_ORDER_INTEL)
95                 return GUINT32_FROM_LE(align_buf);
96         else
97                 return GUINT32_FROM_BE(align_buf);
98 }
99
100 #pragma GCC diagnostic push
101 #pragma GCC diagnostic ignored "-Wunused-function"
102 void tiff_byte_put_int16_unused(guchar *f, guint16 n, TiffByteOrder bo)
103 {
104         guint16 align_buf;
105
106         if (bo == TIFF_BYTE_ORDER_INTEL)
107                 {
108                 align_buf = GUINT16_TO_LE(n);
109                 }
110         else
111                 {
112                 align_buf = GUINT16_TO_BE(n);
113                 }
114
115         memcpy(f, &align_buf, sizeof(guint16));
116 }
117
118 void tiff_byte_put_int32_unused(guchar *f, guint32 n, TiffByteOrder bo)
119 {
120         guint32 align_buf;
121
122         if (bo == TIFF_BYTE_ORDER_INTEL)
123                 {
124                 align_buf = GUINT32_TO_LE(n);
125                 }
126         else
127                 {
128                 align_buf = GUINT32_TO_BE(n);
129                 }
130
131         memcpy(f, &align_buf, sizeof(guint32));
132 }
133 #pragma GCC diagnostic pop
134
135 gint tiff_directory_offset(const guchar *data, const guint len,
136                                 guint *offset, TiffByteOrder *bo)
137 {
138         if (len < 8) return FALSE;
139
140         if (memcmp(data, "II", 2) == 0)
141                 {
142                 *bo = TIFF_BYTE_ORDER_INTEL;
143                 }
144         else if (memcmp(data, "MM", 2) == 0)
145                 {
146                 *bo = TIFF_BYTE_ORDER_MOTOROLA;
147                 }
148         else
149                 {
150                 return FALSE;
151                 }
152
153         if (tiff_byte_get_int16(data + 2, *bo) != 0x002A)
154                 {
155                 return FALSE;
156                 }
157
158         *offset = tiff_byte_get_int32(data + 4, *bo);
159
160         return (*offset < len);
161 }
162
163 using FuncParseIFDEntry = gint (*)(const guchar *, guint, guint, TiffByteOrder, gpointer);
164
165
166 gint tiff_parse_IFD_table(const guchar *tiff, guint offset,
167                           guint size, TiffByteOrder bo,
168                           guint *next_offset,
169                           FuncParseIFDEntry parse_entry, gpointer data)
170 {
171         guint count;
172         guint i;
173         guint next;
174
175
176         /* We should be able to read number of entries in IFD0) */
177         if (size < offset + 2) return -1;
178
179         count = tiff_byte_get_int16(tiff + offset, bo);
180         offset += 2;
181         /* Entries and next IFD offset must be readable */
182         if (size < offset + count * TIFF_TIFD_SIZE + 4) return -1;
183
184         for (i = 0; i < count; i++)
185                 {
186                 parse_entry(tiff, offset + i * TIFF_TIFD_SIZE, size, bo, data);
187                 }
188
189         next = tiff_byte_get_int32(tiff + offset + count * TIFF_TIFD_SIZE, bo);
190         if (next_offset) *next_offset = next;
191
192         return 0;
193 }
194
195 static gint mpo_parse_Index_IFD_entry(const guchar *tiff, guint offset,
196                                  guint size, TiffByteOrder bo,
197                                  gpointer data)
198 {
199         guint tag;
200         guint format;
201         guint count;
202         guint data_val;
203         guint data_offset;
204         guint data_length;
205
206         auto mpo = static_cast<MPOData *>(data);
207
208         tag = tiff_byte_get_int16(tiff + offset + TIFF_TIFD_OFFSET_TAG, bo);
209         format = tiff_byte_get_int16(tiff + offset + TIFF_TIFD_OFFSET_FORMAT, bo);
210         count = tiff_byte_get_int32(tiff + offset + TIFF_TIFD_OFFSET_COUNT, bo);
211         data_val = tiff_byte_get_int32(tiff + offset + TIFF_TIFD_OFFSET_DATA, bo);
212         DEBUG_1("   tag %x format %x count %x data_val %x", tag, format, count, data_val);
213
214         if (tag == 0xb000)
215                 {
216                 mpo->version = data_val;
217                 DEBUG_1("    mpo version %x", mpo->version);
218                 }
219         else if (tag == 0xb001)
220                 {
221                 mpo->num_images = data_val;
222                 DEBUG_1("    num images %x", mpo->num_images);
223                 }
224         else if (tag == 0xb002)
225                 {
226                 guint i;
227                 data_offset = data_val;
228                 data_length = count;
229                 if (size < data_offset || size < data_offset + data_length)
230                         {
231                         return -1;
232                         }
233                 if (count != mpo->num_images * 16)
234                         {
235                         return -1;
236                         }
237
238                 mpo->images = g_new0(MPOEntry, mpo->num_images);
239
240                 for (i = 0; i < mpo->num_images; i++) {
241                         guint image_attr = tiff_byte_get_int32(tiff + data_offset + i * 16, bo);
242                         mpo->images[i].type_code = image_attr & 0xffffff;
243                         mpo->images[i].representative = !!(image_attr & 0x20000000);
244                         mpo->images[i].dependent_child = !!(image_attr & 0x40000000);
245                         mpo->images[i].dependent_parent = !!(image_attr & 0x80000000);
246                         mpo->images[i].length = tiff_byte_get_int32(tiff + data_offset + i * 16 + 4, bo);
247                         mpo->images[i].offset = tiff_byte_get_int32(tiff + data_offset + i * 16 + 8, bo);
248                         mpo->images[i].dep1 = tiff_byte_get_int16(tiff + data_offset + i * 16 + 12, bo);
249                         mpo->images[i].dep2 = tiff_byte_get_int16(tiff + data_offset + i * 16 + 14, bo);
250
251                         if (i == 0)
252                                 {
253                                 mpo->images[i].offset = 0;
254                                 }
255                         else
256                                 {
257                                 mpo->images[i].offset += mpo->mpo_offset;
258                                 }
259
260                         DEBUG_1("   image %x %x %x", image_attr, mpo->images[i].length, mpo->images[i].offset);
261                         }
262                 }
263
264         return 0;
265 }
266
267 static gint mpo_parse_Attributes_IFD_entry(const guchar *tiff, guint offset,
268                                  guint, TiffByteOrder bo,
269                                  gpointer data)
270 {
271         guint tag;
272         guint format;
273         guint count;
274         guint data_val;
275
276         auto mpe = static_cast<MPOEntry *>(data);
277
278         tag = tiff_byte_get_int16(tiff + offset + TIFF_TIFD_OFFSET_TAG, bo);
279         format = tiff_byte_get_int16(tiff + offset + TIFF_TIFD_OFFSET_FORMAT, bo);
280         count = tiff_byte_get_int32(tiff + offset + TIFF_TIFD_OFFSET_COUNT, bo);
281         data_val = tiff_byte_get_int32(tiff + offset + TIFF_TIFD_OFFSET_DATA, bo);
282         DEBUG_1("   tag %x format %x count %x data_val %x", tag, format, count, data_val);
283
284         switch (tag)
285                 {
286                 case 0xb000:
287                         mpe->MPFVersion = data_val;
288                         DEBUG_1("    mpo version %x", data_val);
289                         break;
290                 case 0xb101:
291                         mpe->MPIndividualNum = data_val;
292                         DEBUG_1("    Individual Image Number %x", mpe->MPIndividualNum);
293                         break;
294                 case 0xb201:
295                         mpe->PanOrientation = data_val;
296                         break;
297 /**
298
299 @FIXME
300 Panorama Scanning Orientation PanOrientation 45569 B201 LONG 1 \n
301 Panorama Horizontal Overlap PanOverlap_H 45570 B202 RATIONAL 1 \n
302 Panorama Vertical Overlap PanOverlap_V 45571 B203 RATIONAL 1 \n
303 Base Viewpoint Number BaseViewpointNum 45572 B204 LONG 1 \n
304 Convergence Angle ConvergenceAngle 45573 B205 SRATIONAL 1 \n
305 Baseline Length BaselineLength 45574 B206 RATIONAL 1 \n
306 Divergence Angle VerticalDivergence 45575 B207 SRATIONAL 1 \n
307 Horizontal Axis Distance AxisDistance_X 45576 B208 SRATIONAL 1 \n
308 Vertical Axis Distance AxisDistance_Y 45577 B209 SRATIONAL 1 \n
309 Collimation Axis Distance AxisDistance_Z 45578 B20A SRATIONAL 1 \n
310 Yaw Angle YawAngle 45579 B20B SRATIONAL 1 \n
311 Pitch Angle PitchAngle 45580 B20C SRATIONAL 1 \n
312 Roll Angle RollAngle 45581 B20D
313   */
314                 default:
315                         break;
316                 }
317
318         return 0;
319 }
320
321 MPOData *jpeg_get_mpo_data(const guchar *data, guint size)
322 {
323         guint seg_offset;
324         guint seg_size;
325         if (jpeg_segment_find(data, size, JPEG_MARKER_APP2, "MPF\x00", 4, &seg_offset, &seg_size) && seg_size >16)
326                 {
327                 guint offset;
328                 guint next_offset = 0;
329                 TiffByteOrder bo;
330                 MPOData *mpo;
331                 guint i;
332
333                 DEBUG_1("mpo signature found at %x", seg_offset);
334                 seg_offset += 4;
335                 seg_size -= 4;
336
337                 if (!tiff_directory_offset(data + seg_offset, seg_size, &offset, &bo)) return nullptr;
338
339                 mpo = g_new0(MPOData, 1);
340                 mpo->mpo_offset = seg_offset;
341
342                 tiff_parse_IFD_table(data + seg_offset,  offset , seg_size, bo, &next_offset, mpo_parse_Index_IFD_entry, mpo);
343                 if (!mpo->images) mpo->num_images = 0;
344
345
346                 for (i = 0; i < mpo->num_images; i++)
347                         {
348                         if (mpo->images[i].offset + mpo->images[i].length > size)
349                                 {
350                                 mpo->num_images = i;
351                                 DEBUG_1("MPO file truncated to %d valid images, %d %d", i, mpo->images[i].offset + mpo->images[i].length, size);
352                                 break;
353                                 }
354                         }
355
356                 for (i = 0; i < mpo->num_images; i++)
357                         {
358                         if (i == 0)
359                                 {
360                                 offset = next_offset;
361                                 }
362                         else
363                                 {
364                                 if (!jpeg_segment_find(data + mpo->images[i].offset, mpo->images[i].length, JPEG_MARKER_APP2, "MPF\x00", 4, &seg_offset, &seg_size) || seg_size <=16)
365                                         {
366                                         DEBUG_1("MPO image %d: MPO signature not found", i);
367                                         continue;
368                                         }
369
370                                 seg_offset += 4;
371                                 seg_size -= 4;
372                                 if (!tiff_directory_offset(data + mpo->images[i].offset + seg_offset, seg_size, &offset, &bo))
373                                         {
374                                         DEBUG_1("MPO image %d: invalid directory offset", i);
375                                         continue;
376                                         }
377
378                                 }
379                         tiff_parse_IFD_table(data + mpo->images[i].offset + seg_offset,  offset , seg_size, bo, nullptr, mpo_parse_Attributes_IFD_entry, &mpo->images[i]);
380                         }
381
382                 return mpo;
383                 }
384         return nullptr;
385 }
386
387 void jpeg_mpo_data_free(MPOData *mpo)
388 {
389         if (mpo)
390                 {
391                 if (mpo->images) g_free(mpo->images);
392                 g_free(mpo);
393                 }
394 }
395
396
397 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */