1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "data_buffer.h"
17 #include <cstring>
18 #include <unistd.h>
19 #include <securec.h>
20
21 namespace OHOS {
22 namespace Sharing {
23
DataBuffer(int size)24 DataBuffer::DataBuffer(int size)
25 {
26 data_ = new uint8_t[size + 1];
27 data_[size] = '\0';
28 capacity_ = size;
29 size_ = 0;
30 }
31
DataBuffer(const DataBuffer & other)32 DataBuffer::DataBuffer(const DataBuffer &other) noexcept
33 {
34 if (other.data_ && other.size_) {
35 capacity_ = other.size_;
36 data_ = new uint8_t[capacity_ + 1];
37 auto ret = memcpy_s(data_, capacity_ + 1, other.data_, other.size_);
38 if (ret != EOK) {
39 size_ = other.size_;
40 }
41 size_ = other.size_;
42 }
43 }
44
operator =(const DataBuffer & other)45 DataBuffer &DataBuffer::operator=(const DataBuffer &other) noexcept
46 {
47 if (this != &other) {
48 if (other.data_ && other.size_) {
49 capacity_ = other.size_;
50 delete[] data_;
51 data_ = new uint8_t[capacity_ + 1];
52 auto ret = memcpy_s(data_, capacity_ + 1, other.data_, other.size_);
53 if (ret != EOK) {
54 size_ = other.size_;
55 return *this;
56 }
57 size_ = other.size_;
58 }
59 }
60
61 return *this;
62 }
63
DataBuffer(DataBuffer && other)64 DataBuffer::DataBuffer(DataBuffer &&other) noexcept
65 {
66 data_ = other.data_;
67 size_ = other.size_;
68 capacity_ = other.capacity_;
69 other.data_ = nullptr;
70 other.size_ = 0;
71 other.capacity_ = 0;
72 }
73
operator =(DataBuffer && other)74 DataBuffer &DataBuffer::operator=(DataBuffer &&other) noexcept
75 {
76 if (this != &other) {
77 if (data_) {
78 delete[] data_;
79 }
80 data_ = other.data_;
81 size_ = other.size_;
82 capacity_ = other.capacity_;
83 other.data_ = nullptr;
84 other.size_ = 0;
85 other.capacity_ = 0;
86 }
87
88 return *this;
89 }
90
~DataBuffer()91 DataBuffer::~DataBuffer()
92 {
93 delete[] data_;
94 capacity_ = 0;
95 size_ = 0;
96 }
97
Resize(int size)98 void DataBuffer::Resize(int size)
99 {
100 if (size > capacity_) {
101 capacity_ = size;
102 auto data2 = new uint8_t[capacity_];
103 if (data_ && size_ > 0) {
104 auto ret = memcpy_s(data2, capacity_, data_, size_);
105 if (ret != EOK) {
106 delete[] data2;
107 return;
108 }
109 }
110 if (data_) {
111 delete[] data_;
112 data_ = nullptr;
113 }
114 data_ = data2;
115 } else if (size < capacity_) {
116 if (data_) {
117 delete[] data_;
118 }
119 capacity_ = size;
120 data_ = new uint8_t[capacity_];
121 size_ = 0;
122 }
123 }
124
PushData(const char * data,int dataLen)125 void DataBuffer::PushData(const char *data, int dataLen)
126 {
127 if (!data || dataLen <= 0 || size_ < 0 || capacity_ < 0) {
128 return;
129 }
130
131 if (dataLen + size_ <= capacity_) {
132 auto ret = memcpy_s(data_ + size_, capacity_, data, dataLen);
133 if (ret != EOK) {
134 return;
135 }
136 size_ += dataLen;
137 } else {
138 capacity_ = size_ + dataLen;
139 auto newBuffer = new (std::nothrow) uint8_t[capacity_];
140 if (!newBuffer) {
141 return;
142 }
143 if (data_) {
144 auto ret = memcpy_s(newBuffer, capacity_, data_, size_);
145 if (ret != EOK) {
146 return;
147 }
148 }
149 auto ret = memcpy_s(newBuffer + size_, capacity_ - size_, data, dataLen);
150 if (ret != EOK) {
151 return;
152 }
153 delete[] data_;
154 data_ = newBuffer;
155 size_ = capacity_;
156 }
157 }
158
ReplaceData(const char * data,int dataLen)159 void DataBuffer::ReplaceData(const char *data, int dataLen)
160 {
161 if (!data) {
162 return;
163 }
164
165 if (dataLen > capacity_) {
166 if (data_)
167 delete[] data_;
168 capacity_ = dataLen;
169 data_ = new uint8_t[capacity_];
170 }
171
172 auto ret = memcpy_s(data_, capacity_, data, dataLen);
173 if (ret != EOK) {
174 return;
175 }
176 size_ = dataLen;
177 }
178
SetCapacity(int capacity)179 void DataBuffer::SetCapacity(int capacity)
180 {
181 if (data_) {
182 delete[] data_;
183 }
184
185 if (capacity == 0) {
186 return;
187 }
188 if (capacity > 0) {
189 data_ = new uint8_t[capacity];
190 }
191 capacity_ = capacity;
192 }
193
194 } // namespace Sharing
195 } // namespace OHOS