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 "common/hap_file_data_source.h"
17 
18 #include "common/hap_verify_log.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace Verify {
HapFileDataSource(RandomAccessFile & hapFile,long long offset,long long size,long long position)23 HapFileDataSource::HapFileDataSource(RandomAccessFile& hapFile,
24     long long offset, long long size, long long position)
25     : DataSource(), hapFileRandomAccess(hapFile), fileOffset(offset), sourceSize(size), sourcePosition(position)
26 {
27 }
28 
~HapFileDataSource()29 HapFileDataSource::~HapFileDataSource()
30 {
31 }
32 
HasRemaining() const33 bool HapFileDataSource::HasRemaining() const
34 {
35     return sourcePosition < sourceSize;
36 }
37 
Remaining() const38 long long HapFileDataSource::Remaining() const
39 {
40     return sourceSize - sourcePosition;
41 }
42 
Reset()43 void HapFileDataSource::Reset()
44 {
45     sourcePosition = 0;
46 }
47 
ReadDataAndDigestUpdate(const DigestParameter & digestParam,int32_t chunkSize)48 bool HapFileDataSource::ReadDataAndDigestUpdate(const DigestParameter& digestParam, int32_t chunkSize)
49 {
50     if (!hapFileRandomAccess.ReadFileFromOffsetAndDigestUpdate(digestParam, chunkSize, fileOffset + sourcePosition)) {
51         HAPVERIFY_LOG_ERROR("ReadFileFromOffsetAndDigestUpdate failed");
52         return false;
53     }
54     sourcePosition += chunkSize;
55     return true;
56 }
57 } // namespace Verify
58 } // namespace Security
59 } // namespace OHOS
60