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 WriteBufferAddTerminator(const void *data, size_t size, size_t typeSize);
142
143 bool WriteUnpadBuffer(const void *data, size_t size);
144
145 bool WriteCString(const char *value);
146
147 bool WriteString(const std::string &value);
148
149 bool WriteString16(const std::u16string &value);
150
151 bool WriteString16WithLength(const char16_t *value, size_t len);
152
153 bool WriteString8WithLength(const char *value, size_t len);
154
155 bool WriteParcelable(const Parcelable *object);
156
157 bool WriteStrongParcelable(const sptr<Parcelable> &object);
158
159 bool WriteRemoteObject(const Parcelable *object);
160
161 template<typename T>
162 bool WriteObject(const sptr<T> &object);
163
164 bool ParseFrom(uintptr_t data, size_t size);
165
166 bool ReadBool();
167
168 int8_t ReadInt8();
169
170 int16_t ReadInt16();
171
172 int32_t ReadInt32();
173
174 int64_t ReadInt64();
175
176 uint8_t ReadUint8();
177
178 uint16_t ReadUint16();
179
180 uint32_t ReadUint32();
181
182 uint64_t ReadUint64();
183
184 float ReadFloat();
185
186 double ReadDouble();
187
188 uintptr_t ReadPointer();
189
190 bool ReadBool(bool &value);
191
192 bool ReadInt8(int8_t &value);
193
194 bool ReadInt16(int16_t &value);
195
196 bool ReadInt32(int32_t &value);
197
198 bool ReadInt64(int64_t &value);
199
200 bool ReadUint8(uint8_t &value);
201
202 bool ReadUint16(uint16_t &value);
203
204 bool ReadUint32(uint32_t &value);
205
206 bool ReadUint64(uint64_t &value);
207
208 bool ReadFloat(float &value);
209
210 bool ReadDouble(double &value);
211
212 const uint8_t *ReadBuffer(size_t length);
213
214 const uint8_t *ReadUnpadBuffer(size_t length);
215
216 void SkipBytes(size_t bytes);
217
218 const char *ReadCString();
219
220 const std::string ReadString();
221
222 bool ReadString(std::string &value);
223
224 const std::u16string ReadString16();
225
226 bool ReadString16(std::u16string &value);
227
228 const std::u16string ReadString16WithLength(int32_t &len);
229
230 const std::string ReadString8WithLength(int32_t &len);
231
232 bool RewindRead(size_t newPosition);
233
234 bool RewindWrite(size_t offsets);
235
236 size_t GetReadPosition();
237
238 size_t GetWritePosition();
239
240 template <typename T>
241 T *ReadParcelable();
242
243 template <typename T>
244 sptr<T> ReadStrongParcelable();
245
246 bool CheckOffsets();
247
248 template<typename T>
249 sptr<T> ReadObject();
250
251 bool SetAllocator(Allocator *allocator);
252
253 void InjectOffsets(binder_size_t offsets, size_t offsetSize);
254
255 void FlushBuffer();
256
257 template <typename T1, typename T2>
258 bool WriteVector(const std::vector<T1> &val, bool (Parcel::*Write)(T2));
259 bool WriteBoolVector(const std::vector<bool> &val);
260 bool WriteInt8Vector(const std::vector<int8_t> &val);
261 bool WriteInt16Vector(const std::vector<int16_t> &val);
262 bool WriteInt32Vector(const std::vector<int32_t> &val);
263 bool WriteInt64Vector(const std::vector<int64_t> &val);
264 bool WriteUInt8Vector(const std::vector<uint8_t> &val);
265 bool WriteUInt16Vector(const std::vector<uint16_t> &val);
266 bool WriteUInt32Vector(const std::vector<uint32_t> &val);
267 bool WriteUInt64Vector(const std::vector<uint64_t> &val);
268 bool WriteFloatVector(const std::vector<float> &val);
269 bool WriteDoubleVector(const std::vector<double> &val);
270 bool WriteStringVector(const std::vector<std::string> &val);
271 bool WriteString16Vector(const std::vector<std::u16string> &val);
272
273 template <typename T>
274 bool ReadVector(std::vector<T> *val, bool (Parcel::*Read)(T &));
275 bool ReadBoolVector(std::vector<bool> *val);
276 bool ReadInt8Vector(std::vector<int8_t> *val);
277 bool ReadInt16Vector(std::vector<int16_t> *val);
278 bool ReadInt32Vector(std::vector<int32_t> *val);
279 bool ReadInt64Vector(std::vector<int64_t> *val);
280 bool ReadUInt8Vector(std::vector<uint8_t> *val);
281 bool ReadUInt16Vector(std::vector<uint16_t> *val);
282 bool ReadUInt32Vector(std::vector<uint32_t> *val);
283 bool ReadUInt64Vector(std::vector<uint64_t> *val);
284 bool ReadFloatVector(std::vector<float> *val);
285 bool ReadDoubleVector(std::vector<double> *val);
286 bool ReadStringVector(std::vector<std::string> *val);
287 bool ReadString16Vector(std::vector<std::u16string> *val);
288
289 bool WriteBoolUnaligned(bool value);
290 bool WriteInt8Unaligned(int8_t value);
291 bool WriteInt16Unaligned(int16_t value);
292 bool WriteUint8Unaligned(uint8_t value);
293 bool WriteUint16Unaligned(uint16_t value);
294
295 bool ReadBoolUnaligned();
296 bool ReadInt8Unaligned(int8_t &value);
297 bool ReadInt16Unaligned(int16_t &value);
298 bool ReadUint8Unaligned(uint8_t &value);
299 bool ReadUint16Unaligned(uint16_t &value);
300
301 private:
302 DISALLOW_COPY_AND_MOVE(Parcel);
303 template <typename T>
304 bool Write(T value);
305
306 template <typename T>
307 bool Read(T &value);
308
309 template <typename T>
310 T Read();
311
312 template <typename T>
313 bool ReadPadded(T &value);
314
GetPadSize(size_t size)315 inline size_t GetPadSize(size_t size)
316 {
317 const int SIZE_OFFSET = 3;
318 return (((size + SIZE_OFFSET) & (~SIZE_OFFSET)) - size);
319 }
320
321 bool WriteObjectOffset(binder_size_t offset);
322
323 size_t CalcNewCapacity(size_t minCapacity);
324
325 bool WriteDataBytes(const void *data, size_t size);
326
327 void WritePadBytes(size_t padded);
328
329 bool EnsureWritableCapacity(size_t desireCapacity);
330
331 bool EnsureObjectsCapacity();
332
333 bool WriteParcelableOffset(size_t offset);
334
335 private:
336 uint8_t *data_;
337 size_t readCursor_;
338 size_t writeCursor_;
339 size_t dataSize_;
340 size_t dataCapacity_;
341 size_t maxDataCapacity_;
342 binder_size_t *objectOffsets_;
343 size_t objectCursor_;
344 size_t objectsCapacity_;
345 Allocator *allocator_;
346 std::vector<sptr<Parcelable>> objectHolder_;
347 bool writable_ = true;
348 };
349
350 template <typename T>
WriteObject(const sptr<T> & object)351 bool Parcel::WriteObject(const sptr<T> &object)
352 {
353 if (object == nullptr) {
354 return T::Marshalling(*this, object);
355 }
356 return WriteRemoteObject(object.GetRefPtr());
357 }
358
359 template <typename T>
ReadObject()360 sptr<T> Parcel::ReadObject()
361 {
362 if (!this->CheckOffsets()) {
363 return nullptr;
364 }
365 return T::Unmarshalling(*this);
366 }
367
368 // Read data from the given parcel into this parcelable object.
369 template <typename T>
ReadParcelable()370 T *Parcel::ReadParcelable()
371 {
372 int32_t size = this->ReadInt32();
373 if (size == 0) {
374 return nullptr;
375 }
376
377 return T::Unmarshalling(*this);
378 }
379
380 // Read data from the given parcel into this parcelable object, return sptr.
381 template <typename T>
ReadStrongParcelable()382 sptr<T> Parcel::ReadStrongParcelable()
383 {
384 int32_t size = this->ReadInt32();
385 if (size == 0) {
386 return nullptr;
387 }
388
389 return T::Unmarshalling(*this);
390 }
391
392 } // namespace OHOS
393 #endif
394