1 /*
2 * Copyright (c) 2022-2023 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 "account_info_parcel.h"
17 #include <ipc_types.h>
18 #include <string_ex.h>
19 #include "account_log_wrapper.h"
20
21 namespace OHOS {
22 namespace AccountSA {
23 namespace {
24 const int32_t AVATAR_MAX_SIZE = 10 * 1024 * 1024;
25 }
WriteOhosAccountInfo(MessageParcel & data,const OhosAccountInfo & ohosAccountInfo)26 bool WriteOhosAccountInfo(MessageParcel &data, const OhosAccountInfo &ohosAccountInfo)
27 {
28 if (!data.WriteString16(Str8ToStr16(ohosAccountInfo.name_))) {
29 ACCOUNT_LOGE("write name failed!");
30 return false;
31 }
32 if (!data.WriteString16(Str8ToStr16(ohosAccountInfo.uid_))) {
33 ACCOUNT_LOGE("write uid failed!");
34 return false;
35 }
36 if (!data.WriteString16(Str8ToStr16(ohosAccountInfo.GetRawUid()))) {
37 ACCOUNT_LOGE("write rawUid failed!");
38 return false;
39 }
40 if (!data.WriteInt32(ohosAccountInfo.status_)) {
41 ACCOUNT_LOGE("write status failed!");
42 return false;
43 }
44 if (!data.WriteString16(Str8ToStr16(ohosAccountInfo.nickname_))) {
45 ACCOUNT_LOGE("write nickname failed!");
46 return false;
47 }
48 if (!data.WriteInt32(ohosAccountInfo.avatar_.size() + 1)) {
49 ACCOUNT_LOGE("write avatarSize failed!");
50 return false;
51 }
52 if (!data.WriteRawData(ohosAccountInfo.avatar_.c_str(), ohosAccountInfo.avatar_.size() + 1)) {
53 ACCOUNT_LOGE("write avatar failed!");
54 return false;
55 }
56 if (!data.WriteParcelable(&(ohosAccountInfo.scalableData_))) {
57 ACCOUNT_LOGE("write scalableData failed!");
58 return false;
59 }
60 return true;
61 }
62
ReadAvatarData(MessageParcel & data,std::string & avatarStr)63 static ErrCode ReadAvatarData(MessageParcel &data, std::string &avatarStr)
64 {
65 int32_t avatarSize;
66 if (!data.ReadInt32(avatarSize)) {
67 ACCOUNT_LOGE("read avatarSize failed");
68 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
69 }
70 if ((avatarSize - 1 > AVATAR_MAX_SIZE) || (avatarSize - 1 < 0)) {
71 ACCOUNT_LOGE("avatarSize is invalid");
72 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
73 }
74 auto readRawData = data.ReadRawData(avatarSize);
75 if (readRawData == nullptr) {
76 ACCOUNT_LOGE("read avatar failed");
77 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
78 }
79 const char *avatar = reinterpret_cast<const char *>(readRawData);
80 avatarStr = std::string(avatar, avatarSize - 1);
81 return ERR_OK;
82 }
83
ReadOhosAccountInfo(MessageParcel & data,OhosAccountInfo & ohosAccountInfo)84 ErrCode ReadOhosAccountInfo(MessageParcel &data, OhosAccountInfo &ohosAccountInfo)
85 {
86 std::u16string name;
87 if (!data.ReadString16(name)) {
88 ACCOUNT_LOGE("read name failed");
89 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
90 }
91 std::u16string uid;
92 if (!data.ReadString16(uid)) {
93 ACCOUNT_LOGE("read uid failed");
94 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
95 }
96 std::u16string rawUid;
97 if (!data.ReadString16(rawUid)) {
98 ACCOUNT_LOGE("read rawUid failed");
99 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
100 }
101 int32_t status;
102 if (!data.ReadInt32(status)) {
103 ACCOUNT_LOGE("read status failed");
104 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
105 }
106 std::u16string nickname;
107 if (!data.ReadString16(nickname)) {
108 ACCOUNT_LOGE("read nickname failed");
109 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
110 }
111
112 ErrCode ret = ReadAvatarData(data, ohosAccountInfo.avatar_);
113 if (ret != ERR_OK) {
114 return ret;
115 }
116 sptr<AAFwk::Want> want = data.ReadParcelable<AAFwk::Want>();
117 if (want == nullptr) {
118 ACCOUNT_LOGE("read want failed");
119 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
120 }
121 ohosAccountInfo.name_ = Str16ToStr8(name);
122 ohosAccountInfo.uid_ = Str16ToStr8(uid);
123 ohosAccountInfo.status_ = status;
124 ohosAccountInfo.nickname_ = Str16ToStr8(nickname);
125 ohosAccountInfo.scalableData_ = *want;
126 ohosAccountInfo.SetRawUid(Str16ToStr8(rawUid));
127 return ERR_OK;
128 }
129 } // namespace AccountSA
130 } // namespace OHOS
131