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 <iostream> 20 #include "log.h" 21 #include "pkg_manager.h" 22 #include "securec.h" 23 24 namespace hpackage { 25 #ifndef EOK 26 #define EOK 0 27 #endif 28 29 #define UNUSED(x) (void)(x) 30 31 std::string GetCurrPath(); 32 size_t GetFileSize(const std::string &fileName); 33 std::string GetFilePath(const std::string &fileName); 34 std::string GetName(const std::string &filePath); 35 int32_t CheckFile(const std::string &fileName); 36 uint8_t *MapMemory(const std::string &fileName, size_t size); 37 void ReleaseMemory(uint8_t *memMap, size_t size); 38 void ExtraTimeAndDate(time_t when, uint16_t &date, uint16_t &time); 39 std::string ConvertShaHex(const std::vector<uint8_t> &shaDigest); 40 41 // 读取或写入buffer 42 uint32_t ReadLE32(const uint8_t *buff); 43 void WriteLE32(uint8_t *buff, uint32_t value); 44 uint16_t ReadLE16(const uint8_t *buff); 45 void WriteLE16(uint8_t *buff, uint16_t value); 46 uint64_t ReadLE64(const uint8_t *buff); 47 48 #define PKG_LOGE(format, ...) Logger(updater::ERROR, (__FILE_NAME__), (__LINE__), format, ##__VA_ARGS__) 49 #define PKG_LOGI(format, ...) Logger(updater::INFO, (__FILE_NAME__), (__LINE__), format, ##__VA_ARGS__) 50 #define PKG_LOGW(format, ...) Logger(updater::WARNING, (__FILE_NAME__), (__LINE__), format, ##__VA_ARGS__) 51 52 #define PKG_CHECK(retCode, exper, ...) \ 53 if (!(retCode)) { \ 54 PKG_LOGE(__VA_ARGS__); \ 55 exper; \ 56 } 57 58 #define PKG_ONLY_CHECK(retCode, exper) \ 59 if (!(retCode)) { \ 60 exper; \ 61 } 62 63 #define PKG_IS_TRUE_DONE(retCode, exper) \ 64 if ((retCode)) { \ 65 exper; \ 66 } 67 68 enum { 69 PKG_INFO_BASE = PKG_ERROR_BASE + 100, 70 PKG_NOT_EXIST_ALGORITHM, 71 PKG_EXIT_FILE, 72 PKG_BUFFER_END, 73 PKG_LZ4_FINISH, 74 }; 75 } // namespace hpackage 76 #endif // PKG_UTILS_H