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 implements the `Heap::Data` (a heap allocated data).
32 */
33
34 #include "heap_data.hpp"
35
36 #include "common/code_utils.hpp"
37 #include "common/debug.hpp"
38
39 namespace ot {
40 namespace Heap {
41
SetFrom(const uint8_t * aBuffer,uint16_t aLength)42 Error Data::SetFrom(const uint8_t *aBuffer, uint16_t aLength)
43 {
44 Error error;
45
46 SuccessOrExit(error = UpdateBuffer(aLength));
47 VerifyOrExit(aLength != 0);
48
49 SuccessOrAssert(mData.CopyBytesFrom(aBuffer, aLength));
50
51 exit:
52 return error;
53 }
54
SetFrom(const Message & aMessage)55 Error Data::SetFrom(const Message &aMessage)
56 {
57 return SetFrom(aMessage, aMessage.GetOffset(), aMessage.GetLength() - aMessage.GetOffset());
58 }
59
SetFrom(const Message & aMessage,uint16_t aOffset,uint16_t aLength)60 Error Data::SetFrom(const Message &aMessage, uint16_t aOffset, uint16_t aLength)
61 {
62 Error error;
63
64 VerifyOrExit(aOffset + aLength <= aMessage.GetLength(), error = kErrorParse);
65
66 SuccessOrExit(error = UpdateBuffer(aLength));
67 VerifyOrExit(aLength != 0);
68
69 SuccessOrAssert(aMessage.Read(aOffset, mData.GetBytes(), aLength));
70
71 exit:
72 return error;
73 }
74
SetFrom(Data && aData)75 void Data::SetFrom(Data &&aData)
76 {
77 Free();
78 TakeFrom(aData);
79 }
80
Matches(const uint8_t * aBuffer,uint16_t aLength) const81 bool Data::Matches(const uint8_t *aBuffer, uint16_t aLength) const
82 {
83 bool matches = false;
84
85 VerifyOrExit(aLength == mData.GetLength());
86
87 if (IsNull())
88 {
89 matches = (aLength == 0);
90 }
91 else
92 {
93 matches = mData.MatchesBytesIn(aBuffer);
94 }
95
96 exit:
97 return matches;
98 }
99
Free(void)100 void Data::Free(void)
101 {
102 Heap::Free(mData.GetBytes());
103 mData.Init(nullptr, 0);
104 }
105
UpdateBuffer(uint16_t aNewLength)106 Error Data::UpdateBuffer(uint16_t aNewLength)
107 {
108 Error error = kErrorNone;
109
110 VerifyOrExit(aNewLength != mData.GetLength());
111
112 Heap::Free(mData.GetBytes());
113
114 if (aNewLength == 0)
115 {
116 mData.Init(nullptr, 0);
117 }
118 else
119 {
120 uint8_t *newBuffer = static_cast<uint8_t *>(Heap::CAlloc(aNewLength, sizeof(uint8_t)));
121
122 VerifyOrExit(newBuffer != nullptr, error = kErrorNoBufs);
123 mData.Init(newBuffer, aNewLength);
124 }
125
126 exit:
127 return error;
128 }
129
TakeFrom(Data & aData)130 void Data::TakeFrom(Data &aData)
131 {
132 mData.Init(aData.mData.GetBytes(), aData.GetLength());
133 aData.mData.Init(nullptr, 0);
134 }
135
136 } // namespace Heap
137 } // namespace ot
138