1 /*
2  * Copyright (C) 2023 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 "ext_stream.h"
17 #include "image_log.h"
18 
19 #undef LOG_DOMAIN
20 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_PLUGIN
21 
22 #undef LOG_TAG
23 #define LOG_TAG "ExtStream"
24 
25 namespace {
26     constexpr static size_t SIZE_ZERO = 0;
27 }
28 namespace OHOS {
29 namespace ImagePlugin {
30 struct InputStream {
31     uint8_t* buf;
32     uint32_t size;
33     uint32_t resSize;
34 };
35 
ExtStream(InputDataStream * stream)36 ExtStream::ExtStream(InputDataStream *stream) : stream_(stream)
37 {
38 }
39 
InputStreamInit(InputStream & buf,uint8_t * input,size_t size)40 static void InputStreamInit(InputStream &buf, uint8_t *input, size_t size)
41 {
42     buf.buf = input;
43     buf.size = static_cast<uint32_t>(size);
44     buf.resSize = static_cast<uint32_t>(size);
45 }
46 
Skip(InputDataStream * stream,size_t size)47 static size_t Skip(InputDataStream *stream, size_t size)
48 {
49     if (stream == nullptr) {
50         return SIZE_ZERO;
51     }
52     uint32_t cur = stream->Tell();
53     uint32_t seek = cur + static_cast<uint32_t>(size);
54     if (!stream->Seek(seek)) {
55         IMAGE_LOGE("skip failed, curpositon, skip size.");
56         return SIZE_ZERO;
57     }
58     return size;
59 }
60 
read(void * buffer,size_t size)61 size_t ExtStream::read(void *buffer, size_t size)
62 {
63     if (stream_ == nullptr) {
64         return SIZE_ZERO;
65     }
66     if (buffer == nullptr) {
67         return Skip(stream_, size);
68     }
69     InputStream buf;
70     InputStreamInit(buf, static_cast<uint8_t *>(buffer), size);
71 
72     uint32_t desiredSize = buf.size;
73     if (stream_->GetStreamSize() != SIZE_ZERO && stream_->GetStreamSize() < desiredSize) {
74         desiredSize = stream_->GetStreamSize();
75     }
76     if (!stream_->Read(desiredSize, buf.buf, buf.size, buf.resSize)) {
77         IMAGE_LOGD("read failed, desire read size=%{public}u", buf.resSize);
78         return 0;
79     }
80     return static_cast<size_t>(buf.resSize);
81 }
82 
peek(void * buffer,size_t size) const83 size_t ExtStream::peek(void *buffer, size_t size) const
84 {
85     if (stream_ == nullptr) {
86         return SIZE_ZERO;
87     }
88     if (buffer == nullptr) {
89         IMAGE_LOGE("peek failed, output buffer is null");
90         return SIZE_ZERO;
91     }
92     InputStream buf;
93     InputStreamInit(buf, static_cast<uint8_t *>(buffer), size);
94     if (!stream_->Peek(buf.size, buf.buf, buf.size, buf.resSize)) {
95         IMAGE_LOGE("peek failed, desire read size=%{public}u", buf.resSize);
96         return 0;
97     }
98     return static_cast<size_t>(buf.resSize);
99 }
100 
isAtEnd() const101 bool ExtStream::isAtEnd() const
102 {
103     if (stream_ == nullptr) {
104         return true;
105     }
106     size_t size = stream_->GetStreamSize();
107     return (stream_->Tell() == size);
108 }
109 
getLength() const110 size_t ExtStream::getLength() const
111 {
112     return stream_ == nullptr ? 0 : stream_->GetStreamSize();
113 }
114 } // namespace ImagePlugin
115 } // namespace OHOS
116