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(std::forward_list<View> data,size_t offset)25 Iterator<little_endian>::Iterator(std::forward_list<View> data, size_t offset) {
26 data_ = data;
27 index_ = offset;
28 length_ = 0;
29 for (auto& view : data) {
30 length_ += view.size();
31 }
32 }
33
34 template <bool little_endian>
operator +(int offset)35 Iterator<little_endian> Iterator<little_endian>::operator+(int offset) {
36 auto itr(*this);
37
38 return itr += offset;
39 }
40
41 template <bool little_endian>
operator +=(int offset)42 Iterator<little_endian>& Iterator<little_endian>::operator+=(int offset) {
43 index_ += offset;
44 return *this;
45 }
46
47 template <bool little_endian>
operator ++(int)48 Iterator<little_endian> Iterator<little_endian>::operator++(int) {
49 auto itr(*this);
50 index_++;
51 return itr;
52 }
53
54 template <bool little_endian>
operator ++()55 Iterator<little_endian>& Iterator<little_endian>::operator++() {
56 index_++;
57 return *this;
58 }
59
60 template <bool little_endian>
operator -(int offset)61 Iterator<little_endian> Iterator<little_endian>::operator-(int offset) {
62 auto itr(*this);
63
64 return itr -= offset;
65 }
66
67 template <bool little_endian>
operator -(Iterator<little_endian> & itr)68 int Iterator<little_endian>::operator-(Iterator<little_endian>& itr) {
69 return index_ - itr.index_;
70 }
71
72 template <bool little_endian>
operator -=(int offset)73 Iterator<little_endian>& Iterator<little_endian>::operator-=(int offset) {
74 index_ -= offset;
75
76 return *this;
77 }
78
79 template <bool little_endian>
operator --(int)80 Iterator<little_endian> Iterator<little_endian>::operator--(int) {
81 auto itr(*this);
82 if (index_ != 0) index_--;
83
84 return itr;
85 }
86
87 template <bool little_endian>
operator --()88 Iterator<little_endian>& Iterator<little_endian>::operator--() {
89 if (index_ != 0) index_--;
90
91 return *this;
92 }
93
94 template <bool little_endian>
operator =(const Iterator<little_endian> & itr)95 Iterator<little_endian>& Iterator<little_endian>::operator=(const Iterator<little_endian>& itr) {
96 data_ = itr.data_;
97 index_ = itr.index_;
98
99 return *this;
100 }
101
102 template <bool little_endian>
operator ==(const Iterator<little_endian> & itr) const103 bool Iterator<little_endian>::operator==(const Iterator<little_endian>& itr) const {
104 return index_ == itr.index_;
105 }
106
107 template <bool little_endian>
operator !=(const Iterator<little_endian> & itr) const108 bool Iterator<little_endian>::operator!=(const Iterator<little_endian>& itr) const {
109 return !(*this == itr);
110 }
111
112 template <bool little_endian>
operator <(const Iterator<little_endian> & itr) const113 bool Iterator<little_endian>::operator<(const Iterator<little_endian>& itr) const {
114 return index_ < itr.index_;
115 }
116
117 template <bool little_endian>
operator >(const Iterator<little_endian> & itr) const118 bool Iterator<little_endian>::operator>(const Iterator<little_endian>& itr) const {
119 return index_ > itr.index_;
120 }
121
122 template <bool little_endian>
operator <=(const Iterator<little_endian> & itr) const123 bool Iterator<little_endian>::operator<=(const Iterator<little_endian>& itr) const {
124 return index_ <= itr.index_;
125 }
126
127 template <bool little_endian>
operator >=(const Iterator<little_endian> & itr) const128 bool Iterator<little_endian>::operator>=(const Iterator<little_endian>& itr) const {
129 return index_ >= itr.index_;
130 }
131
132 template <bool little_endian>
operator *() const133 uint8_t Iterator<little_endian>::operator*() const {
134 ASSERT_LOG(index_ < length_, "Index %zu out of bounds: %zu", index_, length_);
135 size_t index = index_;
136
137 for (auto view : data_) {
138 if (index < view.size()) {
139 return view[index];
140 }
141 index -= view.size();
142 }
143 ASSERT_LOG(false, "Out of fragments searching for index %zu", index_);
144 return 0;
145 }
146
147 template <bool little_endian>
NumBytesRemaining() const148 size_t Iterator<little_endian>::NumBytesRemaining() const {
149 if (length_ > index_) {
150 return length_ - index_;
151 } else {
152 return 0;
153 }
154 }
155
156 // Explicit instantiations for both types of Iterators.
157 template class Iterator<true>;
158 template class Iterator<false>;
159 } // namespace packet
160 } // namespace bluetooth
161