1 /*
2  * Copyright (C) 2022 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_packer_stream.h"
17 
18 #include "image_log.h"
19 #include "securec.h"
20 
21 #undef LOG_DOMAIN
22 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
23 
24 #undef LOG_TAG
25 #define LOG_TAG "BufferPackerStream"
26 
27 namespace OHOS {
28 namespace Media {
29 
BufferPackerStream(uint8_t * outputData,uint32_t maxSize)30 BufferPackerStream::BufferPackerStream(uint8_t *outputData, uint32_t maxSize)
31     : outputData_(outputData), maxSize_(maxSize)
32 {}
33 
Write(const uint8_t * buffer,uint32_t size)34 bool BufferPackerStream::Write(const uint8_t *buffer, uint32_t size)
35 {
36     if ((buffer == nullptr) || (size == 0)) {
37         IMAGE_LOGE("input parameter invalid.");
38         return false;
39     }
40     if (outputData_ == nullptr) {
41         IMAGE_LOGE("output stream is null.");
42         return false;
43     }
44     uint32_t leftSize = maxSize_ - offset_;
45     if (size > leftSize) {
46         IMAGE_LOGE("write data:[%{public}lld] out of max size:[%{public}u].",
47             static_cast<long long>(size + offset_), maxSize_);
48         return false;
49     }
50     if (memcpy_s(outputData_ + offset_, leftSize, buffer, size) != EOK) {
51         IMAGE_LOGE("memory copy failed.");
52         return false;
53     }
54     offset_ += size;
55     return true;
56 }
57 
BytesWritten()58 int64_t BufferPackerStream::BytesWritten()
59 {
60     return offset_;
61 }
62 
GetCapicity(size_t & size)63 bool BufferPackerStream::GetCapicity(size_t &size)
64 {
65     size = maxSize_;
66     return true;
67 }
68 
GetType()69 ImagePlugin::OutputStreamType BufferPackerStream::GetType()
70 {
71     return ImagePlugin::OutputStreamType::BUFFER_PACKER;
72 }
73 } // namespace Media
74 } // namespace OHOS
75