• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hilog/log.h"
18 #include "log_tags.h"
19 
20 namespace {
21     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_TAG_DOMAIN_ID_PLUGIN, "ExtStream"};
22     constexpr static size_t SIZE_ZERO = 0;
23 }
24 namespace OHOS {
25 namespace ImagePlugin {
26 using namespace OHOS::HiviewDFX;
27 struct InputStream {
28     uint8_t* buf;
29     uint32_t size;
30     uint32_t resSize;
31 };
32 
ExtStream(InputDataStream * stream)33 ExtStream::ExtStream(InputDataStream *stream) : stream_(stream)
34 {
35 }
36 
InputStreamInit(InputStream & buf,uint8_t * input,size_t size)37 static void InputStreamInit(InputStream &buf, uint8_t *input, size_t size)
38 {
39     buf.buf = input;
40     buf.size = static_cast<uint32_t>(size);
41     buf.resSize = static_cast<uint32_t>(size);
42 }
43 
Skip(InputDataStream * stream,size_t size)44 static size_t Skip(InputDataStream *stream, size_t size)
45 {
46     if (stream == nullptr) {
47         return SIZE_ZERO;
48     }
49     uint32_t cur = stream->Tell();
50     uint32_t seek = cur + static_cast<uint32_t>(size);
51     if (!stream->Seek(seek)) {
52         HiLog::Error(LABEL, "skip failed, curpositon, skip size.");
53         return SIZE_ZERO;
54     }
55     return size;
56 }
57 
read(void * buffer,size_t size)58 size_t ExtStream::read(void *buffer, size_t size)
59 {
60     if (stream_ == nullptr) {
61         return SIZE_ZERO;
62     }
63     if (buffer == nullptr) {
64         return Skip(stream_, size);
65     }
66     InputStream buf;
67     InputStreamInit(buf, static_cast<uint8_t *>(buffer), size);
68 
69     uint32_t desiredSize = buf.size;
70     if (stream_->GetStreamSize() != SIZE_ZERO && stream_->GetStreamSize() < desiredSize) {
71         desiredSize = stream_->GetStreamSize();
72     }
73     if (!stream_->Read(desiredSize, buf.buf, buf.size, buf.resSize)) {
74         HiLog::Error(LABEL, "read failed, desire read size=%{public}u", buf.resSize);
75         return 0;
76     }
77     return static_cast<size_t>(buf.resSize);
78 }
79 
peek(void * buffer,size_t size) const80 size_t ExtStream::peek(void *buffer, size_t size) const
81 {
82     if (stream_ == nullptr) {
83         return SIZE_ZERO;
84     }
85     if (buffer == nullptr) {
86         HiLog::Error(LABEL, "peek failed, output buffer is null");
87         return SIZE_ZERO;
88     }
89     InputStream buf;
90     InputStreamInit(buf, static_cast<uint8_t *>(buffer), size);
91     if (!stream_->Peek(buf.size, buf.buf, buf.size, buf.resSize)) {
92         HiLog::Error(LABEL, "peek failed, desire read size=%{public}u", buf.resSize);
93         return 0;
94     }
95     return static_cast<size_t>(buf.resSize);
96 }
97 
isAtEnd() const98 bool ExtStream::isAtEnd() const
99 {
100     if (stream_ == nullptr) {
101         return true;
102     }
103     size_t size = stream_->GetStreamSize();
104     return (stream_->Tell() == size);
105 }
106 } // namespace ImagePlugin
107 } // namespace OHOS
108