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 "buffer_source_stream.h"
17
18 #include <string>
19 #ifndef _WIN32
20 #include "securec.h"
21 #else
22 #include "memory.h"
23 #endif
24 #include "buffer_packer_stream.h"
25 #include "image_log.h"
26
27 #undef LOG_DOMAIN
28 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
29
30 #undef LOG_TAG
31 #define LOG_TAG "BufferSourceStream"
32
33 namespace OHOS {
34 namespace Media {
35 using namespace std;
36 using namespace ImagePlugin;
37
BufferSourceStream(uint8_t * data,uint32_t size,uint32_t offset)38 BufferSourceStream::BufferSourceStream(uint8_t *data, uint32_t size, uint32_t offset)
39 : inputBuffer_(data), dataSize_(size), dataOffset_(offset)
40 {}
41
~BufferSourceStream()42 BufferSourceStream::~BufferSourceStream()
43 {
44 IMAGE_LOGD("[BufferSourceStream]destructor enter");
45 if (inputBuffer_ != nullptr) {
46 free(inputBuffer_);
47 inputBuffer_ = nullptr;
48 }
49 }
50
CreateSourceStream(const uint8_t * data,uint32_t size)51 std::unique_ptr<BufferSourceStream> BufferSourceStream::CreateSourceStream(const uint8_t *data, uint32_t size)
52 {
53 if ((data == nullptr) || (size == 0)) {
54 IMAGE_LOGE("[BufferSourceStream]input the parameter exception.");
55 return nullptr;
56 }
57 uint8_t *dataCopy = static_cast<uint8_t *>(malloc(size));
58 if (dataCopy == nullptr) {
59 IMAGE_LOGE("[BufferSourceStream]malloc the input data buffer fail.");
60 return nullptr;
61 }
62 errno_t ret = memcpy_s(dataCopy, size, data, size);
63 if (ret != EOK) {
64 free(dataCopy);
65 dataCopy = nullptr;
66 IMAGE_LOGE("[BufferSourceStream]copy the input data fail, ret:%{public}d.", ret);
67 return nullptr;
68 }
69 return make_unique<BufferSourceStream>(dataCopy, size, 0);
70 }
71
Read(uint32_t desiredSize,DataStreamBuffer & outData)72 bool BufferSourceStream::Read(uint32_t desiredSize, DataStreamBuffer &outData)
73 {
74 if (!Peek(desiredSize, outData)) {
75 IMAGE_LOGE("[BufferSourceStream]read fail.");
76 return false;
77 }
78 dataOffset_.fetch_add(outData.dataSize);
79 return true;
80 }
81
Peek(uint32_t desiredSize,DataStreamBuffer & outData)82 bool BufferSourceStream::Peek(uint32_t desiredSize, DataStreamBuffer &outData)
83 {
84 if (desiredSize == 0) {
85 IMAGE_LOGE("[BufferSourceStream]input the parameter exception.");
86 return false;
87 }
88 size_t offset = dataOffset_.load();
89 if (dataSize_ == offset) {
90 IMAGE_LOGE("[BufferSourceStream]buffer read finish, offset:%{public}zu ,dataSize%{public}zu.",
91 offset, dataSize_);
92 return false;
93 }
94 outData.bufferSize = dataSize_ - offset;
95 if (desiredSize > dataSize_ - offset) {
96 desiredSize = dataSize_ - offset;
97 }
98 outData.dataSize = desiredSize;
99 outData.inputStreamBuffer = inputBuffer_ + offset;
100 IMAGE_LOGD("[BufferSourceStream]Peek end. desiredSize:%{public}d, offset:%{public}zu,"
101 "dataSize%{public}zu.", desiredSize, offset, dataSize_);
102 return true;
103 }
104
Read(uint32_t desiredSize,uint8_t * outBuffer,uint32_t bufferSize,uint32_t & readSize)105 bool BufferSourceStream::Read(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize,
106 uint32_t &readSize) __attribute__((no_sanitize("cfi")))
107 {
108 if (!Peek(desiredSize, outBuffer, bufferSize, readSize)) {
109 IMAGE_LOGE("[BufferSourceStream]read fail.");
110 return false;
111 }
112 dataOffset_.fetch_add(readSize);
113 return true;
114 }
115
Peek(uint32_t desiredSize,uint8_t * outBuffer,uint32_t bufferSize,uint32_t & readSize)116 bool BufferSourceStream::Peek(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize,
117 uint32_t &readSize) __attribute__((no_sanitize("cfi")))
118 {
119 if (desiredSize == 0 || outBuffer == nullptr || desiredSize > bufferSize) {
120 IMAGE_LOGE("[BufferSourceStream]input the parameter exception, desiredSize:%{public}u,"
121 "bufferSize:%{public}u.", desiredSize, bufferSize);
122 return false;
123 }
124 size_t offset = dataOffset_.load();
125 if (dataSize_ == offset) {
126 IMAGE_LOGE("[BufferSourceStream]buffer read finish, offset:%{public}zu ,dataSize%{public}zu.",
127 offset, dataSize_);
128 return false;
129 }
130 if (desiredSize > dataSize_ - offset) {
131 desiredSize = dataSize_ - offset;
132 }
133 errno_t ret = memcpy_s(outBuffer, bufferSize, inputBuffer_ + offset, desiredSize);
134 if (ret != EOK) {
135 IMAGE_LOGE("[BufferSourceStream]copy data fail, ret:%{public}d, bufferSize:%{public}u,"
136 "offset:%{public}zu, desiredSize:%{public}u.", ret, bufferSize, offset, desiredSize);
137 return false;
138 }
139 readSize = desiredSize;
140 return true;
141 }
142
Tell()143 uint32_t BufferSourceStream::Tell()
144 {
145 return dataOffset_.load();
146 }
147
Seek(uint32_t position)148 bool BufferSourceStream::Seek(uint32_t position)
149 {
150 if (position > dataSize_) {
151 IMAGE_LOGE("[BufferSourceStream]Seek the position greater than the Data Size,position:%{public}u.",
152 position);
153 return false;
154 }
155 dataOffset_.store(position);
156 return true;
157 }
158
GetStreamSize()159 size_t BufferSourceStream::GetStreamSize()
160 {
161 return dataSize_;
162 }
163
GetDataPtr()164 uint8_t *BufferSourceStream::GetDataPtr()
165 {
166 return inputBuffer_;
167 }
168
GetStreamType()169 uint32_t BufferSourceStream::GetStreamType()
170 {
171 return ImagePlugin::BUFFER_SOURCE_TYPE;
172 }
173
ToOutputDataStream()174 OutputDataStream* BufferSourceStream::ToOutputDataStream()
175 {
176 return new (std::nothrow) BufferPackerStream(inputBuffer_, dataSize_);
177 }
178 } // namespace Media
179 } // namespace OHOS
180