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