1/*
2 * Copyright (c) 2020-2022 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 SECURITY_APP_COMMON_H
17#define SECURITY_APP_COMMON_H
18
19#include <stdint.h>
20
21#include "app_verify_base.h"
22#include "app_verify_pub.h"
23
24#ifdef __cplusplus
25#if __cplusplus
26extern "C" {
27#endif
28#endif
29
30#define MAX_PRINT_LEN 1024
31#define DOUBLE_SIZE  2
32#define MAX_CHARACTER  9
33#define BYTE_BITS  8
34#define MAX_MALLOC_SIZE 0x100000  /* 1M */
35#define MAX_HASH_SIZE 64
36
37#define P_NULL_RETURN_WTTH_LOG(v) \
38do { \
39    if ((v) == NULL) { \
40        LOG_ERROR(#v" is null"); \
41        return V_ERR; \
42    } \
43} \
44while (0)
45
46#define P_NULL_RETURN_RET_WTTH_LOG(v, ret) \
47do { \
48    if ((v) == NULL) { \
49        LOG_ERROR(#v" is null"); \
50        return ret; \
51    } \
52} \
53while (0)
54
55#define P_NULL_RETURN_NULL_WTTH_LOG(v) \
56do { \
57    if ((v) == NULL) { \
58        LOG_ERROR(#v" is null"); \
59        return NULL; \
60    } \
61} \
62while (0)
63
64#define P_ERR_RETURN_WTTH_LOG(v) \
65do { \
66    if ((v) != V_OK) { \
67        LOG_ERROR(#v" not ok"); \
68        return v; \
69    } \
70} \
71while (0)
72
73#define P_NULL_GOTO_WTTH_LOG(v) \
74do { \
75    if ((v) == NULL) { \
76        LOG_ERROR(#v" is null"); \
77        goto EXIT; \
78    } \
79} \
80while (0)
81
82#define P_ERR_GOTO_WTTH_LOG(v) \
83do { \
84    if ((v) != V_OK) { \
85        LOG_ERROR(#v" not ok"); \
86        goto EXIT; \
87    } \
88} \
89while (0)
90
91#define FREE_IF_NOT_NULL(p) \
92do { \
93    APPV_FREE(p); \
94} \
95while (0)
96
97#define APPV_MALLOC(size) malloc(size)
98#define APPV_FREE(buf) \
99do { \
100    if ((buf) != NULL) { \
101        free(buf); \
102        (buf) = NULL; \
103    } \
104} while (0)
105
106long long HapGetInt64(const unsigned char *buf, int32_t len);
107int32_t HapGetInt(const unsigned char *buf, int32_t len);
108uint32_t HapGetUnsignedInt(const unsigned char *buf, int32_t len);
109short HapGetShort(const unsigned char *buf, int32_t len);
110void HapPutInt32(unsigned char *buf, int32_t len, int32_t value);
111#ifdef __cplusplus
112#if __cplusplus
113}
114#endif
115#endif
116
117#endif
118