/* * Copyright (c) 2020-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "app_file.h" #include #include #include #include #include #include #include #include #include "app_centraldirectory.h" #include "app_verify_hal.h" static int32_t g_memoryPageSize = 0; int32_t InitVerify(FileRead *file, const char *filePath, int32_t *handle) { if (handle == NULL || file == NULL || filePath == NULL) { LOG_ERROR("file open error"); return V_ERR_FILE_OPEN; } RegistHalFunc(); char *path = APPV_MALLOC(PATH_MAX + 1); if (path == NULL) { LOG_ERROR("path malloc error"); return V_ERR_MALLOC; } if ((strlen(filePath) > PATH_MAX) || (NULL == realpath(filePath, path))) { APPV_FREE(path); LOG_ERROR("file path error"); return V_ERR_FILE_OPEN; } *handle = open(path, O_RDONLY, 0); if (*handle < 0) { APPV_FREE(path); LOG_PRINT_STR("file open error"); return V_ERR_FILE_OPEN; } if (g_memoryPageSize == 0) { g_memoryPageSize = sysconf(_SC_PAGESIZE); } if (g_memoryPageSize <= 0) { LOG_ERROR("MAP_FAILED %d", g_memoryPageSize); APPV_FREE(path); return V_ERR_FILE_STAT; } file->len = lseek(*handle, 0, SEEK_END); file->fp = *handle; APPV_FREE(path); return V_OK; } int32_t HapMMap(int32_t bufCapacity, int32_t offset, MmapInfo *mmapInfo, const FileRead *file) { if (mmapInfo == NULL || file == NULL || bufCapacity <= 0) { return MMAP_FAILED; } mmapInfo->mapAddr = (char*)(MAP_FAILED); if (file->fp == FILE_OPEN_FAIL_ERROR_NUM) { return FILE_IS_CLOSE; } if (offset < 0 || offset > file->len - bufCapacity) { return READ_OFFSET_OUT_OF_RANGE; } lseek(file->fp, offset, SEEK_SET); if (g_memoryPageSize == 0) { return MMAP_FAILED; } mmapInfo->mmapPosition = (offset / g_memoryPageSize) * g_memoryPageSize; mmapInfo->readMoreLen = (int)(offset - mmapInfo->mmapPosition); mmapInfo->mmapSize = bufCapacity + mmapInfo->readMoreLen; mmapInfo->mapAddr = (char*)(mmap(NULL, mmapInfo->mmapSize, PROT_READ, MAP_SHARED, file->fp, mmapInfo->mmapPosition)); if (mmapInfo->mapAddr == MAP_FAILED) { LOG_ERROR("MAP_FAILED"); return MMAP_FAILED; } return V_OK; } void HapMUnMap(char *mapAddr, int32_t mmapSize) { if (mapAddr == NULL || mmapSize <= 0) { return; } munmap(mapAddr, mmapSize); }