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