1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
* Copyright (C) 2010-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.
*/
#pragma once
#include <utility>
#include <wtf/CheckedArithmetic.h>
#include <wtf/Forward.h>
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/StdLibExtras.h>
#include <wtf/Vector.h>
#include <wtf/persistence/PersistentDecoder.h>
#include <wtf/persistence/PersistentEncoder.h>
namespace WTF::Persistence {
template<typename> struct Coder;
class Decoder;
class Encoder;
template<typename T, typename U> struct Coder<std::pair<T, U>> {
template<typename Encoder>
static void encodeForPersistence(Encoder& encoder, const std::pair<T, U>& pair)
{
encoder << pair.first << pair.second;
}
template<typename Decoder>
static std::optional<std::pair<T, U>> decodeForPersistence(Decoder& decoder)
{
std::optional<T> first;
decoder >> first;
if (!first)
return std::nullopt;
std::optional<U> second;
decoder >> second;
if (!second)
return std::nullopt;
return {{ WTFMove(*first), WTFMove(*second) }};
}
};
template<typename T> struct Coder<std::optional<T>> {
template<typename Encoder>
static void encodeForPersistence(Encoder& encoder, const std::optional<T>& optional)
{
if (!optional) {
encoder << false;
return;
}
encoder << true;
encoder << optional.value();
}
template<typename Decoder>
static std::optional<std::optional<T>> decodeForPersistence(Decoder& decoder)
{
std::optional<bool> isEngaged;
decoder >> isEngaged;
if (!isEngaged)
return std::nullopt;
if (!*isEngaged)
return std::optional<std::optional<T>> { std::optional<T> { std::nullopt } };
std::optional<T> value;
decoder >> value;
if (!value)
return std::nullopt;
return std::optional<std::optional<T>> { std::optional<T> { WTFMove(*value) } };
}
};
template<typename KeyType, typename ValueType> struct Coder<WTF::KeyValuePair<KeyType, ValueType>> {
template<typename Encoder>
static void encodeForPersistence(Encoder& encoder, const WTF::KeyValuePair<KeyType, ValueType>& pair)
{
encoder << pair.key << pair.value;
}
template<typename Decoder>
static std::optional<WTF::KeyValuePair<KeyType, ValueType>> decodeForPersistence(Decoder& decoder)
{
std::optional<KeyType> key;
decoder >> key;
if (!key)
return std::nullopt;
std::optional<ValueType> value;
decoder >>value;
if (!value)
return std::nullopt;
return {{ WTFMove(*key), WTFMove(*value) }};
}
};
template<bool fixedSizeElements, typename T, size_t inlineCapacity> struct VectorCoder;
template<typename T, size_t inlineCapacity> struct VectorCoder<false, T, inlineCapacity> {
template<typename Encoder>
static void encodeForPersistence(Encoder& encoder, const Vector<T, inlineCapacity>& vector)
{
encoder << static_cast<uint64_t>(vector.size());
for (size_t i = 0; i < vector.size(); ++i)
encoder << vector[i];
}
template<typename Decoder>
static std::optional<Vector<T, inlineCapacity>> decodeForPersistence(Decoder& decoder)
{
std::optional<uint64_t> size;
decoder >> size;
if (!size)
return std::nullopt;
Vector<T, inlineCapacity> tmp;
for (size_t i = 0; i < *size; ++i) {
std::optional<T> element;
decoder >> element;
if (!element)
return std::nullopt;
tmp.append(WTFMove(*element));
}
tmp.shrinkToFit();
return tmp;
}
};
template<typename T, size_t inlineCapacity> struct VectorCoder<true, T, inlineCapacity> {
template<typename Encoder>
static void encodeForPersistence(Encoder& encoder, const Vector<T, inlineCapacity>& vector)
{
encoder << static_cast<uint64_t>(vector.size());
encoder.encodeFixedLengthData(asBytes(vector.span()));
}
template<typename Decoder>
static std::optional<Vector<T, inlineCapacity>> decodeForPersistence(Decoder& decoder)
{
std::optional<uint64_t> decodedSize;
decoder >> decodedSize;
if (!decodedSize)
return std::nullopt;
if (!isInBounds<size_t>(*decodedSize))
return std::nullopt;
auto size = static_cast<size_t>(*decodedSize);
// Since we know the total size of the elements, we can allocate the vector in
// one fell swoop. Before allocating we must however make sure that the decoder buffer
// is big enough.
if (!decoder.template bufferIsLargeEnoughToContain<T>(size))
return std::nullopt;
Vector<T, inlineCapacity> temp;
temp.grow(size);
if (!decoder.decodeFixedLengthData({ temp.data(), size * sizeof(T) }))
return std::nullopt;
return temp;
}
};
template<typename T, size_t inlineCapacity> struct Coder<Vector<T, inlineCapacity>> : VectorCoder<std::is_arithmetic<T>::value, T, inlineCapacity> { };
template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg> struct Coder<HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>> {
typedef HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg> HashMapType;
template<typename Encoder>
static void encodeForPersistence(Encoder& encoder, const HashMapType& hashMap)
{
encoder << static_cast<uint64_t>(hashMap.size());
for (typename HashMapType::const_iterator it = hashMap.begin(), end = hashMap.end(); it != end; ++it)
encoder << *it;
}
template<typename Decoder>
static std::optional<HashMapType> decodeForPersistence(Decoder& decoder)
{
std::optional<uint64_t> hashMapSize;
decoder >> hashMapSize;
if (!hashMapSize)
return std::nullopt;
HashMapType tempHashMap;
tempHashMap.reserveInitialCapacity(static_cast<unsigned>(*hashMapSize));
for (uint64_t i = 0; i < *hashMapSize; ++i) {
std::optional<KeyArg> key;
decoder >> key;
if (!key)
return std::nullopt;
std::optional<MappedArg> value;
decoder >> value;
if (!value)
return std::nullopt;
if (!tempHashMap.add(WTFMove(*key), WTFMove(*value)).isNewEntry) {
// The hash map already has the specified key, bail.
return std::nullopt;
}
}
return tempHashMap;
}
};
template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg> struct Coder<UncheckedKeyHashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>> {
typedef UncheckedKeyHashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg> HashMapType;
template<typename Encoder>
static void encodeForPersistence(Encoder& encoder, const HashMapType& hashMap)
{
encoder << static_cast<uint64_t>(hashMap.size());
for (typename HashMapType::const_iterator it = hashMap.begin(), end = hashMap.end(); it != end; ++it)
encoder << *it;
}
template<typename Decoder>
static std::optional<HashMapType> decodeForPersistence(Decoder& decoder)
{
std::optional<uint64_t> hashMapSize;
decoder >> hashMapSize;
if (!hashMapSize)
return std::nullopt;
HashMapType tempHashMap;
tempHashMap.reserveInitialCapacity(static_cast<unsigned>(*hashMapSize));
for (uint64_t i = 0; i < *hashMapSize; ++i) {
std::optional<KeyArg> key;
decoder >> key;
if (!key)
return std::nullopt;
std::optional<MappedArg> value;
decoder >> value;
if (!value)
return std::nullopt;
if (!tempHashMap.add(WTFMove(*key), WTFMove(*value)).isNewEntry) {
// The hash map already has the specified key, bail.
return std::nullopt;
}
}
return tempHashMap;
}
};
template<typename KeyArg, typename HashArg, typename KeyTraitsArg> struct Coder<HashSet<KeyArg, HashArg, KeyTraitsArg>> {
typedef HashSet<KeyArg, HashArg, KeyTraitsArg> HashSetType;
template<typename Encoder>
static void encodeForPersistence(Encoder& encoder, const HashSetType& hashSet)
{
encoder << static_cast<uint64_t>(hashSet.size());
for (typename HashSetType::const_iterator it = hashSet.begin(), end = hashSet.end(); it != end; ++it)
encoder << *it;
}
template<typename Decoder>
static std::optional<HashSetType> decodeForPersistence(Decoder& decoder)
{
std::optional<uint64_t> hashSetSize;
decoder >> hashSetSize;
if (!hashSetSize)
return std::nullopt;
HashSetType tempHashSet;
for (uint64_t i = 0; i < *hashSetSize; ++i) {
std::optional<KeyArg> key;
decoder >> key;
if (!key)
return std::nullopt;
if (!tempHashSet.add(WTFMove(*key)).isNewEntry) {
// The hash map already has the specified key, bail.
return std::nullopt;
}
}
return tempHashSet;
}
};
#define DECLARE_CODER(class) \
template<> struct Coder<class> { \
WTF_EXPORT_PRIVATE static void encodeForPersistence(Encoder&, const class&); \
WTF_EXPORT_PRIVATE static std::optional<class> decodeForPersistence(Decoder&); \
}
DECLARE_CODER(AtomString);
DECLARE_CODER(CString);
DECLARE_CODER(Seconds);
DECLARE_CODER(String);
DECLARE_CODER(SHA1::Digest);
DECLARE_CODER(URL);
DECLARE_CODER(WallTime);
#undef DECLARE_CODER
}
|