| 6 | #define BASE_CONTAINERS_BUFFER_ITERATOR_H_ |
| 7 |
| 8 | #include <type_traits> |
| 9 |
| 10 | #include "base/bit_cast.h" |
| 11 | #include "base/containers/span.h" |
| 12 | #include "base/numerics/checked_math.h" |
| 13 |
| 14 | namespace base { |
| 15 |
| 16 | // BufferIterator is a bounds-checked container utility to access variable- |
| 17 | // length, heterogeneous structures contained within a buffer. If the data are |
| 18 | // homogeneous, use base::span<> instead. |
| 19 | // |
| 20 | // After being created with a weakly-owned buffer, BufferIterator returns |
| 21 | // pointers to structured data within the buffer. After each method call that |
| 22 | // returns data in the buffer, the iterator position is advanced by the byte |
| 23 | // size of the object (or span of objects) returned. If there are not enough |
| 24 | // bytes remaining in the buffer to return the requested object(s), a nullptr |
| 25 | // or empty span is returned. |
| 26 | // |
| 27 | // This class is similar to base::Pickle, which should be preferred for |
| 28 | // serializing to disk. Pickle versions its header and does not support writing |
| 29 | // structures, which are problematic for serialization due to struct padding and |
| 30 | // version shear concerns. |
| 31 | // |
| 32 | // Example usage: |
| 33 | // |
| 34 | // std::vector<uint8_t> buffer(4096); |
| 35 | // if (!ReadSomeData(&buffer, buffer.size())) { |
| 36 | // LOG(ERROR) << "Failed to read data."; |
| 37 | // return false; |
| 38 | // } |
| 39 | // |
| 40 | // BufferIterator<uint8_t> iterator(buffer); |
| 41 | // uint32_t* num_items = iterator.Object<uint32_t>(); |
| 42 | // if (!num_items) { |
| 43 | // LOG(ERROR) << "No num_items field." |
| 44 | // return false; |
| 45 | // } |
| 46 | // |
| 47 | // base::span<const item_struct> items = |
| 48 | // iterator.Span<item_struct>(*num_items); |
| 49 | // if (items.size() != *num_items) { |
| 50 | // LOG(ERROR) << "Not enough items."; |
| 51 | // return false; |
| 52 | // } |
| 53 | // |
| 54 | // // ... validate the objects in |items|. |
| 55 | template <typename B> |
| 56 | class BufferIterator { |
| 57 | public: |
| 58 | static_assert(std::is_same<std::remove_const_t<B>, char>::value || |
| 59 | std::is_same<std::remove_const_t<B>, unsigned char>::value, |
| 60 | "Underlying buffer type must be char-type."); |
| 61 |
| 62 | BufferIterator() {} |
| 63 | BufferIterator(B* data, size_t size) |
| 64 | : BufferIterator(make_span(data, size)) {} |
| 65 | explicit BufferIterator(span<B> buffer) |
| 66 | : buffer_(buffer), remaining_(buffer) {} |
| 67 | ~BufferIterator() {} |
| 68 |
| 69 | // Returns a pointer to a mutable structure T in the buffer at the current |
| 70 | // position. On success, the iterator position is advanced by sizeof(T). If |
| 71 | // there are not sizeof(T) bytes remaining in the buffer, returns nullptr. |
| 72 | template <typename T, |
| 73 | typename = |
| 74 | typename std::enable_if_t<std::is_trivially_copyable<T>::value>> |
| 75 | T* MutableObject() { |
| 76 | size_t size = sizeof(T); |
| 77 | size_t next_position; |
| 78 | if (!CheckAdd(position(), size).AssignIfValid(&next_position)) |
| 79 | return nullptr; |
| 80 | if (next_position > total_size()) |
| 81 | return nullptr; |
| 82 | T* t = bit_cast<T*>(remaining_.data()); |
| 83 | remaining_ = remaining_.subspan(size); |
| 84 | return t; |
| 85 | } |
| 86 |
| 87 | // Returns a const pointer to an object of type T in the buffer at the current |
| 88 | // position. |
| 89 | template <typename T, |
| 90 | typename = |
| 91 | typename std::enable_if_t<std::is_trivially_copyable<T>::value>> |
| 92 | const T* Object() { |
| 93 | return MutableObject<const T>(); |
| 94 | } |
| 95 |
| 96 | // Returns a span of |count| T objects in the buffer at the current position. |
| 97 | // On success, the iterator position is advanced by |sizeof(T) * count|. If |
| 98 | // there are not enough bytes remaining in the buffer to fulfill the request, |
| 99 | // returns an empty span. |
| 100 | template <typename T, |
| 101 | typename = |
| 102 | typename std::enable_if_t<std::is_trivially_copyable<T>::value>> |
| 103 | span<T> MutableSpan(size_t count) { |
| 104 | size_t size; |
| 105 | if (!CheckMul(sizeof(T), count).AssignIfValid(&size)) |
| 106 | return span<T>(); |
| 107 | size_t next_position; |
| 108 | if (!CheckAdd(position(), size).AssignIfValid(&next_position)) |
| 109 | return span<T>(); |
| 110 | if (next_position > total_size()) |
| 111 | return span<T>(); |
| 112 | auto result = span<T>(bit_cast<T*>(remaining_.data()), count); |
| 113 | remaining_ = remaining_.subspan(size); |
| 114 | return result; |
| 115 | } |
| 116 |
| 117 | // Returns a span to |count| const objects of type T in the buffer at the |
| 118 | // current position. |
| 119 | template <typename T, |
| 120 | typename = |
| 121 | typename std::enable_if_t<std::is_trivially_copyable<T>::value>> |
| 122 | span<const T> Span(size_t count) { |
| 123 | return MutableSpan<const T>(count); |
| 124 | } |
| 125 |
| 126 | // Resets the iterator position to the absolute offset |to|. |
| 127 | void Seek(size_t to) { remaining_ = buffer_.subspan(to); } |
| 128 |
| 129 | // Returns the total size of the underlying buffer. |
| 130 | size_t total_size() { return buffer_.size(); } |
| 131 |
| 132 | // Returns the current position in the buffer. |
| 133 | size_t position() { return buffer_.size_bytes() - remaining_.size_bytes(); } |
| 134 |
| 135 | private: |
| 136 | // The original buffer that the iterator was constructed with. |
| 137 | const span<B> buffer_; |
| 138 | // A subspan of |buffer_| containing the remaining bytes to iterate over. |
| 139 | span<B> remaining_; |
| 140 | // Copy and assign allowed. |
| 141 | }; |
| 142 |
| 143 | } // namespace base |
| 144 |
| 145 | #endif // BASE_CONTAINERS_BUFFER_ITERATOR_H_ |
| 146 |