/src/libreoffice/vcl/source/graphic/BinaryDataContainer.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | * |
9 | | */ |
10 | | |
11 | | #include <vcl/BinaryDataContainer.hxx> |
12 | | #include <o3tl/hash_combine.hxx> |
13 | | #include <tools/stream.hxx> |
14 | | #include <unotools/tempfile.hxx> |
15 | | #include <comphelper/lok.hxx> |
16 | | #include <comphelper/seqstream.hxx> |
17 | | #include <comphelper/hash.hxx> |
18 | | #include <sal/log.hxx> |
19 | | |
20 | | #include <vector> |
21 | | |
22 | | struct BinaryDataContainer::Impl |
23 | | { |
24 | | // temp file to store the data out of RAM if necessary |
25 | | std::unique_ptr<utl::TempFileFast> mpFile; |
26 | | // the binary data |
27 | | std::shared_ptr<std::vector<sal_uInt8>> mpData; |
28 | | |
29 | 90.0k | Impl(SvStream& stream, size_t size) { readData(stream, size); } |
30 | | |
31 | | /// Populate mpData from the stream |
32 | | void readData(SvStream& stream, size_t size) |
33 | 90.0k | { |
34 | 90.0k | auto pData = std::make_shared<std::vector<sal_uInt8>>(size); |
35 | 90.0k | if (stream.ReadBytes(pData->data(), pData->size()) == size) |
36 | 88.7k | mpData = std::move(pData); |
37 | 90.0k | } |
38 | | |
39 | | /// ensure the data is in-RAM |
40 | | void ensureSwappedIn() |
41 | 311k | { |
42 | 311k | if (mpData || !mpFile) |
43 | 311k | return; |
44 | | |
45 | 0 | auto pStream = mpFile->GetStream(StreamMode::READ); |
46 | 0 | pStream->Seek(0); |
47 | 0 | readData(*pStream, pStream->remainingSize()); |
48 | | |
49 | | // Horrifying data loss ... |
50 | 0 | SAL_WARN_IF(pStream->GetError(), "vcl", |
51 | 0 | "Inconsistent system - failed to swap image back in"); |
52 | 0 | } |
53 | | |
54 | | void swapOut() |
55 | 0 | { |
56 | 0 | if (mpFile) |
57 | 0 | { |
58 | | // we already have it swapped out. |
59 | 0 | mpData.reset(); |
60 | 0 | return; |
61 | 0 | } |
62 | | |
63 | 0 | if (!mpData || mpData->empty()) |
64 | 0 | return; |
65 | | |
66 | 0 | mpFile.reset(new utl::TempFileFast()); |
67 | 0 | auto pStream = mpFile->GetStream(StreamMode::READWRITE); |
68 | |
|
69 | 0 | pStream->WriteBytes(mpData->data(), mpData->size()); |
70 | |
|
71 | 0 | mpData.reset(); |
72 | 0 | } |
73 | | }; |
74 | | |
75 | | BinaryDataContainer::BinaryDataContainer(SvStream& stream, size_t size) |
76 | 90.0k | : mpImpl(new Impl(stream, size)) |
77 | 90.0k | { |
78 | 90.0k | } |
79 | | |
80 | | size_t BinaryDataContainer::calculateHash() const |
81 | 28 | { |
82 | 28 | size_t nSeed = 0; |
83 | 28 | if (mpImpl && mpImpl->mpData && !mpImpl->mpData->empty()) |
84 | 20 | { |
85 | 20 | o3tl::hash_combine(nSeed, getSize()); |
86 | 20 | for (sal_uInt8 const& rByte : *mpImpl->mpData) |
87 | 115k | o3tl::hash_combine(nSeed, rByte); |
88 | 20 | } |
89 | 28 | return nSeed; |
90 | 28 | } |
91 | | |
92 | | std::vector<unsigned char> BinaryDataContainer::calculateSHA1() const |
93 | 0 | { |
94 | 0 | comphelper::Hash aHashEngine(comphelper::HashType::SHA1); |
95 | 0 | aHashEngine.update(getData(), getSize()); |
96 | 0 | return aHashEngine.finalize(); |
97 | 0 | } |
98 | | |
99 | | css::uno::Sequence<sal_Int8> BinaryDataContainer::getCopyAsByteSequence() const |
100 | 0 | { |
101 | 0 | if (isEmpty()) |
102 | 0 | return css::uno::Sequence<sal_Int8>(); |
103 | 0 | assert(mpImpl); |
104 | |
|
105 | 0 | css::uno::Sequence<sal_Int8> aData(getSize()); |
106 | |
|
107 | 0 | std::copy(mpImpl->mpData->cbegin(), mpImpl->mpData->cend(), aData.getArray()); |
108 | |
|
109 | 0 | return aData; |
110 | 0 | } |
111 | | |
112 | | namespace |
113 | | { |
114 | | /* |
115 | | * Hold a reference on the internal state in case we swap out |
116 | | * and free the vector while someone holds an SvStream pointer. |
117 | | */ |
118 | | class ReferencedMemoryStream : public SvMemoryStream |
119 | | { |
120 | | std::shared_ptr<std::vector<sal_uInt8>> mpData; |
121 | | |
122 | | public: |
123 | | ReferencedMemoryStream(const std::shared_ptr<std::vector<sal_uInt8>>& pData) |
124 | 1.00k | : SvMemoryStream(pData->data(), pData->size(), StreamMode::READ) |
125 | 1.00k | , mpData(pData) |
126 | 1.00k | { |
127 | 1.00k | } |
128 | | }; |
129 | | |
130 | | class ReferencedXInputStream : public comphelper::MemoryInputStream |
131 | | { |
132 | | std::shared_ptr<std::vector<sal_uInt8>> mpData; |
133 | | |
134 | | public: |
135 | | ReferencedXInputStream(const std::shared_ptr<std::vector<sal_uInt8>>& pData) |
136 | 34.3k | : comphelper::MemoryInputStream(reinterpret_cast<const sal_Int8*>(pData->data()), |
137 | 34.3k | pData->size()) |
138 | 34.3k | , mpData(pData) |
139 | 34.3k | { |
140 | 34.3k | } |
141 | | }; |
142 | | } |
143 | | |
144 | | std::unique_ptr<SvStream> BinaryDataContainer::getAsStream() const |
145 | 1.00k | { |
146 | 1.00k | ensureSwappedIn(); // TODO: transfer in streamed chunks |
147 | 1.00k | return std::make_unique<ReferencedMemoryStream>(mpImpl->mpData); |
148 | 1.00k | } |
149 | | |
150 | | css::uno::Reference<css::io::XInputStream> BinaryDataContainer::getAsXInputStream() const |
151 | 34.3k | { |
152 | 34.3k | ensureSwappedIn(); // TODO: transfer in streamed chunks |
153 | 34.3k | return new ReferencedXInputStream(mpImpl->mpData); |
154 | 34.3k | } |
155 | | |
156 | | std::size_t BinaryDataContainer::writeToStream(SvStream& rStream) const |
157 | 0 | { |
158 | 0 | ensureSwappedIn(); // TODO: transfer in streamed chunks |
159 | 0 | return rStream.WriteBytes(getData(), getSize()); |
160 | 0 | } |
161 | | |
162 | | size_t BinaryDataContainer::getSize() const |
163 | 122k | { |
164 | 122k | ensureSwappedIn(); |
165 | 122k | return mpImpl && mpImpl->mpData ? mpImpl->mpData->size() : 0; |
166 | 122k | } |
167 | | |
168 | | size_t BinaryDataContainer::getSizeBytes() const |
169 | 0 | { |
170 | 0 | return mpImpl && mpImpl->mpData ? mpImpl->mpData->size() : 0; |
171 | 0 | } |
172 | | |
173 | | bool BinaryDataContainer::isEmpty() const |
174 | 115k | { |
175 | 115k | ensureSwappedIn(); |
176 | 115k | return !mpImpl || !mpImpl->mpData || mpImpl->mpData->empty(); |
177 | 115k | } |
178 | | |
179 | | const sal_uInt8* BinaryDataContainer::getData() const |
180 | 62.4k | { |
181 | 62.4k | ensureSwappedIn(); |
182 | 62.4k | return mpImpl && mpImpl->mpData ? mpImpl->mpData->data() : nullptr; |
183 | 62.4k | } |
184 | | |
185 | | void BinaryDataContainer::ensureSwappedIn() const |
186 | 335k | { |
187 | 335k | if (mpImpl) |
188 | 311k | mpImpl->ensureSwappedIn(); |
189 | 335k | } |
190 | | |
191 | | void BinaryDataContainer::swapOut() const |
192 | 0 | { |
193 | | // Only bother reducing memory footprint in kit mode - for mobile/online etc. |
194 | 0 | if (!mpImpl || !comphelper::LibreOfficeKit::isActive()) |
195 | 0 | return; |
196 | | |
197 | 0 | mpImpl->swapOut(); |
198 | 0 | } |
199 | | |
200 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |