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