• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "message_sequence_impl.h"
17 
18 #include <cinttypes>
19 #include <cstring>
20 #include <unistd.h>
21 
22 #include "remote_object_impl.h"
23 #include "string_ex.h"
24 
25 namespace OHOS {
MessageSequenceImpl(MessageParcel * parcel)26 MessageSequenceImpl::MessageSequenceImpl(MessageParcel* parcel)
27 {
28     maxCapacityToWrite_ = MAX_CAPACITY_TO_WRITE;
29     if (parcel == nullptr) {
30         nativeParcel_ = std::make_shared<MessageParcel>();
31         owner = true;
32     } else {
33         nativeParcel_ = std::shared_ptr<MessageParcel>(parcel, release);
34         owner = false;
35     }
36 }
37 
~MessageSequenceImpl()38 MessageSequenceImpl::~MessageSequenceImpl()
39 {
40     ZLOGD(LOG_LABEL, "MessageSequence_FFI::Destructor");
41     nativeParcel_ = nullptr;
42 }
43 
release(MessageParcel * parcel)44 void MessageSequenceImpl::release(MessageParcel* parcel)
45 {
46     ZLOGD(LOG_LABEL, "message parcel is created by others, do nothing");
47 }
48 
GetMessageParcel()49 std::shared_ptr<MessageParcel> MessageSequenceImpl::GetMessageParcel()
50 {
51     return nativeParcel_;
52 }
53 
CJ_WriteInterfaceToken(std::u16string value)54 int32_t MessageSequenceImpl::CJ_WriteInterfaceToken(std::u16string value)
55 {
56     if (nativeParcel_ == nullptr) {
57         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
58     }
59     bool writeResult = nativeParcel_->WriteInterfaceToken(value);
60     if (writeResult == false) {
61         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
62     }
63     return 0;
64 }
65 
CJ_ReadInterfaceToken(int32_t * errCode)66 std::u16string MessageSequenceImpl::CJ_ReadInterfaceToken(int32_t* errCode)
67 {
68     if (nativeParcel_ == nullptr) {
69         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
70         return u"";
71     }
72     return nativeParcel_->ReadInterfaceToken();
73 }
74 
CJ_GetSize(int32_t * errCode)75 uint32_t MessageSequenceImpl::CJ_GetSize(int32_t* errCode)
76 {
77     if (nativeParcel_ == nullptr) {
78         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
79         return 0;
80     }
81     return static_cast<uint32_t>(nativeParcel_->GetDataSize());
82 }
83 
CJ_GetCapacity(int32_t * errCode)84 uint32_t MessageSequenceImpl::CJ_GetCapacity(int32_t* errCode)
85 {
86     if (nativeParcel_ == nullptr) {
87         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
88         return 0;
89     }
90     size_t value = nativeParcel_->GetDataCapacity();
91     return static_cast<uint32_t>(value);
92 }
93 
CJ_SetSize(uint32_t value)94 int32_t MessageSequenceImpl::CJ_SetSize(uint32_t value)
95 {
96     if (nativeParcel_ == nullptr) {
97         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
98     }
99     nativeParcel_->SetDataSize(static_cast<size_t>(value));
100     return 0;
101 }
102 
CJ_SetCapacity(uint32_t value)103 int32_t MessageSequenceImpl::CJ_SetCapacity(uint32_t value)
104 {
105     if (nativeParcel_ == nullptr) {
106         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
107     }
108     nativeParcel_->SetDataCapacity(static_cast<size_t>(value));
109     return 0;
110 }
111 
CJ_GetWritableBytes(int32_t * errCode)112 uint32_t MessageSequenceImpl::CJ_GetWritableBytes(int32_t* errCode)
113 {
114     if (nativeParcel_ == nullptr) {
115         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
116         return 0;
117     }
118     size_t value = nativeParcel_->GetWritableBytes();
119     return static_cast<uint32_t>(value);
120 }
121 
CJ_GetReadableBytes(int32_t * errCode)122 uint32_t MessageSequenceImpl::CJ_GetReadableBytes(int32_t* errCode)
123 {
124     if (nativeParcel_ == nullptr) {
125         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
126         return 0;
127     }
128     size_t value = nativeParcel_->GetReadableBytes();
129     return static_cast<uint32_t>(value);
130 }
131 
CJ_GetReadPosition(int32_t * errCode)132 uint32_t MessageSequenceImpl::CJ_GetReadPosition(int32_t* errCode)
133 {
134     if (nativeParcel_ == nullptr) {
135         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
136         return 0;
137     }
138     size_t value = nativeParcel_->GetReadPosition();
139     return static_cast<uint32_t>(value);
140 }
141 
CJ_GetWritePosition(int32_t * errCode)142 uint32_t MessageSequenceImpl::CJ_GetWritePosition(int32_t* errCode)
143 {
144     if (nativeParcel_ == nullptr) {
145         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
146         return 0;
147     }
148     size_t value = nativeParcel_->GetWritePosition();
149     return static_cast<uint32_t>(value);
150 }
151 
CJ_RewindWrite(uint32_t pos)152 int32_t MessageSequenceImpl::CJ_RewindWrite(uint32_t pos)
153 {
154     if (nativeParcel_ == nullptr) {
155         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
156     }
157     bool result = nativeParcel_->RewindWrite(static_cast<size_t>(pos));
158     return result ? 0 : errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
159 }
160 
CJ_RewindRead(uint32_t pos)161 int32_t MessageSequenceImpl::CJ_RewindRead(uint32_t pos)
162 {
163     if (nativeParcel_ == nullptr) {
164         return errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
165     }
166     bool result = nativeParcel_->RewindRead(static_cast<size_t>(pos));
167     return result ? 0 : errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
168 }
169 
CJ_WriteNoException()170 int32_t MessageSequenceImpl::CJ_WriteNoException()
171 {
172     if (nativeParcel_ == nullptr) {
173         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
174     }
175     bool writeResult = nativeParcel_->WriteInt32(0);
176     if (writeResult == false) {
177         ZLOGE(LOG_LABEL, "write int32 failed");
178         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
179     }
180     return 0;
181 }
182 
CJ_ReadException(int32_t * errCode)183 std::string MessageSequenceImpl::CJ_ReadException(int32_t* errCode)
184 {
185     if (nativeParcel_ == nullptr) {
186         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
187         return "";
188     }
189     int32_t code = nativeParcel_->ReadInt32();
190     if (code == 0) {
191         return "";
192     }
193     std::u16string str = nativeParcel_->ReadString16();
194     return Str16ToStr8(str);
195 }
196 
CheckWritePosition()197 bool MessageSequenceImpl::CheckWritePosition()
198 {
199     if (maxCapacityToWrite_ < nativeParcel_->GetWritePosition()) {
200         ZLOGE(LOG_LABEL, "invalid write position, maxCapacityToWrite_:%{public}zu, GetWritePosition:%{public}zu",
201             maxCapacityToWrite_, nativeParcel_->GetWritePosition());
202         return false;
203     }
204     return true;
205 }
206 
CheckWriteCapacity(size_t lenToWrite)207 bool MessageSequenceImpl::CheckWriteCapacity(size_t lenToWrite)
208 {
209     if (CheckWritePosition()) {
210         size_t cap = maxCapacityToWrite_ - nativeParcel_->GetWritePosition();
211         if (cap < lenToWrite) {
212             ZLOGE(LOG_LABEL, "No enough write capacity, cap:%{public}zu, lenToWrite:%{public}zu", cap, lenToWrite);
213             return false;
214         }
215         return true;
216     }
217     return false;
218 }
219 
RewindIfWriteCheckFail(size_t lenToWrite,size_t pos)220 bool MessageSequenceImpl::RewindIfWriteCheckFail(size_t lenToWrite, size_t pos)
221 {
222     if (CheckWritePosition()) {
223         size_t cap = maxCapacityToWrite_ - nativeParcel_->GetWritePosition();
224         if (cap < lenToWrite) {
225             ZLOGE(LOG_LABEL, "No enough write capacity, cap:%{public}zu, lenToWrite:%{public}zu", cap, lenToWrite);
226             nativeParcel_->RewindWrite(pos);
227             return false;
228         }
229         return true;
230     }
231     return false;
232 }
233 
CJ_WriteByte(int8_t value)234 int32_t MessageSequenceImpl::CJ_WriteByte(int8_t value)
235 {
236     if (nativeParcel_ == nullptr) {
237         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
238     }
239     if (CheckWriteCapacity(BYTE_SIZE_32)) {
240         bool result = nativeParcel_->WriteInt8(value);
241         if (!result) {
242             ZLOGE(LOG_LABEL, "write int8 failed");
243             return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
244         }
245         return 0;
246     }
247     return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
248 }
249 
CJ_WriteShort(int16_t value)250 int32_t MessageSequenceImpl::CJ_WriteShort(int16_t value)
251 {
252     if (nativeParcel_ == nullptr) {
253         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
254     }
255     if (CheckWriteCapacity(BYTE_SIZE_32)) {
256         bool result = nativeParcel_->WriteInt16(value);
257         if (!result) {
258             ZLOGE(LOG_LABEL, "write int16 failed");
259             return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
260         }
261         return 0;
262     }
263     return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
264 }
265 
CJ_WriteInt(int32_t value)266 int32_t MessageSequenceImpl::CJ_WriteInt(int32_t value)
267 {
268     if (nativeParcel_ == nullptr) {
269         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
270     }
271     if (CheckWriteCapacity(BYTE_SIZE_32)) {
272         bool result = nativeParcel_->WriteInt32(value);
273         if (!result) {
274             ZLOGE(LOG_LABEL, "write int32 failed");
275             return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
276         }
277         return 0;
278     }
279     return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
280 }
281 
CJ_WriteLong(int64_t value)282 int32_t MessageSequenceImpl::CJ_WriteLong(int64_t value)
283 {
284     if (nativeParcel_ == nullptr) {
285         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
286     }
287     if (CheckWriteCapacity(BYTE_SIZE_64)) {
288         bool result = nativeParcel_->WriteInt64(value);
289         if (!result) {
290             ZLOGE(LOG_LABEL, "write int64 failed");
291             return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
292         }
293         return 0;
294     }
295     return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
296 }
297 
CJ_WriteFloat(float value)298 int32_t MessageSequenceImpl::CJ_WriteFloat(float value)
299 {
300     if (nativeParcel_ == nullptr) {
301         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
302     }
303     if (CheckWriteCapacity(sizeof(float))) {
304         bool result = nativeParcel_->WriteFloat(value);
305         if (!result) {
306             ZLOGE(LOG_LABEL, "write float failed");
307             return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
308         }
309         return 0;
310     }
311     return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
312 }
313 
CJ_WriteDouble(double value)314 int32_t MessageSequenceImpl::CJ_WriteDouble(double value)
315 {
316     if (nativeParcel_ == nullptr) {
317         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
318     }
319     if (CheckWriteCapacity(sizeof(double))) {
320         bool result = nativeParcel_->WriteDouble(value);
321         if (!result) {
322             ZLOGE(LOG_LABEL, "write double failed");
323             return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
324         }
325         return 0;
326     }
327     return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
328 }
329 
CJ_WriteBoolean(int8_t value)330 int32_t MessageSequenceImpl::CJ_WriteBoolean(int8_t value)
331 {
332     return CJ_WriteByte(value);
333 }
334 
CJ_WriteChar(uint8_t value)335 int32_t MessageSequenceImpl::CJ_WriteChar(uint8_t value)
336 {
337     if (nativeParcel_ == nullptr) {
338         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
339     }
340     if (CheckWriteCapacity(BYTE_SIZE_32)) {
341         bool result = nativeParcel_->WriteUint8(value);
342         if (!result) {
343             ZLOGE(LOG_LABEL, "write uint8 failed");
344             return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
345         }
346         return 0;
347     }
348     return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
349 }
350 
CJ_WriteString(std::u16string value)351 int32_t MessageSequenceImpl::CJ_WriteString(std::u16string value)
352 {
353     if (nativeParcel_ == nullptr) {
354         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
355     }
356     if (CheckWriteCapacity(BYTE_SIZE_32 * value.length())) {
357         bool result = nativeParcel_->WriteString16(value);
358         if (!result) {
359             ZLOGE(LOG_LABEL, "write string16 failed");
360             return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
361         }
362         return 0;
363     }
364     return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
365 }
366 
CJ_WriteByteArray(CJByteArray value)367 int32_t MessageSequenceImpl::CJ_WriteByteArray(CJByteArray value)
368 {
369     if (nativeParcel_ == nullptr) {
370         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
371     }
372     if (CheckWriteCapacity(BYTE_SIZE_8 * (value.len + 1))) {
373         size_t pos = nativeParcel_->GetWritePosition();
374         nativeParcel_->WriteUint32(value.len);
375         bool result = false;
376         for (size_t i = 0; i < value.len; i++) {
377             result = nativeParcel_->WriteInt8(value.data[i]);
378             if (!result) {
379                 nativeParcel_->RewindWrite(pos);
380                 ZLOGE(LOG_LABEL, "write int8 failed");
381                 return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
382             }
383         }
384         return 0;
385     }
386     return errorDesc::CHECK_PARAM_ERROR;
387 }
388 
CJ_WriteShortArray(CJShortArray value)389 int32_t MessageSequenceImpl::CJ_WriteShortArray(CJShortArray value)
390 {
391     if (nativeParcel_ == nullptr) {
392         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
393     }
394     if (CheckWriteCapacity(BYTE_SIZE_32 * (value.len + 1))) {
395         size_t pos = nativeParcel_->GetWritePosition();
396         nativeParcel_->WriteUint32(value.len);
397         bool result = false;
398         for (size_t i = 0; i < value.len; i++) {
399             result = nativeParcel_->WriteInt16(value.data[i]);
400             if (!result) {
401                 nativeParcel_->RewindWrite(pos);
402                 ZLOGE(LOG_LABEL, "write int16 failed");
403                 return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
404             }
405         }
406         return 0;
407     }
408     return errorDesc::CHECK_PARAM_ERROR;
409 }
410 
CJ_WriteIntArray(CJIntArray value)411 int32_t MessageSequenceImpl::CJ_WriteIntArray(CJIntArray value)
412 {
413     if (nativeParcel_ == nullptr) {
414         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
415     }
416     if (CheckWriteCapacity(BYTE_SIZE_32 * (value.len + 1))) {
417         size_t pos = nativeParcel_->GetWritePosition();
418         nativeParcel_->WriteUint32(value.len);
419         bool result = false;
420         for (size_t i = 0; i < value.len; i++) {
421             result = nativeParcel_->WriteInt32(value.data[i]);
422             if (!result) {
423                 nativeParcel_->RewindWrite(pos);
424                 ZLOGE(LOG_LABEL, "write int32 failed");
425                 return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
426             }
427         }
428         return 0;
429     }
430     return errorDesc::CHECK_PARAM_ERROR;
431 }
432 
CJ_WriteLongArray(CJLongArray value)433 int32_t MessageSequenceImpl::CJ_WriteLongArray(CJLongArray value)
434 {
435     if (nativeParcel_ == nullptr) {
436         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
437     }
438     if (CheckWriteCapacity(BYTE_SIZE_32 + BYTE_SIZE_64 * value.len)) {
439         size_t pos = nativeParcel_->GetWritePosition();
440         nativeParcel_->WriteUint32(value.len);
441         bool result = false;
442         for (size_t i = 0; i < value.len; i++) {
443             result = nativeParcel_->WriteInt64(value.data[i]);
444             if (!result) {
445                 nativeParcel_->RewindWrite(pos);
446                 ZLOGE(LOG_LABEL, "write int64 failed");
447                 return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
448             }
449         }
450         return 0;
451     }
452     return errorDesc::CHECK_PARAM_ERROR;
453 }
454 
CJ_WriteFloatArray(CJFloatArray value)455 int32_t MessageSequenceImpl::CJ_WriteFloatArray(CJFloatArray value)
456 {
457     if (nativeParcel_ == nullptr) {
458         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
459     }
460     if (CheckWriteCapacity(BYTE_SIZE_32 + sizeof(float) * value.len)) {
461         size_t pos = nativeParcel_->GetWritePosition();
462         nativeParcel_->WriteUint32(value.len);
463         bool result = false;
464         for (size_t i = 0; i < value.len; i++) {
465             result = nativeParcel_->WriteFloat(value.data[i]);
466             if (!result) {
467                 nativeParcel_->RewindWrite(pos);
468                 ZLOGE(LOG_LABEL, "write float failed");
469                 return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
470             }
471         }
472         return 0;
473     }
474     return errorDesc::CHECK_PARAM_ERROR;
475 }
476 
CJ_WriteDoubleArray(CJDoubleArray value)477 int32_t MessageSequenceImpl::CJ_WriteDoubleArray(CJDoubleArray value)
478 {
479     if (nativeParcel_ == nullptr) {
480         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
481     }
482     if (CheckWriteCapacity(BYTE_SIZE_32 + sizeof(double) * value.len)) {
483         size_t pos = nativeParcel_->GetWritePosition();
484         nativeParcel_->WriteUint32(value.len);
485         bool result = false;
486         for (size_t i = 0; i < value.len; i++) {
487             result = nativeParcel_->WriteDouble(value.data[i]);
488             if (!result) {
489                 nativeParcel_->RewindWrite(pos);
490                 ZLOGE(LOG_LABEL, "write double failed");
491                 return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
492             }
493         }
494         return 0;
495     }
496     return errorDesc::CHECK_PARAM_ERROR;
497 }
498 
CJ_WriteBooleanArray(CJByteArray value)499 int32_t MessageSequenceImpl::CJ_WriteBooleanArray(CJByteArray value)
500 {
501     if (nativeParcel_ == nullptr) {
502         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
503     }
504     if (CheckWriteCapacity(BYTE_SIZE_32 * (value.len + 1))) {
505         size_t pos = nativeParcel_->GetWritePosition();
506         nativeParcel_->WriteUint32(value.len);
507         bool result = false;
508         for (size_t i = 0; i < value.len; i++) {
509             result = nativeParcel_->WriteInt8(value.data[i]);
510             if (!result) {
511                 nativeParcel_->RewindWrite(pos);
512                 ZLOGE(LOG_LABEL, "write int8 failed");
513                 return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
514             }
515         }
516         return 0;
517     }
518     return errorDesc::CHECK_PARAM_ERROR;
519 }
520 
CJ_WriteCharArray(CJCharArray value)521 int32_t MessageSequenceImpl::CJ_WriteCharArray(CJCharArray value)
522 {
523     if (nativeParcel_ == nullptr) {
524         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
525     }
526     if (CheckWriteCapacity(BYTE_SIZE_32 * (value.len + 1))) {
527         size_t pos = nativeParcel_->GetWritePosition();
528         nativeParcel_->WriteUint32(value.len);
529         bool result = false;
530         for (size_t i = 0; i < value.len; i++) {
531             result = nativeParcel_->WriteUint8(value.data[i]);
532             if (!result) {
533                 nativeParcel_->RewindWrite(pos);
534                 ZLOGE(LOG_LABEL, "write uint8 failed");
535                 return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
536             }
537         }
538         return 0;
539     }
540     return errorDesc::CHECK_PARAM_ERROR;
541 }
542 
CJ_WriteStringArray(std::u16string value[],uint32_t arrayLength)543 int32_t MessageSequenceImpl::CJ_WriteStringArray(std::u16string value[], uint32_t arrayLength)
544 {
545     if (nativeParcel_ == nullptr) {
546         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
547     }
548     size_t pos = nativeParcel_->GetWritePosition();
549     nativeParcel_->WriteUint32(arrayLength);
550     bool result = false;
551     for (size_t i = 0; i < arrayLength; i++) {
552         if (RewindIfWriteCheckFail(BYTE_SIZE_32 * value[i].length(), pos)) {
553             result = nativeParcel_->WriteString16(value[i]);
554             if (!result) {
555                 nativeParcel_->RewindWrite(pos);
556                 ZLOGE(LOG_LABEL, "write string16 failed");
557                 return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
558             }
559         } else {
560             ZLOGE(LOG_LABEL, "No enough capacity to write");
561             return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
562         }
563     }
564     return 0;
565 }
566 
CJ_WriteArrayBuffer(int32_t typeCode,void * value,size_t byteLength)567 int32_t MessageSequenceImpl::CJ_WriteArrayBuffer(int32_t typeCode, void* value, size_t byteLength)
568 {
569     if (nativeParcel_ == nullptr || value == nullptr) {
570         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
571     }
572     if (CheckWriteCapacity(byteLength)) {
573         bool writeSuccess = CJ_WriteVectorByTypeCode(typeCode, value, byteLength);
574         if (!writeSuccess) {
575             ZLOGE(LOG_LABEL, "write buffer failed");
576             return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
577         }
578         return 0;
579     }
580     return errorDesc::CHECK_PARAM_ERROR;
581 }
582 
CJ_WriteRawDataBuffer(uint8_t * data,int64_t size)583 int32_t MessageSequenceImpl::CJ_WriteRawDataBuffer(uint8_t* data, int64_t size)
584 {
585     if (nativeParcel_ == nullptr) {
586         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
587     }
588     if (data == nullptr) {
589         return errorDesc::CHECK_PARAM_ERROR;
590     }
591     if (!nativeParcel_->WriteRawData(data, size)) {
592         ZLOGE(LOG_LABEL, "write raw data failed");
593         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
594     }
595     return 0;
596 }
597 
CJ_WriteUint32(uint32_t value)598 bool MessageSequenceImpl::CJ_WriteUint32(uint32_t value)
599 {
600     if (nativeParcel_ == nullptr) {
601         return false;
602     }
603     return nativeParcel_->WriteUint32(value);
604 }
605 
CJ_WriteRemoteObject(int64_t object)606 int32_t MessageSequenceImpl::CJ_WriteRemoteObject(int64_t object)
607 {
608     if (nativeParcel_ == nullptr) {
609         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
610     }
611     sptr<IRemoteObject> remoteObject = CJ_rpc_getNativeRemoteObject(object);
612     if (remoteObject == nullptr) {
613         ZLOGE(LOG_LABEL, "remote object is nullptr");
614         return errorDesc::PROXY_OR_REMOTE_OBJECT_INVALID_ERROR;
615     }
616     bool writeResult = nativeParcel_->WriteRemoteObject(remoteObject);
617     if (!writeResult) {
618         ZLOGE(LOG_LABEL, "write remote object failed");
619         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
620     }
621     return 0;
622 }
623 
CJ_WriteRemoteObjectArray(CJLongArray value)624 int32_t MessageSequenceImpl::CJ_WriteRemoteObjectArray(CJLongArray value)
625 {
626     if (nativeParcel_ == nullptr) {
627         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
628     }
629     uint32_t arrayLength = static_cast<uint32_t>(value.len);
630     size_t pos = nativeParcel_->GetWritePosition();
631     bool result = nativeParcel_->WriteInt32(arrayLength);
632     for (size_t i = 0; i < arrayLength; i++) {
633         sptr<IRemoteObject> remoteObject = CJ_rpc_getNativeRemoteObject(value.data[i]);
634         if (remoteObject == nullptr) {
635             ZLOGE(LOG_LABEL, "remote object is nullptr");
636             return errorDesc::PROXY_OR_REMOTE_OBJECT_INVALID_ERROR;
637         }
638         result = nativeParcel_->WriteRemoteObject(remoteObject);
639         if (!result) {
640             nativeParcel_->RewindWrite(pos);
641             ZLOGE(LOG_LABEL, "write remote object failed");
642             return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
643         }
644     }
645     return 0;
646 }
647 
648 template<typename T>
BufferToVector(void * data,size_t byteLength)649 static std::vector<T> BufferToVector(void* data, size_t byteLength)
650 {
651     const T* dataPtr = reinterpret_cast<const T*>(data);
652     std::vector<T> vec;
653     std::copy(dataPtr, dataPtr + byteLength / sizeof(T), std::back_inserter(vec));
654     return vec;
655 }
656 
CJ_WriteVectorByTypeCode(int32_t typeCode,void * data,size_t byteLength)657 bool MessageSequenceImpl::CJ_WriteVectorByTypeCode(int32_t typeCode, void* data, size_t byteLength)
658 {
659     switch (typeCode) {
660         case INT8_ARRAY: {
661             return nativeParcel_->WriteInt8Vector(BufferToVector<int8_t>(data, byteLength));
662         }
663         case UINT8_ARRAY: {
664             return nativeParcel_->WriteUInt8Vector(BufferToVector<uint8_t>(data, byteLength));
665         }
666         case INT16_ARRAY: {
667             return nativeParcel_->WriteInt16Vector(BufferToVector<int16_t>(data, byteLength));
668         }
669         case UINT16_ARRAY: {
670             return nativeParcel_->WriteUInt16Vector(BufferToVector<uint16_t>(data, byteLength));
671         }
672         case INT32_ARRAY: {
673             return nativeParcel_->WriteInt32Vector(BufferToVector<int32_t>(data, byteLength));
674         }
675         case UINT32_ARRAY: {
676             return nativeParcel_->WriteUInt32Vector(BufferToVector<uint32_t>(data, byteLength));
677         }
678         case FLOAT32_ARRAY: {
679             return nativeParcel_->WriteFloatVector(BufferToVector<float>(data, byteLength));
680         }
681         case FLOAT64_ARRAY: {
682             return nativeParcel_->WriteDoubleVector(BufferToVector<double>(data, byteLength));
683         }
684         case BIGINT64_ARRAY: {
685             return nativeParcel_->WriteInt64Vector(BufferToVector<int64_t>(data, byteLength));
686         }
687         case BIGUINT64_ARRAY: {
688             return nativeParcel_->WriteUInt64Vector(BufferToVector<uint64_t>(data, byteLength));
689         }
690         default:
691             ZLOGE(LOG_LABEL, "unsupported typeCode:%{public}d", typeCode);
692             return false;
693     }
694 }
695 
CJ_ReadByte(int32_t * errCode)696 int8_t MessageSequenceImpl::CJ_ReadByte(int32_t* errCode)
697 {
698     if (nativeParcel_ == nullptr) {
699         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
700         return 0;
701     }
702     return nativeParcel_->ReadInt8();
703 }
704 
CJ_ReadShort(int32_t * errCode)705 int16_t MessageSequenceImpl::CJ_ReadShort(int32_t* errCode)
706 {
707     if (nativeParcel_ == nullptr) {
708         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
709         return 0;
710     }
711     return nativeParcel_->ReadInt16();
712 }
713 
CJ_ReadInt(int32_t * errCode)714 int32_t MessageSequenceImpl::CJ_ReadInt(int32_t* errCode)
715 {
716     if (nativeParcel_ == nullptr) {
717         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
718         return 0;
719     }
720     return nativeParcel_->ReadInt32();
721 }
722 
CJ_ReadLong(int32_t * errCode)723 int64_t MessageSequenceImpl::CJ_ReadLong(int32_t* errCode)
724 {
725     if (nativeParcel_ == nullptr) {
726         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
727         return 0;
728     }
729     return nativeParcel_->ReadInt64();
730 }
731 
CJ_ReadFloat(int32_t * errCode)732 float MessageSequenceImpl::CJ_ReadFloat(int32_t* errCode)
733 {
734     if (nativeParcel_ == nullptr) {
735         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
736         return 0;
737     }
738     return nativeParcel_->ReadFloat();
739 }
740 
CJ_ReadDouble(int32_t * errCode)741 double MessageSequenceImpl::CJ_ReadDouble(int32_t* errCode)
742 {
743     if (nativeParcel_ == nullptr) {
744         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
745         return 0;
746     }
747     return nativeParcel_->ReadDouble();
748 }
749 
CJ_ReadBoolean(int32_t * errCode)750 int8_t MessageSequenceImpl::CJ_ReadBoolean(int32_t* errCode)
751 {
752     if (nativeParcel_ == nullptr) {
753         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
754         return 0;
755     }
756     return nativeParcel_->ReadInt8();
757 }
758 
CJ_ReadChar(int32_t * errCode)759 uint8_t MessageSequenceImpl::CJ_ReadChar(int32_t* errCode)
760 {
761     if (nativeParcel_ == nullptr) {
762         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
763         return 0;
764     }
765     return nativeParcel_->ReadUint8();
766 }
767 
CJ_ReadString(int32_t * errCode)768 std::u16string MessageSequenceImpl::CJ_ReadString(int32_t* errCode)
769 {
770     if (nativeParcel_ == nullptr) {
771         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
772         return u"";
773     }
774     return nativeParcel_->ReadString16();
775 }
776 
CheckReadPosition()777 bool MessageSequenceImpl::CheckReadPosition()
778 {
779     if (nativeParcel_->GetDataSize() < nativeParcel_->GetReadPosition()) {
780         ZLOGE(LOG_LABEL, "invalid write position, maxCapacityToWrite_:%{public}zu, GetWritePosition:%{public}zu",
781             maxCapacityToWrite_, nativeParcel_->GetWritePosition());
782         return false;
783     }
784     return true;
785 }
786 
CheckReadLength(size_t arrayLength,size_t typeSize)787 bool MessageSequenceImpl::CheckReadLength(size_t arrayLength, size_t typeSize)
788 {
789     if (CheckReadPosition()) {
790         size_t remainSize = nativeParcel_->GetDataSize() - nativeParcel_->GetReadPosition();
791         if ((arrayLength > remainSize) || ((arrayLength) * (typeSize) > remainSize)) {
792             ZLOGE(LOG_LABEL,
793                 "No enough data to read, arrayLength:%{public}zu, remainSize:%{public}zu,"
794                 "typeSize:%{public}zu, GetDataSize:%{public}zu, GetReadPosition:%{public}zu",
795                 arrayLength, remainSize, typeSize, nativeParcel_->GetDataSize(), nativeParcel_->GetReadPosition());
796             return false;
797         }
798         return true;
799     }
800     return false;
801 }
802 
CJ_ReadByteArray(int32_t * errCode)803 CJByteArray MessageSequenceImpl::CJ_ReadByteArray(int32_t* errCode)
804 {
805     CJByteArray arr = CJByteArray { 0 };
806     if (nativeParcel_ == nullptr) {
807         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
808         return arr;
809     }
810     arr.len = nativeParcel_->ReadUint32();
811     if (arr.len == 0) {
812         return arr;
813     }
814     if (CheckReadLength(static_cast<size_t>(arr.len), BYTE_SIZE_8)) {
815         arr.data = static_cast<int8_t*>(malloc(sizeof(int8_t) * arr.len));
816         if (arr.data == nullptr) {
817             *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
818             return arr;
819         }
820         for (uint32_t i = 0; i < arr.len; i++) {
821             arr.data[i] = nativeParcel_->ReadInt8();
822         }
823         return arr;
824     }
825     *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
826     return arr;
827 }
828 
CJ_ReadShortArray(int32_t * errCode)829 CJShortArray MessageSequenceImpl::CJ_ReadShortArray(int32_t* errCode)
830 {
831     CJShortArray arr = CJShortArray { 0 };
832     if (nativeParcel_ == nullptr) {
833         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
834         return arr;
835     }
836     arr.len = nativeParcel_->ReadUint32();
837     if (arr.len == 0) {
838         return arr;
839     }
840     if (CheckReadLength(static_cast<size_t>(arr.len), BYTE_SIZE_32)) {
841         arr.data = static_cast<int16_t*>(malloc(sizeof(int16_t) * arr.len));
842         if (arr.data == nullptr) {
843             *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
844             return arr;
845         }
846         for (uint32_t i = 0; i < arr.len; i++) {
847             arr.data[i] = nativeParcel_->ReadInt16();
848         }
849         return arr;
850     }
851     *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
852     return arr;
853 }
854 
CJ_ReadIntArray(int32_t * errCode)855 CJIntArray MessageSequenceImpl::CJ_ReadIntArray(int32_t* errCode)
856 {
857     CJIntArray arr = CJIntArray { 0 };
858     if (nativeParcel_ == nullptr) {
859         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
860         return arr;
861     }
862     arr.len = nativeParcel_->ReadUint32();
863     if (arr.len == 0) {
864         return arr;
865     }
866     if (CheckReadLength(static_cast<size_t>(arr.len), BYTE_SIZE_32)) {
867         arr.data = static_cast<int32_t*>(malloc(sizeof(int32_t) * arr.len));
868         if (arr.data == nullptr) {
869             *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
870             return arr;
871         }
872         for (uint32_t i = 0; i < arr.len; i++) {
873             arr.data[i] = nativeParcel_->ReadInt32();
874         }
875         return arr;
876     }
877     *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
878     return arr;
879 }
880 
CJ_ReadLongArray(int32_t * errCode)881 CJLongArray MessageSequenceImpl::CJ_ReadLongArray(int32_t* errCode)
882 {
883     CJLongArray arr = CJLongArray { 0 };
884     if (nativeParcel_ == nullptr) {
885         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
886         return arr;
887     }
888     arr.len = nativeParcel_->ReadUint32();
889     if (arr.len == 0) {
890         return arr;
891     }
892     if (CheckReadLength(static_cast<size_t>(arr.len), BYTE_SIZE_64)) {
893         arr.data = static_cast<int64_t*>(malloc(sizeof(int64_t) * arr.len));
894         if (arr.data == nullptr) {
895             *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
896             return arr;
897         }
898         for (uint32_t i = 0; i < arr.len; i++) {
899             arr.data[i] = nativeParcel_->ReadInt64();
900         }
901         return arr;
902     }
903     *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
904     return arr;
905 }
906 
CJ_ReadFloatArray(int32_t * errCode)907 CJFloatArray MessageSequenceImpl::CJ_ReadFloatArray(int32_t* errCode)
908 {
909     CJFloatArray arr = CJFloatArray { 0 };
910     if (nativeParcel_ == nullptr) {
911         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
912         return arr;
913     }
914     arr.len = nativeParcel_->ReadUint32();
915     if (arr.len == 0) {
916         return arr;
917     }
918     if (CheckReadLength(static_cast<size_t>(arr.len), sizeof(float))) {
919         arr.data = static_cast<float*>(malloc(sizeof(float) * arr.len));
920         if (arr.data == nullptr) {
921             *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
922             return arr;
923         }
924         for (uint32_t i = 0; i < arr.len; i++) {
925             arr.data[i] = nativeParcel_->ReadFloat();
926         }
927         return arr;
928     }
929     *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
930     return arr;
931 }
932 
CJ_ReadDoubleArray(int32_t * errCode)933 CJDoubleArray MessageSequenceImpl::CJ_ReadDoubleArray(int32_t* errCode)
934 {
935     CJDoubleArray arr = CJDoubleArray { 0 };
936     if (nativeParcel_ == nullptr) {
937         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
938         return arr;
939     }
940     arr.len = nativeParcel_->ReadUint32();
941     if (arr.len == 0) {
942         return arr;
943     }
944     if (CheckReadLength(static_cast<size_t>(arr.len), sizeof(double))) {
945         arr.data = static_cast<double*>(malloc(sizeof(double) * arr.len));
946         if (arr.data == nullptr) {
947             *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
948             return arr;
949         }
950         for (uint32_t i = 0; i < arr.len; i++) {
951             arr.data[i] = nativeParcel_->ReadDouble();
952         }
953         return arr;
954     }
955     *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
956     return arr;
957 }
958 
CJ_ReadBooleanArray(int32_t * errCode)959 CJByteArray MessageSequenceImpl::CJ_ReadBooleanArray(int32_t* errCode)
960 {
961     CJByteArray arr = CJByteArray { 0 };
962     if (nativeParcel_ == nullptr) {
963         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
964         return arr;
965     }
966     arr.len = nativeParcel_->ReadUint32();
967     if (arr.len == 0) {
968         return arr;
969     }
970     if (CheckReadLength(static_cast<size_t>(arr.len), BYTE_SIZE_32)) {
971         arr.data = static_cast<int8_t*>(malloc(sizeof(int8_t) * arr.len));
972         if (arr.data == nullptr) {
973             *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
974             return arr;
975         }
976         for (uint32_t i = 0; i < arr.len; i++) {
977             arr.data[i] = nativeParcel_->ReadInt8();
978         }
979         return arr;
980     }
981     *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
982     return arr;
983 }
984 
CJ_ReadCharArray(int32_t * errCode)985 CJCharArray MessageSequenceImpl::CJ_ReadCharArray(int32_t* errCode)
986 {
987     CJCharArray arr = CJCharArray { 0 };
988     if (nativeParcel_ == nullptr) {
989         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
990         return arr;
991     }
992     arr.len = nativeParcel_->ReadUint32();
993     if (arr.len == 0) {
994         return arr;
995     }
996     if (CheckReadLength(static_cast<size_t>(arr.len), BYTE_SIZE_32)) {
997         arr.data = static_cast<uint8_t*>(malloc(sizeof(uint8_t) * arr.len));
998         if (arr.data == nullptr) {
999             *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1000             return arr;
1001         }
1002         for (uint32_t i = 0; i < arr.len; i++) {
1003             arr.data[i] = nativeParcel_->ReadUint8();
1004         }
1005         return arr;
1006     }
1007     *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1008     return arr;
1009 }
1010 
CJ_ReadStringArray(int32_t * errCode)1011 CJStringArray MessageSequenceImpl::CJ_ReadStringArray(int32_t* errCode)
1012 {
1013     CJStringArray arr = CJStringArray { 0 };
1014     if (nativeParcel_ == nullptr) {
1015         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1016         return arr;
1017     }
1018     arr.len = nativeParcel_->ReadUint32();
1019     if (arr.len == 0) {
1020         return arr;
1021     }
1022     if (CheckReadLength(static_cast<size_t>(arr.len), BYTE_SIZE_32)) {
1023         arr.data = static_cast<char**>(malloc(sizeof(char*) * arr.len));
1024         if (arr.data == nullptr) {
1025             *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1026             return arr;
1027         }
1028         std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
1029         for (uint32_t i = 0; i < arr.len; i++) {
1030             if (nativeParcel_->GetReadableBytes() <= 0) {
1031                 break;
1032             }
1033             std::u16string parcelString = nativeParcel_->ReadString16();
1034             std::string str = converter.to_bytes(parcelString);
1035             arr.data[i] = MallocCString(str);
1036         }
1037         return arr;
1038     }
1039     *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1040     return arr;
1041 }
1042 
CJ_ReadInt8ArrayBuffer(int32_t * errCode)1043 std::vector<int8_t> MessageSequenceImpl::CJ_ReadInt8ArrayBuffer(int32_t* errCode)
1044 {
1045     std::vector<int8_t> int8Vector;
1046     if (!nativeParcel_ || !nativeParcel_->ReadInt8Vector(&int8Vector)) {
1047         ZLOGE(LOG_LABEL, "read Int8Vector failed");
1048         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1049     }
1050     return int8Vector;
1051 }
1052 
CJ_ReadUInt8ArrayBuffer(int32_t * errCode)1053 std::vector<uint8_t> MessageSequenceImpl::CJ_ReadUInt8ArrayBuffer(int32_t* errCode)
1054 {
1055     std::vector<uint8_t> uint8Vector;
1056     if (!nativeParcel_ || !nativeParcel_->ReadUInt8Vector(&uint8Vector)) {
1057         ZLOGE(LOG_LABEL, "read UInt8Vector failed");
1058         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1059     }
1060     return uint8Vector;
1061 }
1062 
CJ_ReadInt16ArrayBuffer(int32_t * errCode)1063 std::vector<int16_t> MessageSequenceImpl::CJ_ReadInt16ArrayBuffer(int32_t* errCode)
1064 {
1065     std::vector<int16_t> int16Vector;
1066     if (!nativeParcel_ || !nativeParcel_->ReadInt16Vector(&int16Vector)) {
1067         ZLOGE(LOG_LABEL, "read Int16Vector failed");
1068         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1069     }
1070     return int16Vector;
1071 }
1072 
CJ_ReadUInt16ArrayBuffer(int32_t * errCode)1073 std::vector<uint16_t> MessageSequenceImpl::CJ_ReadUInt16ArrayBuffer(int32_t* errCode)
1074 {
1075     std::vector<uint16_t> uint16Vector;
1076     if (!nativeParcel_ || !nativeParcel_->ReadUInt16Vector(&uint16Vector)) {
1077         ZLOGE(LOG_LABEL, "read UInt16Vector failed");
1078         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1079     }
1080     return uint16Vector;
1081 }
1082 
CJ_ReadInt32ArrayBuffer(int32_t * errCode)1083 std::vector<int32_t> MessageSequenceImpl::CJ_ReadInt32ArrayBuffer(int32_t* errCode)
1084 {
1085     std::vector<int32_t> int32Vector;
1086     if (!nativeParcel_ || !nativeParcel_->ReadInt32Vector(&int32Vector)) {
1087         ZLOGE(LOG_LABEL, "read Int32Vector failed");
1088         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1089     }
1090     return int32Vector;
1091 }
1092 
CJ_ReadUInt32ArrayBuffer(int32_t * errCode)1093 std::vector<uint32_t> MessageSequenceImpl::CJ_ReadUInt32ArrayBuffer(int32_t* errCode)
1094 {
1095     std::vector<uint32_t> uint32Vector;
1096     if (!nativeParcel_ || !nativeParcel_->ReadUInt32Vector(&uint32Vector)) {
1097         ZLOGE(LOG_LABEL, "read UInt32Vector failed");
1098         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1099     }
1100     return uint32Vector;
1101 }
1102 
CJ_ReadFloatArrayBuffer(int32_t * errCode)1103 std::vector<float> MessageSequenceImpl::CJ_ReadFloatArrayBuffer(int32_t* errCode)
1104 {
1105     std::vector<float> floatVector;
1106     if (!nativeParcel_ || !nativeParcel_->ReadFloatVector(&floatVector)) {
1107         ZLOGE(LOG_LABEL, "read FloatVector failed");
1108         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1109     }
1110     return floatVector;
1111 }
1112 
CJ_ReadDoubleArrayBuffer(int32_t * errCode)1113 std::vector<double> MessageSequenceImpl::CJ_ReadDoubleArrayBuffer(int32_t* errCode)
1114 {
1115     std::vector<double> doubleVector;
1116     if (!nativeParcel_ || !nativeParcel_->ReadDoubleVector(&doubleVector)) {
1117         ZLOGE(LOG_LABEL, "read DoubleVector failed");
1118         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1119     }
1120     return doubleVector;
1121 }
1122 
CJ_ReadInt64ArrayBuffer(int32_t * errCode)1123 std::vector<int64_t> MessageSequenceImpl::CJ_ReadInt64ArrayBuffer(int32_t* errCode)
1124 {
1125     std::vector<int64_t> int64Vector;
1126     if (!nativeParcel_ || !nativeParcel_->ReadInt64Vector(&int64Vector)) {
1127         ZLOGE(LOG_LABEL, "read Int64Vector failed");
1128         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1129     }
1130     return int64Vector;
1131 }
1132 
CJ_ReadUInt64ArrayBuffer(int32_t * errCode)1133 std::vector<uint64_t> MessageSequenceImpl::CJ_ReadUInt64ArrayBuffer(int32_t* errCode)
1134 {
1135     std::vector<uint64_t> uint64vector;
1136     if (!nativeParcel_ || !nativeParcel_->ReadUInt64Vector(&uint64vector)) {
1137         ZLOGE(LOG_LABEL, "read UInt64Vector failed");
1138         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1139     }
1140     return uint64vector;
1141 }
1142 
CJ_ReadRawDataBuffer(int64_t size,int32_t * errCode)1143 uint8_t* MessageSequenceImpl::CJ_ReadRawDataBuffer(int64_t size, int32_t* errCode)
1144 {
1145     if (size <= 0) {
1146         *errCode = errorDesc::CHECK_PARAM_ERROR;
1147         return nullptr;
1148     }
1149     if (nativeParcel_ == nullptr) {
1150         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1151         return nullptr;
1152     }
1153     const void* rawData = nativeParcel_->ReadRawData(size);
1154     if (rawData == nullptr) {
1155         ZLOGE(LOG_LABEL, "rawData is null");
1156         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1157         return nullptr;
1158     }
1159     uint8_t* data = static_cast<uint8_t*>(malloc(size));
1160     if (data == nullptr) {
1161         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1162         return nullptr;
1163     }
1164     errno_t status = memcpy_s(data, size, rawData, size);
1165     if (status != EOK) {
1166         free(data);
1167         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1168         return nullptr;
1169     }
1170     return data;
1171 }
1172 
CJ_ReadRemoteObject(int32_t * errCode)1173 RetDataI64 MessageSequenceImpl::CJ_ReadRemoteObject(int32_t* errCode)
1174 {
1175     if (nativeParcel_ == nullptr) {
1176         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1177         return RetDataI64 { INVALID_REMOTE_TYPE, INVALID_ID };
1178     }
1179     sptr<IRemoteObject> value = nativeParcel_->ReadRemoteObject();
1180     if (value == nullptr) {
1181         *errCode = errorDesc::PROXY_OR_REMOTE_OBJECT_INVALID_ERROR;
1182         return RetDataI64 { INVALID_REMOTE_TYPE, INVALID_ID };
1183     }
1184     int32_t type = value->IsProxyObject() ? REMOTE_PROXY : REMOTE_OBJECT;
1185     return RetDataI64{type, CJ_rpc_CreateRemoteObject(value)};
1186 }
1187 
CJ_ReadRemoteObjectArray(int32_t * errCode)1188 RemoteObjectArray MessageSequenceImpl::CJ_ReadRemoteObjectArray(int32_t* errCode)
1189 {
1190     RemoteObjectArray res = RemoteObjectArray { nullptr, nullptr, 0 };
1191     if (nativeParcel_ == nullptr) {
1192         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1193         return res;
1194     }
1195     int32_t arrayLength = nativeParcel_->ReadInt32();
1196     if (arrayLength <= 0) {
1197         return res;
1198     }
1199     if (CheckReadLength(static_cast<size_t>(arrayLength), BYTE_SIZE_32)) {
1200         int32_t* type = static_cast<int32_t*>(malloc(sizeof(int32_t) * arrayLength));
1201         if (type == nullptr) {
1202             *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1203             return res;
1204         }
1205         int64_t* id = static_cast<int64_t*>(malloc(sizeof(int64_t) * arrayLength));
1206         if (id == nullptr) {
1207             *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1208             free(type);
1209             return res;
1210         }
1211         for (uint32_t i = 0; i < static_cast<uint32_t>(arrayLength); i++) {
1212             sptr<IRemoteObject> value = nativeParcel_->ReadRemoteObject();
1213             if (value == nullptr) {
1214                 type[i] = INVALID_REMOTE_TYPE;
1215                 id[i] = INVALID_ID;
1216                 continue;
1217             }
1218             type[i] = value->IsProxyObject() ? REMOTE_PROXY : REMOTE_OBJECT;
1219             id[i] = CJ_rpc_CreateRemoteObject(value);
1220         }
1221         res.len = static_cast<uint32_t>(arrayLength);
1222         res.type = type;
1223         res.id = id;
1224         return res;
1225     }
1226     *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1227     return res;
1228 }
1229 
CJ_CloseFileDescriptor(int32_t fd)1230 void MessageSequenceImpl::CJ_CloseFileDescriptor(int32_t fd)
1231 {
1232     close(fd);
1233 }
1234 
CJ_DupFileDescriptor(int32_t fd)1235 int32_t MessageSequenceImpl::CJ_DupFileDescriptor(int32_t fd)
1236 {
1237     int32_t dupResult = dup(fd);
1238     if (dupResult < 0) {
1239         ZLOGE(LOG_LABEL, "os dup function failed");
1240     }
1241     return dupResult;
1242 }
1243 
CJ_ContainFileDescriptors(int32_t * errCode)1244 bool MessageSequenceImpl::CJ_ContainFileDescriptors(int32_t* errCode)
1245 {
1246     if (nativeParcel_ == nullptr) {
1247         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1248         return false;
1249     }
1250     return nativeParcel_->ContainFileDescriptors();
1251 }
1252 
CJ_WriteFileDescriptor(int32_t fd)1253 int32_t MessageSequenceImpl::CJ_WriteFileDescriptor(int32_t fd)
1254 {
1255     if (nativeParcel_ == nullptr) {
1256         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
1257     }
1258     bool result = nativeParcel_->WriteFileDescriptor(fd);
1259     if (!result) {
1260         ZLOGE(LOG_LABEL, "write file descriptor failed");
1261         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
1262     }
1263     return 0;
1264 }
1265 
CJ_ReadFileDescriptor(int32_t * errCode)1266 int32_t MessageSequenceImpl::CJ_ReadFileDescriptor(int32_t* errCode)
1267 {
1268     if (nativeParcel_ == nullptr) {
1269         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1270         return 0;
1271     }
1272     int32_t result = nativeParcel_->ReadFileDescriptor();
1273     if (result == -1) {
1274         ZLOGE(LOG_LABEL, "read file descriptor failed");
1275         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1276         return 0;
1277     }
1278     return result;
1279 }
1280 
CJ_WriteAshmem(sptr<Ashmem> nativeAshmem)1281 int32_t MessageSequenceImpl::CJ_WriteAshmem(sptr<Ashmem> nativeAshmem)
1282 {
1283     if (nativeParcel_ == nullptr) {
1284         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
1285     }
1286     bool result = nativeParcel_->WriteAshmem(nativeAshmem);
1287     if (!result) {
1288         ZLOGE(LOG_LABEL, "write ashmem failed");
1289         return errorDesc::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR;
1290     }
1291     return 0;
1292 }
1293 
CJ_ReadAshmem(int32_t * errCode)1294 sptr<Ashmem> MessageSequenceImpl::CJ_ReadAshmem(int32_t* errCode)
1295 {
1296     if (nativeParcel_ == nullptr) {
1297         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1298         return nullptr;
1299     }
1300     sptr<Ashmem> nativeAshmem = nativeParcel_->ReadAshmem();
1301     if (nativeAshmem == nullptr) {
1302         ZLOGE(LOG_LABEL, "nativeAshmem is null");
1303         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1304     }
1305     return nativeAshmem;
1306 }
1307 
CJ_GetRawDataCapacity(int32_t * errCode)1308 uint32_t MessageSequenceImpl::CJ_GetRawDataCapacity(int32_t* errCode)
1309 {
1310     if (nativeParcel_ == nullptr) {
1311         *errCode = errorDesc::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR;
1312         return 0;
1313     }
1314     return nativeParcel_->GetRawDataCapacity();
1315 }
1316 } // namespace OHOS
1317