1 /* 2 * Copyright (c) 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 #ifndef MESSAGE_SEQUENCE_IMPL_H 16 #define MESSAGE_SEQUENCE_IMPL_H 17 18 #include "cj_common_ffi.h" 19 #include "ffi_remote_data.h" 20 #include "ipc_skeleton.h" 21 #include "ipc_utils_ffi.h" 22 #include "message_parcel.h" 23 #include "securec.h" 24 25 namespace OHOS { 26 extern "C" { 27 struct MesOption { 28 int flags; 29 int waitTime; 30 }; 31 } 32 class FFI_EXPORT MessageSequenceImpl : public OHOS::FFI::FFIData { 33 DECL_TYPE(MessageSequenceImpl, OHOS::FFI::FFIData) 34 public: 35 explicit MessageSequenceImpl(MessageParcel* parcel); 36 virtual ~MessageSequenceImpl(); 37 std::shared_ptr<MessageParcel> GetMessageParcel(); 38 39 int32_t CJ_WriteInterfaceToken(std::u16string value); 40 std::u16string CJ_ReadInterfaceToken(int32_t* errCode); 41 uint32_t CJ_GetSize(int32_t* errCode); 42 uint32_t CJ_GetCapacity(int32_t* errCode); 43 int32_t CJ_SetSize(uint32_t value); 44 int32_t CJ_SetCapacity(uint32_t value); 45 uint32_t CJ_GetWritableBytes(int32_t* errCode); 46 uint32_t CJ_GetReadableBytes(int32_t* errCode); 47 uint32_t CJ_GetReadPosition(int32_t* errCode); 48 uint32_t CJ_GetWritePosition(int32_t* errCode); 49 int32_t CJ_RewindWrite(uint32_t pos); 50 int32_t CJ_RewindRead(uint32_t pos); 51 int32_t CJ_WriteNoException(); 52 std::string CJ_ReadException(int32_t* errCode); 53 54 int32_t CJ_WriteByte(int8_t value); 55 int32_t CJ_WriteShort(int16_t value); 56 int32_t CJ_WriteInt(int32_t value); 57 int32_t CJ_WriteLong(int64_t value); 58 int32_t CJ_WriteFloat(float value); 59 int32_t CJ_WriteDouble(double value); 60 int32_t CJ_WriteBoolean(int8_t value); 61 int32_t CJ_WriteChar(uint8_t value); 62 int32_t CJ_WriteString(std::u16string value); 63 int32_t CJ_WriteByteArray(CJByteArray value); 64 int32_t CJ_WriteShortArray(CJShortArray value); 65 int32_t CJ_WriteIntArray(CJIntArray value); 66 int32_t CJ_WriteLongArray(CJLongArray value); 67 int32_t CJ_WriteFloatArray(CJFloatArray value); 68 int32_t CJ_WriteDoubleArray(CJDoubleArray value); 69 int32_t CJ_WriteBooleanArray(CJByteArray value); 70 int32_t CJ_WriteCharArray(CJCharArray value); 71 int32_t CJ_WriteStringArray(std::u16string value[], uint32_t arrayLength); 72 int32_t CJ_WriteArrayBuffer(int32_t typeCode, void* value, size_t byteLength); 73 int32_t CJ_WriteRawDataBuffer(uint8_t* data, int64_t size); 74 bool CJ_WriteUint32(uint32_t value); 75 int32_t CJ_WriteRemoteObject(int64_t object); 76 int32_t CJ_WriteRemoteObjectArray(CJLongArray value); 77 78 int8_t CJ_ReadByte(int32_t* errCode); 79 int16_t CJ_ReadShort(int32_t* errCode); 80 int32_t CJ_ReadInt(int32_t* errCode); 81 int64_t CJ_ReadLong(int32_t* errCode); 82 float CJ_ReadFloat(int32_t* errCode); 83 double CJ_ReadDouble(int32_t* errCode); 84 int8_t CJ_ReadBoolean(int32_t* errCode); 85 uint8_t CJ_ReadChar(int32_t* errCode); 86 std::u16string CJ_ReadString(int32_t* errCode); 87 CJByteArray CJ_ReadByteArray(int32_t* errCode); 88 CJShortArray CJ_ReadShortArray(int32_t* errCode); 89 CJIntArray CJ_ReadIntArray(int32_t* errCode); 90 CJLongArray CJ_ReadLongArray(int32_t* errCode); 91 CJFloatArray CJ_ReadFloatArray(int32_t* errCode); 92 CJDoubleArray CJ_ReadDoubleArray(int32_t* errCode); 93 CJByteArray CJ_ReadBooleanArray(int32_t* errCode); 94 CJCharArray CJ_ReadCharArray(int32_t* errCode); 95 CJStringArray CJ_ReadStringArray(int32_t* errCode); 96 std::vector<int8_t> CJ_ReadInt8ArrayBuffer(int32_t* errCode); 97 std::vector<uint8_t> CJ_ReadUInt8ArrayBuffer(int32_t* errCode); 98 std::vector<int16_t> CJ_ReadInt16ArrayBuffer(int32_t* errCode); 99 std::vector<uint16_t> CJ_ReadUInt16ArrayBuffer(int32_t* errCode); 100 std::vector<int32_t> CJ_ReadInt32ArrayBuffer(int32_t* errCode); 101 std::vector<uint32_t> CJ_ReadUInt32ArrayBuffer(int32_t* errCode); 102 std::vector<float> CJ_ReadFloatArrayBuffer(int32_t* errCode); 103 std::vector<double> CJ_ReadDoubleArrayBuffer(int32_t* errCode); 104 std::vector<int64_t> CJ_ReadInt64ArrayBuffer(int32_t* errCode); 105 std::vector<uint64_t> CJ_ReadUInt64ArrayBuffer(int32_t* errCode); 106 uint8_t* CJ_ReadRawDataBuffer(int64_t size, int32_t* errCode); 107 RetDataI64 CJ_ReadRemoteObject(int32_t* errCode); 108 RemoteObjectArray CJ_ReadRemoteObjectArray(int32_t* errCode); 109 110 static void CJ_CloseFileDescriptor(int32_t fd); 111 static int32_t CJ_DupFileDescriptor(int32_t fd); 112 bool CJ_ContainFileDescriptors(int32_t* errCode); 113 int32_t CJ_WriteFileDescriptor(int32_t fd); 114 int32_t CJ_ReadFileDescriptor(int32_t* errCode); 115 uint32_t CJ_GetRawDataCapacity(int32_t* errCode); 116 int32_t CJ_WriteAshmem(sptr<Ashmem> nativeAshmem); 117 sptr<Ashmem> CJ_ReadAshmem(int32_t* errCode); 118 119 private: 120 static void release(MessageParcel* parcel); 121 bool CheckWritePosition(); 122 bool CheckWriteCapacity(size_t lenToWrite); 123 bool RewindIfWriteCheckFail(size_t lenToWrite, size_t pos); 124 bool CheckReadPosition(); 125 bool CheckReadLength(size_t arrayLength, size_t typeSize); 126 bool CJ_WriteVectorByTypeCode(int32_t typeCode, void* data, size_t byteLength); 127 128 bool owner; 129 std::shared_ptr<MessageParcel> nativeParcel_ = nullptr; 130 size_t maxCapacityToWrite_; 131 }; 132 } // namespace OHOS 133 134 #endif 135