Coverage Report

Created: 2026-01-16 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/exiv2/src/image_int.hpp
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
3
#ifndef IMAGE_INT_HPP_
4
#define IMAGE_INT_HPP_
5
6
#include "config.h"
7
8
// *****************************************************************************
9
// included header files
10
#include "slice.hpp"  // for Slice
11
12
#include <cstddef>  // for size_t
13
#include <cstdint>  // for int32_t
14
#include <ostream>  // for ostream, basic_ostream::put
15
#include <string>
16
17
#ifdef EXV_HAVE_STD_FORMAT
18
#include <format>
19
48.2M
#define stringFormat std::format
20
696
#define stringFormatTo std::format_to
21
#else
22
#include <fmt/format.h>
23
#define stringFormat fmt::format
24
#define stringFormatTo fmt::format_to
25
#endif
26
27
// *****************************************************************************
28
// namespace extensions
29
namespace Exiv2::Internal {
30
// *****************************************************************************
31
// class definitions
32
33
/*!
34
 * @brief Helper struct for binary data output via @ref binaryToString.
35
 *
36
 * The only purpose of this struct is to provide a custom
37
 * `operator<<(std::ostream&)` for the output of binary data, that is not
38
 * used for all Slices by default.
39
 */
40
template <typename T>
41
struct binaryToStringHelper;
42
43
/*!
44
 * @brief Actual implementation of the output algorithm described in @ref
45
 * binaryToString
46
 *
47
 * @note Does not throw
48
 */
49
template <typename T>
50
68.9k
std::ostream& operator<<(std::ostream& stream, const binaryToStringHelper<T>& binToStr) noexcept {
51
1.50M
  for (size_t i = 0; i < binToStr.buf_.size(); ++i) {
52
1.43M
    auto c = static_cast<unsigned char>(binToStr.buf_.at(i));
53
1.43M
    if (c != 0 || i != binToStr.buf_.size() - 1) {
54
1.39M
      if (c < 32 || c > 126)
55
632k
        stream.put('.');
56
764k
      else
57
764k
        stream.put(static_cast<char>(c));
58
1.39M
    }
59
1.43M
  }
60
68.9k
  return stream;
61
68.9k
}
std::__1::basic_ostream<char, std::__1::char_traits<char> >& Exiv2::Internal::operator<< <unsigned char*>(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Exiv2::Internal::binaryToStringHelper<unsigned char*> const&)
Line
Count
Source
50
67.8k
std::ostream& operator<<(std::ostream& stream, const binaryToStringHelper<T>& binToStr) noexcept {
51
1.49M
  for (size_t i = 0; i < binToStr.buf_.size(); ++i) {
52
1.42M
    auto c = static_cast<unsigned char>(binToStr.buf_.at(i));
53
1.42M
    if (c != 0 || i != binToStr.buf_.size() - 1) {
54
1.39M
      if (c < 32 || c > 126)
55
629k
        stream.put('.');
56
761k
      else
57
761k
        stream.put(static_cast<char>(c));
58
1.39M
    }
59
1.42M
  }
60
67.8k
  return stream;
61
67.8k
}
std::__1::basic_ostream<char, std::__1::char_traits<char> >& Exiv2::Internal::operator<< <Exiv2::Slice<unsigned char*> const>(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Exiv2::Internal::binaryToStringHelper<Exiv2::Slice<unsigned char*> const> const&)
Line
Count
Source
50
1.06k
std::ostream& operator<<(std::ostream& stream, const binaryToStringHelper<T>& binToStr) noexcept {
51
7.66k
  for (size_t i = 0; i < binToStr.buf_.size(); ++i) {
52
6.60k
    auto c = static_cast<unsigned char>(binToStr.buf_.at(i));
53
6.60k
    if (c != 0 || i != binToStr.buf_.size() - 1) {
54
5.86k
      if (c < 32 || c > 126)
55
3.15k
        stream.put('.');
56
2.71k
      else
57
2.71k
        stream.put(static_cast<char>(c));
58
5.86k
    }
59
6.60k
  }
60
1.06k
  return stream;
61
1.06k
}
62
63
template <typename T>
64
struct binaryToStringHelper {
65
68.9k
  explicit constexpr binaryToStringHelper(Slice<T>&& myBuf) noexcept : buf_(std::move(myBuf)) {
66
68.9k
  }
Exiv2::Internal::binaryToStringHelper<unsigned char*>::binaryToStringHelper(Exiv2::Slice<unsigned char*>&&)
Line
Count
Source
65
67.8k
  explicit constexpr binaryToStringHelper(Slice<T>&& myBuf) noexcept : buf_(std::move(myBuf)) {
66
67.8k
  }
Exiv2::Internal::binaryToStringHelper<Exiv2::Slice<unsigned char*> const>::binaryToStringHelper(Exiv2::Slice<Exiv2::Slice<unsigned char*> const>&&)
Line
Count
Source
65
1.06k
  explicit constexpr binaryToStringHelper(Slice<T>&& myBuf) noexcept : buf_(std::move(myBuf)) {
66
1.06k
  }
67
68
  // the Slice is stored by value to avoid dangling references, in case we
69
  // invoke:
70
  // binaryToString(makeSlice(buf, 0, n));
71
  // <- buf_ would be now dangling, were it a reference
72
  Slice<T> buf_;
73
};
74
75
/*!
76
 * @brief format binary data for display in @ref Image::printStructure()
77
 *
78
 * This function creates a new helper class that can be passed to a
79
 * `std::ostream` for output. It creates a printable version of the binary
80
 * data in the slice sl according to the following rules:
81
 * - characters with numeric values larger than 0x20 (= space) and smaller
82
 *   or equal to 0x7F (Delete) are printed as ordinary characters
83
 * - characters outside of that range are printed as '.'
84
 * - if the last element of the slice is 0, then it is omitted
85
 *
86
 * @param[in] sl  Slice containing binary data buffer that should be
87
 *     printed.
88
 *
89
 * @return Helper object, that can be passed into a std::ostream and
90
 *     produces an output according to the aforementioned rules.
91
 *
92
 * @throw This function does not throw. The output of the helper object to
93
 *     the stream throws neither.
94
 */
95
template <typename T>
96
68.9k
constexpr auto binaryToString(Slice<T>&& sl) noexcept {
97
68.9k
  return binaryToStringHelper<T>(std::move(sl));
98
68.9k
}
auto Exiv2::Internal::binaryToString<unsigned char*>(Exiv2::Slice<unsigned char*>&&)
Line
Count
Source
96
67.8k
constexpr auto binaryToString(Slice<T>&& sl) noexcept {
97
67.8k
  return binaryToStringHelper<T>(std::move(sl));
98
67.8k
}
auto Exiv2::Internal::binaryToString<Exiv2::Slice<unsigned char*> const>(Exiv2::Slice<Exiv2::Slice<unsigned char*> const>&&)
Line
Count
Source
96
1.06k
constexpr auto binaryToString(Slice<T>&& sl) noexcept {
97
1.06k
  return binaryToStringHelper<T>(std::move(sl));
98
1.06k
}
99
100
/// @brief indent output for kpsRecursive in \em printStructure() \em .
101
std::string indent(size_t i);
102
103
}  // namespace Exiv2::Internal
104
105
#endif  // #ifndef IMAGE_INT_HPP_