• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 "app_file.h"
17 #include <fcntl.h>
18 #include <limits.h>
19 #include <stdbool.h>
20 #include <string.h>
21 #include <sys/mman.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include "app_centraldirectory.h"
26 #include "app_verify_hal.h"
27 
28 static int32_t g_memoryPageSize = 0;
InitVerify(FileRead * file,const char * filePath,int32_t * handle)29 int32_t InitVerify(FileRead *file, const char *filePath, int32_t *handle)
30 {
31     if (handle == NULL || file == NULL || filePath == NULL) {
32         LOG_ERROR("file open error");
33         return V_ERR_FILE_OPEN;
34     }
35     RegistHalFunc();
36     char *path = APPV_MALLOC(PATH_MAX + 1);
37     if (path == NULL) {
38         LOG_ERROR("path malloc error");
39         return V_ERR_MALLOC;
40     }
41     if ((strlen(filePath) > PATH_MAX) || (NULL == realpath(filePath, path))) {
42         APPV_FREE(path);
43         LOG_ERROR("file path error");
44         return V_ERR_FILE_OPEN;
45     }
46     *handle = open(path, O_RDONLY, 0);
47     if (*handle < 0) {
48         APPV_FREE(path);
49         LOG_PRINT_STR("file open error");
50         return V_ERR_FILE_OPEN;
51     }
52     if (g_memoryPageSize == 0) {
53         g_memoryPageSize = sysconf(_SC_PAGESIZE);
54     }
55     if (g_memoryPageSize <= 0) {
56         LOG_ERROR("MAP_FAILED %d", g_memoryPageSize);
57         APPV_FREE(path);
58         return V_ERR_FILE_STAT;
59     }
60     file->len = lseek(*handle, 0, SEEK_END);
61     file->fp = *handle;
62     APPV_FREE(path);
63     return V_OK;
64 }
65 
HapMMap(int32_t bufCapacity,int32_t offset,MmapInfo * mmapInfo,const FileRead * file)66 int32_t HapMMap(int32_t bufCapacity, int32_t offset, MmapInfo *mmapInfo, const FileRead *file)
67 {
68     if (mmapInfo == NULL || file == NULL || bufCapacity <= 0) {
69         return MMAP_FAILED;
70     }
71     mmapInfo->mapAddr = (char*)(MAP_FAILED);
72     if (file->fp == FILE_OPEN_FAIL_ERROR_NUM) {
73         return FILE_IS_CLOSE;
74     }
75     if (offset < 0 || offset > file->len - bufCapacity) {
76         return READ_OFFSET_OUT_OF_RANGE;
77     }
78     lseek(file->fp, offset, SEEK_SET);
79     if (g_memoryPageSize == 0) {
80         return MMAP_FAILED;
81     }
82     mmapInfo->mmapPosition = (offset / g_memoryPageSize) * g_memoryPageSize;
83     mmapInfo->readMoreLen = (int)(offset - mmapInfo->mmapPosition);
84     mmapInfo->mmapSize = bufCapacity + mmapInfo->readMoreLen;
85     mmapInfo->mapAddr = (char*)(mmap(NULL, mmapInfo->mmapSize, PROT_READ,
86         MAP_SHARED, file->fp, mmapInfo->mmapPosition));
87     if (mmapInfo->mapAddr == MAP_FAILED) {
88         LOG_ERROR("MAP_FAILED");
89         return MMAP_FAILED;
90     }
91     return V_OK;
92 }
93 
HapMUnMap(char * mapAddr,int32_t mmapSize)94 void HapMUnMap(char *mapAddr, int32_t mmapSize)
95 {
96     if (mapAddr == NULL || mmapSize <= 0) {
97         return;
98     }
99     munmap(mapAddr, mmapSize);
100 }
101