1/*
2 * Copyright (C) 2015-2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/FileSystem.h>
29#include <wtf/FunctionDispatcher.h>
30#include <wtf/SHA1.h>
31#include <wtf/ThreadSafeRefCounted.h>
32#include <wtf/text/WTFString.h>
33
34#if PLATFORM(COCOA)
35#include <wtf/OSObjectPtr.h>
36#endif
37
38#if USE(SOUP)
39#include <WebCore/GRefPtrSoup.h>
40#endif
41
42namespace WebKit {
43
44class SharedMemory;
45
46namespace NetworkCache {
47
48class Data {
49public:
50 Data() { }
51 Data(const uint8_t*, size_t);
52
53 ~Data() { }
54
55 static Data empty();
56#if !OS(WINDOWS)
57 static Data adoptMap(void* map, size_t, int fd);
58#endif
59
60#if PLATFORM(COCOA)
61 enum class Backing { Buffer, Map };
62 Data(OSObjectPtr<dispatch_data_t>&&, Backing = Backing::Buffer);
63#endif
64#if USE(SOUP)
65 Data(GRefPtr<SoupBuffer>&&, int fd = -1);
66#elif OS(WINDOWS)
67 explicit Data(Vector<uint8_t>&&);
68 Data(FileSystem::PlatformFileHandle, size_t offset, size_t);
69#endif
70 bool isNull() const;
71 bool isEmpty() const { return !m_size; }
72
73 const uint8_t* data() const;
74 size_t size() const { return m_size; }
75 bool isMap() const { return m_isMap; }
76 RefPtr<SharedMemory> tryCreateSharedMemory() const;
77
78 Data subrange(size_t offset, size_t) const;
79
80 bool apply(const Function<bool (const uint8_t*, size_t)>&) const;
81
82 Data mapToFile(const String& path) const;
83
84#if PLATFORM(COCOA)
85 dispatch_data_t dispatchData() const { return m_dispatchData.get(); }
86#endif
87
88#if USE(SOUP)
89 SoupBuffer* soupBuffer() const { return m_buffer.get(); }
90#endif
91private:
92#if PLATFORM(COCOA)
93 mutable OSObjectPtr<dispatch_data_t> m_dispatchData;
94#endif
95#if USE(SOUP)
96 mutable GRefPtr<SoupBuffer> m_buffer;
97 int m_fileDescriptor { -1 };
98#endif
99#if OS(WINDOWS)
100 Vector<uint8_t> m_buffer;
101#endif
102 mutable const uint8_t* m_data { nullptr };
103 size_t m_size { 0 };
104 bool m_isMap { false };
105};
106
107Data concatenate(const Data&, const Data&);
108bool bytesEqual(const Data&, const Data&);
109#if !OS(WINDOWS)
110Data adoptAndMapFile(int, size_t offset, size_t);
111#else
112Data adoptAndMapFile(FileSystem::PlatformFileHandle, size_t offset, size_t);
113#endif
114#if USE(GLIB) && !PLATFORM(WIN)
115Data adoptAndMapFile(GFileIOStream*, size_t offset, size_t);
116#endif
117#if !OS(WINDOWS)
118Data mapFile(const char* path);
119#endif
120Data mapFile(const String& path);
121
122using Salt = std::array<uint8_t, 8>;
123
124Optional<Salt> readOrMakeSalt(const String& path);
125SHA1::Digest computeSHA1(const Data&, const Salt&);
126
127}
128
129}
130