/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 | 81.5k | std::string string_from_unterminated(const char* data, size_t data_length) { |
14 | 81.5k | if (data_length == 0) { |
15 | 1.87k | return {}; |
16 | 1.87k | } |
17 | 79.6k | const size_t StringLength = strnlen(data, data_length); |
18 | 79.6k | return {data, StringLength}; |
19 | 81.5k | } |
20 | | |
21 | | namespace Exiv2 { |
22 | 2.05k | uint64_t readQWORDTag(const BasicIo::UniquePtr& io) { |
23 | 2.05k | Internal::enforce(QWORD <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata); |
24 | 2.05k | auto FieldBuf = io->read(QWORD); |
25 | 2.05k | return FieldBuf.read_uint64(0, littleEndian); |
26 | 2.05k | } |
27 | | |
28 | 33.8k | uint32_t readDWORDTag(const BasicIo::UniquePtr& io) { |
29 | 33.8k | Internal::enforce(DWORD <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata); |
30 | 33.8k | DataBuf FieldBuf = io->read(DWORD); |
31 | 33.8k | return FieldBuf.read_uint32(0, littleEndian); |
32 | 33.8k | } |
33 | | |
34 | 87.8k | uint16_t readWORDTag(const BasicIo::UniquePtr& io) { |
35 | 87.8k | Internal::enforce(WORD <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata); |
36 | 87.8k | DataBuf FieldBuf = io->read(WORD); |
37 | 87.8k | return FieldBuf.read_uint16(0, littleEndian); |
38 | 87.8k | } |
39 | | |
40 | 2.19k | std::string readStringWcharTag(const BasicIo::UniquePtr& io, size_t length) { |
41 | 2.19k | Internal::enforce(length >= 2 && length <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata); |
42 | 2.19k | DataBuf FieldBuf(length + 1); |
43 | 2.19k | io->readOrThrow(FieldBuf.data(), length, ErrorCode::kerFailedToReadImageData); |
44 | 2.19k | std::string wst(FieldBuf.begin(), FieldBuf.end() - 3); |
45 | 2.19k | if (wst.size() % 2 != 0) |
46 | 906 | Exiv2::convertStringCharset(wst, "UCS-2LE", "UTF-8"); |
47 | 2.19k | Exiv2::convertStringCharset(wst, "UCS-2LE", "UTF-8"); |
48 | 2.19k | return wst; |
49 | 2.19k | } |
50 | | |
51 | 31.3k | std::string readStringTag(const BasicIo::UniquePtr& io, size_t length) { |
52 | 31.3k | Internal::enforce(length <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata); |
53 | 31.3k | DataBuf FieldBuf(length + 1); |
54 | 31.3k | io->readOrThrow(FieldBuf.data(), length, ErrorCode::kerFailedToReadImageData); |
55 | 31.3k | return Exiv2::toString(FieldBuf.data()).substr(0, length); |
56 | 31.3k | } |
57 | | |
58 | 1.13k | std::string getAspectRatio(uint64_t width, uint64_t height) { |
59 | 1.13k | if (height == 0 || width == 0) |
60 | 24 | return std::to_string(width) + ":" + std::to_string(height); |
61 | | |
62 | 1.11k | auto ratioWidth = width / std::gcd(width, height); |
63 | 1.11k | auto ratioHeight = height / std::gcd(width, height); |
64 | 1.11k | return std::to_string(ratioWidth) + ":" + std::to_string(ratioHeight); |
65 | 1.13k | } |
66 | | |
67 | | } // namespace Exiv2 |