2020-12-04 00:01:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Apple Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "SerializedNFA.h"
|
|
|
|
|
2020-12-07 20:56:51 +00:00
|
|
|
#include "NFA.h"
|
|
|
|
|
2020-12-04 00:01:23 +00:00
|
|
|
#if ENABLE(CONTENT_EXTENSIONS)
|
|
|
|
|
|
|
|
namespace WebCore {
|
|
|
|
namespace ContentExtensions {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool writeAllToFile(FileSystem::PlatformFileHandle file, const T& container)
|
|
|
|
{
|
2021-06-06 05:25:41 +00:00
|
|
|
auto bytes = reinterpret_cast<const uint8_t*>(container.data());
|
2020-12-04 00:01:23 +00:00
|
|
|
size_t bytesLength = container.size() * sizeof(container[0]);
|
2021-06-06 05:25:41 +00:00
|
|
|
auto end = bytes + bytesLength;
|
2020-12-04 00:01:23 +00:00
|
|
|
while (bytes < end) {
|
|
|
|
auto written = FileSystem::writeToFile(file, bytes, bytesLength);
|
|
|
|
if (written == -1)
|
|
|
|
return false;
|
|
|
|
bytes += written;
|
|
|
|
bytesLength -= written;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-30 16:11:40 +00:00
|
|
|
std::optional<SerializedNFA> SerializedNFA::serialize(NFA&& nfa)
|
2020-12-04 00:01:23 +00:00
|
|
|
{
|
|
|
|
auto file = FileSystem::invalidPlatformFileHandle;
|
|
|
|
auto filename = FileSystem::openTemporaryFile("SerializedNFA", file);
|
|
|
|
if (!FileSystem::isHandleValid(file))
|
Next step toward using std::optional directly instead of through WTF::Optional typedef
https://bugs.webkit.org/show_bug.cgi?id=226280
Reviewed by Chris Dumez.
Source/JavaScriptCore:
* <many files>: Accept the renaming done by do-webcore-rename.
* yarr/YarrSyntaxChecker.cpp: Since the style checker complained about this file,
tweaked style to make it happy after the renaming done by do-webcore-rename, and
also hand-updated Optional to std::optional as long as we were touching it.
Source/WebCore:
* <many files>: Accept the renaming done by do-webcore-rename.
* Modules/webauthn/fido/DeviceRequestConverter.h: Since style checker complained
about the names of some arguments, fixed them, and also hand-updated Optional to
std::optional as long as we were touching it.
* loader/EmptyClients.cpp: Since style checker complained about the mix of
WEBCORE_EXPORT and inlined functions, moved them out of line, and
also hand-updated Optional to std::optional as long as we were touching it.
Also removed is<EmptyFrameLoaderClient>().
* loader/EmptyFrameLoaderClient.h: Ditto.
Source/WebCore/PAL:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebDriver:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKit:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy:
* Storage/StorageTracker.cpp:
(WebKit::StorageTracker::diskUsageForOrigin): Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy/mac:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy/win:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WTF:
* <many files>: Accept the renaming done by do-webcore-rename.
* wtf/Optional.h: Remove WTF::nullopt_t and WTF::makeOptional.
* wtf/URLHelpers.cpp:
(WTF::URLHelpers::mapHostName): Convert from nullopt to std::nullopt.
Tools:
* Scripts/do-webcore-rename: Use script to rename valueOr, WTF::nullopt, WTF::nullopt_t,
WTF::Optional, WTF::makeOptional, and makeOptional. Other renamings can't necessarily
be done by the script and so will be done in later passes.
* <many files>: Accept the renaming done by do-webcore-rename.
Canonical link: https://commits.webkit.org/238228@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@278185 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2021-05-28 01:26:23 +00:00
|
|
|
return std::nullopt;
|
2020-12-04 00:01:23 +00:00
|
|
|
|
|
|
|
bool wroteSuccessfully = writeAllToFile(file, nfa.nodes)
|
|
|
|
&& writeAllToFile(file, nfa.transitions)
|
|
|
|
&& writeAllToFile(file, nfa.targets)
|
|
|
|
&& writeAllToFile(file, nfa.epsilonTransitionsTargets)
|
|
|
|
&& writeAllToFile(file, nfa.actions);
|
|
|
|
if (!wroteSuccessfully) {
|
|
|
|
FileSystem::closeFile(file);
|
|
|
|
FileSystem::deleteFile(filename);
|
Next step toward using std::optional directly instead of through WTF::Optional typedef
https://bugs.webkit.org/show_bug.cgi?id=226280
Reviewed by Chris Dumez.
Source/JavaScriptCore:
* <many files>: Accept the renaming done by do-webcore-rename.
* yarr/YarrSyntaxChecker.cpp: Since the style checker complained about this file,
tweaked style to make it happy after the renaming done by do-webcore-rename, and
also hand-updated Optional to std::optional as long as we were touching it.
Source/WebCore:
* <many files>: Accept the renaming done by do-webcore-rename.
* Modules/webauthn/fido/DeviceRequestConverter.h: Since style checker complained
about the names of some arguments, fixed them, and also hand-updated Optional to
std::optional as long as we were touching it.
* loader/EmptyClients.cpp: Since style checker complained about the mix of
WEBCORE_EXPORT and inlined functions, moved them out of line, and
also hand-updated Optional to std::optional as long as we were touching it.
Also removed is<EmptyFrameLoaderClient>().
* loader/EmptyFrameLoaderClient.h: Ditto.
Source/WebCore/PAL:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebDriver:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKit:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy:
* Storage/StorageTracker.cpp:
(WebKit::StorageTracker::diskUsageForOrigin): Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy/mac:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy/win:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WTF:
* <many files>: Accept the renaming done by do-webcore-rename.
* wtf/Optional.h: Remove WTF::nullopt_t and WTF::makeOptional.
* wtf/URLHelpers.cpp:
(WTF::URLHelpers::mapHostName): Convert from nullopt to std::nullopt.
Tools:
* Scripts/do-webcore-rename: Use script to rename valueOr, WTF::nullopt, WTF::nullopt_t,
WTF::Optional, WTF::makeOptional, and makeOptional. Other renamings can't necessarily
be done by the script and so will be done in later passes.
* <many files>: Accept the renaming done by do-webcore-rename.
Canonical link: https://commits.webkit.org/238228@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@278185 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2021-05-28 01:26:23 +00:00
|
|
|
return std::nullopt;
|
2020-12-04 00:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mappedSuccessfully = false;
|
|
|
|
FileSystem::MappedFileData mappedFile(file, FileSystem::MappedFileMode::Private, mappedSuccessfully);
|
|
|
|
FileSystem::closeFile(file);
|
|
|
|
FileSystem::deleteFile(filename);
|
|
|
|
if (!mappedSuccessfully)
|
Next step toward using std::optional directly instead of through WTF::Optional typedef
https://bugs.webkit.org/show_bug.cgi?id=226280
Reviewed by Chris Dumez.
Source/JavaScriptCore:
* <many files>: Accept the renaming done by do-webcore-rename.
* yarr/YarrSyntaxChecker.cpp: Since the style checker complained about this file,
tweaked style to make it happy after the renaming done by do-webcore-rename, and
also hand-updated Optional to std::optional as long as we were touching it.
Source/WebCore:
* <many files>: Accept the renaming done by do-webcore-rename.
* Modules/webauthn/fido/DeviceRequestConverter.h: Since style checker complained
about the names of some arguments, fixed them, and also hand-updated Optional to
std::optional as long as we were touching it.
* loader/EmptyClients.cpp: Since style checker complained about the mix of
WEBCORE_EXPORT and inlined functions, moved them out of line, and
also hand-updated Optional to std::optional as long as we were touching it.
Also removed is<EmptyFrameLoaderClient>().
* loader/EmptyFrameLoaderClient.h: Ditto.
Source/WebCore/PAL:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebDriver:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKit:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy:
* Storage/StorageTracker.cpp:
(WebKit::StorageTracker::diskUsageForOrigin): Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy/mac:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy/win:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WTF:
* <many files>: Accept the renaming done by do-webcore-rename.
* wtf/Optional.h: Remove WTF::nullopt_t and WTF::makeOptional.
* wtf/URLHelpers.cpp:
(WTF::URLHelpers::mapHostName): Convert from nullopt to std::nullopt.
Tools:
* Scripts/do-webcore-rename: Use script to rename valueOr, WTF::nullopt, WTF::nullopt_t,
WTF::Optional, WTF::makeOptional, and makeOptional. Other renamings can't necessarily
be done by the script and so will be done in later passes.
* <many files>: Accept the renaming done by do-webcore-rename.
Canonical link: https://commits.webkit.org/238228@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@278185 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2021-05-28 01:26:23 +00:00
|
|
|
return std::nullopt;
|
2020-12-04 00:01:23 +00:00
|
|
|
|
|
|
|
Metadata metadata {
|
|
|
|
nfa.nodes.size(),
|
|
|
|
nfa.transitions.size(),
|
|
|
|
nfa.targets.size(),
|
|
|
|
nfa.epsilonTransitionsTargets.size(),
|
|
|
|
nfa.actions.size(),
|
|
|
|
0,
|
|
|
|
nfa.nodes.size() * sizeof(nfa.nodes[0]),
|
|
|
|
nfa.nodes.size() * sizeof(nfa.nodes[0])
|
|
|
|
+ nfa.transitions.size() * sizeof(nfa.transitions[0]),
|
|
|
|
nfa.nodes.size() * sizeof(nfa.nodes[0])
|
|
|
|
+ nfa.transitions.size() * sizeof(nfa.transitions[0])
|
|
|
|
+ nfa.targets.size() * sizeof(nfa.targets[0]),
|
|
|
|
nfa.nodes.size() * sizeof(nfa.nodes[0])
|
|
|
|
+ nfa.transitions.size() * sizeof(nfa.transitions[0])
|
|
|
|
+ nfa.targets.size() * sizeof(nfa.targets[0])
|
|
|
|
+ nfa.epsilonTransitionsTargets.size() * sizeof(nfa.epsilonTransitionsTargets[0])
|
|
|
|
};
|
|
|
|
|
|
|
|
nfa.clear();
|
|
|
|
|
|
|
|
return {{ WTFMove(mappedFile), WTFMove(metadata) }};
|
|
|
|
}
|
|
|
|
|
|
|
|
SerializedNFA::SerializedNFA(FileSystem::MappedFileData&& file, Metadata&& metadata)
|
|
|
|
: m_file(WTFMove(file))
|
|
|
|
, m_metadata(WTFMove(metadata))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
const T* SerializedNFA::pointerAtOffsetInFile(size_t offset) const
|
|
|
|
{
|
2021-06-08 17:35:15 +00:00
|
|
|
return reinterpret_cast<const T*>(static_cast<const uint8_t*>(m_file.data()) + offset);
|
2020-12-04 00:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto SerializedNFA::nodes() const -> const Range<ImmutableNFANode>
|
|
|
|
{
|
|
|
|
return { pointerAtOffsetInFile<ImmutableNFANode>(m_metadata.nodesOffset), m_metadata.nodesSize };
|
|
|
|
}
|
|
|
|
|
|
|
|
auto SerializedNFA::transitions() const -> const Range<ImmutableRange<char>>
|
|
|
|
{
|
|
|
|
return { pointerAtOffsetInFile<ImmutableRange<char>>(m_metadata.transitionsOffset), m_metadata.transitionsSize };
|
|
|
|
}
|
|
|
|
|
|
|
|
auto SerializedNFA::targets() const -> const Range<uint32_t>
|
|
|
|
{
|
|
|
|
return { pointerAtOffsetInFile<uint32_t>(m_metadata.targetsOffset), m_metadata.targetsSize };
|
|
|
|
}
|
|
|
|
|
|
|
|
auto SerializedNFA::epsilonTransitionsTargets() const -> const Range<uint32_t>
|
|
|
|
{
|
|
|
|
return { pointerAtOffsetInFile<uint32_t>(m_metadata.epsilonTransitionsTargetsOffset), m_metadata.epsilonTransitionsTargetsSize };
|
|
|
|
}
|
|
|
|
|
|
|
|
auto SerializedNFA::actions() const -> const Range<uint64_t>
|
|
|
|
{
|
|
|
|
return { pointerAtOffsetInFile<uint64_t>(m_metadata.actionsOffset), m_metadata.actionsSize };
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ContentExtensions
|
|
|
|
} // namespace WebCore
|
|
|
|
|
|
|
|
#endif // ENABLE(CONTENT_EXTENSIONS)
|