c_parcel.cpp 30.8 KB
Newer Older
F
fanxiaoyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "c_parcel_internal.h"

F
fanxiaoyu 已提交
18
#include <securec.h>
F
fanxiaoyu 已提交
19 20
#include <string_ex.h>
#include "c_remote_object_internal.h"
C
chenchong_666 已提交
21 22
#include "log_tags.h"
#include "ipc_debug.h"
F
fanxiaoyu 已提交
23 24

using namespace OHOS;
C
chenchong_666 已提交
25
static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "CParcel" };
F
fanxiaoyu 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

MessageParcelHolder::MessageParcelHolder(OHOS::MessageParcel *parcel)
    : isExternal_(true)
{
    if (parcel == nullptr) {
        parcel_ = new (std::nothrow) MessageParcel();
        isExternal_ = false;
    } else {
        parcel_ = parcel;
    }
}

MessageParcelHolder::~MessageParcelHolder(void)
{
    if (!isExternal_ && parcel_ != nullptr) {
        delete parcel_;
    }
}

static bool IsValidParcel(const CParcel *parcel, const char *promot)
{
    if (parcel == nullptr) {
C
chenchong_666 已提交
48
        ZLOGE(LOG_LABEL, "%{public}s: parcel is null\n", promot);
F
fanxiaoyu 已提交
49 50 51
        return false;
    }
    if (parcel->parcel_ == nullptr) {
C
chenchong_666 已提交
52
        ZLOGE(LOG_LABEL, "%{public}s: wrapper parcel is null\n", promot);
F
fanxiaoyu 已提交
53 54 55 56 57
        return false;
    }
    return true;
}

T
tanyanying 已提交
58 59 60 61 62 63
static bool WriteAndCheckArrayLength(CParcel *parcel, bool isNull, int32_t len)
{
    if (len < -1) {
        return false;
    }
    if (!isNull && len < 0) {
C
chenchong_666 已提交
64
        ZLOGE(LOG_LABEL, "%{public}s: not null array has invalid length: %d\n", __func__, len);
T
tanyanying 已提交
65 66 67
        return false;
    }
    if (isNull && len > 0) {
C
chenchong_666 已提交
68
        ZLOGE(LOG_LABEL, "%{public}s: null array has invalid length: %d\n", __func__, len);
T
tanyanying 已提交
69 70 71 72 73 74 75 76
        return false;
    }
    return parcel->parcel_->WriteInt32(len);
}

static bool ReadAndCheckArrayLength(const CParcel *parcel, int32_t &len)
{
    if (!parcel->parcel_->ReadInt32(len)) {
C
chenchong_666 已提交
77
        ZLOGE(LOG_LABEL, "%{public}s: read array length from native parcel failed\n", __func__);
T
tanyanying 已提交
78 79 80
        return false;
    }
    if (len < -1) {
C
chenchong_666 已提交
81
        ZLOGE(LOG_LABEL, "%{public}s: length is invalid: %d\n", __func__, len);
T
tanyanying 已提交
82 83 84 85 86 87
        return false;
    }
    if (len <= 0) { // null array
        return true;
    }
    if (static_cast<uint32_t>(len) > parcel->parcel_->GetReadableBytes()) {
C
chenchong_666 已提交
88
        ZLOGE(LOG_LABEL, "%{public}s: readable bytes are too short in parcel: %d\n", __func__, len);
T
tanyanying 已提交
89
        return false;
90
    }
T
tanyanying 已提交
91 92 93
    return true;
}

F
fanxiaoyu 已提交
94 95 96 97
CParcel *CParcelObtain(void)
{
    CParcel *holder = new (std::nothrow) MessageParcelHolder();
    if (holder == nullptr) {
C
chenchong_666 已提交
98
        ZLOGE(LOG_LABEL, "%{public}s: malloc messsage parcel holder failed\n", __func__);
F
fanxiaoyu 已提交
99 100 101 102 103 104 105 106 107
        return nullptr;
    }
    holder->IncStrongRef(nullptr);
    return holder;
}

void CParcelIncStrongRef(CParcel *parcel)
{
    if (parcel == nullptr) {
C
chenchong_666 已提交
108
        ZLOGE(LOG_LABEL, "%{public}s: parcel is nullptr\n", __func__);
F
fanxiaoyu 已提交
109 110 111 112 113 114 115 116
        return;
    }
    parcel->IncStrongRef(nullptr);
}

void CParcelDecStrongRef(CParcel *parcel)
{
    if (parcel == nullptr) {
C
chenchong_666 已提交
117
        ZLOGE(LOG_LABEL, "%{public}s: parcel is nullptr\n", __func__);
F
fanxiaoyu 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        return;
    }
    parcel->DecStrongRef(nullptr);
}

bool CParcelWriteBool(CParcel *parcel, bool value)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->WriteBool(value);
}

bool CParcelReadBool(const CParcel *parcel, bool *value)
{
    if (!IsValidParcel(parcel, __func__) || value == nullptr) {
        return false;
    }
    return parcel->parcel_->ReadBool(*value);
}

bool CParcelWriteInt8(CParcel *parcel, int8_t value)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->WriteInt8(value);
}

bool CParcelReadInt8(const CParcel *parcel, int8_t *value)
{
    if (!IsValidParcel(parcel, __func__) || value == nullptr) {
        return false;
    }
    return parcel->parcel_->ReadInt8(*value);
}

bool CParcelWriteInt16(CParcel *parcel, int16_t value)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->WriteInt16(value);
}

bool CParcelReadInt16(const CParcel *parcel, int16_t *value)
{
    if (!IsValidParcel(parcel, __func__) || value == nullptr) {
        return false;
    }
    return parcel->parcel_->ReadInt16(*value);
}

bool CParcelWriteInt32(CParcel *parcel, int32_t value)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->WriteInt32(value);
}

bool CParcelReadInt32(const CParcel *parcel, int32_t *value)
{
    if (!IsValidParcel(parcel, __func__) || value == nullptr) {
        return false;
    }
    return parcel->parcel_->ReadInt32(*value);
}

bool CParcelWriteInt64(CParcel *parcel, int64_t value)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->WriteInt64(value);
}

bool CParcelReadInt64(const CParcel *parcel, int64_t *value)
{
    if (!IsValidParcel(parcel, __func__) || value == nullptr) {
        return false;
    }
    return parcel->parcel_->ReadInt64(*value);
}

bool CParcelWriteFloat(CParcel *parcel, float value)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->WriteFloat(value);
}

bool CParcelReadFloat(const CParcel *parcel, float *value)
{
    if (!IsValidParcel(parcel, __func__) || value == nullptr) {
        return false;
    }
    return parcel->parcel_->ReadFloat(*value);
}

bool CParcelWriteDouble(CParcel *parcel, double value)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->WriteDouble(value);
}

bool CParcelReadDouble(const CParcel *parcel, double *value)
{
    if (!IsValidParcel(parcel, __func__) || value == nullptr) {
        return false;
    }
    return parcel->parcel_->ReadDouble(*value);
}

bool CParcelWriteString(CParcel *parcel, const char *stringData, int32_t length)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    if (stringData == nullptr) {
        if (length != -1) {
C
chenchong_666 已提交
242
            ZLOGE(LOG_LABEL, "%{public}s: stringData is null, len: %d\n", __func__, length);
F
fanxiaoyu 已提交
243 244 245 246 247 248
            return false;
        }
        std::string value;
        return parcel->parcel_->WriteString(value);
    }
    if (length < 0) {
C
chenchong_666 已提交
249
        ZLOGE(LOG_LABEL, "%{public}s: stringData len is invalid: %d\n", __func__, length);
F
fanxiaoyu 已提交
250 251 252 253 254 255 256 257 258 259 260
        return false;
    }
    std::string value(stringData, length);
    return parcel->parcel_->WriteString(value);
}

bool CParcelReadString(const CParcel *parcel, void *stringData, OnCParcelBytesAllocator allocator)
{
    if (!IsValidParcel(parcel, __func__) || allocator == nullptr) {
        return false;
    }
F
fanxiaoyu 已提交
261 262
    std::string value;
    if (!parcel->parcel_->ReadString(value)) {
C
chenchong_666 已提交
263
        ZLOGE(LOG_LABEL, "%{public}s: read string from parcel failed\n", __func__);
F
fanxiaoyu 已提交
264 265
        return false;
    }
F
fanxiaoyu 已提交
266 267 268
    char *buffer = nullptr;
    bool isSuccess = allocator(stringData, &buffer, value.length());
    if (!isSuccess) {
C
chenchong_666 已提交
269
        ZLOGE(LOG_LABEL, "%{public}s: allocate string buffer is null\n", __func__);
F
fanxiaoyu 已提交
270 271
        return false;
    }
F
fanxiaoyu 已提交
272
    if (value.length() > 0 && memcpy_s(buffer, value.length(), value.data(), value.length()) != EOK) {
C
chenchong_666 已提交
273
        ZLOGE(LOG_LABEL, "%{public}s: memcpy string failed\n", __func__);
F
fanxiaoyu 已提交
274 275
        return false;
    }
F
fanxiaoyu 已提交
276 277 278 279 280 281 282 283 284 285
    return true;
}

bool CParcelWriteString16(CParcel *parcel, const char *str, int32_t strLen)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    if (str == nullptr) {
        if (strLen != -1) {
C
chenchong_666 已提交
286
            ZLOGE(LOG_LABEL, "%{public}s: str is null, len: %d\n", __func__, strLen);
F
fanxiaoyu 已提交
287 288 289 290 291 292
            return false;
        }
        std::u16string value;
        return parcel->parcel_->WriteString16(value);
    }
    if (strLen < 0) {
C
chenchong_666 已提交
293
        ZLOGE(LOG_LABEL, "%{public}s: str len is invalid: %d\n", __func__, strLen);
F
fanxiaoyu 已提交
294 295 296 297
        return false;
    }
    std::u16string u16string = Str8ToStr16(std::string(str, strLen));
    if (u16string.length() == 0 && strLen != 0) {
C
chenchong_666 已提交
298
        ZLOGE(LOG_LABEL, "%{public}s: convert u16string failed: %d\n", __func__, strLen);
F
fanxiaoyu 已提交
299 300 301 302 303 304 305 306 307 308
        return false;
    }
    return parcel->parcel_->WriteString16(u16string);
}

bool CParcelReadString16(const CParcel *parcel, void *stringData, OnCParcelBytesAllocator allocator)
{
    if (!IsValidParcel(parcel, __func__) || allocator == nullptr) {
        return false;
    }
F
fanxiaoyu 已提交
309 310
    std::u16string u16string;
    if (!parcel->parcel_->ReadString16(u16string)) {
C
chenchong_666 已提交
311
        ZLOGE(LOG_LABEL, "%{public}s: read u16string from parcel failed\n", __func__);
F
fanxiaoyu 已提交
312 313
        return false;
    }
F
fanxiaoyu 已提交
314
    std::string value = Str16ToStr8(u16string);
F
fanxiaoyu 已提交
315
    if (u16string.length() != 0 && value.length() == 0) {
C
chenchong_666 已提交
316
        ZLOGE(LOG_LABEL, "%{public}s: u16string len: %u, string len: %u\n", __func__,
T
tanyanying 已提交
317
            static_cast<uint32_t>(u16string.length()), static_cast<uint32_t>(value.length()));
F
fanxiaoyu 已提交
318 319 320 321 322
        return false;
    }
    char *buffer = nullptr;
    bool isSuccess = allocator(stringData, &buffer, value.length());
    if (!isSuccess) {
C
chenchong_666 已提交
323
        ZLOGE(LOG_LABEL, "%{public}s: allocate string buffer is null\n", __func__);
F
fanxiaoyu 已提交
324 325
        return false;
    }
F
fanxiaoyu 已提交
326
    if (value.length() > 0 && memcpy_s(buffer, value.length(), value.data(), value.length()) != EOK) {
C
chenchong_666 已提交
327
        ZLOGE(LOG_LABEL, "%{public}s: memcpy string16 failed\n", __func__);
F
fanxiaoyu 已提交
328 329
        return false;
    }
F
fanxiaoyu 已提交
330 331 332 333 334 335 336 337
    return true;
}

bool CParcelWriteInterfaceToken(CParcel *parcel, const char *token, int32_t tokenLen)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
F
fanxiaoyu 已提交
338
    if (token == nullptr || tokenLen < 0) {
C
chenchong_666 已提交
339
        ZLOGE(LOG_LABEL, "%{public}s: token len is invalid: %d\n", __func__, tokenLen);
F
fanxiaoyu 已提交
340 341 342
        return false;
    }
    std::u16string u16string = Str8ToStr16(std::string(token, tokenLen));
F
fanxiaoyu 已提交
343
    if (u16string.length() == 0 && tokenLen != 0) {
C
chenchong_666 已提交
344
        ZLOGE(LOG_LABEL, "%{public}s: convert token to u16string failed: %d\n", __func__, tokenLen);
F
fanxiaoyu 已提交
345 346 347 348 349 350 351 352 353 354 355 356
        return false;
    }
    return parcel->parcel_->WriteInterfaceToken(u16string);
}

bool CParcelReadInterfaceToken(const CParcel *parcel, void *token, OnCParcelBytesAllocator allocator)
{
    if (!IsValidParcel(parcel, __func__) || allocator == nullptr) {
        return false;
    }
    std::u16string u16string = parcel->parcel_->ReadInterfaceToken();
    std::string value = Str16ToStr8(u16string);
F
fanxiaoyu 已提交
357
    if (u16string.length() != 0 && value.length() == 0) {
C
chenchong_666 已提交
358
        ZLOGE(LOG_LABEL, "%{public}s: u16string len: %u, string len: %u\n", __func__,
T
tanyanying 已提交
359
            static_cast<uint32_t>(u16string.length()), static_cast<uint32_t>(value.length()));
F
fanxiaoyu 已提交
360 361 362 363 364
        return false;
    }
    char *buffer = nullptr;
    bool isSuccess = allocator(token, &buffer, value.length());
    if (!isSuccess) {
C
chenchong_666 已提交
365
        ZLOGE(LOG_LABEL, "%{public}s: allocate interface token buffer failed\n", __func__);
F
fanxiaoyu 已提交
366 367
        return false;
    }
F
fanxiaoyu 已提交
368
    if (value.length() > 0 && memcpy_s(buffer, value.length(), value.data(), value.length()) != EOK) {
C
chenchong_666 已提交
369
        ZLOGE(LOG_LABEL, "%{public}s: memcpy interface token failed\n", __func__);
F
fanxiaoyu 已提交
370 371
        return false;
    }
F
fanxiaoyu 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    return true;
}

bool CParcelWriteRemoteObject(CParcel *parcel, const CRemoteObject *object)
{
    if (!IsValidParcel(parcel, __func__) || !IsValidRemoteObject(object, __func__)) {
        return false;
    }
    return parcel->parcel_->WriteRemoteObject(object->remote_);
}

CRemoteObject *CParcelReadRemoteObject(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return nullptr;
    }
    sptr<IRemoteObject> remote = parcel->parcel_->ReadRemoteObject();
    if (remote == nullptr) {
C
chenchong_666 已提交
390
        ZLOGE(LOG_LABEL, "%{public}s: read remote object is null\n", __func__);
F
fanxiaoyu 已提交
391 392 393
        return nullptr;
    }
    CRemoteObject *holder = nullptr;
T
tanyanying 已提交
394
    holder = new (std::nothrow) CRemoteObjectHolder();
F
fanxiaoyu 已提交
395
    if (holder == nullptr) {
C
chenchong_666 已提交
396
        ZLOGE(LOG_LABEL, "%{public}s: craete remote object holder failed\n", __func__);
F
fanxiaoyu 已提交
397 398 399 400 401
        return nullptr;
    }
    holder->remote_ = remote;
    holder->IncStrongRef(nullptr);
    return holder;
F
fanxiaoyu 已提交
402
}
F
fanxiaoyu 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

bool CParcelWriteFileDescriptor(CParcel *parcel, int32_t fd)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->WriteFileDescriptor(fd);
}

bool CParcelReadFileDescriptor(const CParcel *parcel, int32_t *fd)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    if (fd == nullptr) {
C
chenchong_666 已提交
418
        ZLOGE(LOG_LABEL, "%{public}s: fd is null\n", __func__);
F
fanxiaoyu 已提交
419 420 421 422 423 424
        return false;
    }
    *fd = parcel->parcel_->ReadFileDescriptor();
    return (*fd < 0) ? false : true;
}

F
fanxiaoyu 已提交
425 426 427 428 429 430
bool CParcelWriteBuffer(CParcel *parcel, const uint8_t *buffer, uint32_t len)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    if (buffer == nullptr) {
C
chenchong_666 已提交
431
        ZLOGE(LOG_LABEL, "%{public}s: buffer is null: %d\n", __func__, len);
F
fanxiaoyu 已提交
432 433 434 435 436 437 438 439 440 441 442 443
        return false;
    }
    return parcel->parcel_->WriteBuffer(buffer, len);
}

bool CParcelReadBuffer(const CParcel *parcel, uint8_t *value, uint32_t len)
{
    if (!IsValidParcel(parcel, __func__) || value == nullptr) {
        return false;
    }
    const uint8_t *data = parcel->parcel_->ReadBuffer(len);
    if (data == nullptr) {
C
chenchong_666 已提交
444
        ZLOGE(LOG_LABEL, "%{public}s: read buffer failed\n", __func__);
F
fanxiaoyu 已提交
445 446 447
        return false;
    }
    if (len > 0 && memcpy_s(value, len, data, len) != EOK) {
C
chenchong_666 已提交
448
        ZLOGE(LOG_LABEL, "%{public}s: copy buffer failed\n", __func__);
F
fanxiaoyu 已提交
449 450 451 452 453
        return false;
    }
    return true;
}

T
tanyanying 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
bool CParcelWriteRawData(CParcel *parcel, const uint8_t *buffer, uint32_t len)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->WriteRawData(buffer, len);
}

const uint8_t *CParcelReadRawData(const CParcel *parcel, uint32_t len)
{
    if (!IsValidParcel(parcel, __func__)) {
        return nullptr;
    }
    const void *data = parcel->parcel_->ReadRawData(len);
    if (data == nullptr) {
C
chenchong_666 已提交
469
        ZLOGE(LOG_LABEL, "%{public}s: read raw data from native failed\n", __func__);
T
tanyanying 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
        return nullptr;
    }
    return reinterpret_cast<const uint8_t *>(data);
}

template <typename T>
static bool WriteVector(CParcel *parcel, const char *func, const T *array, int32_t len,
    bool (Parcel::*Write)(const std::vector<T> &val))
{
    if (!IsValidParcel(parcel, func) || (array == nullptr && len > 0)) {
        return false;
    }
    std::vector<T> value(array, array + len);
    return (parcel->parcel_->*Write)(value);
}

template <typename T>
static bool ReadVector(const CParcel *parcel, const char *func, void *value,
    bool (*OnCParcelTypeAllocator)(void *value, T **buffer, int32_t len),
    bool (Parcel::*Read)(std::vector<T> *val))
{
    if (!IsValidParcel(parcel, func) || OnCParcelTypeAllocator == nullptr) {
        return false;
    }
    std::vector<T> array;
    if (!(parcel->parcel_->*Read)(&array)) {
C
chenchong_666 已提交
496
        ZLOGE(LOG_LABEL, "%{public}s: read type vector from native failed\n", func);
T
tanyanying 已提交
497 498 499 500 501
        return false;
    }
    T *buffer = nullptr;
    bool isSuccess = OnCParcelTypeAllocator(value, &buffer, array.size());
    if (!isSuccess) {
C
chenchong_666 已提交
502
        ZLOGE(LOG_LABEL, "%{public}s: allocate type array buffer failed\n", func);
T
tanyanying 已提交
503 504 505 506
        return false;
    }
    int32_t len = array.size() * sizeof(T);
    if (array.size() > 0 && memcpy_s(buffer, len, array.data(), len) != EOK) {
C
chenchong_666 已提交
507
        ZLOGE(LOG_LABEL, "%{public}s: memcpy type buffer failed\n", func);
T
tanyanying 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
        return false;
    }
    return true;
}

bool CParcelWriteBoolArray(CParcel *parcel, const bool *array, int32_t len)
{
    return WriteVector(parcel, __func__, array, len, &Parcel::WriteBoolVector);
}

bool CParcelReadBoolArray(const CParcel *parcel, void *value, OnCParcelBoolAllocator allocator)
{
    if (!IsValidParcel(parcel, __func__) || allocator == nullptr) {
        return false;
    }
    std::vector<bool> array;
    if (!parcel->parcel_->ReadBoolVector(&array)) {
C
chenchong_666 已提交
525
        ZLOGE(LOG_LABEL, "%{public}s: read bool vector from native failed\n", __func__);
T
tanyanying 已提交
526 527 528 529 530
        return false;
    }
    bool *buffer = nullptr;
    bool isSuccess = allocator(value, &buffer, array.size());
    if (!isSuccess) {
C
chenchong_666 已提交
531
        ZLOGE(LOG_LABEL, "%{public}s: allocate bool array buffer failed\n", __func__);
T
tanyanying 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
        return false;
    }
    if (array.size() > 0) {
        for (size_t i = 0; i < array.size(); ++i) {
            buffer[i] = array[i];
        }
    }
    return true;
}

bool CParcelWriteInt8Array(CParcel *parcel, const int8_t *array, int32_t len)
{
    return WriteVector(parcel, __func__, array, len, &Parcel::WriteInt8Vector);
}

bool CParcelReadInt8Array(const CParcel *parcel, void *value, OnCParcelInt8Allocator allocator)
{
    return ReadVector(parcel, __func__, value, allocator, &Parcel::ReadInt8Vector);
}

bool CParcelWriteInt16Array(CParcel *parcel, const int16_t *array, int32_t len)
{
    return WriteVector(parcel, __func__, array, len, &Parcel::WriteInt16Vector);
}

bool CParcelReadInt16Array(const CParcel *parcel, void *value, OnCParcelInt16Allocator allocator)
{
    return ReadVector(parcel, __func__, value, allocator, &Parcel::ReadInt16Vector);
}

bool CParcelWriteInt32Array(CParcel *parcel, const int32_t *array, int32_t len)
{
    return WriteVector(parcel, __func__, array, len, &Parcel::WriteInt32Vector);
}

bool CParcelReadInt32Array(const CParcel *parcel, void *value, OnCParcelInt32Allocator allocator)
{
    return ReadVector(parcel, __func__, value, allocator, &Parcel::ReadInt32Vector);
}

bool CParcelWriteInt64Array(CParcel *parcel, const int64_t *array, int32_t len)
{
    return WriteVector(parcel, __func__, array, len, &Parcel::WriteInt64Vector);
}

bool CParcelReadInt64Array(const CParcel *parcel, void *value, OnCParcelInt64Allocator allocator)
{
    return ReadVector(parcel, __func__, value, allocator, &Parcel::ReadInt64Vector);
}

bool CParcelWriteFloatArray(CParcel *parcel, const float *array, int32_t len)
{
    return WriteVector(parcel, __func__, array, len, &Parcel::WriteFloatVector);
}

bool CParcelReadFloatArray(const CParcel *parcel, void *value, OnCParcelFloatAllocator allocator)
{
    return ReadVector(parcel, __func__, value, allocator, &Parcel::ReadFloatVector);
}

bool CParcelWriteDoubleArray(CParcel *parcel, const double *array, int32_t len)
{
    return WriteVector(parcel, __func__, array, len, &Parcel::WriteDoubleVector);
}

bool CParcelReadDoubleArray(const CParcel *parcel, void *value, OnCParcelDoubleAllocator allocator)
{
    return ReadVector(parcel, __func__, value, allocator, &Parcel::ReadDoubleVector);
}

bool CParcelWriteStringArray(CParcel *parcel, const void *value,
    int32_t len, OnStringArrayWrite writer)
{
    if (!IsValidParcel(parcel, __func__) || writer == nullptr) {
        return false;
    }
    std::vector<std::string> stringVector;
    if (len > 0 && !writer(reinterpret_cast<void *>(&stringVector),
        value, static_cast<uint32_t>(len))) {
C
chenchong_666 已提交
611
        ZLOGE(LOG_LABEL, "%{public}s: write string array to vector failed\n", __func__);
T
tanyanying 已提交
612 613 614
        return false;
    }
    if (!parcel->parcel_->WriteStringVector(stringVector)) {
C
chenchong_666 已提交
615
        ZLOGE(LOG_LABEL, "%{public}s: write string array to parcel failed\n", __func__);
T
tanyanying 已提交
616 617 618 619 620
        return false;
    }
    return true;
}

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
bool CParcelWriteString16Array(CParcel *parcel, const void *value,
    int32_t len, OnString16ArrayWrite writer)
{
    if (!IsValidParcel(parcel, __func__) || writer == nullptr) {
        return false;
    }
    std::vector<std::u16string> string16Vector;
    if (len > 0 && !writer(reinterpret_cast<void *>(&string16Vector),
        value, static_cast<uint32_t>(len))) {
        ZLOGE(LOG_LABEL, "%{public}s: write u16string array to vector failed\n", __func__);
        return false;
    }

    if (!parcel->parcel_->WriteString16Vector(string16Vector)) {
        ZLOGE(LOG_LABEL, "%{public}s: write u16string array to parcel failed\n", __func__);
        return false;
    }
    return true;
}

T
tanyanying 已提交
641 642 643 644
bool CParcelWriteStringElement(void *data, const char *value, int32_t len)
{
    std::vector<std::string> *stringVector = reinterpret_cast<std::vector<std::string> *>(data);
    if (stringVector == nullptr) {
C
chenchong_666 已提交
645
        ZLOGE(LOG_LABEL, "%{public}s: stringVector is null\n", __func__);
T
tanyanying 已提交
646 647 648
        return false;
    }
    if (len < 0) {
C
chenchong_666 已提交
649
        ZLOGE(LOG_LABEL, "%{public}s: string len is invalid: %d\n", __func__, len);
T
tanyanying 已提交
650 651 652 653 654 655
        return false;
    }
    stringVector->push_back(std::string(value, len));
    return true;
}

656
bool CParcelWritU16stringElement(void *data, const char *value, int32_t len)
C
chenchong_666 已提交
657 658 659 660 661 662 663 664 665 666
{
    std::vector<std::u16string> *u16stringVector = reinterpret_cast<std::vector<std::u16string> *>(data);
    if (u16stringVector == nullptr) {
        ZLOGE(LOG_LABEL, "%{public}s: stringVector is null\n", __func__);
        return false;
    }
    if (len < 0) {
        ZLOGE(LOG_LABEL, "%{public}s: string len is invalid: %d\n", __func__, len);
        return false;
    }
667 668 669 670 671

    std::u16string u16str = std::wstring_convert< std::codecvt_utf8_utf16<char16_t>, char16_t >{}.from_bytes(
        std::string(value, len));
    u16stringVector->push_back(u16str);

C
chenchong_666 已提交
672 673 674
    return true;
}

T
tanyanying 已提交
675 676 677 678 679 680 681
bool CParcelReadStringArray(const CParcel *parcel, void *value, OnStringArrayRead reader)
{
    if (!IsValidParcel(parcel, __func__) || reader == nullptr) {
        return false;
    }
    std::vector<std::string> stringVector;
    if (!parcel->parcel_->ReadStringVector(&stringVector)) {
C
chenchong_666 已提交
682
        ZLOGE(LOG_LABEL, "%{public}s: read string array from parcel failed\n", __func__);
T
tanyanying 已提交
683 684 685
        return false;
    }
    if (!reader(reinterpret_cast<void *>(&stringVector), value, stringVector.size())) {
C
chenchong_666 已提交
686
        ZLOGE(LOG_LABEL, "%{public}s: read string to vector failed\n", __func__);
T
tanyanying 已提交
687 688 689 690 691
        return false;
    }
    return true;
}

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
bool CParcelReadString16Array(const CParcel *parcel, void *value, OnString16ArrayRead reader)
{
    if (!IsValidParcel(parcel, __func__) || reader == nullptr) {
        return false;
    }
    std::vector<std::u16string> string16Vector;
    if (!parcel->parcel_->ReadString16Vector(&string16Vector)) {
        ZLOGE(LOG_LABEL, "%{public}s: read u16string array from parcel failed\n", __func__);
        return false;
    }
    if (!reader(reinterpret_cast<void *>(&string16Vector), value, string16Vector.size())) {
        ZLOGE(LOG_LABEL, "%{public}s: read u16string to vector failed\n", __func__);
        return false;
    }
    return true;
}

bool CParcelReadString16Element(uint32_t index, const void *data, void *value,
    OnCParcelBytesAllocator16 allocator)
{
    if (data == nullptr || allocator == nullptr) {
        ZLOGE(LOG_LABEL, "%{public}s: invalid data and allocator\n", __func__);
        return false;
    }
    const std::vector<std::u16string> *string16Vector =
        reinterpret_cast<const std::vector<std::u16string> *>(data);

    if (index >= string16Vector->size()) {
        ZLOGE(LOG_LABEL, "%{public}s: invalid index: %u, size: %u\n", __func__,
            index, static_cast<uint32_t>(string16Vector->size()));
        return false;
    }

    const std::u16string &string16Value = (*string16Vector)[index];
    int16_t *buffer = nullptr;
    bool isSuccess = allocator(value, &buffer, string16Value.length());
    if (!isSuccess) {
        ZLOGE(LOG_LABEL, "%{public}s: allocate string16Value buffer failed\n", __func__);
        return false;
    }

    if (string16Value.length() > 0 &&
        memcpy_s(buffer, string16Value.length() * sizeof(char16_t),
            string16Value.data(), string16Value.length() * sizeof(char16_t)) != EOK) {
        ZLOGE(LOG_LABEL, "%{public}s: memcpy string16Value failed\n", __func__);
        return false;
    }
    return true;
}

T
tanyanying 已提交
742 743 744 745
bool CParcelReadStringElement(uint32_t index, const void *data, void *value,
    OnCParcelBytesAllocator allocator)
{
    if (data == nullptr || allocator == nullptr) {
C
chenchong_666 已提交
746
        ZLOGE(LOG_LABEL, "%{public}s: invalid data and allocator\n", __func__);
T
tanyanying 已提交
747 748 749 750 751
        return false;
    }
    const std::vector<std::string> *stringVector =
        reinterpret_cast<const std::vector<std::string> *>(data);
    if (index >= stringVector->size()) {
C
chenchong_666 已提交
752
        ZLOGE(LOG_LABEL, "%{public}s: invalid index: %u, size: %u\n", __func__,
T
tanyanying 已提交
753 754 755
            index, static_cast<uint32_t>(stringVector->size()));
        return false;
    }
C
chenchong_666 已提交
756

T
tanyanying 已提交
757 758 759 760
    const std::string &stringValue = (*stringVector)[index];
    char *buffer = nullptr;
    bool isSuccess = allocator(value, &buffer, stringValue.length());
    if (!isSuccess) {
C
chenchong_666 已提交
761
        ZLOGE(LOG_LABEL, "%{public}s: allocate string buffer failed\n", __func__);
T
tanyanying 已提交
762 763
        return false;
    }
C
chenchong_666 已提交
764

T
tanyanying 已提交
765 766
    if (stringValue.length() > 0 &&
        memcpy_s(buffer, stringValue.length(), stringValue.data(), stringValue.length()) != EOK) {
C
chenchong_666 已提交
767
        ZLOGE(LOG_LABEL, "%{public}s: memcpy string failed\n", __func__);
T
tanyanying 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
        return false;
    }
    return true;
}

bool CParcelWriteParcelableArray(CParcel *parcel, const void *value, int32_t len,
    OnCParcelWriteElement elementWriter)
{
    if (!IsValidParcel(parcel, __func__) || elementWriter == nullptr) {
        return false;
    }
    size_t pos = parcel->parcel_->GetWritePosition();
    if (!WriteAndCheckArrayLength(parcel, len < 0, len)) {
        return false;
    }
    for (int32_t i = 0; i < len; ++i) {
        if (!elementWriter(parcel, value, static_cast<unsigned long>(i))) {
C
chenchong_666 已提交
785
            ZLOGE(LOG_LABEL, "%{public}s: write parcelable for index: %d failed\n", __func__, i);
T
tanyanying 已提交
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
            parcel->parcel_->RewindWrite(pos);
            return false;
        }
    }
    return true;
}

bool CParcelReadParcelableArray(const CParcel *parcel, void *value,
    OnCParcelAllocator allocator, OnCParcelReadElement elementReader)
{
    if (!IsValidParcel(parcel, __func__) || elementReader == nullptr) {
        return false;
    }
    size_t pos = parcel->parcel_->GetReadPosition();
    int32_t length;
    if (!ReadAndCheckArrayLength(parcel, length)) {
        return false;
    }
    if (!allocator(value, length)) {
C
chenchong_666 已提交
805
        ZLOGE(LOG_LABEL, "%{public}s: allocator failed\n", __func__);
T
tanyanying 已提交
806 807 808 809 810
        return false;
    }
    // length == -1 means null array, and will return true
    for (int32_t i = 0; i < length; ++i) {
        if (!elementReader(parcel, value, static_cast<unsigned long>(i))) {
C
chenchong_666 已提交
811
            ZLOGE(LOG_LABEL, "%{public}s: read parcelable for index: %d failed\n", __func__, i);
T
tanyanying 已提交
812 813 814 815 816 817 818
            parcel->parcel_->RewindRead(pos);
            return false;
        }
    }
    return true;
}

F
fanxiaoyu 已提交
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
uint32_t CParcelGetDataSize(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->GetDataSize();
}

bool CParcelSetDataSize(CParcel *parcel, uint32_t new_size)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->SetDataSize(new_size);
}

uint32_t CParcelGetDataCapacity(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->GetDataCapacity();
}

bool CParcelSetDataCapacity(CParcel *parcel, uint32_t new_size)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->SetDataCapacity(new_size);
}

uint32_t CParcelGetMaxCapacity(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->GetMaxCapacity();
}

bool CParcelSetMaxCapacity(CParcel *parcel, uint32_t new_size)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->SetMaxCapacity(new_size);
}

uint32_t CParcelGetWritableBytes(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->GetWritableBytes();
}

uint32_t CParcelGetReadableBytes(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->GetReadableBytes();
}

uint32_t CParcelGetReadPosition(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->GetReadPosition();
}

uint32_t CParcelGetWritePosition(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->GetWritePosition();
}

bool CParcelRewindRead(CParcel *parcel, uint32_t new_pos)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->RewindRead(new_pos);
}

bool CParcelRewindWrite(CParcel *parcel, uint32_t new_pos)
{
    if (!IsValidParcel(parcel, __func__)) {
        return 0;
    }
    return parcel->parcel_->RewindWrite(new_pos);
}
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929

bool CParcelWriteAshmem(CParcel *parcel, CAshmem *ashmem)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->WriteAshmem(ashmem->ashmem_);
}

CAshmem *CParcelReadAshmem(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return nullptr;
    }
    sptr<Ashmem> ashmem = parcel->parcel_->ReadAshmem();
    if (ashmem == nullptr) {
C
chenchong_666 已提交
930
        ZLOGE(LOG_LABEL, "%{public}s: read ashmem failed\n", __func__);
931 932 933 934
        return nullptr;
    }
    CAshmem *cashmem = new (std::nothrow) CAshmem(ashmem);
    if (cashmem == nullptr) {
C
chenchong_666 已提交
935
        ZLOGE(LOG_LABEL, "%{public}s: new ashmem failed\n", __func__);
936 937 938 939
        return nullptr;
    }
    ashmem->IncStrongRef(nullptr);
    return cashmem;
C
chenchong_666 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
}

bool CParcelContainFileDescriptors(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->ContainFileDescriptors();
}

size_t CParcelGetRawDataSize(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return -1;
    }
    return parcel->parcel_->GetRawDataSize();
}

size_t CParcelGetRawDataCapacity(const CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return -1;
    }
    return parcel->parcel_->GetRawDataCapacity();
}

void CParcelClearFileDescriptor(CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return;
    }
    parcel->parcel_->ClearFileDescriptor();
}

void CParcelSetClearFdFlag(CParcel *parcel)
{
    if (!IsValidParcel(parcel, __func__)) {
        return;
    }
    parcel->parcel_->SetClearFdFlag();
}

bool CParcelAppend(CParcel *parcel, CParcel *data)
{
    if (!IsValidParcel(parcel, __func__)) {
        return false;
    }
    return parcel->parcel_->Append(*(data->parcel_));
988
}