1 | /* |
2 | * Copyright (C) 2011 Google 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 are |
6 | * met: |
7 | * |
8 | * * Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * * Redistributions in binary form must reproduce the above |
11 | * copyright notice, this list of conditions and the following disclaimer |
12 | * in the documentation and/or other materials provided with the |
13 | * distribution. |
14 | * * Neither the name of Google Inc. nor the names of its |
15 | * contributors may be used to endorse or promote products derived from |
16 | * this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | #include "config.h" |
32 | #include "SharedBufferChunkReader.h" |
33 | |
34 | #if ENABLE(MHTML) |
35 | |
36 | // FIXME: This class is overkill. Remove this class and just iterate the segments of a SharedBuffer |
37 | // using the cool new SharedBuffer::begin() and SharedBuffer::end() instead of using this class. |
38 | |
39 | #include "SharedBuffer.h" |
40 | |
41 | namespace WebCore { |
42 | |
43 | SharedBufferChunkReader::SharedBufferChunkReader(SharedBuffer* buffer, const Vector<char>& separator) |
44 | : m_buffer(buffer) |
45 | , m_bufferPosition(0) |
46 | , m_segment(0) |
47 | , m_segmentLength(0) |
48 | , m_segmentIndex(0) |
49 | , m_reachedEndOfFile(false) |
50 | , m_separator(separator) |
51 | , m_separatorIndex(0) |
52 | { |
53 | } |
54 | |
55 | SharedBufferChunkReader::SharedBufferChunkReader(SharedBuffer* buffer, const char* separator) |
56 | : m_buffer(buffer) |
57 | , m_bufferPosition(0) |
58 | , m_segment(0) |
59 | , m_segmentLength(0) |
60 | , m_segmentIndex(0) |
61 | , m_reachedEndOfFile(false) |
62 | , m_separatorIndex(0) |
63 | { |
64 | setSeparator(separator); |
65 | } |
66 | |
67 | void SharedBufferChunkReader::setSeparator(const Vector<char>& separator) |
68 | { |
69 | m_separator = separator; |
70 | } |
71 | |
72 | void SharedBufferChunkReader::setSeparator(const char* separator) |
73 | { |
74 | m_separator.clear(); |
75 | m_separator.append(separator, strlen(separator)); |
76 | } |
77 | |
78 | bool SharedBufferChunkReader::nextChunk(Vector<char>& chunk, bool includeSeparator) |
79 | { |
80 | if (m_reachedEndOfFile) |
81 | return false; |
82 | |
83 | chunk.clear(); |
84 | while (true) { |
85 | while (m_segmentIndex < m_segmentLength) { |
86 | char currentCharacter = m_segment[m_segmentIndex++]; |
87 | if (currentCharacter != m_separator[m_separatorIndex]) { |
88 | if (m_separatorIndex > 0) { |
89 | ASSERT_WITH_SECURITY_IMPLICATION(m_separatorIndex <= m_separator.size()); |
90 | chunk.append(m_separator.data(), m_separatorIndex); |
91 | m_separatorIndex = 0; |
92 | } |
93 | chunk.append(currentCharacter); |
94 | continue; |
95 | } |
96 | m_separatorIndex++; |
97 | if (m_separatorIndex == m_separator.size()) { |
98 | if (includeSeparator) |
99 | chunk.appendVector(m_separator); |
100 | m_separatorIndex = 0; |
101 | return true; |
102 | } |
103 | } |
104 | |
105 | // Read the next segment. |
106 | m_segmentIndex = 0; |
107 | m_bufferPosition += m_segmentLength; |
108 | // Let's pretend all the data is in one block. |
109 | // FIXME: This class should be removed in favor of just iterating the segments of the SharedBuffer. |
110 | m_segment = m_buffer->data() + m_bufferPosition; |
111 | m_segmentLength = m_buffer->size() - m_bufferPosition; |
112 | if (!m_segmentLength) { |
113 | m_reachedEndOfFile = true; |
114 | if (m_separatorIndex > 0) |
115 | chunk.append(m_separator.data(), m_separatorIndex); |
116 | return !chunk.isEmpty(); |
117 | } |
118 | } |
119 | ASSERT_NOT_REACHED(); |
120 | return false; |
121 | } |
122 | |
123 | String SharedBufferChunkReader::nextChunkAsUTF8StringWithLatin1Fallback(bool includeSeparator) |
124 | { |
125 | Vector<char> data; |
126 | if (!nextChunk(data, includeSeparator)) |
127 | return String(); |
128 | |
129 | return data.size() ? String::fromUTF8WithLatin1Fallback(data.data(), data.size()) : emptyString(); |
130 | } |
131 | |
132 | size_t SharedBufferChunkReader::peek(Vector<char>& data, size_t requestedSize) |
133 | { |
134 | data.clear(); |
135 | if (requestedSize <= m_segmentLength - m_segmentIndex) { |
136 | data.append(m_segment + m_segmentIndex, requestedSize); |
137 | return requestedSize; |
138 | } |
139 | |
140 | size_t readBytesCount = m_segmentLength - m_segmentIndex; |
141 | data.append(m_segment + m_segmentIndex, readBytesCount); |
142 | |
143 | size_t bufferPosition = m_bufferPosition + m_segmentLength; |
144 | const char* segment = 0; |
145 | |
146 | // Let's pretend all the data is in one block. |
147 | // FIXME: This class should be removed in favor of just iterating the segments of the SharedBuffer. |
148 | if (bufferPosition != m_buffer->size()) { |
149 | segment = m_buffer->data() + bufferPosition; |
150 | size_t segmentLength = m_buffer->size() - bufferPosition; |
151 | if (segmentLength > requestedSize) |
152 | segmentLength = requestedSize; |
153 | data.append(segment, segmentLength); |
154 | readBytesCount += segmentLength; |
155 | bufferPosition += segmentLength; |
156 | } |
157 | return readBytesCount; |
158 | } |
159 | |
160 | } |
161 | |
162 | #endif |
163 | |