1 /*
2  * Copyright (C) 2023 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 APP_VERIFY_PARCEL_UTIL_H
16 #define APP_VERIFY_PARCEL_UTIL_H
17 
18 #include "app_domain_verify_hilog.h"
19 #include "comm_define.h"
20 #include "string_ex.h"
21 
22 #define WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(type, parcel, data)                                              \
23     do {                                                                                                       \
24         if (!(parcel).Write##type(data)) {                                                                     \
25             APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "fail to write %{public}s type", #type); \
26             return false;                                                                                      \
27         }                                                                                                      \
28     } while (0)
29 
30 #define WRITE_PARCEL_AND_RETURN_IF_FAIL(type, parcel, data)                                                    \
31     do {                                                                                                       \
32         if (!(parcel).Write##type(data)) {                                                                     \
33             APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "fail to write %{public}s type", #type); \
34             return;                                                                                            \
35         }                                                                                                      \
36     } while (0)
37 
38 #define WRITE_PARCEL_AND_RETURN_INT_IF_FAIL(type, parcel, data)                                                \
39     do {                                                                                                       \
40         if (!(parcel).Write##type(data)) {                                                                     \
41             APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "fail to write %{public}s type", #type); \
42             return CommonErrorCode::E_INTERNAL_ERR;                                                            \
43         }                                                                                                      \
44     } while (0)
45 
46 #define READ_PARCEL_AND_RETURN_INT_IF_FAIL(type, parcel, data)                                                \
47     do {                                                                                                      \
48         if (!(parcel).Read##type(data)) {                                                                     \
49             APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "fail to read %{public}s type", #type); \
50             return CommonErrorCode::E_INTERNAL_ERR;                                                           \
51         }                                                                                                     \
52     } while (0)
53 
54 #define READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(type, parcel, data)                                              \
55     do {                                                                                                      \
56         if (!(parcel).Read##type(data)) {                                                                     \
57             APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "fail to read %{public}s type", #type); \
58             return false;                                                                                     \
59         }                                                                                                     \
60     } while (0)
61 
62 #define MAX_PARCEL_ARRAY_SIZE (99)
63 namespace OHOS::AppDomainVerify {
IsInvalidParcelArraySize(int size)64 inline bool IsInvalidParcelArraySize(int size)
65 {
66     if (size > MAX_PARCEL_ARRAY_SIZE) {
67         APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "is invalid parcel array size %{public}d", size);
68         return true;
69     }
70     return false;
71 }
72 }
73 #endif