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