crashtesting: negative index seen on loading forum-de3-15472.ods
[LibreOffice.git] / tools / source / fsys / fileutil.cxx
blobec20e0a513bf79168b942a6fe08c7b3b203a7c69
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <tools/fileutil.hxx>
11 #if defined _WIN32
12 #include <osl/file.hxx>
13 #include <o3tl/char16_t2wchar_t.hxx>
14 #define WIN32_LEAN_AND_MEAN
15 #include <Windows.h>
16 #include <davclnt.h>
17 #endif
19 namespace
21 #if defined _WIN32
22 OUString UNCToDavURL(LPCWSTR sUNC)
24 DWORD nSize = 1024;
25 auto bufURL(std::make_unique<wchar_t[]>(nSize));
26 DWORD nResult = DavGetHTTPFromUNCPath(sUNC, bufURL.get(), &nSize);
27 if (nResult == ERROR_INSUFFICIENT_BUFFER)
29 bufURL = std::make_unique<wchar_t[]>(nSize);
30 nResult = DavGetHTTPFromUNCPath(sUNC, bufURL.get(), &nSize);
32 return nResult == ERROR_SUCCESS ? OUString(o3tl::toU(bufURL.get())) : OUString();
34 #endif
37 namespace tools
39 bool IsMappedWebDAVPath([[maybe_unused]] const OUString& rURL, [[maybe_unused]] OUString* pRealURL)
41 #if defined _WIN32
42 if (rURL.startsWithIgnoreAsciiCase("file:"))
44 OUString aSystemPath;
45 if (osl::FileBase::getSystemPathFromFileURL(rURL, aSystemPath) == osl::FileBase::E_None)
47 DWORD nSize = MAX_PATH;
48 auto bufUNC(std::make_unique<char[]>(nSize));
49 DWORD nResult = WNetGetUniversalNameW(o3tl::toW(aSystemPath.getStr()),
50 UNIVERSAL_NAME_INFO_LEVEL, bufUNC.get(), &nSize);
51 if (nResult == ERROR_MORE_DATA)
53 bufUNC = std::make_unique<char[]>(nSize);
54 nResult = WNetGetUniversalNameW(o3tl::toW(aSystemPath.getStr()),
55 UNIVERSAL_NAME_INFO_LEVEL, bufUNC.get(), &nSize);
57 if (nResult == NO_ERROR || nResult == ERROR_BAD_DEVICE)
59 NETRESOURCEW aReq{};
60 if (nResult == ERROR_BAD_DEVICE) // The path could already be an UNC
61 aReq.lpRemoteName = const_cast<LPWSTR>(o3tl::toW(aSystemPath.getStr()));
62 else
64 auto pInfo = reinterpret_cast<LPUNIVERSAL_NAME_INFOW>(bufUNC.get());
65 aReq.lpRemoteName = pInfo->lpUniversalName;
67 nSize = 1024;
68 auto bufInfo(std::make_unique<char[]>(nSize));
69 LPWSTR pSystem = nullptr;
70 nResult = WNetGetResourceInformationW(&aReq, bufInfo.get(), &nSize, &pSystem);
71 if (nResult == ERROR_MORE_DATA)
73 bufInfo = std::make_unique<char[]>(nSize);
74 nResult = WNetGetResourceInformationW(&aReq, bufInfo.get(), &nSize, &pSystem);
76 if (nResult == NO_ERROR)
78 LPNETRESOURCEW pInfo = reinterpret_cast<LPNETRESOURCEW>(bufInfo.get());
79 if (wcscmp(pInfo->lpProvider, L"Web Client Network") == 0)
81 if (pRealURL)
82 *pRealURL = UNCToDavURL(aReq.lpRemoteName);
83 return true;
89 #endif
90 return false;
93 } // namespace tools
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */