• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <algorithm>
17 #include <cstdlib>
18 #include <securec.h>
19 #include "mc_data_buffer.h"
20 #include "mechbody_controller_log.h"
21 
22 namespace OHOS {
23 namespace MechBodyController {
24 namespace {
25     const std::string TAG = "MechDataBuffer";
26 }
MechDataBuffer(size_t capacity)27 MechDataBuffer::MechDataBuffer(size_t capacity)
28 {
29     if (capacity != 0 && capacity < MECH_MAX_BUFFER_SIZE) {
30         data_ = new uint8_t[capacity] {0};
31         if (data_ != nullptr) {
32             capacity_ = capacity;
33             rangeLength_ = 0;
34         }
35     }
36 }
37 
~MechDataBuffer()38 MechDataBuffer::~MechDataBuffer()
39 {
40     if (data_ != nullptr) {
41         delete[] data_;
42         data_ = nullptr;
43     }
44 }
45 
Size()46 size_t MechDataBuffer::Size()
47 {
48     return rangeLength_;
49 }
50 
Offset()51 size_t MechDataBuffer::Offset()
52 {
53     return rangeOffset_;
54 }
55 
Capacity()56 size_t MechDataBuffer::Capacity()
57 {
58     return capacity_;
59 }
60 
Data()61 uint8_t *MechDataBuffer::Data()
62 {
63     if (data_ == nullptr) {
64         return nullptr;
65     }
66     return data_ + rangeOffset_;
67 }
68 
SetRange(size_t offset,size_t size)69 int32_t MechDataBuffer::SetRange(size_t offset, size_t size)
70 {
71     if (!(offset <= capacity_) || !(offset + size <= capacity_)) {
72         return INVALID_PARAMETERS_ERR;
73     }
74 
75     rangeOffset_ = offset;
76     rangeLength_ = size;
77     return ERR_OK;
78 }
79 
AppendUint8(uint8_t value)80 int32_t MechDataBuffer::AppendUint8(uint8_t value)
81 {
82     if (data_ == nullptr || rangeOffset_ + rangeLength_ + sizeof(value) > capacity_) {
83         return INVALID_PARAMETERS_ERR;
84     }
85     if (memcpy_s(Data() + rangeLength_, capacity_ - rangeOffset_ - rangeLength_, &value, sizeof(value)) != 0) {
86         return INVALID_PARAMETERS_ERR;
87     }
88     rangeLength_ += sizeof(value);
89     return ERR_OK;
90 }
91 
AppendUint16(uint16_t value)92 int32_t MechDataBuffer::AppendUint16(uint16_t value)
93 {
94     if (data_ == nullptr || rangeOffset_ + rangeLength_ + sizeof(value) > capacity_) {
95         return INVALID_PARAMETERS_ERR;
96     }
97     if (memcpy_s(Data() + rangeLength_, capacity_ - rangeOffset_ - rangeLength_, &value, sizeof(value)) != 0) {
98         return INVALID_PARAMETERS_ERR;
99     }
100     rangeLength_ += sizeof(value);
101     return ERR_OK;
102 }
103 
AppendUint32(uint32_t value)104 int32_t MechDataBuffer::AppendUint32(uint32_t value)
105 {
106     if (data_ == nullptr || rangeOffset_ + rangeLength_ + sizeof(value) > capacity_) {
107         return INVALID_PARAMETERS_ERR;
108     }
109     if (memcpy_s(Data() + rangeLength_, capacity_ - rangeOffset_ - rangeLength_, &value, sizeof(value)) != 0) {
110         return INVALID_PARAMETERS_ERR;
111     }
112     rangeLength_ += sizeof(value);
113     return ERR_OK;
114 }
115 
AppendUint64(uint64_t value)116 int32_t MechDataBuffer::AppendUint64(uint64_t value)
117 {
118     if (data_ == nullptr || rangeOffset_ + rangeLength_ + sizeof(value) > capacity_) {
119         return INVALID_PARAMETERS_ERR;
120     }
121     if (memcpy_s(Data() + rangeLength_, capacity_ - rangeOffset_ - rangeLength_, &value, sizeof(value)) != 0) {
122         return INVALID_PARAMETERS_ERR;
123     }
124     rangeLength_ += sizeof(value);
125     return ERR_OK;
126 }
127 
AppendFloat(float value)128 int32_t MechDataBuffer::AppendFloat(float value)
129 {
130     if (data_ == nullptr || rangeOffset_ + rangeLength_ + sizeof(value) > capacity_) {
131         return INVALID_PARAMETERS_ERR;
132     }
133     if (memcpy_s(Data() + rangeLength_, capacity_ - rangeOffset_ - rangeLength_, &value, sizeof(value)) != 0) {
134         return INVALID_PARAMETERS_ERR;
135     }
136     rangeLength_ += sizeof(value);
137     return ERR_OK;
138 }
139 
AppendDataBuffer(std::shared_ptr<MechDataBuffer> data)140 int32_t MechDataBuffer::AppendDataBuffer(std::shared_ptr<MechDataBuffer> data)
141 {
142     if (data_ == nullptr || rangeOffset_ + rangeLength_ + data->Size() > capacity_) {
143         return INVALID_PARAMETERS_ERR;
144     }
145     if (memcpy_s(Data() + rangeLength_, capacity_ - rangeOffset_ - rangeLength_, data->Data(), data->Size()) != 0) {
146         return INVALID_PARAMETERS_ERR;
147     }
148     rangeLength_ += data->Size();
149     return ERR_OK;
150 }
151 
ReadUint8(size_t offset,uint8_t & outValue)152 int32_t MechDataBuffer::ReadUint8(size_t offset, uint8_t& outValue)
153 {
154     if (data_ == nullptr || rangeOffset_ + offset + sizeof(uint8_t) > rangeLength_) {
155         return INVALID_PARAMETERS_ERR;
156     }
157     if (memcpy_s(&outValue, sizeof(outValue), Data() + offset, sizeof(uint8_t)) != 0) {
158         return INVALID_PARAMETERS_ERR;
159     }
160     return ERR_OK;
161 }
162 
ReadUint16(size_t offset,uint16_t & outValue)163 int32_t MechDataBuffer::ReadUint16(size_t offset, uint16_t& outValue)
164 {
165     if (data_ == nullptr || rangeOffset_ + offset + sizeof(uint16_t) > rangeLength_) {
166         return INVALID_PARAMETERS_ERR;
167     }
168     if (memcpy_s(&outValue, sizeof(outValue), Data() + offset, sizeof(uint16_t)) != 0) {
169         return INVALID_PARAMETERS_ERR;
170     }
171     return ERR_OK;
172 }
173 
ReadUint32(size_t offset,uint32_t & outValue)174 int32_t MechDataBuffer::ReadUint32(size_t offset, uint32_t& outValue)
175 {
176     if (data_ == nullptr || rangeOffset_ + offset + sizeof(uint32_t) > rangeLength_) {
177         return INVALID_PARAMETERS_ERR;
178     }
179     if (memcpy_s(&outValue, sizeof(outValue), Data() + offset, sizeof(uint32_t)) != 0) {
180         return INVALID_PARAMETERS_ERR;
181     }
182     return ERR_OK;
183 }
184 
ReadUint64(size_t offset,uint64_t & outValue)185 int32_t MechDataBuffer::ReadUint64(size_t offset, uint64_t& outValue)
186 {
187     if (data_ == nullptr || rangeOffset_ + offset + sizeof(uint64_t) > rangeLength_) {
188         return INVALID_PARAMETERS_ERR;
189     }
190     if (memcpy_s(&outValue, sizeof(outValue), Data() + offset, sizeof(uint64_t)) != 0) {
191         return INVALID_PARAMETERS_ERR;
192     }
193     return ERR_OK;
194 }
195 
ReadFloat(size_t offset,float & outValue)196 int32_t MechDataBuffer::ReadFloat(size_t offset, float& outValue)
197 {
198     if (data_ == nullptr || rangeOffset_ + offset + sizeof(float) > rangeLength_) {
199         return INVALID_PARAMETERS_ERR;
200     }
201     if (memcpy_s(&outValue, sizeof(outValue), Data() + offset, sizeof(float)) != 0) {
202         return INVALID_PARAMETERS_ERR;
203     }
204     return ERR_OK;
205 }
206 
ReadInt16(size_t offset,int16_t & outValue)207 int32_t MechDataBuffer::ReadInt16(size_t offset, int16_t& outValue)
208 {
209     if (data_ == nullptr || rangeOffset_ + offset + sizeof(int16_t) > rangeLength_) {
210         return INVALID_PARAMETERS_ERR;
211     }
212     if (memcpy_s(&outValue, sizeof(outValue), Data() + offset, sizeof(int16_t)) != 0) {
213         return INVALID_PARAMETERS_ERR;
214     }
215     return ERR_OK;
216 }
217 }  // namespace MechBodyController
218 }  // namespace OHOS
219