1 /*
2  * Copyright (C) 2016 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 #ifndef MEMORY_READER_H_
18 #define MEMORY_READER_H_
19 
20 #include <cstdint>
21 #include <tuple>
22 
23 #include "wifilogd/local_utils.h"
24 
25 namespace android {
26 namespace wifilogd {
27 
28 // A non-owning adapter, which helps with reading protocol messages out
29 // of a buffer. This is, essentially, the inverse of ByteBuffer.
30 //
31 // The user is responsible for ensuring that a MemoryReader does not
32 // outlive the underlying buffer.
33 class MemoryReader {
34  public:
MemoryReader(const void * buf,size_t buflen)35   MemoryReader(const void* buf, size_t buflen)
36       : head_(static_cast<const uint8_t*>(buf)), n_bytes_avail_(buflen) {}
37 
MemoryReader(std::tuple<const uint8_t *,size_t> buf)38   explicit MemoryReader(std::tuple<const uint8_t*, size_t> buf)
39       : head_(std::get<0>(buf)), n_bytes_avail_(std::get<1>(buf)) {}
40 
41   // Copies a |T| out of our referenced memory, aborting if sizeof(T) exceeds
42   // the number of bytes available. On success, updates the read position, and
43   // the number of bytes available.
44   template <typename T>
CopyOutOrDie()45   T CopyOutOrDie() {
46     CHECK(head_);
47     const T out{local_utils::CopyFromBufferOrDie<T>(head_, n_bytes_avail_)};
48     head_ += sizeof(T);
49     n_bytes_avail_ -= sizeof(T);
50     return out;
51   }
52 
53   // Returns a pointer to the next bytes available for reading. Aborts if
54   // the number of available bytes is less than |n_bytes|. On success, updates
55   // the read position, and the number of bytes available.
56   RETURNS_NONNULL const uint8_t* GetBytesOrDie(size_t n_bytes);
57 
58   // Returns true if the referenced memory is non-NULL, and has at least
59   // one byte available for reading.
60   explicit operator bool() const { return head_ && n_bytes_avail_; }
61 
62   // Returns the number of bytes available for reading.
size()63   size_t size() const { return (head_ ? n_bytes_avail_ : 0); }
64 
65  private:
66   const uint8_t* head_;  // non-owned
67   size_t n_bytes_avail_;
68 };
69 
70 }  // namespace wifilogd
71 }  // namespace android
72 
73 #endif  // MEMORY_READER_H_
74