1 /*
2 * Copyright (c) 2021 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 #ifndef OHOS_UTILS_PARCEL_H
17 #define OHOS_UTILS_PARCEL_H
18
19 #include <string>
20 #include <vector>
21 #include "nocopyable.h"
22 #include "refbase.h"
23 #include "flat_obj.h"
24
25 namespace OHOS {
26
27 class Parcel;
28
29 class Parcelable : public virtual RefBase {
30 public:
31 virtual ~Parcelable() = default;
32
33 Parcelable();
34 explicit Parcelable(bool asRemote);
35
36 // Write a parcelable object to the given parcel.
37 // The object position is saved into Parcel if set asRemote_ to
38 // true, and this intends to use in kernel data transaction.
39 // Returns true being written on success or false if any error occur.
40 virtual bool Marshalling(Parcel &parcel) const = 0;
41
42 // NOTICE! A static Unmarshalling function must also be implemented, so
43 // that you can get data from the given parcel into this parcelable object.
44 // See "static TestParcelable *Unmarshalling(Parcel &parcel)" as an example.
45
46 enum BehaviorFlag { IPC = 0x01, RPC = 0x02, HOLD_OBJECT = 0x10 };
47
SetBehavior(BehaviorFlag b)48 inline void SetBehavior(BehaviorFlag b) const
49 {
50 behavior_ |= static_cast<uint8_t>(b);
51 }
52
ClearBehavior(BehaviorFlag b)53 inline void ClearBehavior(BehaviorFlag b) const
54 {
55 behavior_ &= ~(static_cast<uint8_t>(b));
56 }
57
TestBehavior(BehaviorFlag b)58 inline bool TestBehavior(BehaviorFlag b) const
59 {
60 return behavior_ & (static_cast<uint8_t>(b));
61 }
62
63 public:
64 bool asRemote_;
65 mutable uint8_t behavior_;
66 };
67
68 class Allocator {
69 public:
70 virtual ~Allocator() = default;
71
72 virtual void *Realloc(void *data, size_t newSize) = 0;
73
74 virtual void *Alloc(size_t size) = 0;
75
76 virtual void Dealloc(void *data) = 0;
77 };
78
79 class DefaultAllocator : public Allocator {
80 public:
81 virtual void *Alloc(size_t size) override;
82
83 virtual void Dealloc(void *data) override;
84 private:
85 virtual void *Realloc(void *data, size_t newSize) override;
86 };
87
88 class Parcel {
89 public:
90 Parcel();
91 explicit Parcel(Allocator *allocator);
92
93 virtual ~Parcel();
94
95 size_t GetDataSize() const;
96
97 uintptr_t GetData() const;
98
99 binder_size_t GetObjectOffsets() const;
100
101 size_t GetOffsetsSize() const;
102
103 size_t GetWritableBytes() const;
104
105 size_t GetReadableBytes() const;
106
107 size_t GetDataCapacity() const;
108
109 bool SetDataCapacity(size_t newCapacity);
110
111 bool SetDataSize(size_t dataSize);
112
113 bool SetMaxCapacity(size_t maxCapacity);
114
115 bool WriteBool(bool value);
116
117 bool WriteInt8(int8_t value);
118
119 bool WriteInt16(int16_t value);
120
121 bool WriteInt32(int32_t value);
122
123 bool WriteInt64(int64_t value);
124
125 bool WriteUint8(uint8_t value);
126
127 bool WriteUint16(uint16_t value);
128
129 bool WriteUint32(uint32_t value);
130
131 bool WriteUint64(uint64_t value);
132
133 bool WriteFloat(float value);
134
135 bool WriteDouble(double value);
136
137 bool WritePointer(uintptr_t value);
138
139 bool WriteBuffer(const void *data, size_t size);
140
141 bool WriteUnpadBuffer(const void *data, size_t size);
142
143 bool WriteCString(const char *value);
144
145 bool WriteString(const std::string &value);
146
147 bool WriteString16(const std::u16string &value);
148
149 bool WriteString16WithLength(const char16_t *value, size_t len);
150
151 bool WriteParcelable(const Parcelable *object);
152
153 bool WriteStrongParcelable(const sptr<Parcelable> &object);
154
155 bool WriteRemoteObject(const Parcelable *object);
156
157 template<typename T>
158 bool WriteObject(const sptr<T> &object);
159
160 bool ParseFrom(uintptr_t data, size_t size);
161
162 bool ReadBool();
163
164 int8_t ReadInt8();
165
166 int16_t ReadInt16();
167
168 int32_t ReadInt32();
169
170 int64_t ReadInt64();
171
172 uint8_t ReadUint8();
173
174 uint16_t ReadUint16();
175
176 uint32_t ReadUint32();
177
178 uint64_t ReadUint64();
179
180 float ReadFloat();
181
182 double ReadDouble();
183
184 uintptr_t ReadPointer();
185
186 bool ReadBool(bool &value);
187
188 bool ReadInt8(int8_t &value);
189
190 bool ReadInt16(int16_t &value);
191
192 bool ReadInt32(int32_t &value);
193
194 bool ReadInt64(int64_t &value);
195
196 bool ReadUint8(uint8_t &value);
197
198 bool ReadUint16(uint16_t &value);
199
200 bool ReadUint32(uint32_t &value);
201
202 bool ReadUint64(uint64_t &value);
203
204 bool ReadFloat(float &value);
205
206 bool ReadDouble(double &value);
207
208 const uint8_t *ReadBuffer(size_t length);
209
210 const uint8_t *ReadUnpadBuffer(size_t length);
211
212 void SkipBytes(size_t bytes);
213
214 const char *ReadCString();
215
216 const std::string ReadString();
217
218 bool ReadString(std::string &value);
219
220 const std::u16string ReadString16();
221
222 bool ReadString16(std::u16string &value);
223
224 const std::u16string ReadString16WithLength(int32_t &len);
225
226 bool RewindRead(size_t newPosition);
227
228 bool RewindWrite(size_t offsets);
229
230 size_t GetReadPosition();
231
232 size_t GetWritePosition();
233
234 template <typename T>
235 T *ReadParcelable();
236
237 template <typename T>
238 sptr<T> ReadStrongParcelable();
239
240 bool CheckOffsets();
241
242 template<typename T>
243 sptr<T> ReadObject();
244
245 bool SetAllocator(Allocator *allocator);
246
247 void InjectOffsets(binder_size_t offsets, size_t offsetSize);
248
249 void FlushBuffer();
250
251 template <typename T1, typename T2>
252 bool WriteVector(const std::vector<T1> &val, bool (Parcel::*Write)(T2));
253 bool WriteBoolVector(const std::vector<bool> &val);
254 bool WriteInt8Vector(const std::vector<int8_t> &val);
255 bool WriteInt16Vector(const std::vector<int16_t> &val);
256 bool WriteInt32Vector(const std::vector<int32_t> &val);
257 bool WriteInt64Vector(const std::vector<int64_t> &val);
258 bool WriteUInt8Vector(const std::vector<uint8_t> &val);
259 bool WriteUInt16Vector(const std::vector<uint16_t> &val);
260 bool WriteUInt32Vector(const std::vector<uint32_t> &val);
261 bool WriteUInt64Vector(const std::vector<uint64_t> &val);
262 bool WriteFloatVector(const std::vector<float> &val);
263 bool WriteDoubleVector(const std::vector<double> &val);
264 bool WriteStringVector(const std::vector<std::string> &val);
265 bool WriteString16Vector(const std::vector<std::u16string> &val);
266
267 template <typename T>
268 bool ReadVector(std::vector<T> *val, bool (Parcel::*Read)(T &));
269 bool ReadBoolVector(std::vector<bool> *val);
270 bool ReadInt8Vector(std::vector<int8_t> *val);
271 bool ReadInt16Vector(std::vector<int16_t> *val);
272 bool ReadInt32Vector(std::vector<int32_t> *val);
273 bool ReadInt64Vector(std::vector<int64_t> *val);
274 bool ReadUInt8Vector(std::vector<uint8_t> *val);
275 bool ReadUInt16Vector(std::vector<uint16_t> *val);
276 bool ReadUInt32Vector(std::vector<uint32_t> *val);
277 bool ReadUInt64Vector(std::vector<uint64_t> *val);
278 bool ReadFloatVector(std::vector<float> *val);
279 bool ReadDoubleVector(std::vector<double> *val);
280 bool ReadStringVector(std::vector<std::string> *val);
281 bool ReadString16Vector(std::vector<std::u16string> *val);
282
283 bool WriteBoolUnaligned(bool value);
284 bool WriteInt8Unaligned(int8_t value);
285 bool WriteInt16Unaligned(int16_t value);
286 bool WriteUint8Unaligned(uint8_t value);
287 bool WriteUint16Unaligned(uint16_t value);
288
289
290 bool ReadBoolUnaligned();
291 bool ReadInt8Unaligned(int8_t &value);
292 bool ReadInt16Unaligned(int16_t &value);
293 bool ReadUint8Unaligned(uint8_t &value);
294 bool ReadUint16Unaligned(uint16_t &value);
295
296 private:
297 DISALLOW_COPY_AND_MOVE(Parcel);
298 template <typename T>
299 bool Write(T value);
300
301 template <typename T>
302 bool Read(T &value);
303
304 template <typename T>
305 T Read();
306
307 template <typename T>
308 bool ReadPadded(T &value);
309
GetPadSize(size_t size)310 inline size_t GetPadSize(size_t size)
311 {
312 const int SIZE_OFFSET = 3;
313 return (((size + SIZE_OFFSET) & (~SIZE_OFFSET)) - size);
314 }
315
316 bool WriteObjectOffset(binder_size_t offset);
317
318 size_t CalcNewCapacity(size_t minCapacity);
319
320 bool WriteDataBytes(const void *data, size_t size);
321
322 void WritePadBytes(size_t padded);
323
324 bool EnsureWritableCapacity(size_t desireCapacity);
325
326 bool EnsureObjectsCapacity();
327
328 bool WriteParcelableOffset(size_t offset);
329
330 private:
331 uint8_t *data_;
332 size_t readCursor_;
333 size_t writeCursor_;
334 size_t dataSize_;
335 size_t dataCapacity_;
336 size_t maxDataCapacity_;
337 binder_size_t *objectOffsets_;
338 size_t objectCursor_;
339 size_t objectsCapacity_;
340 Allocator *allocator_;
341 std::vector<sptr<Parcelable>> objectHolder_;
342 bool writable_ = true;
343 };
344
345 template <typename T>
WriteObject(const sptr<T> & object)346 bool Parcel::WriteObject(const sptr<T> &object)
347 {
348 if (object == nullptr) {
349 return T::Marshalling(*this, object);
350 }
351 return WriteRemoteObject(object.GetRefPtr());
352 }
353
354 template <typename T>
ReadObject()355 sptr<T> Parcel::ReadObject()
356 {
357 if (!this->CheckOffsets()) {
358 return nullptr;
359 }
360 return T::Unmarshalling(*this);
361 }
362
363 // Read data from the given parcel into this parcelable object.
364 template <typename T>
ReadParcelable()365 T *Parcel::ReadParcelable()
366 {
367 int32_t size = this->ReadInt32();
368 if (size == 0) {
369 return nullptr;
370 }
371
372 return T::Unmarshalling(*this);
373 }
374
375 // Read data from the given parcel into this parcelable object, return sptr.
376 template <typename T>
ReadStrongParcelable()377 sptr<T> Parcel::ReadStrongParcelable()
378 {
379 int32_t size = this->ReadInt32();
380 if (size == 0) {
381 return nullptr;
382 }
383
384 return T::Unmarshalling(*this);
385 }
386
387 } // namespace OHOS
388 #endif
389