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) const36 Iterator<little_endian> Iterator<little_endian>::operator+(int offset) const {
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) const55 Iterator<little_endian> Iterator<little_endian>::operator-(int offset) const {
56   auto itr(*this);
57 
58   return itr -= offset;
59 }
60 
61 template <bool little_endian>
operator -(const Iterator<little_endian> & itr) const62 int Iterator<little_endian>::operator-(const Iterator<little_endian>& itr) const {
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) {
76     index_--;
77   }
78   return *this;
79 }
80 
81 template <bool little_endian>
operator =(const Iterator<little_endian> & itr)82 Iterator<little_endian>& Iterator<little_endian>::operator=(const Iterator<little_endian>& itr) {
83   if (this == &itr) {
84     return *this;
85   }
86   this->data_ = itr.data_;
87   this->begin_ = itr.begin_;
88   this->end_ = itr.end_;
89   this->index_ = itr.index_;
90   return *this;
91 }
92 
93 template <bool little_endian>
operator ==(const Iterator<little_endian> & itr) const94 bool Iterator<little_endian>::operator==(const Iterator<little_endian>& itr) const {
95   return index_ == itr.index_;
96 }
97 
98 template <bool little_endian>
operator !=(const Iterator<little_endian> & itr) const99 bool Iterator<little_endian>::operator!=(const Iterator<little_endian>& itr) const {
100   return !(*this == itr);
101 }
102 
103 template <bool little_endian>
operator <(const Iterator<little_endian> & itr) const104 bool Iterator<little_endian>::operator<(const Iterator<little_endian>& itr) const {
105   return index_ < itr.index_;
106 }
107 
108 template <bool little_endian>
operator >(const Iterator<little_endian> & itr) const109 bool Iterator<little_endian>::operator>(const Iterator<little_endian>& itr) const {
110   return index_ > itr.index_;
111 }
112 
113 template <bool little_endian>
operator <=(const Iterator<little_endian> & itr) const114 bool Iterator<little_endian>::operator<=(const Iterator<little_endian>& itr) const {
115   return index_ <= itr.index_;
116 }
117 
118 template <bool little_endian>
operator >=(const Iterator<little_endian> & itr) const119 bool Iterator<little_endian>::operator>=(const Iterator<little_endian>& itr) const {
120   return index_ >= itr.index_;
121 }
122 
123 template <bool little_endian>
operator *() const124 uint8_t Iterator<little_endian>::operator*() const {
125   ASSERT_LOG(
126       NumBytesRemaining() > 0,
127       "Index %zu out of bounds: [%zu,%zu)",
128       index_,
129       begin_,
130       end_);
131   size_t index = index_;
132 
133   for (auto view : data_) {
134     if (index < view.size()) {
135       return view[index];
136     }
137     index -= view.size();
138   }
139   ASSERT_LOG(false, "Out of fragments searching for index %zu", index_);
140   return 0;
141 }
142 
143 template <bool little_endian>
NumBytesRemaining() const144 size_t Iterator<little_endian>::NumBytesRemaining() const {
145   if (end_ > index_ && index_ >= begin_) {
146     return end_ - index_;
147   }
148   return 0;
149 }
150 
151 template <bool little_endian>
Subrange(size_t index,size_t length) const152 Iterator<little_endian> Iterator<little_endian>::Subrange(size_t index, size_t length) const {
153   Iterator<little_endian> to_return(*this);
154   if (to_return.NumBytesRemaining() > index) {
155     to_return.index_ = to_return.index_ + index;
156     to_return.begin_ = to_return.index_;
157     if (to_return.NumBytesRemaining() >= length) {
158       to_return.end_ = to_return.index_ + length;
159     }
160   } else {
161     to_return.end_ = 0;
162   }
163 
164   return to_return;
165 }
166 
167 // Explicit instantiations for both types of Iterators.
168 template class Iterator<true>;
169 template class Iterator<false>;
170 }  // namespace packet
171 }  // namespace bluetooth
172