1 /* 2 * Copyright (c) 2024-2024 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 #ifndef SIGNATRUETOOLS_RANDOM_ACCESS_FILE_H 17 #define SIGNATRUETOOLS_RANDOM_ACCESS_FILE_H 18 19 #include <cerrno> 20 #include <fcntl.h> 21 #include <sys/mman.h> 22 #include <sys/stat.h> 23 #include <unistd.h> 24 #include <string> 25 #include <cstring> 26 #include <fstream> 27 28 #include "export_define.h" 29 #include "byte_buffer.h" 30 #include "digest_parameter.h" 31 32 namespace OHOS { 33 namespace SignatureTools { 34 35 struct MmapInfo { 36 int64_t mmapPosition; 37 int32_t readMoreLen = 0; 38 int32_t mmapSize = 0; 39 char* mapAddr; 40 }; 41 42 class RandomAccessFile { 43 public: 44 DLL_EXPORT RandomAccessFile(); 45 DLL_EXPORT ~RandomAccessFile(); 46 DLL_EXPORT bool Init(const std::string& filePath); 47 DLL_EXPORT int64_t GetLength() const; 48 DLL_EXPORT int32_t ReadFileFullyFromOffset(ByteBuffer& buffer, int64_t offset); 49 DLL_EXPORT int32_t ReadFileFullyFromOffset(char buf[], int64_t offset, int64_t bufCapacity); 50 DLL_EXPORT int32_t WriteToFile(ByteBuffer& buffer, int64_t position, int64_t length); 51 DLL_EXPORT bool ReadFileFromOffsetAndDigestUpdate(const DigestParameter& digestParam, int32_t chunkSize, 52 int64_t offset); 53 54 private: 55 int32_t DoMMap(int32_t bufCapacity, int64_t offset, MmapInfo& mmapInfo); 56 bool CheckLittleEndian(); 57 static int32_t memoryPageSize; 58 int32_t fd = 0; 59 int64_t fileLength; 60 }; 61 } // namespace SignatureTools 62 } // namespace OHOS 63 #endif // SIGNATRUETOOLS_RANDOM_ACCESS_FILE_H 64