1 /* 2 * Copyright (c) 2024-2025 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 return nullptr; \ 50 } \ 51 } while (0) 52 53 #define REWIND_IF_WRITE_CHECK_FAIL(env, lenToWrite, pos, napiParcel) \ 54 do { \ 55 CHECK_WRITE_POSITION(env, napiParcel); \ 56 size_t cap = (napiParcel)->maxCapacityToWrite_ - (napiParcel)->nativeParcel_->GetWritePosition(); \ 57 if (cap < (lenToWrite)) { \ 58 ZLOGE(LOG_LABEL, "No enough write capacity, cap:%{public}zu, lenToWrite:%{public}zu", cap, lenToWrite); \ 59 (napiParcel)->nativeParcel_->RewindWrite(pos); \ 60 napi_throw_range_error(env, nullptr, "No enough capacity to write"); \ 61 return nullptr; \ 62 } \ 63 } while (0) 64 65 #define CHECK_READ_LENGTH(env, arrayLength, typeSize, napiParcel) \ 66 do { \ 67 CHECK_READ_POSITION(env, napiParcel); \ 68 size_t remainSize = (napiParcel)->nativeParcel_->GetDataSize() - \ 69 (napiParcel)->nativeParcel_->GetReadPosition(); \ 70 if (((arrayLength) < 0) || ((arrayLength) > remainSize) || (((arrayLength) * (typeSize)) > remainSize)) { \ 71 ZLOGE(LOG_LABEL, "No enough data to read, arrayLength:%{public}zu, remainSize:%{public}zu," \ 72 "typeSize:%{public}zu, GetDataSize:%{public}zu, GetReadPosition:%{public}zu", arrayLength, \ 73 remainSize, typeSize, (napiParcel)->nativeParcel_->GetDataSize(), \ 74 (napiParcel)->nativeParcel_->GetReadPosition()); \ 75 napi_throw_range_error(env, nullptr, "No enough data to read"); \ 76 return nullptr; \ 77 } \ 78 } while (0) 79 80 #define CHECK_BREAK(cond) \ 81 if (!(cond)) { \ 82 break; \ 83 } 84 85 constexpr size_t MAX_CAPACITY_TO_WRITE = 200 * 1024; 86 constexpr size_t MAX_BYTES_LENGTH = 40960; 87 constexpr size_t BYTE_SIZE_8 = 1; 88 constexpr size_t BYTE_SIZE_16 = 2; 89 constexpr size_t BYTE_SIZE_32 = 4; 90 constexpr size_t BYTE_SIZE_64 = 8; 91 constexpr size_t ARGV_INDEX_0 = 0; 92 constexpr size_t ARGV_INDEX_1 = 1; 93 constexpr size_t ARGV_LENGTH_1 = 1; 94 constexpr size_t ARGV_LENGTH_2 = 2; 95 constexpr size_t REQUIRED_ARGS_COUNT_1 = 1; // "requires 1 parameter" 96 constexpr size_t ENUM_TYPECODE_COUNT = 10; 97 98 enum TypeCode { 99 INT8_ARRAY = 0, 100 UINT8_ARRAY = 1, 101 INT16_ARRAY = 2, 102 UINT16_ARRAY = 3, 103 INT32_ARRAY = 4, 104 UINT32_ARRAY = 5, 105 FLOAT32_ARRAY = 6, 106 FLOAT64_ARRAY = 7, 107 BIGINT64_ARRAY = 8, 108 BIGUINT64_ARRAY = 9, 109 }; 110 111 } // namespace OHOS 112 #endif // NAPI_IPC_OHOS_NAPI_RPC_COMMON_H