• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024-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 "parcel_wrapper.h"
17 
18 #include <securec.h>
19 #include <sys/types.h>
20 
21 #include <codecvt>
22 #include <cstddef>
23 #include <cstdint>
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 #include "ashmem.h"
29 #include "cxx.h"
30 #include "message_option.h"
31 #include "message_parcel.h"
32 #include "parcel.h"
33 #include "remote/wrapper.rs.h"
34 #include "string_ex.h"
35 
36 namespace OHOS {
37 namespace IpcRust {
NewMessageParcel()38 std::unique_ptr<MessageParcel> NewMessageParcel()
39 {
40     return std::make_unique<MessageParcel>();
41 }
42 
NewMessageOption()43 std::unique_ptr<MessageOption> NewMessageOption()
44 {
45     return std::make_unique<MessageOption>();
46 }
47 
AsParcel(const MessageParcel & msgParcel)48 Parcel const *AsParcel(const MessageParcel &msgParcel)
49 {
50     auto msgParcelMut = const_cast<MessageParcel *>(&msgParcel);
51     return reinterpret_cast<Parcel *>(msgParcelMut);
52 }
53 
AsParcelMut(MessageParcel & msgParcel)54 Parcel *AsParcelMut(MessageParcel &msgParcel)
55 {
56     return reinterpret_cast<Parcel *>(&msgParcel);
57 }
58 
WriteInterfaceToken(MessageParcel & msgParcel,rust::str name)59 bool WriteInterfaceToken(MessageParcel &msgParcel, rust::str name)
60 {
61     std::u16string s = Str8ToStr16(std::string(name));
62     return msgParcel.WriteInterfaceToken(s);
63 }
64 
ReadInterfaceToken(MessageParcel & msgParcel)65 rust::string ReadInterfaceToken(MessageParcel &msgParcel)
66 {
67     return msgParcel.ReadInterfaceToken().data();
68 }
69 
WriteRemoteObject(MessageParcel & msgParcel,std::unique_ptr<IRemoteObjectWrapper> object)70 bool WriteRemoteObject(MessageParcel &msgParcel, std::unique_ptr<IRemoteObjectWrapper> object)
71 {
72     if (object->is_raw_) {
73         return false;
74     } else {
75         return msgParcel.WriteRemoteObject(object->sptr_);
76     }
77 }
78 
ReadRemoteObject(MessageParcel & msgParcel)79 std::unique_ptr<IRemoteObjectWrapper> ReadRemoteObject(MessageParcel &msgParcel)
80 {
81     sptr<IRemoteObject> remote = msgParcel.ReadRemoteObject();
82     if (remote == nullptr) {
83         return nullptr;
84     }
85     auto wrapper = std::make_unique<IRemoteObjectWrapper>();
86     wrapper->is_raw_ = false;
87     wrapper->sptr_ = std::move(remote);
88     return wrapper;
89 }
90 
WriteBuffer(MessageParcel & msgParcel,rust::slice<const uint8_t> buffer)91 bool WriteBuffer(MessageParcel &msgParcel, rust::slice<const uint8_t> buffer)
92 {
93     return msgParcel.WriteBuffer(buffer.data(), buffer.size());
94 }
95 
ReadBuffer(MessageParcel & msgParcel,size_t len,rust::vec<uint8_t> & buffer)96 bool ReadBuffer(MessageParcel &msgParcel, size_t len, rust::vec<uint8_t> &buffer)
97 {
98     if (len == 0) {
99         return true;
100     }
101     const uint8_t *data = msgParcel.ReadBuffer(len);
102     if (data == nullptr) {
103         return false;
104     }
105     if (memcpy_s(buffer.data(), len, data, len) != EOK) {
106         return false;
107     }
108     return true;
109 }
110 
ReadString(Parcel & parcel,rust::string & val)111 bool ReadString(Parcel &parcel, rust::string &val)
112 {
113     std::string v;
114     if (!parcel.ReadString(v)) {
115         return false;
116     }
117 
118     val = rust::string::lossy(v);
119     // If the two strings are different, it is because the string 'v' contains invalid UTF-8 data.
120     return std::string(val) == v;
121 }
122 
WriteString(Parcel & parcel,const rust::str val)123 bool WriteString(Parcel &parcel, const rust::str val)
124 {
125     auto s = std::string(val);
126     return parcel.WriteString(s);
127 }
128 
WriteString16(Parcel & parcel,const rust::str val)129 bool WriteString16(Parcel &parcel, const rust::str val)
130 {
131     std::u16string u16string = Str8ToStr16(std::string(val));
132     return parcel.WriteString16(u16string);
133 }
ReadString16(Parcel & parcel)134 rust::string ReadString16(Parcel &parcel)
135 {
136     std::u16string u16string;
137     parcel.ReadString16(u16string);
138     return rust::string(u16string.data());
139 }
140 
RustVec2CppVec(rust::slice<const T> val)141 template<typename T> std::vector<T> RustVec2CppVec(rust::slice<const T> val)
142 {
143     std::vector<T> v;
144     for (auto i : val) {
145         v.push_back(i);
146     }
147     return v;
148 }
149 
WriteBoolVector(Parcel & parcel,rust::slice<const bool> val)150 bool WriteBoolVector(Parcel &parcel, rust::slice<const bool> val)
151 {
152     return parcel.WriteBoolVector(RustVec2CppVec(val));
153 }
154 
WriteInt8Vector(Parcel & parcel,rust::slice<const int8_t> val)155 bool WriteInt8Vector(Parcel &parcel, rust::slice<const int8_t> val)
156 {
157     return parcel.WriteInt8Vector(RustVec2CppVec(val));
158 }
159 
WriteInt16Vector(Parcel & parcel,rust::slice<const int16_t> val)160 bool WriteInt16Vector(Parcel &parcel, rust::slice<const int16_t> val)
161 {
162     return parcel.WriteInt16Vector(RustVec2CppVec(val));
163 }
WriteInt32Vector(Parcel & parcel,rust::slice<const int32_t> val)164 bool WriteInt32Vector(Parcel &parcel, rust::slice<const int32_t> val)
165 {
166     return parcel.WriteInt32Vector(RustVec2CppVec(val));
167 }
WriteInt64Vector(Parcel & parcel,rust::slice<const int64_t> val)168 bool WriteInt64Vector(Parcel &parcel, rust::slice<const int64_t> val)
169 {
170     return parcel.WriteInt64Vector(RustVec2CppVec(val));
171 }
WriteUInt8Vector(Parcel & parcel,rust::slice<const uint8_t> val)172 bool WriteUInt8Vector(Parcel &parcel, rust::slice<const uint8_t> val)
173 {
174     return parcel.WriteUInt8Vector(RustVec2CppVec(val));
175 }
WriteUInt16Vector(Parcel & parcel,rust::slice<const uint16_t> val)176 bool WriteUInt16Vector(Parcel &parcel, rust::slice<const uint16_t> val)
177 {
178     return parcel.WriteUInt16Vector(RustVec2CppVec(val));
179 }
WriteUInt32Vector(Parcel & parcel,rust::slice<const uint32_t> val)180 bool WriteUInt32Vector(Parcel &parcel, rust::slice<const uint32_t> val)
181 {
182     return parcel.WriteUInt32Vector(RustVec2CppVec(val));
183 }
WriteUInt64Vector(Parcel & parcel,rust::slice<const uint64_t> val)184 bool WriteUInt64Vector(Parcel &parcel, rust::slice<const uint64_t> val)
185 {
186     return parcel.WriteUInt64Vector(RustVec2CppVec(val));
187 }
WriteFloatVector(Parcel & parcel,rust::slice<const float> val)188 bool WriteFloatVector(Parcel &parcel, rust::slice<const float> val)
189 {
190     return parcel.WriteFloatVector(RustVec2CppVec(val));
191 }
WriteDoubleVector(Parcel & parcel,rust::slice<const double> val)192 bool WriteDoubleVector(Parcel &parcel, rust::slice<const double> val)
193 {
194     return parcel.WriteDoubleVector(RustVec2CppVec(val));
195 }
196 
WriteStringVector(Parcel & parcel,rust::slice<const rust::string> val)197 bool WriteStringVector(Parcel &parcel, rust::slice<const rust::string> val)
198 {
199     std::vector<std::string> v;
200     for (auto rust_s : val) {
201         v.push_back(std::string(rust_s));
202     }
203     return parcel.WriteStringVector(v);
204 }
205 
WriteString16Vector(Parcel & parcel,rust::slice<const rust::string> val)206 bool WriteString16Vector(Parcel &parcel, rust::slice<const rust::string> val)
207 {
208     std::vector<std::u16string> v;
209     for (auto rust_s : val) {
210         std::u16string u16string = Str8ToStr16(std::string(rust_s));
211         v.push_back(u16string);
212     }
213     return parcel.WriteString16Vector(v);
214 }
215 
ReadVector(Parcel & parcel,rust::vec<T> & val,bool (Parcel::* ReadVec)(std::vector<T> *))216 template<typename T> bool ReadVector(Parcel &parcel, rust::vec<T> &val, bool (Parcel::*ReadVec)(std::vector<T> *))
217 {
218     std::vector<T> v;
219     if (!(parcel.*ReadVec)(&v)) {
220         return false;
221     }
222     for (auto i : v) {
223         val.push_back(i);
224     }
225     return true;
226 }
227 
ReadBoolVector(Parcel & parcel,rust::vec<bool> & val)228 bool ReadBoolVector(Parcel &parcel, rust::vec<bool> &val)
229 {
230     return ReadVector(parcel, val, &Parcel::ReadBoolVector);
231 }
ReadInt8Vector(Parcel & parcel,rust::vec<int8_t> & val)232 bool ReadInt8Vector(Parcel &parcel, rust::vec<int8_t> &val)
233 {
234     return ReadVector(parcel, val, &Parcel::ReadInt8Vector);
235 }
ReadInt16Vector(Parcel & parcel,rust::vec<int16_t> & val)236 bool ReadInt16Vector(Parcel &parcel, rust::vec<int16_t> &val)
237 {
238     return ReadVector(parcel, val, &Parcel::ReadInt16Vector);
239 }
ReadInt32Vector(Parcel & parcel,rust::vec<int32_t> & val)240 bool ReadInt32Vector(Parcel &parcel, rust::vec<int32_t> &val)
241 {
242     return ReadVector(parcel, val, &Parcel::ReadInt32Vector);
243 }
ReadInt64Vector(Parcel & parcel,rust::vec<int64_t> & val)244 bool ReadInt64Vector(Parcel &parcel, rust::vec<int64_t> &val)
245 {
246     return ReadVector(parcel, val, &Parcel::ReadInt64Vector);
247 }
ReadUInt8Vector(Parcel & parcel,rust::vec<uint8_t> & val)248 bool ReadUInt8Vector(Parcel &parcel, rust::vec<uint8_t> &val)
249 {
250     return ReadVector(parcel, val, &Parcel::ReadUInt8Vector);
251 }
ReadUInt16Vector(Parcel & parcel,rust::vec<uint16_t> & val)252 bool ReadUInt16Vector(Parcel &parcel, rust::vec<uint16_t> &val)
253 {
254     return ReadVector(parcel, val, &Parcel::ReadUInt16Vector);
255 }
ReadUInt32Vector(Parcel & parcel,rust::vec<uint32_t> & val)256 bool ReadUInt32Vector(Parcel &parcel, rust::vec<uint32_t> &val)
257 {
258     return ReadVector(parcel, val, &Parcel::ReadUInt32Vector);
259 }
ReadUInt64Vector(Parcel & parcel,rust::vec<uint64_t> & val)260 bool ReadUInt64Vector(Parcel &parcel, rust::vec<uint64_t> &val)
261 {
262     return ReadVector(parcel, val, &Parcel::ReadUInt64Vector);
263 }
ReadFloatVector(Parcel & parcel,rust::vec<float> & val)264 bool ReadFloatVector(Parcel &parcel, rust::vec<float> &val)
265 {
266     return ReadVector(parcel, val, &Parcel::ReadFloatVector);
267 }
ReadDoubleVector(Parcel & parcel,rust::vec<double> & val)268 bool ReadDoubleVector(Parcel &parcel, rust::vec<double> &val)
269 {
270     return ReadVector(parcel, val, &Parcel::ReadDoubleVector);
271 }
272 
ReadStringVector(Parcel & parcel,rust::vec<rust::string> & val)273 bool ReadStringVector(Parcel &parcel, rust::vec<rust::string> &val)
274 {
275     std::vector<std::string> v;
276     if (!parcel.ReadStringVector(&v)) {
277         return false;
278     }
279     for (auto s : v) {
280         val.push_back(s.data());
281     }
282     return true;
283 }
284 
ReadString16Vector(Parcel & parcel,rust::vec<rust::string> & val)285 bool ReadString16Vector(Parcel &parcel, rust::vec<rust::string> &val)
286 {
287     std::vector<std::u16string> v;
288     if (!parcel.ReadString16Vector(&v)) {
289         return false;
290     }
291     for (auto i : v) {
292         val.push_back(i.data());
293     }
294     return true;
295 }
296 
297 } // namespace IpcRust
298 } // namespace OHOS