Coverage Report

Created: 2026-01-25 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/exiv2/src/cr2header_int.cpp
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
3
#include "cr2header_int.hpp"
4
5
#include "tags.hpp"
6
#include "tiffimage_int.hpp"
7
#include "types.hpp"
8
9
#include <algorithm>
10
#include <cstdint>
11
#include <cstring>
12
13
namespace Exiv2::Internal {
14
14.7k
Cr2Header::Cr2Header(ByteOrder byteOrder) : TiffHeaderBase(42, 16, byteOrder, 0x00000010) {
15
14.7k
}
16
17
14.7k
bool Cr2Header::read(const byte* pData, size_t size) {
18
14.7k
  if (!pData || size < 16) {
19
0
    return false;
20
0
  }
21
22
14.7k
  if (pData[0] == 'I' && pData[0] == pData[1]) {
23
1.24k
    setByteOrder(littleEndian);
24
13.4k
  } else if (pData[0] == 'M' && pData[0] == pData[1]) {
25
3.86k
    setByteOrder(bigEndian);
26
9.60k
  } else {
27
9.60k
    return false;
28
9.60k
  }
29
5.11k
  if (tag() != getUShort(pData + 2, byteOrder()))
30
641
    return false;
31
4.46k
  setOffset(getULong(pData + 4, byteOrder()));
32
4.46k
  if (0 != memcmp(pData + 8, cr2sig_.data(), 4))
33
4.30k
    return false;
34
168
  offset2_ = getULong(pData + 12, byteOrder());
35
36
168
  return true;
37
4.46k
}
38
39
0
DataBuf Cr2Header::write() const {
40
0
  DataBuf buf(16);
41
0
  switch (byteOrder()) {
42
0
    case littleEndian:
43
0
      buf.write_uint8(0, 'I');
44
0
      break;
45
0
    case bigEndian:
46
0
      buf.write_uint8(0, 'M');
47
0
      break;
48
0
    default:
49
0
      break;
50
0
  }
51
0
  buf.write_uint8(1, buf.read_uint8(0));
52
53
0
  buf.write_uint16(2, tag(), byteOrder());
54
0
  buf.write_uint32(4, 0x00000010, byteOrder());
55
0
  std::copy(cr2sig_.begin(), cr2sig_.end(), buf.begin() + 8);
56
  // Write a dummy value for the RAW IFD offset. The offset-writer is used to set this offset in a second pass.
57
0
  buf.write_uint32(12, 0x00000000, byteOrder());
58
0
  return buf;
59
0
}  // Cr2Header::write
60
61
0
bool Cr2Header::isImageTag(uint16_t tag, IfdId group, const PrimaryGroups& /*pPrimaryGroups*/) const {
62
  // CR2 image tags are all IFD2 and IFD3 tags
63
0
  if (group == IfdId::ifd2Id || group == IfdId::ifd3Id)
64
0
    return true;
65
  // ...and any (IFD0) tag that is in the TIFF image tags list
66
0
  return isTiffImageTag(tag, group);
67
0
}
68
69
}  // namespace Exiv2::Internal