Coverage Report

Created: 2026-10-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/exiv2/src/helper_functions.cpp
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
3
#include "helper_functions.hpp"
4
5
#include "basicio.hpp"
6
#include "convert.hpp"
7
#include "enforce.hpp"
8
#include "types.hpp"
9
10
#include <cstring>
11
#include <numeric>
12
13
3.41k
std::string string_from_unterminated(const char* data, size_t data_length) {
14
3.41k
  if (data_length == 0) {
15
0
    return {};
16
0
  }
17
3.41k
  const size_t StringLength = strnlen(data, data_length);
18
3.41k
  return {data, StringLength};
19
3.41k
}
20
21
namespace Exiv2 {
22
33
uint64_t readQWORDTag(const BasicIo::UniquePtr& io) {
23
33
  Internal::enforce(QWORD <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata);
24
33
  auto FieldBuf = io->read(QWORD);
25
33
  return FieldBuf.read_uint64(0, littleEndian);
26
33
}
27
28
241k
uint32_t readDWORDTag(const BasicIo::UniquePtr& io) {
29
241k
  Internal::enforce(DWORD <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata);
30
241k
  DataBuf FieldBuf = io->read(DWORD);
31
241k
  return FieldBuf.read_uint32(0, littleEndian);
32
241k
}
33
34
160
uint16_t readWORDTag(const BasicIo::UniquePtr& io) {
35
160
  Internal::enforce(WORD <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata);
36
160
  DataBuf FieldBuf = io->read(WORD);
37
160
  return FieldBuf.read_uint16(0, littleEndian);
38
160
}
39
40
0
std::string readStringWcharTag(const BasicIo::UniquePtr& io, size_t length) {
41
0
  Internal::enforce(length >= 2 && length <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata);
42
0
  DataBuf FieldBuf(length + 1);
43
0
  io->readOrThrow(FieldBuf.data(), length, ErrorCode::kerFailedToReadImageData);
44
0
  std::string wst(FieldBuf.begin(), FieldBuf.end() - 3);
45
0
  if (wst.size() % 2 != 0)
46
0
    Exiv2::convertStringCharset(wst, "UCS-2LE", "UTF-8");
47
0
  Exiv2::convertStringCharset(wst, "UCS-2LE", "UTF-8");
48
0
  return wst;
49
0
}
50
51
371k
std::string readStringTag(const BasicIo::UniquePtr& io, size_t length) {
52
371k
  Internal::enforce(length <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata);
53
371k
  DataBuf FieldBuf(length + 1);
54
371k
  io->readOrThrow(FieldBuf.data(), length, ErrorCode::kerFailedToReadImageData);
55
371k
  return Exiv2::toString(FieldBuf.data()).substr(0, length);
56
371k
}
57
58
3.12k
std::string getAspectRatio(uint64_t width, uint64_t height) {
59
3.12k
  if (height == 0 || width == 0)
60
152
    return std::to_string(width) + ":" + std::to_string(height);
61
62
2.96k
  auto ratioWidth = width / std::gcd(width, height);
63
2.96k
  auto ratioHeight = height / std::gcd(width, height);
64
2.96k
  return std::to_string(ratioWidth) + ":" + std::to_string(ratioHeight);
65
3.12k
}
66
67
}  // namespace Exiv2