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 GetFilePath(const std::string &fileName); 37 std::string GetName(const std::string &filePath); 38 int32_t CheckFile(const std::string &fileName); 39 uint8_t *MapMemory(const std::string &fileName, size_t size); 40 void ReleaseMemory(uint8_t *memMap, size_t size); 41 void ExtraTimeAndDate(time_t when, uint16_t &date, uint16_t &time); 42 std::string ConvertShaHex(const std::vector<uint8_t> &shaDigest); 43 44 // 读取或写入buffer 45 uint32_t ReadLE32(const uint8_t *buff); 46 void WriteLE32(uint8_t *buff, uint32_t value); 47 uint16_t ReadLE16(const uint8_t *buff); 48 void WriteLE16(uint8_t *buff, uint16_t value); 49 uint64_t ReadLE64(const uint8_t *buff); 50 51 #define PKG_LOGE(format, ...) Logger(Updater::ERROR, (__FILE_NAME__), (__LINE__), format, ##__VA_ARGS__) 52 #define PKG_LOGI(format, ...) Logger(Updater::INFO, (__FILE_NAME__), (__LINE__), format, ##__VA_ARGS__) 53 #define PKG_LOGW(format, ...) Logger(Updater::WARNING, (__FILE_NAME__), (__LINE__), format, ##__VA_ARGS__) 54 55 #define PKG_CHECK(retCode, exper, ...) \ 56 if (!(retCode)) { \ 57 PKG_LOGE(__VA_ARGS__); \ 58 exper; \ 59 } 60 61 #define PKG_ONLY_CHECK(retCode, exper) \ 62 if (!(retCode)) { \ 63 exper; \ 64 } 65 66 #define PKG_IS_TRUE_DONE(retCode, exper) \ 67 if ((retCode)) { \ 68 exper; \ 69 } 70 71 enum { 72 PKG_INFO_BASE = PKG_ERROR_BASE + 100, 73 PKG_NOT_EXIST_ALGORITHM, 74 PKG_EXIT_FILE, 75 PKG_BUFFER_END, 76 PKG_LZ4_FINISH, 77 }; 78 } // namespace Hpackage 79 80 #ifdef _WIN32 81 #define HAVE_MMAP 1 82 #define MAP_ANON 0x20 83 #define MAP_POPULATE 0x08000 84 #define MAP_PRIVATE 0x02 85 #define MS_ASYNC 1 86 #define PROT_NONE 0x0 87 #define PROT_READ 0x1 88 #define PROT_WRITE 0x2 89 #define PROT_EXEC 0x4 90 #define MAP_FAILED ((void *) -1) 91 92 void *mmap(void *addr, size_t length, int prot, int flags, int fd, size_t offset); 93 int msync(void *addr, size_t len, int flags); 94 int munmap(void *addr, size_t len); 95 #endif // _WIN32 96 #endif // PKG_UTILS_H