• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef HAP_RANDOM_ACCESS_FILE_H
16 #define HAP_RANDOM_ACCESS_FILE_H
17 
18 #include "common/export_define.h"
19 #include "common/hap_byte_buffer.h"
20 #include "util/digest_parameter.h"
21 
22 namespace OHOS {
23 namespace Security {
24 namespace Verify {
25 struct MmapInfo {
26     long long mmapPosition;
27     int readMoreLen;
28     int mmapSize;
29     char* mapAddr;
30 };
31 
32 class RandomAccessFile {
33 public:
34     DLL_EXPORT RandomAccessFile();
35     DLL_EXPORT ~RandomAccessFile();
36     DLL_EXPORT bool Init(const std::string& filePath);
37     DLL_EXPORT long long GetLength() const;
38     DLL_EXPORT long long ReadFileFullyFromOffset(HapByteBuffer& buffer, long long offset);
39     DLL_EXPORT long long ReadFileFullyFromOffset(char buf[], long long offset, int bufCapacity);
40     bool ReadFileFromOffsetAndDigestUpdate(const DigestParameter& digestParam, int chunkSize, long long offset);
41 
42 private:
43     long long DoMMap(int bufCapacity, long long offset, MmapInfo& mmapInfo);
44 
45     static const int FILE_OPEN_FAIL_ERROR_NUM;
46     static int memoryPageSize;
47     int fd;
48     long long fileLength;
49 };
50 } // namespace Verify
51 } // namespace Security
52 } // namespace OHOS
53 #endif // HAP_RANDOM_ACCESS_FILE_H
54