1 /*
2  * Copyright (c) 2024 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 NAPI_IPC_OHOS_NAPI_RPC_COMMON_H
17 #define NAPI_IPC_OHOS_NAPI_RPC_COMMON_H
18 
19 #include "napi_rpc_error.h"
20 
21 namespace OHOS {
22 static NapiError napiErr;
23 
24 #define CHECK_WRITE_POSITION(env, napiParcel)                                                                         \
25     do {                                                                                                              \
26         if ((napiParcel)->maxCapacityToWrite_ < (napiParcel)->nativeParcel_->GetWritePosition()) {                    \
27             ZLOGE(LOG_LABEL, "invalid write position, maxCapacityToWrite_:%{public}zu, GetWritePosition:%{public}zu", \
28                 (napiParcel)->maxCapacityToWrite_, (napiParcel)->nativeParcel_->GetWritePosition());                  \
29             return napiErr.ThrowError(env, errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR);                          \
30         }                                                                                                             \
31     } while (0)
32 
33 #define CHECK_READ_POSITION(env, napiParcel)                                                                 \
34     do {                                                                                                     \
35         if ((napiParcel)->nativeParcel_->GetDataSize() < (napiParcel)->nativeParcel_->GetReadPosition()) {   \
36             ZLOGE(LOG_LABEL, "invalid read position, GetDataSize:%{public}zu, GetReadPosition:%{public}zu",  \
37                 (napiParcel)->nativeParcel_->GetDataSize(), (napiParcel)->nativeParcel_->GetReadPosition()); \
38             return napiErr.ThrowError(env, errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR);                \
39         }                                                                                                    \
40     } while (0)
41 
42 #define CHECK_WRITE_CAPACITY(env, lenToWrite, napiParcel)                                                             \
43     do {                                                                                                              \
44         CHECK_WRITE_POSITION(env, napiParcel);                                                                        \
45         size_t cap = (napiParcel)->maxCapacityToWrite_ - (napiParcel)->nativeParcel_->GetWritePosition();             \
46         if (cap < (lenToWrite)) {                                                                                     \
47             ZLOGE(LOG_LABEL, "No enough write capacity, cap:%{public}zu, lenToWrite:%{public}zu", cap, lenToWrite);   \
48             napi_throw_range_error(env, nullptr, "No enough capacity to write");                                      \
49         }                                                                                                             \
50     } while (0)
51 
52 #define REWIND_IF_WRITE_CHECK_FAIL(env, lenToWrite, pos, napiParcel)                                                  \
53     do {                                                                                                              \
54         CHECK_WRITE_POSITION(env, napiParcel);                                                                        \
55         size_t cap = (napiParcel)->maxCapacityToWrite_ - (napiParcel)->nativeParcel_->GetWritePosition();             \
56         if (cap < (lenToWrite)) {                                                                                     \
57             ZLOGE(LOG_LABEL, "No enough write capacity, cap:%{public}zu, lenToWrite:%{public}zu", cap, lenToWrite);   \
58             (napiParcel)->nativeParcel_->RewindWrite(pos);                                                            \
59             napi_throw_range_error(env, nullptr, "No enough capacity to write");                                      \
60         }                                                                                                             \
61     } while (0)
62 
63 #define CHECK_READ_LENGTH(env, arrayLength, typeSize, napiParcel)                                                    \
64     do {                                                                                                             \
65         CHECK_READ_POSITION(env, napiParcel);                                                                        \
66         size_t remainSize = (napiParcel)->nativeParcel_->GetDataSize() -                                             \
67             (napiParcel)->nativeParcel_->GetReadPosition();                                                          \
68         if (((arrayLength) < 0) || ((arrayLength) > remainSize) || (((arrayLength) * (typeSize)) > remainSize)) {    \
69             ZLOGE(LOG_LABEL, "No enough data to read, arrayLength:%{public}zu, remainSize:%{public}zu,"              \
70                 "typeSize:%{public}zu, GetDataSize:%{public}zu, GetReadPosition:%{public}zu", arrayLength,           \
71                 remainSize, typeSize, (napiParcel)->nativeParcel_->GetDataSize(),                                    \
72                 (napiParcel)->nativeParcel_->GetReadPosition());                                                     \
73             napi_throw_range_error(env, nullptr, "No enough data to read");                                          \
74         }                                                                                                            \
75     } while (0)
76 
77 constexpr size_t MAX_CAPACITY_TO_WRITE = 200 * 1024;
78 constexpr size_t MAX_BYTES_LENGTH = 40960;
79 constexpr size_t BYTE_SIZE_8 = 1;
80 constexpr size_t BYTE_SIZE_16 = 2;
81 constexpr size_t BYTE_SIZE_32 = 4;
82 constexpr size_t BYTE_SIZE_64 = 8;
83 constexpr size_t ARGV_INDEX_0 = 0;
84 constexpr size_t ARGV_INDEX_1 = 1;
85 constexpr size_t ARGV_LENGTH_1 = 1;
86 constexpr size_t ARGV_LENGTH_2 = 2;
87 constexpr size_t REQUIRED_ARGS_COUNT_1 = 1;  // "requires 1 parameter"
88 constexpr size_t ENUM_TYPECODE_COUNT = 10;
89 
90 enum TypeCode {
91     INT8_ARRAY        = 0,
92     UINT8_ARRAY       = 1,
93     INT16_ARRAY       = 2,
94     UINT16_ARRAY      = 3,
95     INT32_ARRAY       = 4,
96     UINT32_ARRAY      = 5,
97     FLOAT32_ARRAY     = 6,
98     FLOAT64_ARRAY     = 7,
99     BIGINT64_ARRAY    = 8,
100     BIGUINT64_ARRAY   = 9,
101 };
102 
103 } // namespace OHOS
104 #endif //  NAPI_IPC_OHOS_NAPI_RPC_COMMON_H