1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "serial_buffer.h"
17 #include <new>
18 #include "securec.h"
19 #include "db_errno.h"
20 #include "communicator_type_define.h"
21 #include "log_print.h"
22
23 namespace DistributedDB {
~SerialBuffer()24 SerialBuffer::~SerialBuffer()
25 {
26 if (!isExternalStackMemory_ && oringinalBytes_ != nullptr) {
27 delete[] oringinalBytes_;
28 }
29 oringinalBytes_ = nullptr;
30 bytes_ = nullptr;
31 externalBytes_ = nullptr;
32 }
33
SetExtendHeadLength(uint32_t extendHeaderLen)34 void SerialBuffer::SetExtendHeadLength(uint32_t extendHeaderLen)
35 {
36 extendHeadLen_ = extendHeaderLen;
37 }
38
GetExtendHeadLength() const39 uint32_t SerialBuffer::GetExtendHeadLength() const
40 {
41 return extendHeadLen_;
42 }
43
44 // In case buffer be directly send out, so padding is needed
AllocBufferByPayloadLength(uint32_t inPayloadLen,uint32_t inHeaderLen)45 int SerialBuffer::AllocBufferByPayloadLength(uint32_t inPayloadLen, uint32_t inHeaderLen)
46 {
47 if (oringinalBytes_ != nullptr || bytes_ != nullptr || externalBytes_ != nullptr ||
48 BYTE_8_ALIGN(static_cast<uint64_t>(payloadLen_) + static_cast<uint64_t>(headerLen_)) > INT32_MAX) {
49 return -E_NOT_PERMIT;
50 }
51
52 payloadLen_ = inPayloadLen;
53 headerLen_ = inHeaderLen;
54 totalLen_ = BYTE_8_ALIGN(payloadLen_ + headerLen_);
55 paddingLen_ = totalLen_ - payloadLen_ - headerLen_;
56 if (totalLen_ == 0 || totalLen_ > MAX_TOTAL_LEN) {
57 return -E_INVALID_ARGS;
58 }
59 oringinalBytes_ = new (std::nothrow) uint8_t[totalLen_ + extendHeadLen_]();
60 if (oringinalBytes_ == nullptr) {
61 return -E_OUT_OF_MEMORY;
62 }
63 bytes_ = oringinalBytes_ + extendHeadLen_;
64 return E_OK;
65 }
66
67 // In case assemble fragment to frame, so no padding is needed, using frameLen as inTotalLen
AllocBufferByTotalLength(uint32_t inTotalLen,uint32_t inHeaderLen)68 int SerialBuffer::AllocBufferByTotalLength(uint32_t inTotalLen, uint32_t inHeaderLen)
69 {
70 if (bytes_ != nullptr || externalBytes_ != nullptr) {
71 return -E_NOT_PERMIT;
72 }
73 if (inTotalLen == 0 || inTotalLen > MAX_TOTAL_LEN || inTotalLen < inHeaderLen) {
74 return -E_INVALID_ARGS;
75 }
76
77 totalLen_ = inTotalLen;
78 headerLen_ = inHeaderLen;
79 payloadLen_ = totalLen_ - headerLen_;
80 paddingLen_ = 0;
81 bytes_ = new (std::nothrow) uint8_t[inTotalLen]();
82 if (bytes_ == nullptr) {
83 return -E_OUT_OF_MEMORY;
84 }
85 oringinalBytes_ = bytes_;
86 return E_OK;
87 }
88
89 // In case directly received, inTotalLen not include the padding, using frameLen as inTotalLen
SetExternalBuff(const uint8_t * buff,uint32_t inTotalLen,uint32_t inHeaderLen)90 int SerialBuffer::SetExternalBuff(const uint8_t *buff, uint32_t inTotalLen, uint32_t inHeaderLen)
91 {
92 if (bytes_ != nullptr || externalBytes_ != nullptr) {
93 return -E_NOT_PERMIT;
94 }
95 if (buff == nullptr || inTotalLen == 0 || inTotalLen > MAX_TOTAL_LEN || inTotalLen < inHeaderLen) {
96 return -E_INVALID_ARGS;
97 }
98
99 totalLen_ = inTotalLen;
100 headerLen_ = inHeaderLen;
101 payloadLen_ = totalLen_ - headerLen_;
102 paddingLen_ = 0;
103 isExternalStackMemory_ = true;
104 externalBytes_ = buff;
105 return E_OK;
106 }
107
Clone(int & outErrorNo)108 SerialBuffer *SerialBuffer::Clone(int &outErrorNo)
109 {
110 SerialBuffer *twinBuffer = new (std::nothrow) SerialBuffer();
111 if (twinBuffer == nullptr) {
112 outErrorNo = -E_OUT_OF_MEMORY;
113 return nullptr;
114 }
115 if (bytes_ == nullptr) {
116 twinBuffer->bytes_ = nullptr;
117 } else {
118 twinBuffer->bytes_ = new (std::nothrow) uint8_t[totalLen_];
119 if (twinBuffer->bytes_ == nullptr) {
120 outErrorNo = -E_OUT_OF_MEMORY;
121 delete twinBuffer;
122 twinBuffer = nullptr;
123 return nullptr;
124 }
125 errno_t errCode = memcpy_s(twinBuffer->bytes_, totalLen_, bytes_, totalLen_);
126 if (errCode != EOK) {
127 outErrorNo = -E_SECUREC_ERROR;
128 delete twinBuffer;
129 twinBuffer = nullptr;
130 return nullptr;
131 }
132 }
133 twinBuffer->oringinalBytes_ = twinBuffer->bytes_;
134 twinBuffer->externalBytes_ = externalBytes_;
135 twinBuffer->totalLen_ = totalLen_;
136 twinBuffer->headerLen_ = headerLen_;
137 twinBuffer->payloadLen_ = payloadLen_;
138 twinBuffer->paddingLen_ = paddingLen_;
139 twinBuffer->isExternalStackMemory_ = isExternalStackMemory_;
140 twinBuffer->extendHeadLen_ = extendHeadLen_;
141 outErrorNo = E_OK;
142 return twinBuffer;
143 }
144
ConvertForCrossThread()145 int SerialBuffer::ConvertForCrossThread()
146 {
147 if (externalBytes_ == nullptr) {
148 // No associated external stack memory. Do nothing and return E_OK.
149 return E_OK;
150 }
151 // Logic guarantee all the member value: isExternalStackMemory_ is true; bytes_ is nullptr; totalLen_ is correct.
152 bytes_ = new (std::nothrow) uint8_t[totalLen_];
153 if (bytes_ == nullptr) {
154 return -E_OUT_OF_MEMORY;
155 }
156 errno_t errCode = memcpy_s(bytes_, totalLen_, externalBytes_, totalLen_);
157 if (errCode != EOK) {
158 delete[] bytes_;
159 bytes_ = nullptr;
160 return -E_SECUREC_ERROR;
161 }
162 // Reset external related info
163 externalBytes_ = nullptr;
164 isExternalStackMemory_ = false;
165 oringinalBytes_ = bytes_;
166 extendHeadLen_ = 0;
167 return E_OK;
168 }
169
GetSize() const170 uint32_t SerialBuffer::GetSize() const
171 {
172 if (bytes_ == nullptr && externalBytes_ == nullptr) {
173 return 0;
174 }
175 return totalLen_;
176 }
177
GetOringinalAddr() const178 uint8_t *SerialBuffer::GetOringinalAddr() const
179 {
180 return oringinalBytes_;
181 }
182
GetWritableBytesForEntireBuffer()183 std::pair<uint8_t *, uint32_t> SerialBuffer::GetWritableBytesForEntireBuffer()
184 {
185 if (bytes_ == nullptr) {
186 return std::make_pair(nullptr, 0);
187 } else {
188 return std::make_pair(bytes_, totalLen_);
189 }
190 }
191
GetWritableBytesForEntireFrame()192 std::pair<uint8_t *, uint32_t> SerialBuffer::GetWritableBytesForEntireFrame()
193 {
194 if (bytes_ == nullptr) {
195 return std::make_pair(nullptr, 0);
196 } else {
197 return std::make_pair(bytes_, totalLen_ - paddingLen_);
198 }
199 }
200
GetWritableBytesForHeader()201 std::pair<uint8_t *, uint32_t> SerialBuffer::GetWritableBytesForHeader()
202 {
203 if (bytes_ == nullptr) {
204 return std::make_pair(nullptr, 0);
205 } else {
206 return std::make_pair(bytes_, headerLen_);
207 }
208 }
209
GetWritableBytesForPayload()210 std::pair<uint8_t *, uint32_t> SerialBuffer::GetWritableBytesForPayload()
211 {
212 if (bytes_ == nullptr) {
213 return std::make_pair(nullptr, 0);
214 } else {
215 return std::make_pair(bytes_ + headerLen_, payloadLen_);
216 }
217 }
218
219 // For receive case, using Const Function
GetReadOnlyBytesForEntireBuffer() const220 std::pair<const uint8_t *, uint32_t> SerialBuffer::GetReadOnlyBytesForEntireBuffer() const
221 {
222 if (isExternalStackMemory_) {
223 return std::make_pair(externalBytes_, totalLen_);
224 } else if (bytes_ != nullptr) {
225 return std::make_pair(bytes_, totalLen_);
226 } else {
227 return std::make_pair(nullptr, 0);
228 }
229 }
230
GetReadOnlyBytesForEntireFrame() const231 std::pair<const uint8_t *, uint32_t> SerialBuffer::GetReadOnlyBytesForEntireFrame() const
232 {
233 if (isExternalStackMemory_) {
234 return std::make_pair(externalBytes_, totalLen_ - paddingLen_);
235 } else if (bytes_ != nullptr) {
236 return std::make_pair(bytes_, totalLen_ - paddingLen_);
237 } else {
238 return std::make_pair(nullptr, 0);
239 }
240 }
241
GetReadOnlyBytesForHeader() const242 std::pair<const uint8_t *, uint32_t> SerialBuffer::GetReadOnlyBytesForHeader() const
243 {
244 if (isExternalStackMemory_) {
245 return std::make_pair(externalBytes_, headerLen_);
246 } else if (bytes_ != nullptr) {
247 return std::make_pair(bytes_, headerLen_);
248 } else {
249 return std::make_pair(nullptr, 0);
250 }
251 }
252
GetReadOnlyBytesForPayload() const253 std::pair<const uint8_t *, uint32_t> SerialBuffer::GetReadOnlyBytesForPayload() const
254 {
255 if (isExternalStackMemory_) {
256 return std::make_pair(externalBytes_ + headerLen_, payloadLen_);
257 } else if (bytes_ != nullptr) {
258 return std::make_pair(bytes_ + headerLen_, payloadLen_);
259 } else {
260 return std::make_pair(nullptr, 0);
261 }
262 }
263 } // namespace DistributedDB
264