Coverage Report

Created: 2025-11-14 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libraw/src/metadata/makernotes.cpp
Line
Count
Source
1
/* -*- C++ -*-
2
 * Copyright 2019-2025 LibRaw LLC (info@libraw.org)
3
 *
4
 LibRaw uses code from dcraw.c -- Dave Coffin's raw photo decoder,
5
 dcraw.c is copyright 1997-2018 by Dave Coffin, dcoffin a cybercom o net.
6
 LibRaw do not use RESTRICTED code from dcraw.c
7
8
 LibRaw is free software; you can redistribute it and/or modify
9
 it under the terms of the one of two licenses as you choose:
10
11
1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
12
   (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
13
14
2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
15
   (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
16
17
 */
18
19
#include "../../internal/dcraw_defs.h"
20
21
1.78k
void LibRaw::parseSigmaMakernote (INT64 base, int uptag, unsigned /*dng_writer*/) {
22
1.78k
unsigned wb_table1 [] = {
23
1.78k
  LIBRAW_WBI_Auto, LIBRAW_WBI_Daylight, LIBRAW_WBI_Shade, LIBRAW_WBI_Cloudy,
24
1.78k
  LIBRAW_WBI_Tungsten, LIBRAW_WBI_Fluorescent, LIBRAW_WBI_Flash,
25
1.78k
  LIBRAW_WBI_Custom, LIBRAW_WBI_Custom1, LIBRAW_WBI_Custom2
26
1.78k
};
27
28
1.78k
  unsigned entries, tag, type, len;
29
1.78k
  INT64 save;
30
1.78k
  unsigned i;
31
32
1.78k
  entries = get2();
33
1.78k
  if (entries > 1000)
34
355
    return;
35
56.6k
  while (entries--) {
36
55.2k
    tiff_get(base, &tag, &type, &len, &save);
37
38
55.2k
  if (callbacks.makernotes_cb)
39
0
    {
40
0
      INT64 _savepos = ifp->tell();
41
0
      callbacks.makernotes_cb(callbacks.makernotesparser_data, tag | (uptag << 16), type, len, order, ifp, base);
42
0
      fseek(ifp, _savepos, SEEK_SET);
43
0
    }
44
45
55.2k
    if (tag == 0x0027) {
46
338
      ilm.LensID = get2();
47
54.9k
    } else if (tag == 0x002a) {
48
1.43k
      ilm.MinFocal = getrealf(type);
49
1.43k
      ilm.MaxFocal = getrealf(type);
50
53.4k
    } else if (tag == 0x002b) {
51
281
      ilm.MaxAp4MinFocal = getrealf(type);
52
281
      ilm.MaxAp4MaxFocal = getrealf(type);
53
53.1k
    } else if (tag == 0x0120) {
54
1.07k
      const unsigned tblsz = (sizeof wb_table1 / sizeof wb_table1[0]);
55
1.07k
      if ((len >= tblsz) && (len%3 == 0) && len/3 <= tblsz) {
56
1.29k
        for (i=0; i<(len/3); i++) {
57
1.12k
          icWBC[wb_table1[i]][0] = (int)(getreal(type)*10000.0);
58
1.12k
          icWBC[wb_table1[i]][1] = icWBC[wb_table1[i]][3] = (int)(getreal(type)*10000.0);
59
1.12k
          icWBC[wb_table1[i]][2] = (int)(getreal(type)*10000.0);
60
1.12k
        }
61
168
      }
62
1.07k
    }
63
55.2k
    fseek(ifp, save, SEEK_SET);
64
55.2k
  }
65
66
1.42k
  return;
67
1.78k
}
68
69
void LibRaw::parse_makernote_0xc634(INT64 base, int uptag, unsigned dng_writer)
70
61.5k
{
71
72
61.5k
  if (metadata_blocks++ > LIBRAW_MAX_METADATA_BLOCKS)
73
1
    throw LIBRAW_EXCEPTION_IO_CORRUPT;
74
75
61.5k
  if (!strncmp(make, "NIKON", 5))
76
727
  {
77
727
    parseNikonMakernote(base, uptag, AdobeDNG);
78
727
    return;
79
727
  }
80
60.7k
  else if (!strncasecmp(make, "LEICA", 5))
81
405
  {
82
405
    parseLeicaMakernote(base, uptag, is_0xc634);
83
405
    return;
84
405
  }
85
86
60.3k
  short morder, sorder = order;
87
60.3k
  char buf[10];
88
60.3k
  INT64 fsize = ifp->size();
89
90
60.3k
  fread(buf, 1, 10, ifp);
91
92
60.3k
  if (!strcmp(buf, "EPSON"))
93
83
  {
94
83
    parseEpsonMakernote(base, uptag, AdobeDNG);
95
83
    return;
96
83
  }
97
60.2k
  else if (!strcmp(buf, "SIGMA"))
98
79
  {
99
79
    parseSigmaMakernote(base, uptag, AdobeDNG);
100
79
    return;
101
79
  }
102
103
60.2k
  unsigned entries, tag, type, len, c;
104
60.2k
  INT64 save;
105
106
60.2k
  uchar *CanonCameraInfo = NULL;
107
60.2k
  unsigned lenCanonCameraInfo = 0;
108
60.2k
  unsigned typeCanonCameraInfo = 0;
109
110
60.2k
  uchar *table_buf_0x0116;
111
60.2k
  ushort table_buf_0x0116_len = 0;
112
60.2k
  uchar *table_buf_0x2010;
113
60.2k
  ushort table_buf_0x2010_len = 0;
114
60.2k
  uchar *table_buf_0x9050;
115
60.2k
  ushort table_buf_0x9050_len = 0;
116
60.2k
  uchar *table_buf_0x9400;
117
60.2k
  ushort table_buf_0x9400_len = 0;
118
60.2k
  uchar *table_buf_0x9402;
119
60.2k
  ushort table_buf_0x9402_len = 0;
120
60.2k
  uchar *table_buf_0x9403;
121
60.2k
  ushort table_buf_0x9403_len = 0;
122
60.2k
  uchar *table_buf_0x9406;
123
60.2k
  ushort table_buf_0x9406_len = 0;
124
60.2k
  uchar *table_buf_0x940c;
125
60.2k
  ushort table_buf_0x940c_len = 0;
126
60.2k
  uchar *table_buf_0x940e;
127
60.2k
  ushort table_buf_0x940e_len = 0;
128
129
60.2k
  if (!strcmp(buf, "OLYMPUS") || !strcmp(buf, "PENTAX ") || !strncmp(buf,"OM SYS",6)||
130
36.0k
      (!strncmp(make, "SAMSUNG", 7) && (dng_writer == CameraDNG)))
131
24.9k
  {
132
24.9k
    base = ftell(ifp) - 10;
133
24.9k
    fseek(ifp, -2, SEEK_CUR);
134
24.9k
    order = get2();
135
24.9k
    if (buf[0] == 'O')
136
510
      get2();
137
24.4k
    else if (buf[0] == 'P')
138
23.6k
      is_PentaxRicohMakernotes = 1;
139
24.9k
  }
140
35.2k
  else if (is_PentaxRicohMakernotes && (dng_writer == CameraDNG))
141
13.2k
  {
142
13.2k
    base = ftell(ifp) - 10;
143
13.2k
    fseek(ifp, -4, SEEK_CUR);
144
13.2k
    order = get2();
145
13.2k
  }
146
22.0k
  else if (!strncmp(buf, "SONY", 4) ||
147
20.8k
           !strcmp(buf, "Panasonic"))
148
1.40k
  {
149
1.40k
    order = 0x4949;
150
1.40k
    fseek(ifp, 2, SEEK_CUR);
151
1.40k
  }
152
20.6k
  else if (!strncmp(buf, "FUJIFILM", 8))
153
93
  {
154
93
    base = ftell(ifp) - 10;
155
93
    order = 0x4949;
156
93
    fseek(ifp, 2, SEEK_CUR);
157
93
  }
158
20.5k
  else if (!strcmp(buf, "OLYMP") ||
159
20.3k
           !strcmp(buf, "Ricoh"))
160
294
  {
161
294
    fseek(ifp, -2, SEEK_CUR);
162
294
  }
163
20.2k
  else if (!strcmp(buf, "AOC") || !strcmp(buf, "QVC"))
164
1.18k
  {
165
1.18k
    fseek(ifp, -4, SEEK_CUR);
166
1.18k
  }
167
19.0k
  else
168
19.0k
  {
169
19.0k
    fseek(ifp, -10, SEEK_CUR);
170
19.0k
    if ((!strncmp(make, "SAMSUNG", 7) && (dng_writer == AdobeDNG)))
171
455
      base = ftell(ifp);
172
19.0k
  }
173
174
60.2k
  entries = get2();
175
60.2k
  if (entries > 1000)
176
8.33k
    return;
177
178
51.8k
  if (!strncasecmp(make, "SONY", 4) ||
179
47.1k
      !strncasecmp(make, "Konica", 6) ||
180
46.7k
      !strncasecmp(make, "Minolta", 7) ||
181
44.8k
      (!strncasecmp(make, "Hasselblad", 10) &&
182
2.61k
       (!strncasecmp(model, "Stellar", 7) ||
183
2.36k
        !strncasecmp(model, "Lunar", 5) ||
184
2.12k
        !strncasecmp(model, "Lusso", 5) ||
185
2.03k
        !strncasecmp(model, "HV", 2))))
186
7.64k
    is_Sony = 1;
187
188
51.8k
  if (!is_Olympus &&
189
31.6k
      (!strncmp(make, "OLYMPUS", 7) || !strncmp(make, "OM Digi", 7) ||
190
30.3k
      (!strncasecmp(make, "CLAUSS", 6) && !strncasecmp(model, "piX 5oo", 7)))) {
191
1.26k
    is_Olympus = 1;
192
1.26k
    OlympusDNG_SubDirOffsetValid =
193
1.26k
          strncmp(model, "E-300", 5) && strncmp(model, "E-330", 5) &&
194
1.20k
          strncmp(model, "E-400", 5) && strncmp(model, "E-500", 5) &&
195
1.19k
          strncmp(model, "E-1", 3);
196
1.26k
  }
197
198
51.8k
  morder = order;
199
8.74M
  while (entries--)
200
8.69M
  {
201
8.69M
    order = morder;
202
203
8.69M
    tiff_get(base, &tag, &type, &len, &save);
204
205
8.69M
    INT64 pos = ifp->tell();
206
8.69M
    if (len > 8 && pos + len > 2 * fsize)
207
7.32M
    {
208
7.32M
      fseek(ifp, save, SEEK_SET); // Recover tiff-read position!!
209
7.32M
      continue;
210
7.32M
    }
211
1.37M
    tag |= uptag << 16;
212
1.37M
    if (len > 100 * 1024 * 1024)
213
0
      goto next; // 100Mb tag? No!
214
215
1.37M
  if (callbacks.makernotes_cb)
216
0
    {
217
0
      INT64 _savepos = ifp->tell();
218
0
      callbacks.makernotes_cb(callbacks.makernotesparser_data, tag, type, len, order, ifp, base);
219
0
      fseek(ifp, _savepos, SEEK_SET);
220
0
    }
221
222
1.37M
    if (!strncmp(make, "Canon", 5))
223
165k
    {
224
165k
      if (tag == 0x000d && len < 256000)
225
3.24k
      { // camera info
226
3.24k
        if (!tagtypeIs(LIBRAW_EXIFTAG_TYPE_LONG))
227
1.36k
        {
228
1.36k
          CanonCameraInfo = (uchar *)calloc(MAX(16, len),1);
229
1.36k
          fread(CanonCameraInfo, len, 1, ifp);
230
1.36k
        }
231
1.87k
        else
232
1.87k
        {
233
1.87k
          CanonCameraInfo = (uchar *)calloc(MAX(16, len * 4),1);
234
1.87k
          fread(CanonCameraInfo, len, 4, ifp);
235
1.87k
        }
236
3.24k
        lenCanonCameraInfo = len;
237
3.24k
        typeCanonCameraInfo = type;
238
3.24k
      }
239
240
162k
      else if (tag == 0x0010) // Canon ModelID
241
3.55k
      {
242
3.55k
        unique_id = get4();
243
3.55k
        setCanonBodyFeatures(unique_id);
244
3.55k
        if (lenCanonCameraInfo)
245
2.02k
        {
246
2.02k
          processCanonCameraInfo(unique_id, CanonCameraInfo, lenCanonCameraInfo,
247
2.02k
                                 typeCanonCameraInfo, AdobeDNG);
248
2.02k
          free(CanonCameraInfo);
249
2.02k
          CanonCameraInfo = 0;
250
2.02k
          lenCanonCameraInfo = 0;
251
2.02k
        }
252
3.55k
      }
253
254
158k
      else
255
158k
        parseCanonMakernotes(tag, type, len, AdobeDNG);
256
165k
    }
257
258
1.20M
    else if (!strncmp(make, "FUJI", 4)) {
259
1.39k
      parseFujiMakernotes(tag, type, len, AdobeDNG);
260
261
1.20M
    } else if (!strncasecmp(make, "Hasselblad", 10) && !is_Sony) {
262
86.1k
      if (tag == 0x0011) {
263
176
        imHassy.SensorCode = getint(type);
264
85.9k
      } else if ((tag == 0x0015) && tagtypeIs(LIBRAW_EXIFTAG_TYPE_ASCII)) {
265
986
        stmread (imHassy.SensorUnitConnector, len, ifp);
266
4.37k
        for (int i=0; i<(int)len; i++) {
267
4.12k
          if(!isalnum(imHassy.SensorUnitConnector[i]) &&
268
1.93k
             (imHassy.SensorUnitConnector[i]!=' ')    &&
269
1.63k
             (imHassy.SensorUnitConnector[i]!='/')    &&
270
1.27k
             (imHassy.SensorUnitConnector[i]!='-')) {
271
740
            imHassy.SensorUnitConnector[0] = 0;
272
740
            break;
273
740
          }
274
4.12k
        }
275
84.9k
      } else if (tag == 0x0016) {
276
238
        imHassy.CoatingCode = getint(type);
277
84.7k
      } else if ((tag == 0x002a) &&
278
784
                 tagtypeIs(LIBRAW_EXIFTAG_TYPE_SRATIONAL) &&
279
416
                 (len == 12) &&
280
91
                 imHassy.SensorUnitConnector[0]) {
281
880
        FORC4 for (int i = 0; i < 3; i++)
282
660
                imHassy.mnColorMatrix[c][i] = getreal(type);
283
284
84.6k
      } else if ((tag == 0x0031) &&
285
415
                 imHassy.SensorUnitConnector[0]) {
286
43
        imHassy.RecommendedCrop[0] = getint(type);
287
43
        imHassy.RecommendedCrop[1] = getint(type);
288
43
      }
289
290
1.12M
    } else if (is_Olympus) {
291
292
763k
      if ((tag == 0x2010) || (tag == 0x2020) || (tag == 0x2030) ||
293
753k
          (tag == 0x2031) || (tag == 0x2040) || (tag == 0x2050) ||
294
748k
          (tag == 0x3000))
295
15.9k
      {
296
15.9k
        fseek(ifp, save - 4, SEEK_SET);
297
15.9k
        fseek(ifp, base + get4(), SEEK_SET);
298
15.9k
        parse_makernote_0xc634(base, tag, dng_writer);
299
15.9k
      }
300
301
763k
      if (!OlympusDNG_SubDirOffsetValid &&
302
56.4k
          ((len > 4) ||
303
47.4k
           ((tagtypeIs(LIBRAW_EXIFTAG_TYPE_SHORT) ||
304
46.4k
            tagtypeIs(LIBRAW_EXIFTAG_TYPE_SSHORT)) && (len > 2)) ||
305
47.0k
           ((tagtypeIs(LIBRAW_EXIFTAG_TYPE_LONG) ||
306
46.2k
             tagtypeIs(LIBRAW_EXIFTAG_TYPE_SLONG)) && (len > 1)) ||
307
46.6k
           tagtypeIs(LIBRAW_EXIFTAG_TYPE_RATIONAL) ||
308
46.2k
           (type > LIBRAW_EXIFTAG_TYPE_SLONG))) {
309
26.0k
        goto skip_Oly_broken_tags;
310
26.0k
      }
311
737k
      else {
312
737k
        parseOlympusMakernotes(base, tag, type, len, AdobeDNG);
313
737k
      }
314
763k
    skip_Oly_broken_tags:;
315
763k
    }
316
317
357k
    else if (!strncmp(make, "PENTAX", 6)  ||
318
351k
             !strncmp(model, "PENTAX", 6) ||
319
350k
             is_PentaxRicohMakernotes)
320
181k
    {
321
181k
      parsePentaxMakernotes(base, tag, type, len, dng_writer);
322
181k
    }
323
176k
    else if (!strncmp(make, "SAMSUNG", 7))
324
15.0k
    {
325
15.0k
      if (dng_writer == AdobeDNG)
326
12.6k
        parseSamsungMakernotes(base, tag, type, len, dng_writer);
327
2.38k
      else
328
2.38k
        parsePentaxMakernotes(base, tag, type, len, dng_writer);
329
15.0k
    }
330
161k
    else if (is_Sony)
331
117k
    {
332
117k
      parseSonyMakernotes(
333
117k
          base, tag, type, len, AdobeDNG,
334
117k
          table_buf_0x0116, table_buf_0x0116_len,
335
117k
          table_buf_0x2010, table_buf_0x2010_len,
336
117k
          table_buf_0x9050, table_buf_0x9050_len,
337
117k
          table_buf_0x9400, table_buf_0x9400_len,
338
117k
          table_buf_0x9402, table_buf_0x9402_len,
339
117k
          table_buf_0x9403, table_buf_0x9403_len,
340
117k
          table_buf_0x9406, table_buf_0x9406_len,
341
117k
          table_buf_0x940c, table_buf_0x940c_len,
342
117k
          table_buf_0x940e, table_buf_0x940e_len);
343
117k
    }
344
1.37M
  next:
345
1.37M
    fseek(ifp, save, SEEK_SET);
346
1.37M
  }
347
348
50.3k
  order = sorder;
349
50.3k
}
350
351
void LibRaw::parse_makernote(INT64 base, int uptag)
352
656k
{
353
354
656k
  if (metadata_blocks++ > LIBRAW_MAX_METADATA_BLOCKS)
355
17
    throw LIBRAW_EXCEPTION_IO_CORRUPT;
356
357
656k
  if (!strncmp(make, "NIKON", 5))
358
47.4k
  {
359
47.4k
    parseNikonMakernote(base, uptag, nonDNG);
360
47.4k
    return;
361
47.4k
  }
362
609k
  else if (!strncasecmp(make, "LEICA", 5))
363
29.5k
  {
364
29.5k
    parseLeicaMakernote(base, uptag, is_0x927c);
365
29.5k
    return;
366
29.5k
  }
367
368
579k
  if (!strncmp(make, "Nokia", 5))
369
1.28k
    return;
370
371
578k
  char buf[10];
372
578k
  char another_buf[128];
373
374
578k
  fseek(ifp, -12, SEEK_CUR);
375
578k
  fread (another_buf, 1, 12, ifp);
376
578k
  if (!strncmp(another_buf, "SONY", 4) ||
377
575k
      !strncmp(another_buf, "VHAB", 4)) { // Sony branded as Hasselblad
378
4.49k
    is_Sony = 1;
379
4.49k
  }
380
381
578k
  fread(buf, 1, 10, ifp);
382
383
578k
  if (!strncmp(buf, "KDK", 3)  || /* these aren't TIFF tables */
384
577k
      !strncmp(buf, "VER", 3)  ||
385
577k
      !strncmp(buf, "IIII", 4) ||
386
576k
      !strncmp(buf, "MMMM", 4))
387
2.09k
    return;
388
389
576k
  if (!strcmp(buf, "EPSON"))
390
2.95k
  {
391
2.95k
    parseEpsonMakernote(base, uptag, nonDNG);
392
2.95k
    return;
393
2.95k
  }
394
573k
  else if (!strcmp(buf, "SIGMA"))
395
1.70k
  {
396
1.70k
    parseSigmaMakernote(base, uptag, CameraDNG);
397
1.70k
    return;
398
1.70k
  }
399
400
401
571k
  unsigned entries, tag, type, len, c;
402
571k
  INT64 save;
403
571k
  unsigned wb[4] = {0, 0, 0, 0};
404
571k
  short morder, sorder = order;
405
406
571k
  uchar *CanonCameraInfo = 0;;
407
571k
  unsigned lenCanonCameraInfo = 0;
408
571k
  unsigned typeCanonCameraInfo = 0;
409
571k
  imCanon.wbi = 0;
410
411
571k
  uchar *table_buf_0x0116;
412
571k
  ushort table_buf_0x0116_len = 0;
413
571k
  uchar *table_buf_0x2010;
414
571k
  ushort table_buf_0x2010_len = 0;
415
571k
  uchar *table_buf_0x9050;
416
571k
  ushort table_buf_0x9050_len = 0;
417
571k
  uchar *table_buf_0x9400;
418
571k
  ushort table_buf_0x9400_len = 0;
419
571k
  uchar *table_buf_0x9402;
420
571k
  ushort table_buf_0x9402_len = 0;
421
571k
  uchar *table_buf_0x9403;
422
571k
  ushort table_buf_0x9403_len = 0;
423
571k
  uchar *table_buf_0x9406;
424
571k
  ushort table_buf_0x9406_len = 0;
425
571k
  uchar *table_buf_0x940c;
426
571k
  ushort table_buf_0x940c_len = 0;
427
571k
  uchar *table_buf_0x940e;
428
571k
  ushort table_buf_0x940e_len = 0;
429
430
571k
  INT64 fsize = ifp->size();
431
432
  /*
433
       The MakerNote might have its own TIFF header (possibly with
434
       its own byte-order!), or it might just be a table.
435
  */
436
437
571k
  if (!strncmp(buf, "KC", 2) || /* Konica KD-400Z, KD-510Z */
438
570k
      !strncmp(buf, "MLY", 3))  /* Minolta DiMAGE G series */
439
3.45k
  {
440
3.45k
    order = 0x4d4d;
441
3.45k
  INT64 ii;
442
2.61M
    while ((ii = ftell(ifp)) < data_offset && ii < 16384LL)
443
2.61M
    {
444
2.61M
      wb[0] = wb[2];
445
2.61M
      wb[2] = wb[1];
446
2.61M
      wb[1] = wb[3];
447
2.61M
    if (feof(ifp))
448
1.90k
      break;
449
2.61M
      wb[3] = get2();
450
2.61M
      if (wb[1] == 256 && wb[3] == 256 && wb[0] > 256 && wb[0] < 640 &&
451
2.81k
          wb[2] > 256 && wb[2] < 640)
452
4.45k
        FORC4 cam_mul[c] = float(wb[c]);
453
2.61M
    }
454
3.45k
    goto quit;
455
3.45k
  }
456
457
568k
  if (!strcmp(buf, "OLYMPUS") || !strncmp(buf, "OM SYS",6) ||
458
567k
      !strcmp(buf, "PENTAX "))
459
1.05k
  {
460
1.05k
    base = ftell(ifp) - 10;
461
1.05k
    fseek(ifp, -2, SEEK_CUR);
462
1.05k
  if (buf[1] == 'M')
463
241
    get4();
464
1.05k
    order = get2();
465
1.05k
    if (buf[0] == 'O')
466
536
      get2();
467
1.05k
  }
468
567k
  else if (!strncmp(buf, "SONY", 4) || // DSLR-A100
469
562k
           !strcmp(buf, "Panasonic")) {
470
6.40k
    if (buf[0] == 'S')
471
5.07k
      is_Sony = 1;
472
6.40k
    goto nf;
473
6.40k
  }
474
560k
  else if (!strncmp(buf, "FUJIFILM", 8))
475
760
  {
476
760
    base = ftell(ifp) - 10;
477
7.16k
  nf:
478
7.16k
    order = 0x4949;
479
7.16k
    fseek(ifp, 2, SEEK_CUR);
480
7.16k
  }
481
559k
  else if (!strcmp (buf, "OLYMP")    ||
482
559k
           !strncmp(buf, "LEICA", 5) ||
483
556k
           !strcmp (buf, "Ricoh"))
484
3.79k
  {
485
3.79k
    fseek(ifp, -2, SEEK_CUR);
486
3.79k
  }
487
556k
  else if (!strcmp(buf, "AOC") || // Pentax, tribute to Asahi Optical Co.
488
555k
           !strcmp(buf, "QVC"))   // Casio, from "QV-Camera"
489
1.10k
  {
490
1.10k
    fseek(ifp, -4, SEEK_CUR);
491
1.10k
  }
492
555k
  else if (!strncmp(buf, "CMT3", 4))
493
717
  {
494
717
    order = sget2((uchar *)(buf + 4));
495
717
    fseek(ifp, 2L, SEEK_CUR);
496
717
  }
497
554k
  else if (libraw_internal_data.unpacker_data.CR3_CTMDtag)
498
0
  {
499
0
    order = sget2((uchar *)buf);
500
0
    fseek(ifp, -2L, SEEK_CUR);
501
0
  }
502
554k
  else
503
554k
  {
504
554k
    fseek(ifp, -10, SEEK_CUR);
505
554k
    if (!strncmp(make, "SAMSUNG", 7))
506
38.6k
      base = ftell(ifp);
507
554k
  }
508
509
568k
  if (!is_Olympus &&
510
455k
      (!strncasecmp(make, "Olympus", 7) || !strncmp(make, "OM Digi", 7) ||
511
452k
      (!strncasecmp(make, "CLAUSS", 6) && !strncasecmp(model, "piX 5oo", 7)))) {
512
2.23k
    is_Olympus = 1;
513
2.23k
  }
514
515
568k
  if (!is_Sony &&
516
330k
      (!strncasecmp(make, "SONY", 4) ||
517
326k
       !strncasecmp(make, "Konica", 6) ||
518
325k
       !strncasecmp(make, "Minolta", 7) ||
519
325k
       (!strncasecmp(make, "Hasselblad", 10) &&
520
6.13k
        (!strncasecmp(model, "Stellar", 7) ||
521
6.13k
         !strncasecmp(model, "Lunar", 5) ||
522
6.13k
         !strncasecmp(model, "Lusso", 5) ||
523
6.12k
         !strncasecmp(model, "HV", 2))))) {
524
4.89k
    is_Sony = 1;
525
4.89k
  }
526
527
568k
  if (strcasestr(make, "Kodak") &&
528
60.5k
      (sget2((uchar *)buf) > 1) && // check number of entries
529
57.5k
      (sget2((uchar *)buf) < 128) &&
530
46.8k
      (sget2((uchar *)(buf + 4)) > 0) && // check type
531
38.8k
      (sget2((uchar *)(buf + 4)) < 13) &&
532
7.81k
      (sget4((uchar *)(buf + 6)) < 256) // check count
533
568k
  )
534
1.41k
    imKodak.MakerNoteKodak8a = 1; // Kodak P712 / P850 / P880
535
536
568k
  entries = get2();
537
568k
  if (entries > 1000)
538
55.3k
    return;
539
540
512k
  morder = order;
541
40.7M
  while (entries--)
542
40.2M
  {
543
40.2M
    order = morder;
544
40.2M
    tiff_get(base, &tag, &type, &len, &save);
545
40.2M
    tag |= uptag << 16;
546
547
40.2M
    INT64 _pos = ftell(ifp);
548
40.2M
    if (len > 100 * 1024 * 1024)
549
20.1M
  goto next; // 100Mb tag? No!
550
20.0M
    if (len > 8 && _pos + len > 2 * fsize)
551
13.9M
    {
552
13.9M
      fseek(ifp, save, SEEK_SET); // Recover tiff-read position!!
553
13.9M
      continue;
554
13.9M
    }
555
556
6.11M
  if (callbacks.makernotes_cb)
557
0
    {
558
0
      INT64 _savepos = ifp->tell();
559
0
      callbacks.makernotes_cb(callbacks.makernotesparser_data, tag, type, len, order, ifp, base);
560
0
      fseek(ifp, _savepos, SEEK_SET);
561
0
    }
562
563
6.11M
    if (imKodak.MakerNoteKodak8a)
564
59.1k
    {
565
59.1k
      if ((tag == 0xff00) && tagtypeIs(LIBRAW_EXIFTAG_TYPE_LONG) && (len == 1))
566
1.93k
      {
567
1.93k
        INT64 _pos1 = get4();
568
1.93k
        if ((_pos1 < fsize) && (_pos1 > 0))
569
626
        {
570
626
          fseek(ifp, _pos1, SEEK_SET);
571
626
          parse_makernote(base, tag);
572
626
        }
573
1.93k
      }
574
57.2k
      else if (tag == 0xff00f90b)
575
259
      {
576
259
        imKodak.clipBlack = get2();
577
259
      }
578
56.9k
      else if (tag == 0xff00f90c)
579
262
      {
580
262
        imKodak.clipWhite = imgdata.color.linear_max[0] =
581
262
            imgdata.color.linear_max[1] = imgdata.color.linear_max[2] =
582
262
                imgdata.color.linear_max[3] = get2();
583
262
      }
584
59.1k
    }
585
6.05M
    else if (!strncmp(make, "Canon", 5))
586
1.55M
    {
587
1.55M
      if (tag == 0x000d && len < 256000) // camera info
588
34.4k
      {
589
34.4k
        if (!tagtypeIs(LIBRAW_EXIFTAG_TYPE_LONG))
590
14.2k
        {
591
14.2k
          CanonCameraInfo = (uchar *)calloc(MAX(16, len),1);
592
14.2k
          fread(CanonCameraInfo, len, 1, ifp);
593
14.2k
        }
594
20.2k
        else
595
20.2k
        {
596
20.2k
          CanonCameraInfo = (uchar *)calloc(MAX(16, len * 4),1);
597
20.2k
          fread(CanonCameraInfo, len, 4, ifp);
598
20.2k
        }
599
34.4k
        lenCanonCameraInfo = len;
600
34.4k
        typeCanonCameraInfo = type;
601
34.4k
      }
602
603
1.51M
      else if (tag == 0x0010) // Canon ModelID
604
52.9k
      {
605
52.9k
        unique_id = get4();
606
52.9k
        setCanonBodyFeatures(unique_id);
607
52.9k
        if (lenCanonCameraInfo)
608
24.0k
        {
609
24.0k
          processCanonCameraInfo(unique_id, CanonCameraInfo, lenCanonCameraInfo,
610
24.0k
                                 typeCanonCameraInfo, nonDNG);
611
24.0k
    if(CanonCameraInfo)
612
24.0k
            free(CanonCameraInfo);
613
24.0k
          CanonCameraInfo = 0;
614
24.0k
          lenCanonCameraInfo = 0;
615
24.0k
        }
616
52.9k
      }
617
618
1.46M
      else
619
1.46M
        parseCanonMakernotes(tag, type, len, nonDNG);
620
1.55M
    }
621
622
4.50M
    else if (!strncmp(make, "FUJI", 4))
623
191k
      parseFujiMakernotes(tag, type, len, nonDNG);
624
625
4.31M
    else if (!strncasecmp(model, "Hasselblad X1D", 14) ||
626
4.31M
             !strncasecmp(model, "Hasselblad H6D", 14) ||
627
4.29M
             !strncasecmp(model, "Hasselblad A6D", 14))
628
19.3k
    {
629
19.3k
      if (tag == 0x0045)
630
141
      {
631
141
        imHassy.BaseISO = get4();
632
141
      }
633
19.1k
      else if (tag == 0x0046)
634
312
      {
635
312
        imHassy.Gain = getreal(type);
636
312
      }
637
19.3k
    }
638
639
4.29M
    else if (!strncmp(make, "PENTAX", 6) ||
640
3.90M
             !strncmp(make, "RICOH", 5) ||
641
3.54M
             !strncmp(model, "PENTAX", 6))
642
769k
    {
643
769k
      if (!strncmp(model, "GR", 2) ||
644
603k
          !strncmp(model, "GXR", 3))
645
220k
      {
646
220k
        parseRicohMakernotes(base, tag, type, len, CameraDNG);
647
220k
      }
648
549k
      else
649
549k
      {
650
549k
        parsePentaxMakernotes(base, tag, type, len, nonDNG);
651
549k
      }
652
769k
    }
653
654
3.52M
    else if (!strncmp(make, "SAMSUNG", 7))
655
415k
    {
656
415k
      if (!dng_version)
657
196k
        parseSamsungMakernotes(base, tag, type, len, nonDNG);
658
218k
      else
659
218k
        parsePentaxMakernotes(base, tag, type, len, CameraDNG);
660
415k
    }
661
662
3.11M
    else if (is_Sony)
663
2.15M
    {
664
2.15M
      if ((tag == 0xb028) && (len == 1) && tagtypeIs(LIBRAW_EXIFTAG_TYPE_LONG)) // DSLR-A100
665
2.06k
      {
666
2.06k
        if ((c = get4()))
667
1.72k
        {
668
1.72k
          fseek(ifp, c, SEEK_SET);
669
1.72k
          parse_makernote(base, tag);
670
1.72k
        }
671
2.06k
      }
672
2.15M
      else
673
2.15M
      {
674
2.15M
        parseSonyMakernotes(
675
2.15M
            base, tag, type, len, nonDNG,
676
2.15M
            table_buf_0x0116, table_buf_0x0116_len,
677
2.15M
            table_buf_0x2010, table_buf_0x2010_len,
678
2.15M
            table_buf_0x9050, table_buf_0x9050_len,
679
2.15M
            table_buf_0x9400, table_buf_0x9400_len,
680
2.15M
            table_buf_0x9402, table_buf_0x9402_len,
681
2.15M
            table_buf_0x9403, table_buf_0x9403_len,
682
2.15M
            table_buf_0x9406, table_buf_0x9406_len,
683
2.15M
            table_buf_0x940c, table_buf_0x940c_len,
684
2.15M
            table_buf_0x940e, table_buf_0x940e_len);
685
2.15M
      }
686
2.15M
    }
687
6.11M
    fseek(ifp, _pos, SEEK_SET);
688
689
6.11M
    if (!strncasecmp(make, "Hasselblad", 10) && !is_Sony) {
690
65.4k
      if (tag == 0x0011)
691
784
        imHassy.SensorCode = getint(type);
692
64.6k
      else if (tag == 0x0016)
693
394
        imHassy.CoatingCode = getint(type);
694
64.2k
      else if ((tag == 0x002a) &&
695
1.04k
               tagtypeIs(LIBRAW_EXIFTAG_TYPE_SRATIONAL) &&
696
268
               (len == 12)) {
697
1.63k
        FORC4 for (int ii = 0; ii < 3; ii++)
698
1.22k
                imHassy.mnColorMatrix[c][ii] = getreal(type);
699
700
64.1k
      } else if (tag == 0x0031) {
701
434
        imHassy.RecommendedCrop[0] = getint(type);
702
434
        imHassy.RecommendedCrop[1] = getint(type);
703
434
      }
704
65.4k
    }
705
706
6.11M
    if ((tag == 0x0004 || tag == 0x0114) && !strncmp(make, "KONICA", 6))
707
6.31k
    {
708
6.31k
      fseek(ifp, tag == 0x0004 ? 140 : 160, SEEK_CUR);
709
6.31k
      switch (get2())
710
6.31k
      {
711
291
      case 72:
712
291
        flip = 0;
713
291
        break;
714
114
      case 76:
715
114
        flip = 6;
716
114
        break;
717
316
      case 82:
718
316
        flip = 5;
719
316
        break;
720
6.31k
      }
721
6.31k
    }
722
723
6.11M
    if (is_Olympus) {
724
1.28M
      INT64 _pos2 = ftell(ifp);
725
1.28M
      if ((tag == 0x2010) || (tag == 0x2020) || (tag == 0x2030) ||
726
1.27M
          (tag == 0x2031) || (tag == 0x2040) || (tag == 0x2050) ||
727
1.25M
          (tag == 0x3000))
728
41.6k
      {
729
41.6k
        if (tagtypeIs(LIBRAW_EXIFTOOLTAGTYPE_binary)) {
730
33.9k
          parse_makernote(base, tag);
731
732
33.9k
        } else if (tagtypeIs(LIBRAW_EXIFTOOLTAGTYPE_ifd) ||
733
7.11k
                   tagtypeIs(LIBRAW_EXIFTOOLTAGTYPE_int32u)) {
734
966
          fseek(ifp, base + get4(), SEEK_SET);
735
966
          parse_makernote(base, tag);
736
966
        }
737
738
1.24M
      } else {
739
1.24M
        parseOlympusMakernotes(base, tag, type, len, nonDNG);
740
1.24M
      }
741
1.28M
      fseek(ifp, _pos2, SEEK_SET);
742
1.28M
    }
743
744
6.11M
    if ((tag == 0x0015) &&
745
9.19k
        tagtypeIs(LIBRAW_EXIFTAG_TYPE_ASCII) &&
746
3.32k
        is_raw)
747
3.01k
    { // Hasselblad
748
3.01k
      stmread (imHassy.SensorUnitConnector, len, ifp);
749
3.01k
    }
750
751
6.11M
    if (tagtypeIs(LIBRAW_EXIFTAG_TYPE_UNDEFINED) &&
752
93.9k
        ((tag == 0x0081) || // Minolta
753
93.6k
         (tag == 0x0100)))  // Olympus
754
1.19k
    {
755
1.19k
      thumb_offset = ftell(ifp);
756
1.19k
      thumb_length = len;
757
1.19k
    }
758
6.11M
    if ((tag == 0x0088) && // Minolta, possibly Olympus too
759
4.71k
        tagtypeIs(LIBRAW_EXIFTAG_TYPE_LONG) &&
760
1.15k
        (thumb_offset = get4()))
761
700
      thumb_offset += base;
762
763
6.11M
    if ((tag == 0x0089) && // Minolta, possibly Olympus too
764
3.25k
        tagtypeIs(LIBRAW_EXIFTAG_TYPE_LONG))
765
483
      thumb_length = get4();
766
767
6.11M
    if (tagtypeIs(LIBRAW_EXIFTAG_TYPE_UNDEFINED) &&  // Nikon
768
93.9k
        ((tag == 0x008c) ||
769
93.4k
         (tag == 0x0096))) {
770
793
      meta_offset = ftell(ifp);
771
793
    }
772
773
6.11M
    if ((tag == 0x00a1) &&
774
1.34k
        tagtypeIs(LIBRAW_EXIFTAG_TYPE_UNDEFINED) &&
775
688
        strncasecmp(make, "Samsung", 7))
776
433
    {
777
433
      order = 0x4949;
778
433
      fseek(ifp, 140, SEEK_CUR);
779
1.29k
      FORC3 cam_mul[c] = float(get4());
780
433
    }
781
782
6.11M
    if (tag == 0xb001 && tagtypeIs(LIBRAW_EXIFTAG_TYPE_SHORT)) // Sony ModelID
783
1.46k
    {
784
1.46k
      unique_id = get2();
785
1.46k
    }
786
6.11M
    if (tag == 0x0200 && len == 3) // Olympus
787
759
      shot_order = (get4(), get4());
788
789
6.11M
    if (tag == 0x0f00 && tagtypeIs(LIBRAW_EXIFTAG_TYPE_UNDEFINED))
790
991
    {
791
991
      if (len == 614)
792
332
        fseek(ifp, 176, SEEK_CUR);
793
659
      else if (len == 734 || len == 1502) // Kodak, Minolta, Olympus
794
100
        fseek(ifp, 148, SEEK_CUR);
795
559
      else
796
559
        goto next;
797
432
      goto get2_256;
798
991
    }
799
800
6.11M
    if (tag == 0x2011 && len == 2) // Casio
801
373
    {
802
805
    get2_256:
803
805
      order = 0x4d4d;
804
805
      cam_mul[0] = float(get2()) / 256.0f;
805
805
      cam_mul[2] = float(get2()) / 256.0f;
806
805
    }
807
808
26.2M
  next:
809
26.2M
    fseek(ifp, save, SEEK_SET);
810
26.2M
  }
811
511k
quit:
812
511k
  order = sorder;
813
511k
}