• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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_view.h"
18 
19 #include <algorithm>
20 
21 #include <base/logging.h>
22 
23 namespace test_vendor_lib {
24 namespace packets {
25 
26 template <bool little_endian>
PacketView(const std::forward_list<class View> fragments)27 PacketView<little_endian>::PacketView(const std::forward_list<class View> fragments)
28     : fragments_(fragments), length_(0) {
29   for (auto fragment : fragments_) {
30     length_ += fragment.size();
31   }
32 }
33 
34 template <bool little_endian>
PacketView(std::shared_ptr<std::vector<uint8_t>> packet)35 PacketView<little_endian>::PacketView(std::shared_ptr<std::vector<uint8_t>> packet)
36     : fragments_({View(packet, 0, packet->size())}), length_(packet->size()) {}
37 
38 template <bool little_endian>
begin() const39 Iterator<little_endian> PacketView<little_endian>::begin() const {
40   return Iterator<little_endian>(this->fragments_, 0);
41 }
42 
43 template <bool little_endian>
end() const44 Iterator<little_endian> PacketView<little_endian>::end() const {
45   return Iterator<little_endian>(this->fragments_, size());
46 }
47 
48 template <bool little_endian>
operator [](size_t index) const49 uint8_t PacketView<little_endian>::operator[](size_t index) const {
50   return at(index);
51 }
52 
53 template <bool little_endian>
at(size_t index) const54 uint8_t PacketView<little_endian>::at(size_t index) const {
55   CHECK(index < length_) << "Index " << index << " out of bounds";
56   for (const auto& fragment : fragments_) {
57     if (index < fragment.size()) {
58       return fragment[index];
59     }
60     index -= fragment.size();
61   }
62   CHECK(false) << "Out of fragments searching for Index " << index;
63   return 0;
64 }
65 
66 template <bool little_endian>
size() const67 size_t PacketView<little_endian>::size() const {
68   return length_;
69 }
70 
71 template <bool little_endian>
SubViewList(size_t begin,size_t end) const72 std::forward_list<View> PacketView<little_endian>::SubViewList(size_t begin, size_t end) const {
73   CHECK(begin <= end) << "Begin " << begin << " is past end";
74   CHECK(end <= length_) << "End " << end << " is too large";
75   std::forward_list<View> view_list;
76   std::forward_list<View>::iterator it = view_list.before_begin();
77   size_t length = end - begin;
78   for (const auto& fragment : fragments_) {
79     if (begin >= fragment.size()) {
80       begin -= fragment.size();
81     } else {
82       View view(fragment, begin, begin + std::min(length, fragment.size() - begin));
83       length -= view.size();
84       it = view_list.insert_after(it, view);
85       begin = 0;
86     }
87   }
88   return view_list;
89 }
90 
91 template <bool little_endian>
SubViewLittleEndian(size_t begin,size_t end) const92 PacketView<true> PacketView<little_endian>::SubViewLittleEndian(size_t begin, size_t end) const {
93   return PacketView<true>(SubViewList(begin, end));
94 }
95 
96 template <bool little_endian>
SubViewBigEndian(size_t begin,size_t end) const97 PacketView<false> PacketView<little_endian>::SubViewBigEndian(size_t begin, size_t end) const {
98   return PacketView<false>(SubViewList(begin, end));
99 }
100 
101 // Explicit instantiations for both types of PacketViews.
102 template class PacketView<true>;
103 template class PacketView<false>;
104 
105 }  // namespace packets
106 }  // namespace test_vendor_lib
107