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 PKG_UTILS_H 16 #define PKG_UTILS_H 17 18 #include <cstring> 19 #include "log.h" 20 #include "pkg_manager.h" 21 #include "securec.h" 22 23 namespace Hpackage { 24 #ifndef EOK 25 #define EOK 0 26 #endif 27 28 #define UNUSED(x) (void)(x) 29 30 #ifdef __WIN32 31 #undef ERROR 32 #endif 33 34 std::string GetCurrPath(); 35 size_t GetFileSize(const std::string &fileName); 36 std::string GetName(const std::string &filePath); 37 std::string GetFilePath(const std::string &fileName); 38 int32_t CheckFile(const std::string &fileName, int type); 39 uint8_t *AnonymousMap(const std::string &fileName, size_t size); 40 uint8_t *FileMap(const std::string &path); 41 void ReleaseMemory(uint8_t *memMap, size_t size); 42 void ExtraTimeAndDate(time_t when, uint16_t &date, uint16_t &time); 43 std::string ConvertShaHex(const std::vector<uint8_t> &shaDigest); 44 45 // 读取或写入buffer 46 uint32_t ReadLE32(const uint8_t *buff); 47 void WriteLE32(uint8_t *buff, uint32_t value); 48 uint16_t ReadLE16(const uint8_t *buff); 49 void WriteLE16(uint8_t *buff, uint16_t value); 50 uint64_t ReadLE64(const uint8_t *buff); 51 52 #define PKG_LOGE(format, ...) Logger(Updater::ERROR, (__FILE_NAME__), (__LINE__), format, ##__VA_ARGS__) 53 #define PKG_LOGI(format, ...) Logger(Updater::INFO, (__FILE_NAME__), (__LINE__), format, ##__VA_ARGS__) 54 #define PKG_LOGW(format, ...) Logger(Updater::WARNING, (__FILE_NAME__), (__LINE__), format, ##__VA_ARGS__) 55 56 enum { 57 PKG_INFO_BASE = PKG_ERROR_BASE + 100, 58 PKG_NOT_EXIST_ALGORITHM, 59 PKG_EXIT_FILE, 60 PKG_BUFFER_END, 61 PKG_LZ4_FINISH, 62 }; 63 64 struct __attribute__((packed)) PkgSignComment { 65 uint16_t signCommentTotalLen = 0; 66 uint16_t signCommentAppendLen = 0; 67 }; 68 } // namespace Hpackage 69 70 #ifdef _WIN32 71 #define HAVE_MMAP 1 72 #define MAP_ANON 0x20 73 #define MAP_POPULATE 0x08000 74 #define MAP_PRIVATE 0x02 75 #define MS_ASYNC 1 76 #define PROT_NONE 0x0 77 #define PROT_READ 0x1 78 #define PROT_WRITE 0x2 79 #define PROT_EXEC 0x4 80 #define MAP_FAILED ((void *) -1) 81 82 void *mmap(void *addr, size_t length, int prot, int flags, int fd, size_t offset); 83 int msync(void *addr, size_t len, int flags); 84 int munmap(void *addr, size_t len); 85 #endif // _WIN32 86 #endif // PKG_UTILS_H