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 if (maxSize_ < offset_) {
45 IMAGE_LOGE("offset:[%{public}lld] out of max size:[%{public}u].",
46 static_cast<long long>(offset_), maxSize_);
47 return false;
48 }
49 uint32_t leftSize = maxSize_ - offset_;
50 if (size > leftSize) {
51 IMAGE_LOGE("write data:[%{public}lld] out of max size:[%{public}u].",
52 static_cast<long long>(size + offset_), maxSize_);
53 return false;
54 }
55 if (memcpy_s(outputData_ + offset_, leftSize, buffer, size) != EOK) {
56 IMAGE_LOGE("memory copy failed.");
57 return false;
58 }
59 offset_ += size;
60 return true;
61 }
62
BytesWritten()63 int64_t BufferPackerStream::BytesWritten()
64 {
65 return offset_;
66 }
67
GetCapicity(size_t & size)68 bool BufferPackerStream::GetCapicity(size_t &size)
69 {
70 size = maxSize_;
71 return true;
72 }
73
GetType()74 ImagePlugin::OutputStreamType BufferPackerStream::GetType()
75 {
76 return ImagePlugin::OutputStreamType::BUFFER_PACKER;
77 }
78 } // namespace Media
79 } // namespace OHOS
80