1 /*
2  *  Copyright (c) 2021, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file includes definitions for `Heap::Data` (heap allocated data).
32  */
33 
34 #ifndef HEAP_DATA_HPP_
35 #define HEAP_DATA_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include "common/data.hpp"
40 #include "common/heap.hpp"
41 #include "common/message.hpp"
42 
43 namespace ot {
44 namespace Heap {
45 
46 /**
47  * Represents a heap allocated data.
48  *
49  */
50 class Data
51 {
52 public:
53     /**
54      * Initializes the `Heap::Data` as empty.
55      *
56      */
Data(void)57     Data(void) { mData.Init(nullptr, 0); }
58 
59     /**
60      * This is the move constructor for `Heap::Data`.
61      *
62      * `Heap::Data` is non-copyable (copy constructor is deleted) but move constructor is provided to allow it to to be
63      * used as return type (return by value) from functions/methods (which will then use move semantics).
64      *
65      * @param[in] aData   An rvalue reference to another `Heap::Data` to move from.
66      *
67      */
Data(Data && aData)68     Data(Data &&aData) { TakeFrom(aData); }
69 
70     /**
71      * This is the destructor for `Heap::Data` object.
72      *
73      */
~Data(void)74     ~Data(void) { Free(); }
75 
76     /**
77      * Indicates whether or not the `Heap::Data` is null (i.e., it was never successfully set or it was
78      * freed).
79      *
80      * @retval TRUE  The `Heap::Data` is null.
81      * @retval FALSE The `Heap::Data` is not null.
82      *
83      */
IsNull(void) const84     bool IsNull(void) const { return (mData.GetBytes() == nullptr); }
85 
86     /**
87      * Returns a pointer to the `Heap::Data` bytes buffer.
88      *
89      * @returns A pointer to data buffer or `nullptr` if the `Heap::Data` is null (never set or freed).
90      *
91      */
GetBytes(void) const92     const uint8_t *GetBytes(void) const { return mData.GetBytes(); }
93 
94     /**
95      * Returns the `Heap::Data` length.
96      *
97      * @returns The data length (number of bytes) or zero if the `HeapData` is null.
98      *
99      */
GetLength(void) const100     uint16_t GetLength(void) const { return mData.GetLength(); }
101 
102     /**
103      * Sets the `Heap::Data` from the content of a given buffer.
104      *
105      * @param[in] aBuffer     The buffer to copy bytes from.
106      * @param[in] aLength     The buffer length (number of bytes).
107      *
108      * @retval kErrorNone     Successfully set the `Heap::Data`.
109      * @retval kErrorNoBufs   Failed to allocate buffer.
110      *
111      */
112     Error SetFrom(const uint8_t *aBuffer, uint16_t aLength);
113 
114     /**
115      * Sets the `Heap::Data` from the content of a given message.
116      *
117      * The bytes are copied from current offset in @p aMessage till the end of the message.
118      *
119      * @param[in] aMessage    The message to copy bytes from (starting from offset till the end of message).
120      *
121      * @retval kErrorNone     Successfully set the `Heap::Data`.
122      * @retval kErrorNoBufs   Failed to allocate buffer.
123      *
124      */
125     Error SetFrom(const Message &aMessage);
126 
127     /**
128      * Sets the `Heap::Data` from the content of a given message read at a given offset up to a given
129      * length.
130      *
131      * @param[in] aMessage    The message to read and copy bytes from.
132      * @param[in] aOffset     The offset into the message to start reading the bytes from.
133      * @param[in] aLength     The number of bytes to read from message. If @p aMessage contains fewer bytes than
134      *                        requested, then `Heap::Data` is cleared and this method returns `kErrorParse`.
135      *
136      * @retval kErrorNone     Successfully set the `Heap::Data`.
137      * @retval kErrorNoBufs   Failed to allocate buffer.
138      * @retval kErrorParse    Not enough bytes in @p aMessage to read the requested @p aLength bytes.
139      *
140      */
141     Error SetFrom(const Message &aMessage, uint16_t aOffset, uint16_t aLength);
142 
143     /**
144      * Sets the `Heap::Data` from another one (move semantics).
145      *
146      * @param[in] aData   The other `Heap::Data` to set from (rvalue reference).
147      *
148      */
149     void SetFrom(Data &&aData);
150 
151     /**
152      * Appends the bytes from `Heap::Data` to a given message.
153      *
154      * @param[in] aMessage   The message to append the bytes into.
155      *
156      * @retval kErrorNone     Successfully copied the bytes from `Heap::Data` into @p aMessage.
157      * @retval kErrorNoBufs   Failed to allocate buffer.
158      *
159      */
CopyBytesTo(Message & aMessage) const160     Error CopyBytesTo(Message &aMessage) const { return aMessage.AppendBytes(mData.GetBytes(), mData.GetLength()); }
161 
162     /**
163      * Copies the bytes from `Heap::Data` into a given buffer.
164      *
165      * It is up to the caller to ensure that @p aBuffer has enough space for the current data length.
166      *
167      * @param[in] aBuffer     A pointer to buffer to copy the bytes into.
168      *
169      */
CopyBytesTo(uint8_t * aBuffer) const170     void CopyBytesTo(uint8_t *aBuffer) const { return mData.CopyBytesTo(aBuffer); }
171 
172     /**
173      * Compares the `Data` content with the bytes from a given buffer.
174      *
175      * @param[in] aBuffer   A pointer to a buffer to compare with the data.
176      * @param[in] aLength   The length of @p aBuffer.
177      *
178      * @retval TRUE   The `Data` content matches the bytes in @p aBuffer.
179      * @retval FALSE  The `Data` content does not match the byes in @p aBuffer.
180      *
181      */
182     bool Matches(const uint8_t *aBuffer, uint16_t aLength) const;
183 
184     /**
185      * Overloads operator `==` to compare the `Data` content with the content from another one.
186      *
187      * @param[in] aOtherData   The other `Data` to compare with.
188      *
189      * @retval TRUE   The two `Data` instances have matching content (same length and same bytes).
190      * @retval FALSE  The two `Data` instances do not have matching content.
191      *
192      */
operator ==(const Data & aOtherData) const193     bool operator==(const Data &aOtherData) const { return mData == aOtherData.mData; }
194 
195     /**
196      * Frees any buffer allocated by the `Heap::Data`.
197      *
198      * The `Heap::Data` destructor will automatically call `Free()`. This method allows caller to free the buffer
199      * explicitly.
200      *
201      */
202     void Free(void);
203 
204     Data(const Data &)            = delete;
205     Data &operator=(const Data &) = delete;
206 
207 private:
208     Error UpdateBuffer(uint16_t aNewLength);
209     void  TakeFrom(Data &aData);
210 
211     MutableData<kWithUint16Length> mData;
212 };
213 
214 } // namespace Heap
215 } // namespace ot
216 
217 #endif // HEAP_DATA_HPP_
218