Replace WTF::move with WTFMove
[WebKit-https.git] / Source / WebCore / platform / SharedBuffer.cpp
1 /*
2  * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
4  * Copyright (C) 2015 Canon Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
26  */
27
28 #include "config.h"
29 #include "SharedBuffer.h"
30
31 #include <algorithm>
32 #include <wtf/unicode/UTF8.h>
33
34 namespace WebCore {
35
36 #if !USE(NETWORK_CFDATA_ARRAY_CALLBACK)
37
38 static const unsigned segmentSize = 0x1000;
39 static const unsigned segmentPositionMask = 0x0FFF;
40
41 static inline unsigned segmentIndex(unsigned position)
42 {
43     return position / segmentSize;
44 }
45
46 static inline unsigned offsetInSegment(unsigned position)
47 {
48     return position & segmentPositionMask;
49 }
50
51 static inline char* allocateSegment() WARN_UNUSED_RETURN;
52 static inline char* allocateSegment()
53 {
54     return static_cast<char*>(fastMalloc(segmentSize));
55 }
56
57 static inline void freeSegment(char* p)
58 {
59     fastFree(p);
60 }
61
62 #endif
63
64 SharedBuffer::SharedBuffer()
65     : m_buffer(adoptRef(new DataBuffer))
66 {
67 }
68
69 SharedBuffer::SharedBuffer(unsigned size)
70     : m_size(size)
71     , m_buffer(adoptRef(new DataBuffer))
72 {
73 }
74
75 SharedBuffer::SharedBuffer(const char* data, unsigned size)
76     : m_buffer(adoptRef(new DataBuffer))
77 {
78     append(data, size);
79 }
80
81 SharedBuffer::SharedBuffer(const unsigned char* data, unsigned size)
82     : m_buffer(adoptRef(new DataBuffer))
83 {
84     append(reinterpret_cast<const char*>(data), size);
85 }
86
87 SharedBuffer::SharedBuffer(MappedFileData&& fileData)
88     : m_buffer(adoptRef(new DataBuffer))
89     , m_fileData(WTFMove(fileData))
90 {
91 }
92
93 SharedBuffer::~SharedBuffer()
94 {
95     clear();
96 }
97
98 RefPtr<SharedBuffer> SharedBuffer::createWithContentsOfFile(const String& filePath)
99 {
100     bool mappingSuccess;
101     MappedFileData mappedFileData(filePath, mappingSuccess);
102
103     if (!mappingSuccess)
104         return SharedBuffer::createFromReadingFile(filePath);
105
106     return adoptRef(new SharedBuffer(WTFMove(mappedFileData)));
107 }
108
109 PassRefPtr<SharedBuffer> SharedBuffer::adoptVector(Vector<char>& vector)
110 {
111     RefPtr<SharedBuffer> buffer = create();
112     buffer->m_buffer->data.swap(vector);
113     buffer->m_size = buffer->m_buffer->data.size();
114     return buffer.release();
115 }
116
117 unsigned SharedBuffer::size() const
118 {
119     if (hasPlatformData())
120         return platformDataSize();
121     
122     if (m_fileData)
123         return m_fileData.size();
124
125     return m_size;
126 }
127
128 const char* SharedBuffer::data() const
129 {
130     if (hasPlatformData())
131         return platformData();
132
133     if (m_fileData)
134         return static_cast<const char*>(m_fileData.data());
135
136 #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
137     if (const char* buffer = singleDataArrayBuffer())
138         return buffer;
139 #endif
140
141     return this->buffer().data();
142 }
143
144 PassRefPtr<ArrayBuffer> SharedBuffer::createArrayBuffer() const
145 {
146     RefPtr<ArrayBuffer> arrayBuffer = ArrayBuffer::createUninitialized(static_cast<unsigned>(size()), sizeof(char));
147
148     const char* segment = 0;
149     unsigned position = 0;
150     while (unsigned segmentSize = getSomeData(segment, position)) {
151         memcpy(static_cast<char*>(arrayBuffer->data()) + position, segment, segmentSize);
152         position += segmentSize;
153     }
154
155     if (position != arrayBuffer->byteLength()) {
156         ASSERT_NOT_REACHED();
157         // Don't return the incomplete ArrayBuffer.
158         return 0;
159     }
160
161     return arrayBuffer.release();
162 }
163
164 void SharedBuffer::append(SharedBuffer* data)
165 {
166     if (maybeAppendPlatformData(data))
167         return;
168 #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
169     if (maybeAppendDataArray(data))
170         return;
171 #endif
172
173     const char* segment;
174     size_t position = 0;
175     while (size_t length = data->getSomeData(segment, position)) {
176         append(segment, length);
177         position += length;
178     }
179 }
180
181 void SharedBuffer::append(const char* data, unsigned length)
182 {
183     if (!length)
184         return;
185
186     maybeTransferMappedFileData();
187     maybeTransferPlatformData();
188
189 #if !USE(NETWORK_CFDATA_ARRAY_CALLBACK)
190     unsigned positionInSegment = offsetInSegment(m_size - m_buffer->data.size());
191     m_size += length;
192
193     if (m_size <= segmentSize) {
194         // No need to use segments for small resource data
195         if (m_buffer->data.isEmpty())
196             m_buffer->data.reserveInitialCapacity(length);
197         appendToDataBuffer(data, length);
198         return;
199     }
200
201     char* segment;
202     if (!positionInSegment) {
203         segment = allocateSegment();
204         m_segments.append(segment);
205     } else
206         segment = m_segments.last() + positionInSegment;
207
208     unsigned segmentFreeSpace = segmentSize - positionInSegment;
209     unsigned bytesToCopy = std::min(length, segmentFreeSpace);
210
211     for (;;) {
212         memcpy(segment, data, bytesToCopy);
213         if (static_cast<unsigned>(length) == bytesToCopy)
214             break;
215
216         length -= bytesToCopy;
217         data += bytesToCopy;
218         segment = allocateSegment();
219         m_segments.append(segment);
220         bytesToCopy = std::min(length, segmentSize);
221     }
222 #else
223     m_size += length;
224     if (m_buffer->data.isEmpty())
225         m_buffer->data.reserveInitialCapacity(length);
226     appendToDataBuffer(data, length);
227 #endif
228 }
229
230 void SharedBuffer::append(const Vector<char>& data)
231 {
232     append(data.data(), data.size());
233 }
234
235 void SharedBuffer::clear()
236 {
237     m_fileData = { };
238
239     clearPlatformData();
240     
241 #if !USE(NETWORK_CFDATA_ARRAY_CALLBACK)
242     for (char* segment : m_segments)
243         freeSegment(segment);
244     m_segments.clear();
245 #else
246     m_dataArray.clear();
247 #endif
248
249     m_size = 0;
250     clearDataBuffer();
251 }
252
253 Ref<SharedBuffer> SharedBuffer::copy() const
254 {
255     Ref<SharedBuffer> clone { adoptRef(*new SharedBuffer) };
256
257     if (hasPlatformData() || m_fileData) {
258         clone->append(data(), size());
259         return clone;
260     }
261
262     clone->m_size = m_size;
263     clone->m_buffer->data.reserveCapacity(m_size);
264     clone->m_buffer->data.append(m_buffer->data.data(), m_buffer->data.size());
265
266 #if !USE(NETWORK_CFDATA_ARRAY_CALLBACK)
267     for (char* segment : m_segments)
268         clone->m_buffer->data.append(segment, segmentSize);
269 #else
270     for (auto& data : m_dataArray)
271         clone->m_dataArray.append(data.get());
272 #endif
273     ASSERT(clone->size() == size());
274
275     return clone;
276 }
277
278 void SharedBuffer::duplicateDataBufferIfNecessary() const
279 {
280     size_t currentCapacity = m_buffer->data.capacity();
281     if (m_buffer->hasOneRef() || m_size <= currentCapacity)
282         return;
283
284     size_t newCapacity = std::max(static_cast<size_t>(m_size), currentCapacity * 2);
285     RefPtr<DataBuffer> newBuffer = adoptRef(new DataBuffer);
286     newBuffer->data.reserveInitialCapacity(newCapacity);
287     newBuffer->data = m_buffer->data;
288     m_buffer = newBuffer.release();
289 }
290
291 void SharedBuffer::appendToDataBuffer(const char *data, unsigned length) const
292 {
293     duplicateDataBufferIfNecessary();
294     m_buffer->data.append(data, length);
295 }
296
297 void SharedBuffer::clearDataBuffer()
298 {
299     if (!m_buffer->hasOneRef())
300         m_buffer = adoptRef(new DataBuffer);
301     else
302         m_buffer->data.clear();
303 }
304
305 #if !USE(CF)
306 void SharedBuffer::hintMemoryNotNeededSoon()
307 {
308 }
309 #endif
310
311 #if !USE(NETWORK_CFDATA_ARRAY_CALLBACK)
312
313 void SharedBuffer::copyBufferAndClear(char* destination, unsigned bytesToCopy) const
314 {
315     for (char* segment : m_segments) {
316         unsigned effectiveBytesToCopy = std::min(bytesToCopy, segmentSize);
317         memcpy(destination, segment, effectiveBytesToCopy);
318         destination += effectiveBytesToCopy;
319         bytesToCopy -= effectiveBytesToCopy;
320         freeSegment(segment);
321     }
322     m_segments.clear();
323 }
324
325 #endif
326
327 const Vector<char>& SharedBuffer::buffer() const
328 {
329     unsigned bufferSize = m_buffer->data.size();
330     if (m_size > bufferSize) {
331         duplicateDataBufferIfNecessary();
332         m_buffer->data.resize(m_size);
333         copyBufferAndClear(m_buffer->data.data() + bufferSize, m_size - bufferSize);
334     }
335     return m_buffer->data;
336 }
337
338 unsigned SharedBuffer::getSomeData(const char*& someData, unsigned position) const
339 {
340     unsigned totalSize = size();
341     if (position >= totalSize) {
342         someData = 0;
343         return 0;
344     }
345
346     if (hasPlatformData() || m_fileData) {
347         ASSERT_WITH_SECURITY_IMPLICATION(position < size());
348         someData = data() + position;
349         return totalSize - position;
350     }
351
352     ASSERT_WITH_SECURITY_IMPLICATION(position < m_size);
353     unsigned consecutiveSize = m_buffer->data.size();
354     if (position < consecutiveSize) {
355         someData = m_buffer->data.data() + position;
356         return consecutiveSize - position;
357     }
358  
359     position -= consecutiveSize;
360 #if !USE(NETWORK_CFDATA_ARRAY_CALLBACK)
361     unsigned segments = m_segments.size();
362     unsigned maxSegmentedSize = segments * segmentSize;
363     unsigned segment = segmentIndex(position);
364     if (segment < segments) {
365         unsigned bytesLeft = totalSize - consecutiveSize;
366         unsigned segmentedSize = std::min(maxSegmentedSize, bytesLeft);
367
368         unsigned positionInSegment = offsetInSegment(position);
369         someData = m_segments[segment] + positionInSegment;
370         return segment == segments - 1 ? segmentedSize - position : segmentSize - positionInSegment;
371     }
372     ASSERT_NOT_REACHED();
373     return 0;
374 #else
375     return copySomeDataFromDataArray(someData, position);
376 #endif
377 }
378
379 void SharedBuffer::maybeTransferMappedFileData()
380 {
381     if (m_fileData) {
382         auto fileData = WTFMove(m_fileData);
383         append(static_cast<const char*>(fileData.data()), fileData.size());
384     }
385 }
386
387 #if !USE(CF) && !USE(SOUP)
388
389 inline void SharedBuffer::clearPlatformData()
390 {
391 }
392
393 inline void SharedBuffer::maybeTransferPlatformData()
394 {
395 }
396
397 inline bool SharedBuffer::hasPlatformData() const
398 {
399     return false;
400 }
401
402 inline const char* SharedBuffer::platformData() const
403 {
404     ASSERT_NOT_REACHED();
405
406     return 0;
407 }
408
409 inline unsigned SharedBuffer::platformDataSize() const
410 {
411     ASSERT_NOT_REACHED();
412     
413     return 0;
414 }
415
416 inline bool SharedBuffer::maybeAppendPlatformData(SharedBuffer*)
417 {
418     return false;
419 }
420
421 #endif
422
423 PassRefPtr<SharedBuffer> utf8Buffer(const String& string)
424 {
425     // Allocate a buffer big enough to hold all the characters.
426     const int length = string.length();
427     Vector<char> buffer(length * 3);
428
429     // Convert to runs of 8-bit characters.
430     char* p = buffer.data();
431     WTF::Unicode::ConversionResult result;
432     if (length) {
433         if (string.is8Bit()) {
434             const LChar* d = string.characters8();
435             result = WTF::Unicode::convertLatin1ToUTF8(&d, d + length, &p, p + buffer.size());
436         } else {
437             const UChar* d = string.characters16();
438             result = WTF::Unicode::convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), true);
439         }
440         if (result != WTF::Unicode::conversionOK)
441             return nullptr;
442     }
443
444     buffer.shrink(p - buffer.data());
445     return SharedBuffer::adoptVector(buffer);
446 }
447
448 } // namespace WebCore