xref: /aosp_15_r20/external/pigweed/pw_multibuf/stream.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_multibuf/stream.h"
16 
17 #include <algorithm>
18 #include <cstring>
19 
20 #include "pw_status/status.h"
21 #include "pw_status/status_with_size.h"
22 #include "pw_status/try.h"
23 #include "pw_stream/seek.h"
24 
25 namespace pw::multibuf {
26 
DoWrite(ConstByteSpan data)27 Status Stream::DoWrite(ConstByteSpan data) {
28   while (!data.empty()) {
29     if (iterator_ == multibuf_.end()) {
30       return Status::OutOfRange();
31     }
32 
33     Chunk* current_chunk = iterator_.chunk();
34     const size_t remaining_in_chunk =
35         current_chunk->size() - iterator_.byte_index();
36 
37     const size_t bytes_to_copy = std::min(data.size(), remaining_in_chunk);
38 
39     std::memcpy(current_chunk->data() + iterator_.byte_index(),
40                 data.data(),
41                 bytes_to_copy);
42 
43     data = data.subspan(bytes_to_copy);
44     multibuf_offset_ += bytes_to_copy;
45     iterator_ += bytes_to_copy;
46   }
47 
48   return OkStatus();
49 }
50 
DoRead(ByteSpan destination)51 StatusWithSize Stream::DoRead(ByteSpan destination) {
52   size_t bytes_written = 0;
53 
54   while (!destination.empty()) {
55     if (iterator_ == multibuf_.end()) {
56       if (bytes_written > 0) {
57         return StatusWithSize(bytes_written);
58       }
59       return StatusWithSize::OutOfRange();
60     }
61 
62     Chunk* current_chunk = iterator_.chunk();
63     const size_t remaining_in_chunk =
64         current_chunk->size() - iterator_.byte_index();
65 
66     const size_t bytes_to_copy =
67         std::min(remaining_in_chunk, destination.size());
68     std::memcpy(destination.data(),
69                 current_chunk->data() + iterator_.byte_index(),
70                 bytes_to_copy);
71 
72     destination = destination.subspan(bytes_to_copy);
73     bytes_written += bytes_to_copy;
74     multibuf_offset_ += bytes_to_copy;
75     iterator_ += bytes_to_copy;
76   }
77 
78   return StatusWithSize(bytes_written);
79 }
80 
DoSeek(ptrdiff_t offset,Whence origin)81 Status Stream::DoSeek(ptrdiff_t offset, Whence origin) {
82   size_t new_offset = multibuf_offset_;
83   PW_TRY(stream::CalculateSeek(offset, origin, multibuf_.size(), new_offset));
84 
85   if (new_offset < multibuf_offset_) {
86     return Status::OutOfRange();
87   }
88 
89   iterator_ += new_offset - multibuf_offset_;
90   multibuf_offset_ = new_offset;
91   return iterator_ == multibuf_.end() ? Status::OutOfRange() : OkStatus();
92 }
93 
94 }  // namespace pw::multibuf
95