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