1 | /* |
2 | * Copyright (C) 2015 Igalia S.L |
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 | #include "config.h" |
27 | #include "NetworkCacheData.h" |
28 | |
29 | #include "SharedMemory.h" |
30 | #include <fcntl.h> |
31 | #include <sys/mman.h> |
32 | #include <sys/stat.h> |
33 | #include <sys/types.h> |
34 | #include <unistd.h> |
35 | |
36 | #if USE(GLIB) && !PLATFORM(WIN) |
37 | #include <gio/gfiledescriptorbased.h> |
38 | #endif |
39 | |
40 | namespace WebKit { |
41 | namespace NetworkCache { |
42 | |
43 | Data::Data(const uint8_t* data, size_t size) |
44 | : m_size(size) |
45 | { |
46 | uint8_t* copiedData = static_cast<uint8_t*>(fastMalloc(size)); |
47 | memcpy(copiedData, data, size); |
48 | m_buffer = adoptGRef(soup_buffer_new_with_owner(copiedData, size, copiedData, fastFree)); |
49 | } |
50 | |
51 | Data::Data(GRefPtr<SoupBuffer>&& buffer, int fd) |
52 | : m_buffer(buffer) |
53 | , m_fileDescriptor(fd) |
54 | , m_size(buffer ? buffer->length : 0) |
55 | , m_isMap(m_size && fd != -1) |
56 | { |
57 | } |
58 | |
59 | Data Data::empty() |
60 | { |
61 | GRefPtr<SoupBuffer> buffer = adoptGRef(soup_buffer_new(SOUP_MEMORY_TAKE, nullptr, 0)); |
62 | return { WTFMove(buffer) }; |
63 | } |
64 | |
65 | const uint8_t* Data::data() const |
66 | { |
67 | return m_buffer ? reinterpret_cast<const uint8_t*>(m_buffer->data) : nullptr; |
68 | } |
69 | |
70 | bool Data::isNull() const |
71 | { |
72 | return !m_buffer; |
73 | } |
74 | |
75 | bool Data::apply(const Function<bool (const uint8_t*, size_t)>& applier) const |
76 | { |
77 | if (!m_size) |
78 | return false; |
79 | |
80 | return applier(reinterpret_cast<const uint8_t*>(m_buffer->data), m_buffer->length); |
81 | } |
82 | |
83 | Data Data::subrange(size_t offset, size_t size) const |
84 | { |
85 | if (!m_buffer) |
86 | return { }; |
87 | |
88 | GRefPtr<SoupBuffer> subBuffer = adoptGRef(soup_buffer_new_subbuffer(m_buffer.get(), offset, size)); |
89 | return { WTFMove(subBuffer) }; |
90 | } |
91 | |
92 | Data concatenate(const Data& a, const Data& b) |
93 | { |
94 | if (a.isNull()) |
95 | return b; |
96 | if (b.isNull()) |
97 | return a; |
98 | |
99 | size_t size = a.size() + b.size(); |
100 | uint8_t* data = static_cast<uint8_t*>(fastMalloc(size)); |
101 | memcpy(data, a.soupBuffer()->data, a.soupBuffer()->length); |
102 | memcpy(data + a.soupBuffer()->length, b.soupBuffer()->data, b.soupBuffer()->length); |
103 | GRefPtr<SoupBuffer> buffer = adoptGRef(soup_buffer_new_with_owner(data, size, data, fastFree)); |
104 | return { WTFMove(buffer) }; |
105 | } |
106 | |
107 | struct MapWrapper { |
108 | ~MapWrapper() |
109 | { |
110 | munmap(map, size); |
111 | close(fileDescriptor); |
112 | } |
113 | |
114 | void* map; |
115 | size_t size; |
116 | int fileDescriptor; |
117 | }; |
118 | |
119 | static void deleteMapWrapper(MapWrapper* wrapper) |
120 | { |
121 | delete wrapper; |
122 | } |
123 | |
124 | Data Data::adoptMap(void* map, size_t size, int fd) |
125 | { |
126 | ASSERT(map); |
127 | ASSERT(map != MAP_FAILED); |
128 | MapWrapper* wrapper = new MapWrapper { map, size, fd }; |
129 | GRefPtr<SoupBuffer> buffer = adoptGRef(soup_buffer_new_with_owner(map, size, wrapper, reinterpret_cast<GDestroyNotify>(deleteMapWrapper))); |
130 | return { WTFMove(buffer), fd }; |
131 | } |
132 | |
133 | #if USE(GLIB) && !PLATFORM(WIN) |
134 | Data adoptAndMapFile(GFileIOStream* stream, size_t offset, size_t size) |
135 | { |
136 | GInputStream* inputStream = g_io_stream_get_input_stream(G_IO_STREAM(stream)); |
137 | int fd = g_file_descriptor_based_get_fd(G_FILE_DESCRIPTOR_BASED(inputStream)); |
138 | return adoptAndMapFile(fd, offset, size); |
139 | } |
140 | #endif |
141 | |
142 | RefPtr<SharedMemory> Data::tryCreateSharedMemory() const |
143 | { |
144 | if (isNull() || !isMap()) |
145 | return nullptr; |
146 | |
147 | return SharedMemory::wrapMap(const_cast<char*>(m_buffer->data), m_buffer->length, m_fileDescriptor); |
148 | } |
149 | |
150 | } // namespace NetworkCache |
151 | } // namespace WebKit |
152 | |