• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     const char *avatar = reinterpret_cast<const char *>(data.ReadRawData(avatarSize));
75     if (avatar == nullptr) {
76         ACCOUNT_LOGE("read avatar failed");
77         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
78     }
79     avatarStr = std::string(avatar, avatarSize - 1);
80     return ERR_OK;
81 }
82 
ReadOhosAccountInfo(MessageParcel & data,OhosAccountInfo & ohosAccountInfo)83 ErrCode ReadOhosAccountInfo(MessageParcel &data, OhosAccountInfo &ohosAccountInfo)
84 {
85     std::u16string name;
86     if (!data.ReadString16(name)) {
87         ACCOUNT_LOGE("read name failed");
88         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
89     }
90     std::u16string uid;
91     if (!data.ReadString16(uid)) {
92         ACCOUNT_LOGE("read uid failed");
93         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
94     }
95     std::u16string rawUid;
96     if (!data.ReadString16(rawUid)) {
97         ACCOUNT_LOGE("read rawUid failed");
98         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
99     }
100     int32_t status;
101     if (!data.ReadInt32(status)) {
102         ACCOUNT_LOGE("read status failed");
103         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
104     }
105     std::u16string nickname;
106     if (!data.ReadString16(nickname)) {
107         ACCOUNT_LOGE("read nickname failed");
108         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
109     }
110 
111     ErrCode ret = ReadAvatarData(data, ohosAccountInfo.avatar_);
112     if (ret != ERR_OK) {
113         return ret;
114     }
115     sptr<AAFwk::Want> want = data.ReadParcelable<AAFwk::Want>();
116     if (want == nullptr) {
117         ACCOUNT_LOGE("read want failed");
118         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
119     }
120     ohosAccountInfo.name_ = Str16ToStr8(name);
121     ohosAccountInfo.uid_ = Str16ToStr8(uid);
122     ohosAccountInfo.status_ = status;
123     ohosAccountInfo.nickname_ = Str16ToStr8(nickname);
124     ohosAccountInfo.scalableData_ = *want;
125     ohosAccountInfo.SetRawUid(Str16ToStr8(rawUid));
126     return ERR_OK;
127 }
128 }  // namespace AccountSA
129 }  // namespace OHOS
130