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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
* Copyright (c) 2022, Lucas Chollet <lucas.chollet@free.fr>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CircularBuffer.h>
#include <AK/MemMem.h>
#include <AK/Stream.h>
namespace AK {
CircularBuffer::CircularBuffer(ByteBuffer buffer)
: m_buffer(move(buffer))
{
}
ErrorOr<CircularBuffer> CircularBuffer::create_empty(size_t size)
{
auto temporary_buffer = TRY(ByteBuffer::create_uninitialized(size));
CircularBuffer circular_buffer { move(temporary_buffer) };
return circular_buffer;
}
ErrorOr<CircularBuffer> CircularBuffer::create_initialized(ByteBuffer buffer)
{
CircularBuffer circular_buffer { move(buffer) };
circular_buffer.m_used_space = circular_buffer.m_buffer.size();
return circular_buffer;
}
size_t CircularBuffer::empty_space() const
{
return capacity() - m_used_space;
}
size_t CircularBuffer::used_space() const
{
return m_used_space;
}
size_t CircularBuffer::capacity() const
{
return m_buffer.size();
}
size_t CircularBuffer::seekback_limit() const
{
return m_seekback_limit;
}
bool CircularBuffer::is_wrapping_around() const
{
return capacity() <= m_reading_head + m_used_space;
}
Optional<size_t> CircularBuffer::offset_of(StringView needle, Optional<size_t> from, Optional<size_t> until) const
{
auto const read_from = from.value_or(0);
auto const read_until = until.value_or(m_used_space);
VERIFY(read_from <= read_until);
Array<ReadonlyBytes, 2> spans {};
spans[0] = next_read_span();
auto const original_span_0_size = spans[0].size();
if (read_from > 0)
spans[0] = spans[0].slice(min(spans[0].size(), read_from));
if (spans[0].size() + read_from > read_until)
spans[0] = spans[0].trim(read_until - read_from);
else if (is_wrapping_around())
spans[1] = m_buffer.span().slice(max(original_span_0_size, read_from) - original_span_0_size, min(read_until, m_used_space) - original_span_0_size);
auto maybe_found = AK::memmem(spans.begin(), spans.end(), needle.bytes());
if (maybe_found.has_value())
*maybe_found += read_from;
return maybe_found;
}
void CircularBuffer::clear()
{
m_reading_head = 0;
m_used_space = 0;
m_seekback_limit = 0;
}
Bytes CircularBuffer::next_write_span()
{
if (is_wrapping_around())
return m_buffer.span().slice(m_reading_head + m_used_space - capacity(), capacity() - m_used_space);
return m_buffer.span().slice(m_reading_head + m_used_space, capacity() - (m_reading_head + m_used_space));
}
ReadonlyBytes CircularBuffer::next_read_span() const
{
return m_buffer.span().slice(m_reading_head, min(capacity() - m_reading_head, m_used_space));
}
ReadonlyBytes CircularBuffer::next_read_span_with_seekback(size_t distance) const
{
VERIFY(m_seekback_limit <= capacity());
VERIFY(distance <= m_seekback_limit);
// Note: We are adding the capacity once here to ensure that we can wrap around the negative space by using modulo.
auto read_offset = (capacity() + m_reading_head + m_used_space - distance) % capacity();
return m_buffer.span().slice(read_offset, min(capacity() - read_offset, distance));
}
size_t CircularBuffer::write(ReadonlyBytes bytes)
{
auto remaining = bytes.size();
while (remaining > 0) {
auto const next_span = next_write_span();
if (next_span.size() == 0)
break;
auto const written_bytes = bytes.slice(bytes.size() - remaining).copy_trimmed_to(next_span);
m_used_space += written_bytes;
m_seekback_limit += written_bytes;
if (m_seekback_limit > capacity())
m_seekback_limit = capacity();
remaining -= written_bytes;
}
return bytes.size() - remaining;
}
Bytes CircularBuffer::read(Bytes bytes)
{
auto remaining = bytes.size();
while (remaining > 0) {
auto const next_span = next_read_span();
if (next_span.size() == 0)
break;
auto written_bytes = next_span.copy_trimmed_to(bytes.slice(bytes.size() - remaining));
m_used_space -= written_bytes;
m_reading_head += written_bytes;
if (m_reading_head >= capacity())
m_reading_head -= capacity();
remaining -= written_bytes;
}
return bytes.trim(bytes.size() - remaining);
}
ErrorOr<Bytes> CircularBuffer::read_with_seekback(Bytes bytes, size_t distance)
{
if (distance > m_seekback_limit)
return Error::from_string_literal("Tried a seekback read beyond the seekback limit");
auto remaining = bytes.size();
while (remaining > 0) {
auto const next_span = next_read_span_with_seekback(distance);
if (next_span.size() == 0)
break;
auto written_bytes = next_span.copy_trimmed_to(bytes.slice(bytes.size() - remaining));
distance -= written_bytes;
remaining -= written_bytes;
}
return bytes.trim(bytes.size() - remaining);
}
ErrorOr<void> CircularBuffer::discard(size_t discarding_size)
{
if (m_used_space < discarding_size)
return Error::from_string_literal("Can not discard more data than what the buffer contains");
m_used_space -= discarding_size;
m_reading_head = (m_reading_head + discarding_size) % capacity();
return {};
}
ErrorOr<size_t> CircularBuffer::fill_from_stream(Stream& stream)
{
auto next_span = next_write_span();
if (next_span.size() == 0)
return 0;
auto bytes = TRY(stream.read_some(next_span));
m_used_space += bytes.size();
m_seekback_limit += bytes.size();
if (m_seekback_limit > capacity())
m_seekback_limit = capacity();
return bytes.size();
}
ErrorOr<size_t> CircularBuffer::flush_to_stream(Stream& stream)
{
auto next_span = next_read_span();
if (next_span.size() == 0)
return 0;
auto written_bytes = TRY(stream.write_some(next_span));
m_used_space -= written_bytes;
m_reading_head += written_bytes;
if (m_reading_head >= capacity())
m_reading_head -= capacity();
return written_bytes;
}
ErrorOr<size_t> CircularBuffer::copy_from_seekback(size_t distance, size_t length)
{
if (distance > m_seekback_limit)
return Error::from_string_literal("Tried a seekback copy beyond the seekback limit");
auto remaining_length = length;
while (remaining_length > 0) {
if (empty_space() == 0)
break;
auto next_span = next_read_span_with_seekback(distance);
if (next_span.size() == 0)
break;
auto length_written = write(next_span.trim(remaining_length));
remaining_length -= length_written;
// If we copied right from the end of the seekback area (i.e. our length is larger than the distance)
// and the last copy was one complete "chunk", we can now double the distance to copy twice as much data in one go.
if (remaining_length > distance && length_written == distance)
distance *= 2;
}
return length - remaining_length;
}
ErrorOr<Vector<CircularBuffer::Match>> CircularBuffer::find_copy_in_seekback(size_t maximum_length, size_t minimum_length, Optional<Vector<size_t> const&> distance_hints) const
{
VERIFY(minimum_length > 0);
// Clip the maximum length to the amount of data that we actually store.
if (maximum_length > m_used_space)
maximum_length = m_used_space;
if (maximum_length < minimum_length)
return Vector<Match> {};
Vector<Match> matches;
if (distance_hints.has_value()) {
// If we have any hints, verify and use those.
for (auto const& distance : distance_hints.value()) {
// TODO: This does not yet support looping repetitions.
if (distance < minimum_length)
continue;
auto needle_offset = (capacity() + m_reading_head) % capacity();
auto haystack_offset = (capacity() + m_reading_head - distance) % capacity();
for (size_t i = 0; i < minimum_length; i++) {
if (m_buffer[needle_offset] != m_buffer[haystack_offset])
break;
needle_offset = (needle_offset + 1) % capacity();
haystack_offset = (haystack_offset + 1) % capacity();
if (i + 1 == minimum_length)
TRY(matches.try_empend(distance, minimum_length));
}
}
} else {
// Otherwise, use memmem to find the initial matches.
// Note: We have the read head as our reference point, but `next_read_span_with_seekback` isn't aware of that and continues to use the write head.
// Therefore, we need to make sure to slice off the extraneous bytes from the end of the span and shift the returned distances by the correct amount.
size_t haystack_offset_from_start = 0;
Vector<ReadonlyBytes, 2> haystack;
haystack.append(next_read_span_with_seekback(m_seekback_limit));
if (haystack[0].size() < m_seekback_limit - used_space())
haystack.append(next_read_span_with_seekback(m_seekback_limit - haystack[0].size()));
haystack.last() = haystack.last().trim(haystack.last().size() - used_space());
auto needle = next_read_span().trim(minimum_length);
auto memmem_match = AK::memmem(haystack.begin(), haystack.end(), needle);
while (memmem_match.has_value()) {
auto match_offset = memmem_match.release_value();
// Add the match to the list of matches to work with.
TRY(matches.try_empend(m_seekback_limit - used_space() - haystack_offset_from_start - match_offset, minimum_length));
auto size_to_discard = match_offset + 1;
// Trim away the already processed bytes from the haystack.
haystack_offset_from_start += size_to_discard;
while (size_to_discard > 0) {
if (haystack[0].size() < size_to_discard) {
size_to_discard -= haystack[0].size();
haystack.remove(0);
} else {
haystack[0] = haystack[0].slice(size_to_discard);
break;
}
}
if (haystack.size() == 0)
break;
// Try and find the next match.
memmem_match = AK::memmem(haystack.begin(), haystack.end(), needle);
}
}
// From now on, all matches that we have stored have at least a length of `minimum_length` and they all refer to the same value.
// For the remaining part, we will keep checking the next byte incrementally and keep eliminating matches until we eliminated all of them.
Vector<Match> next_matches;
for (size_t offset = minimum_length; offset < maximum_length; offset++) {
auto needle_data = m_buffer[(capacity() + m_reading_head + offset) % capacity()];
for (auto const& match : matches) {
auto haystack_data = m_buffer[(capacity() + m_reading_head - match.distance + offset) % capacity()];
if (haystack_data != needle_data)
continue;
TRY(next_matches.try_empend(match.distance, match.length + 1));
}
if (next_matches.size() == 0)
return matches;
swap(matches, next_matches);
next_matches.clear_with_capacity();
}
return matches;
}
}
|