@@ -70,16 +70,19 @@ void request_storage_base_pimpl::append(char const *data, size_t size) {
70
70
new (std::nothrow) char [chunk_size_], 0 ));
71
71
}
72
72
std::pair<char *, size_t > *chunk = &chunks_.back ();
73
+ BOOST_ASSERT (chunk_size_ >= chunk->second );
73
74
size_t remaining = chunk_size_ - chunk->second ;
74
75
while (remaining < size) {
75
- std::memcpy (chunk->first + chunk->second , data, size - remaining);
76
- chunk->second += size - remaining;
77
- data += remaining;
78
- size -= remaining;
76
+ size_t bytes_to_write = std::min (size - remaining, chunk_size_);
77
+ std::memcpy (chunk->first + chunk->second , data, bytes_to_write);
78
+ chunk->second += bytes_to_write;
79
+ BOOST_ASSERT (chunk->second <= chunk_size_);
80
+ data += bytes_to_write;
81
+ size -= bytes_to_write;
79
82
chunks_.push_back (std::make_pair (
80
83
new (std::nothrow) char [chunk_size_], 0 ));
81
84
chunk = &chunks_.back ();
82
- remaining = chunk_size_ - chunk-> second ;
85
+ remaining = chunk_size_;
83
86
}
84
87
if (size > 0 ) {
85
88
std::memcpy (chunk->first + chunk->second , data, size);
@@ -92,16 +95,20 @@ size_t request_storage_base_pimpl::read(char *destination, size_t offset, size_t
92
95
// First we find which chunk we're going to read from using the provided
93
96
// offset and some arithmetic to determine the correct one.
94
97
size_t chunk_index = offset / chunk_size_;
98
+ offset = offset % chunk_size_;
95
99
96
100
// Then we start copying up to size data either until we've reached the end
97
101
// or we're
98
102
size_t chunks_count = chunks_.size ();
99
103
size_t read_count = 0 ;
100
104
while (size > 0 && chunk_index < chunks_.size ()) {
101
105
size_t bytes_to_read = std::min (chunks_[chunk_index].second , size);
102
- std::memcpy (destination + read_count, chunks_[chunk_index].first , bytes_to_read);
106
+ std::memcpy (destination + read_count,
107
+ chunks_[chunk_index].first + offset,
108
+ bytes_to_read);
103
109
read_count += bytes_to_read;
104
110
size -= bytes_to_read;
111
+ offset = 0 ;
105
112
++chunk_index;
106
113
}
107
114
return read_count;
0 commit comments