1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci#ifndef PKG_UTILS_H
16fb299fa2Sopenharmony_ci#define PKG_UTILS_H
17fb299fa2Sopenharmony_ci
18fb299fa2Sopenharmony_ci#include <cstring>
19fb299fa2Sopenharmony_ci#include "log.h"
20fb299fa2Sopenharmony_ci#include "pkg_manager.h"
21fb299fa2Sopenharmony_ci#include "securec.h"
22fb299fa2Sopenharmony_ci
23fb299fa2Sopenharmony_cinamespace Hpackage {
24fb299fa2Sopenharmony_ci#ifndef EOK
25fb299fa2Sopenharmony_ci#define EOK 0
26fb299fa2Sopenharmony_ci#endif
27fb299fa2Sopenharmony_ci
28fb299fa2Sopenharmony_ci#define UNUSED(x) (void)(x)
29fb299fa2Sopenharmony_ci
30fb299fa2Sopenharmony_ci#ifdef __WIN32
31fb299fa2Sopenharmony_ci#undef ERROR
32fb299fa2Sopenharmony_ci#endif
33fb299fa2Sopenharmony_ci
34fb299fa2Sopenharmony_cistd::string GetCurrPath();
35fb299fa2Sopenharmony_cisize_t GetFileSize(const std::string &fileName);
36fb299fa2Sopenharmony_cistd::string GetName(const std::string &filePath);
37fb299fa2Sopenharmony_cistd::string GetFilePath(const std::string &fileName);
38fb299fa2Sopenharmony_ciint32_t CheckFile(const std::string &fileName, int type);
39fb299fa2Sopenharmony_ciuint8_t *AnonymousMap(const std::string &fileName, size_t size);
40fb299fa2Sopenharmony_ciuint8_t *FileMap(const std::string &path);
41fb299fa2Sopenharmony_civoid ReleaseMemory(uint8_t *memMap, size_t size);
42fb299fa2Sopenharmony_civoid ExtraTimeAndDate(time_t when, uint16_t &date, uint16_t &time);
43fb299fa2Sopenharmony_cistd::string ConvertShaHex(const std::vector<uint8_t> &shaDigest);
44fb299fa2Sopenharmony_ci
45fb299fa2Sopenharmony_ci// 读取或写入buffer
46fb299fa2Sopenharmony_ciuint32_t ReadLE32(const uint8_t *buff);
47fb299fa2Sopenharmony_civoid WriteLE32(uint8_t *buff, uint32_t value);
48fb299fa2Sopenharmony_ciuint16_t ReadLE16(const uint8_t *buff);
49fb299fa2Sopenharmony_civoid WriteLE16(uint8_t *buff, uint16_t value);
50fb299fa2Sopenharmony_ciuint64_t ReadLE64(const uint8_t *buff);
51fb299fa2Sopenharmony_ci
52fb299fa2Sopenharmony_ci#define PKG_LOGE(format, ...) Logger(Updater::ERROR, (UPDATER_LOG_FILE_NAME), (__LINE__), format, ##__VA_ARGS__)
53fb299fa2Sopenharmony_ci#define PKG_LOGI(format, ...) Logger(Updater::INFO, (UPDATER_LOG_FILE_NAME), (__LINE__), format, ##__VA_ARGS__)
54fb299fa2Sopenharmony_ci#define PKG_LOGW(format, ...) Logger(Updater::WARNING, (UPDATER_LOG_FILE_NAME), (__LINE__), format, ##__VA_ARGS__)
55fb299fa2Sopenharmony_ci
56fb299fa2Sopenharmony_cienum {
57fb299fa2Sopenharmony_ci    PKG_INFO_BASE = PKG_ERROR_BASE + 100,
58fb299fa2Sopenharmony_ci    PKG_NOT_EXIST_ALGORITHM,
59fb299fa2Sopenharmony_ci    PKG_EXIT_FILE,
60fb299fa2Sopenharmony_ci    PKG_BUFFER_END,
61fb299fa2Sopenharmony_ci    PKG_LZ4_FINISH,
62fb299fa2Sopenharmony_ci};
63fb299fa2Sopenharmony_ci
64fb299fa2Sopenharmony_cistruct __attribute__((packed)) PkgSignComment {
65fb299fa2Sopenharmony_ci    uint16_t signCommentTotalLen = 0;
66fb299fa2Sopenharmony_ci    uint16_t signCommentAppendLen = 0;
67fb299fa2Sopenharmony_ci};
68fb299fa2Sopenharmony_ci} // namespace Hpackage
69fb299fa2Sopenharmony_ci
70fb299fa2Sopenharmony_ci#ifdef _WIN32
71fb299fa2Sopenharmony_ci#define HAVE_MMAP 1
72fb299fa2Sopenharmony_ci#define MAP_ANON 0x20
73fb299fa2Sopenharmony_ci#define MAP_POPULATE 0x08000
74fb299fa2Sopenharmony_ci#define MAP_PRIVATE 0x02
75fb299fa2Sopenharmony_ci#define MS_ASYNC 1
76fb299fa2Sopenharmony_ci#define PROT_NONE 0x0
77fb299fa2Sopenharmony_ci#define PROT_READ 0x1
78fb299fa2Sopenharmony_ci#define PROT_WRITE 0x2
79fb299fa2Sopenharmony_ci#define PROT_EXEC 0x4
80fb299fa2Sopenharmony_ci#define MAP_FAILED ((void *) -1)
81fb299fa2Sopenharmony_ci
82fb299fa2Sopenharmony_civoid *mmap(void *addr, size_t length, int prot, int flags, int fd, size_t offset);
83fb299fa2Sopenharmony_ciint msync(void *addr, size_t len, int flags);
84fb299fa2Sopenharmony_ciint munmap(void *addr, size_t len);
85fb299fa2Sopenharmony_ci#endif // _WIN32
86fb299fa2Sopenharmony_ci#endif // PKG_UTILS_H