LibreOffice Module svl (master) 1
msodocumentlockfile.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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
11#include <algorithm>
12#include <ucbhelper/content.hxx>
14#include <o3tl/string_view.hxx>
15
16#include <com/sun/star/io/IOException.hpp>
17#include <com/sun/star/io/XOutputStream.hpp>
18#include <com/sun/star/io/XInputStream.hpp>
19#include <com/sun/star/ucb/XCommandEnvironment.hpp>
20
21namespace svt
22{
23namespace
24{
25bool isWordFormat(std::u16string_view sExt)
26{
27 return o3tl::equalsIgnoreAsciiCase(sExt, u"DOC") || o3tl::equalsIgnoreAsciiCase(sExt, u"DOCX")
28 || o3tl::equalsIgnoreAsciiCase(sExt, u"RTF")
29 || o3tl::equalsIgnoreAsciiCase(sExt, u"ODT");
30}
31
32bool isExcelFormat(std::u16string_view sExt)
33{
34 return //sExt.equalsIgnoreAsciiCase("XLS") || // MSO does not create lockfile for XLS
35 o3tl::equalsIgnoreAsciiCase(sExt, u"XLSX") || o3tl::equalsIgnoreAsciiCase(sExt, u"ODS");
36}
37
38bool isPowerPointFormat(std::u16string_view sExt)
39{
40 return o3tl::equalsIgnoreAsciiCase(sExt, u"PPTX") || o3tl::equalsIgnoreAsciiCase(sExt, u"PPT")
41 || o3tl::equalsIgnoreAsciiCase(sExt, u"ODP");
42}
43
44// Need to generate different lock file name for MSO.
45OUString GenerateMSOLockFileURL(std::u16string_view aOrigURL)
46{
48
49 // For text documents MSO Word cuts some of the first characters of the file name
50 OUString sFileName = aURL.GetLastName();
51 const OUString sExt = aURL.GetFileExtension();
52
53 if (isWordFormat(sExt))
54 {
55 const sal_Int32 nFileNameLength = sFileName.getLength() - sExt.getLength() - 1;
56 if (nFileNameLength >= 8)
57 sFileName = sFileName.copy(2);
58 else if (nFileNameLength == 7)
59 sFileName = sFileName.copy(1);
60 }
61 aURL.setName(Concat2View("~$" + sFileName));
63}
64}
65
66// static
68{
71 const OUString sExt = aDocURL.GetFileExtension();
72 if (isWordFormat(sExt))
73 eResult = AppType::Word;
74 else if (isExcelFormat(sExt))
75 eResult = AppType::Excel;
76
77 return eResult;
78}
79
80MSODocumentLockFile::MSODocumentLockFile(std::u16string_view aOrigURL)
81 : GenDocumentLockFile(GenerateMSOLockFileURL(aOrigURL))
82 , m_eAppType(getAppType(aOrigURL))
83{
84}
85
87
89 std::unique_lock<std::mutex>& /*rGuard*/, const LockFileEntry& aEntry,
90 const css::uno::Reference<css::io::XOutputStream>& xOutput)
91{
92 // Reallocate the date with the right size, different lock file size for different components
93 int nLockFileSize = m_eAppType == AppType::Word ? MSO_WORD_LOCKFILE_SIZE
95 css::uno::Sequence<sal_Int8> aData(nLockFileSize);
96 auto pData = aData.getArray();
97
98 // Write out the user name's length as a single byte integer
99 // The maximum length is 52 in MSO, so we'll need to truncate the user name if it's longer
100 OUString aUserName = aEntry[LockFileComponent::OOOUSERNAME];
101 int nIndex = 0;
102 pData[nIndex] = static_cast<sal_Int8>(
103 std::min(aUserName.getLength(), sal_Int32(MSO_USERNAME_MAX_LENGTH)));
104
105 if (aUserName.getLength() > MSO_USERNAME_MAX_LENGTH)
106 aUserName = aUserName.copy(0, MSO_USERNAME_MAX_LENGTH);
107
108 // From the second position write out the user name using one byte characters.
109 nIndex = 1;
110 for (int nChar = 0; nChar < aUserName.getLength(); ++nChar)
111 {
112 pData[nIndex] = static_cast<sal_Int8>(aUserName[nChar]);
113 ++nIndex;
114 }
115
116 // Fill up the remaining bytes with dummy data
117 switch (m_eAppType)
118 {
119 case AppType::Word:
120 while (nIndex < MSO_USERNAME_MAX_LENGTH + 2)
121 {
122 pData[nIndex] = static_cast<sal_Int8>(0);
123 ++nIndex;
124 }
125 break;
127 pData[nIndex] = static_cast<sal_Int8>(0);
128 ++nIndex;
129 [[fallthrough]];
130 case AppType::Excel:
131 while (nIndex < MSO_USERNAME_MAX_LENGTH + 3)
132 {
133 pData[nIndex] = static_cast<sal_Int8>(0x20);
134 ++nIndex;
135 }
136 break;
137 }
138
139 // At the next position we have the user name's length again, but now as a 2 byte integer
140 pData[nIndex] = static_cast<sal_Int8>(
141 std::min(aUserName.getLength(), sal_Int32(MSO_USERNAME_MAX_LENGTH)));
142 ++nIndex;
143 pData[nIndex] = 0;
144 ++nIndex;
145
146 // And the user name again with unicode characters
147 for (int nChar = 0; nChar < aUserName.getLength(); ++nChar)
148 {
149 pData[nIndex] = static_cast<sal_Int8>(aUserName[nChar] & 0xff);
150 ++nIndex;
151 pData[nIndex] = static_cast<sal_Int8>(aUserName[nChar] >> 8);
152 ++nIndex;
153 }
154
155 // Fill the remaining part with dummy bits
156 switch (m_eAppType)
157 {
158 case AppType::Word:
159 while (nIndex < nLockFileSize)
160 {
161 pData[nIndex] = static_cast<sal_Int8>(0);
162 ++nIndex;
163 }
164 break;
165 case AppType::Excel:
167 while (nIndex < nLockFileSize)
168 {
169 pData[nIndex] = static_cast<sal_Int8>(0x20);
170 ++nIndex;
171 if (nIndex < nLockFileSize)
172 {
173 pData[nIndex] = static_cast<sal_Int8>(0);
174 ++nIndex;
175 }
176 }
177 break;
178 }
179
180 xOutput->writeBytes(aData);
181}
182
183css::uno::Reference<css::io::XInputStream>
184MSODocumentLockFile::OpenStream(std::unique_lock<std::mutex>& /*rGuard*/)
185{
186 css::uno::Reference<css::ucb::XCommandEnvironment> xEnv;
188
189 // the file can be opened readonly, no locking will be done
190 return aSourceContent.openStreamNoLock();
191}
192
193LockFileEntry MSODocumentLockFile::GetLockDataImpl(std::unique_lock<std::mutex>& rGuard)
194{
195 LockFileEntry aResult;
196 css::uno::Reference<css::io::XInputStream> xInput = OpenStream(rGuard);
197 if (!xInput.is())
198 throw css::uno::RuntimeException();
199
200 const sal_Int32 nBufLen = 256;
201 css::uno::Sequence<sal_Int8> aBuf(nBufLen);
202 const sal_Int32 nRead = xInput->readBytes(aBuf, nBufLen);
203 xInput->closeInput();
204 if (nRead >= 162)
205 {
206 // Reverse engineering of MS Office Owner Files format (MS Office 2016 tested).
207 // It starts with a single byte with name length, after which characters of username go
208 // in current Windows 8-bit codepage.
209 // For Word lockfiles, the name is followed by zero bytes up to position 54.
210 // For PowerPoint lockfiles, the name is followed by a single zero byte, and then 0x20
211 // bytes up to position 55.
212 // For Excel lockfiles, the name is followed by 0x20 bytes up to position 55.
213 // At those positions in each type of lockfile, a name length 2-byte word goes, followed
214 // by UTF-16-LE-encoded copy of username. Spaces or some garbage follow up to the end of
215 // the lockfile (total 162 bytes for Word, 165 bytes for Excel/PowerPoint).
216 // Apparently MS Office does not allow username to be longer than 52 characters (trying
217 // to enter more in its options dialog results in error messages stating this limit).
218 const int nACPLen = aBuf[0];
219 if (nACPLen > 0 && nACPLen <= 52) // skip wrong format
220 {
221 const sal_Int8* pBuf = aBuf.getConstArray() + 54;
222 int nUTF16Len = *pBuf; // try Word position
223 // If UTF-16 length is 0x20, then ACP length is also less than maximal, which means
224 // that in Word lockfile case, at least two preceding bytes would be zero. Both
225 // Excel and PowerPoint lockfiles would have at least one of those bytes non-zero.
226 if (nUTF16Len == 0x20 && (*(pBuf - 1) != 0 || *(pBuf - 2) != 0))
227 nUTF16Len = *++pBuf; // use Excel/PowerPoint position
228
229 if (nUTF16Len > 0 && nUTF16Len <= 52) // skip wrong format
230 {
231 OUStringBuffer str(nUTF16Len);
232 sal_uInt8 const* p = reinterpret_cast<sal_uInt8 const*>(pBuf + 2);
233 for (int i = 0; i != nUTF16Len; ++i)
234 {
235 str.append(sal_Unicode(p[0] | (sal_uInt32(p[1]) << 8)));
236 p += 2;
237 }
238 aResult[LockFileComponent::OOOUSERNAME] = str.makeStringAndClear();
239 }
240 }
241 }
242 return aResult;
243}
244
246{
247 std::unique_lock aGuard(m_aMutex);
248
249 // TODO/LATER: the removing is not atomic, is it possible in general to make it atomic?
250 LockFileEntry aNewEntry = GenerateOwnEntry();
251 LockFileEntry aFileData = GetLockDataImpl(aGuard);
252
254 throw css::io::IOException(); // not the owner, access denied
255
257}
258
260{
262 const OUString sExt = aDocURL.GetFileExtension();
263
264 return isWordFormat(sExt) || isExcelFormat(sExt) || isPowerPointFormat(sExt);
265}
266
267} // namespace svt
268
269/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
OUString GetFileExtension() const
Generalized class for LO and MSO lockfile handling.
void RemoveFileDirectly()
Only delete lockfile, disregarding ownership.
static LockFileEntry GenerateOwnEntry()
const OUString & GetURL() const
static INetURLObject ResolveLinks(const INetURLObject &aDocURL)
virtual void WriteEntryToStream(std::unique_lock< std::mutex > &rGuard, const LockFileEntry &aEntry, const css::uno::Reference< css::io::XOutputStream > &xStream) override
virtual ~MSODocumentLockFile() override
virtual css::uno::Reference< css::io::XInputStream > OpenStream(std::unique_lock< std::mutex > &rGuard) override
virtual void RemoveFile() override
Delete the Lockfile, if current user is the owner.
MSODocumentLockFile(std::u16string_view aOrigURL)
static bool IsMSOSupportedFileFormat(std::u16string_view aURL)
static AppType getAppType(std::u16string_view sOrigURL)
virtual LockFileEntry GetLockDataImpl(std::unique_lock< std::mutex > &rGuard) override
css::uno::Reference< css::io::XInputStream > openStreamNoLock()
URL aURL
sal_Int32 nIndex
void * p
aBuf
std::unique_ptr< sal_Int32[]> pData
#define MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE
#define MSO_WORD_LOCKFILE_SIZE
#define MSO_USERNAME_MAX_LENGTH
constexpr OUStringLiteral aData
Reference< XComponentContext > getProcessComponentContext()
int i
bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
unsigned char sal_uInt8
sal_uInt16 sal_Unicode
signed char sal_Int8