1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "packet/iterator.h"
18
19 #include "os/log.h"
20
21 namespace bluetooth {
22 namespace packet {
23
24 template <bool little_endian>
Iterator(const std::forward_list<View> & data,size_t offset)25 Iterator<little_endian>::Iterator(const std::forward_list<View>& data, size_t offset) {
26 data_ = data;
27 index_ = offset;
28 begin_ = 0;
29 end_ = 0;
30 for (auto& view : data) {
31 end_ += view.size();
32 }
33 }
34
35 template <bool little_endian>
operator +(int offset)36 Iterator<little_endian> Iterator<little_endian>::operator+(int offset) {
37 auto itr(*this);
38
39 return itr += offset;
40 }
41
42 template <bool little_endian>
operator +=(int offset)43 Iterator<little_endian>& Iterator<little_endian>::operator+=(int offset) {
44 index_ += offset;
45 return *this;
46 }
47
48 template <bool little_endian>
operator ++()49 Iterator<little_endian>& Iterator<little_endian>::operator++() {
50 index_++;
51 return *this;
52 }
53
54 template <bool little_endian>
operator -(int offset)55 Iterator<little_endian> Iterator<little_endian>::operator-(int offset) {
56 auto itr(*this);
57
58 return itr -= offset;
59 }
60
61 template <bool little_endian>
operator -(Iterator<little_endian> & itr)62 int Iterator<little_endian>::operator-(Iterator<little_endian>& itr) {
63 return index_ - itr.index_;
64 }
65
66 template <bool little_endian>
operator -=(int offset)67 Iterator<little_endian>& Iterator<little_endian>::operator-=(int offset) {
68 index_ -= offset;
69
70 return *this;
71 }
72
73 template <bool little_endian>
operator --()74 Iterator<little_endian>& Iterator<little_endian>::operator--() {
75 if (index_ != 0) index_--;
76
77 return *this;
78 }
79
80 template <bool little_endian>
operator =(const Iterator<little_endian> & itr)81 Iterator<little_endian>& Iterator<little_endian>::operator=(const Iterator<little_endian>& itr) {
82 if (this == &itr) return *this;
83 this->data_ = itr.data_;
84 this->begin_ = itr.begin_;
85 this->end_ = itr.end_;
86 this->index_ = itr.index_;
87 return *this;
88 }
89
90 template <bool little_endian>
operator ==(const Iterator<little_endian> & itr) const91 bool Iterator<little_endian>::operator==(const Iterator<little_endian>& itr) const {
92 return index_ == itr.index_;
93 }
94
95 template <bool little_endian>
operator !=(const Iterator<little_endian> & itr) const96 bool Iterator<little_endian>::operator!=(const Iterator<little_endian>& itr) const {
97 return !(*this == itr);
98 }
99
100 template <bool little_endian>
operator <(const Iterator<little_endian> & itr) const101 bool Iterator<little_endian>::operator<(const Iterator<little_endian>& itr) const {
102 return index_ < itr.index_;
103 }
104
105 template <bool little_endian>
operator >(const Iterator<little_endian> & itr) const106 bool Iterator<little_endian>::operator>(const Iterator<little_endian>& itr) const {
107 return index_ > itr.index_;
108 }
109
110 template <bool little_endian>
operator <=(const Iterator<little_endian> & itr) const111 bool Iterator<little_endian>::operator<=(const Iterator<little_endian>& itr) const {
112 return index_ <= itr.index_;
113 }
114
115 template <bool little_endian>
operator >=(const Iterator<little_endian> & itr) const116 bool Iterator<little_endian>::operator>=(const Iterator<little_endian>& itr) const {
117 return index_ >= itr.index_;
118 }
119
120 template <bool little_endian>
operator *() const121 uint8_t Iterator<little_endian>::operator*() const {
122 ASSERT_LOG(index_ < end_ && !(begin_ > index_), "Index %zu out of bounds: [%zu,%zu)", index_, begin_, end_);
123 size_t index = index_;
124
125 for (auto view : data_) {
126 if (index < view.size()) {
127 return view[index];
128 }
129 index -= view.size();
130 }
131 ASSERT_LOG(false, "Out of fragments searching for index %zu", index_);
132 return 0;
133 }
134
135 template <bool little_endian>
NumBytesRemaining() const136 size_t Iterator<little_endian>::NumBytesRemaining() const {
137 if (end_ > index_ && !(begin_ > index_)) {
138 return end_ - index_;
139 } else {
140 return 0;
141 }
142 }
143
144 template <bool little_endian>
Subrange(size_t index,size_t length) const145 Iterator<little_endian> Iterator<little_endian>::Subrange(size_t index, size_t length) const {
146 Iterator<little_endian> to_return(*this);
147 if (to_return.NumBytesRemaining() > index) {
148 to_return.index_ = to_return.index_ + index;
149 to_return.begin_ = to_return.index_;
150 if (to_return.NumBytesRemaining() >= length) {
151 to_return.end_ = to_return.index_ + length;
152 }
153 } else {
154 to_return.end_ = 0;
155 }
156
157 return to_return;
158 }
159
160 // Explicit instantiations for both types of Iterators.
161 template class Iterator<true>;
162 template class Iterator<false>;
163 } // namespace packet
164 } // namespace bluetooth
165