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