Add Parcel Library
[platform/core/base/bundle.git] / parcel / stub.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "parcel/api/parcel.h"
18 #include "parcel/common.hh"
19 #include "parcel/log_private.hh"
20 #include "parcel/parcel.hh"
21 #include "parcel/parcelable.hh"
22
23 using namespace tizen_base;
24
25 extern "C" EXPORT int parcel_create(parcel_h *parcel) {
26   if (parcel == nullptr) {
27     _E("Invalid parameter");
28     return PARCEL_ERROR_INVALID_PARAMETER;
29   }
30
31   auto* h = new (std::nothrow) Parcel();
32   if (h == nullptr) {
33     _E("Out of memory");
34     return PARCEL_ERROR_OUT_OF_MEMORY;
35   }
36
37   *parcel = static_cast<parcel_h>(h);
38   return PARCEL_ERROR_NONE;
39 }
40
41 extern "C" EXPORT int parcel_destroy(parcel_h parcel) {
42   if (parcel == nullptr) {
43     _E("Invalid parameter");
44     return PARCEL_ERROR_INVALID_PARAMETER;
45   }
46
47   auto* h = static_cast<Parcel*>(parcel);
48   delete h;
49   return PARCEL_ERROR_NONE;
50 }
51
52 extern "C" EXPORT int parcel_clone(parcel_h parcel, parcel_h* clone) {
53   if (parcel == nullptr || clone == nullptr) {
54     _E("Invalid parameter");
55     return PARCEL_ERROR_INVALID_PARAMETER;
56   }
57
58   auto* h = static_cast<Parcel*>(parcel);
59   auto* c = new (std::nothrow) Parcel(*h);
60   if (c == nullptr) {
61     _E("Out of memory");
62     return PARCEL_ERROR_OUT_OF_MEMORY;
63   }
64
65   *clone = static_cast<parcel_h>(c);
66   return PARCEL_ERROR_NONE;
67 }
68
69 extern "C" EXPORT int parcel_burst_write(parcel_h parcel, const void *buf,
70     uint32_t size) {
71   if (parcel == nullptr || buf == nullptr || size == 0) {
72     _E("Invalid parameter");
73     return PARCEL_ERROR_INVALID_PARAMETER;
74   }
75
76   auto* h = static_cast<Parcel*>(parcel);
77   h->Write(buf, size);
78   return PARCEL_ERROR_NONE;
79 }
80
81 extern "C" EXPORT int parcel_burst_read(parcel_h parcel, void *buf,
82     uint32_t size) {
83   if (parcel == nullptr || buf == nullptr || size == 0) {
84     _E("Invalid parameter");
85     return PARCEL_ERROR_INVALID_PARAMETER;
86   }
87
88   auto* h = static_cast<Parcel*>(parcel);
89   h->Read(buf, size);
90   return PARCEL_ERROR_NONE;
91 }
92
93 extern "C" EXPORT int parcel_write_bool(parcel_h parcel, bool val) {
94   if (parcel == nullptr) {
95     _E("Invalid parameter");
96     return PARCEL_ERROR_INVALID_PARAMETER;
97   }
98
99   auto* h = static_cast<Parcel*>(parcel);
100   h->WriteBool(val);
101   return PARCEL_ERROR_NONE;
102 }
103
104 extern "C" EXPORT int parcel_write_byte(parcel_h parcel, char val) {
105   if (parcel == nullptr) {
106     _E("Invalid parameter");
107     return PARCEL_ERROR_INVALID_PARAMETER;
108   }
109
110   auto* h = static_cast<Parcel*>(parcel);
111   h->WriteByte(val);
112   return PARCEL_ERROR_NONE;
113 }
114
115 extern "C" EXPORT int parcel_write_uint16(parcel_h parcel, uint16_t val) {
116   if (parcel == nullptr) {
117     _E("Invalid parameter");
118     return PARCEL_ERROR_INVALID_PARAMETER;
119   }
120
121   auto* h = static_cast<Parcel*>(parcel);
122   h->WriteUInt16(val);
123   return PARCEL_ERROR_NONE;
124 }
125
126 extern "C" EXPORT int parcel_write_uint32(parcel_h parcel, uint32_t val) {
127   if (parcel == nullptr) {
128     _E("Invalid parameter");
129     return PARCEL_ERROR_INVALID_PARAMETER;
130   }
131
132   auto* h = static_cast<Parcel*>(parcel);
133   h->WriteUInt32(val);
134   return PARCEL_ERROR_NONE;
135 }
136
137 extern "C" EXPORT int parcel_write_uint64(parcel_h parcel, uint64_t val) {
138   if (parcel == nullptr) {
139     _E("Invalid parameter");
140     return PARCEL_ERROR_INVALID_PARAMETER;
141   }
142
143   auto* h = static_cast<Parcel*>(parcel);
144   h->WriteUInt64(val);
145   return PARCEL_ERROR_NONE;
146 }
147
148 extern "C" EXPORT int parcel_write_int16(parcel_h parcel, int16_t val) {
149   if (parcel == nullptr) {
150     _E("Invalid parameter");
151     return PARCEL_ERROR_INVALID_PARAMETER;
152   }
153
154   auto* h = static_cast<Parcel*>(parcel);
155   h->WriteInt16(val);
156   return PARCEL_ERROR_NONE;
157 }
158
159 extern "C" EXPORT int parcel_write_int32(parcel_h parcel, int32_t val) {
160   if (parcel == nullptr) {
161     _E("Invalid parameter");
162     return PARCEL_ERROR_INVALID_PARAMETER;
163   }
164
165   auto* h = static_cast<Parcel*>(parcel);
166   h->WriteInt32(val);
167   return PARCEL_ERROR_NONE;
168 }
169
170 extern "C" EXPORT int parcel_write_int64(parcel_h parcel, int64_t val) {
171   if (parcel == nullptr) {
172     _E("Invalid parameter");
173     return PARCEL_ERROR_INVALID_PARAMETER;
174   }
175
176   auto* h = static_cast<Parcel*>(parcel);
177   h->WriteInt64(val);
178   return PARCEL_ERROR_NONE;
179 }
180
181 extern "C" EXPORT int parcel_write_float(parcel_h parcel, float val) {
182   if (parcel == nullptr) {
183     _E("Invalid parameter");
184     return PARCEL_ERROR_INVALID_PARAMETER;
185   }
186
187   auto* h = static_cast<Parcel*>(parcel);
188   h->WriteFloat(val);
189   return PARCEL_ERROR_NONE;
190 }
191
192 extern "C" EXPORT int parcel_write_double(parcel_h parcel, double val) {
193   if (parcel == nullptr) {
194     _E("Invalid parameter");
195     return PARCEL_ERROR_INVALID_PARAMETER;
196   }
197
198   auto* h = static_cast<Parcel*>(parcel);
199   h->WriteDouble(val);
200   return PARCEL_ERROR_NONE;
201 }
202
203 extern "C" EXPORT int parcel_write_string(parcel_h parcel, const char* str) {
204   if (parcel == nullptr || str == nullptr) {
205     _E("Invalid parameter");
206     return PARCEL_ERROR_INVALID_PARAMETER;
207   }
208
209   auto* h = static_cast<Parcel*>(parcel);
210   h->WriteCString(str);
211   return PARCEL_ERROR_NONE;
212 }
213
214 extern "C" EXPORT int parcel_write_bundle(parcel_h parcel, bundle* b) {
215   if (parcel == nullptr || b == nullptr) {
216     _E("Invalid parameter");
217     return PARCEL_ERROR_INVALID_PARAMETER;
218   }
219
220   auto* h = static_cast<Parcel*>(parcel);
221   h->WriteCBundle(b);
222   return PARCEL_ERROR_NONE;
223 }
224
225 extern "C" EXPORT int parcel_read_bool(parcel_h parcel, bool* val) {
226   if (parcel == nullptr || val == nullptr) {
227     _E("Invalid parameter");
228     return PARCEL_ERROR_INVALID_PARAMETER;
229   }
230
231   auto* h = static_cast<Parcel*>(parcel);
232   return h->ReadBool(val);
233 }
234
235 extern "C" EXPORT int parcel_read_byte(parcel_h parcel, char* val) {
236   if (parcel == nullptr || val == nullptr) {
237     _E("Invalid parameter");
238     return PARCEL_ERROR_INVALID_PARAMETER;
239   }
240
241   auto* h = static_cast<Parcel*>(parcel);
242   return h->ReadByte(val);
243 }
244
245 extern "C" EXPORT int parcel_read_uint16(parcel_h parcel, uint16_t* val) {
246   if (parcel == nullptr || val == nullptr) {
247     _E("Invalid parameter");
248     return PARCEL_ERROR_INVALID_PARAMETER;
249   }
250
251   auto* h = static_cast<Parcel*>(parcel);
252   return h->ReadUInt16(val);
253 }
254
255 extern "C" EXPORT int parcel_read_uint32(parcel_h parcel, uint32_t* val) {
256   if (parcel == nullptr || val == nullptr) {
257     _E("Invalid parameter");
258     return PARCEL_ERROR_INVALID_PARAMETER;
259   }
260
261   auto* h = static_cast<Parcel*>(parcel);
262   return h->ReadUInt32(val);
263 }
264
265 extern "C" EXPORT int parcel_read_uint64(parcel_h parcel, uint64_t* val) {
266   if (parcel == nullptr || val == nullptr) {
267     _E("Invalid parameter");
268     return PARCEL_ERROR_INVALID_PARAMETER;
269   }
270
271   auto* h = static_cast<Parcel*>(parcel);
272   return h->ReadUInt64(val);
273 }
274
275 extern "C" EXPORT int parcel_read_int16(parcel_h parcel, int16_t* val) {
276   if (parcel == nullptr || val == nullptr) {
277     _E("Invalid parameter");
278     return PARCEL_ERROR_INVALID_PARAMETER;
279   }
280
281   auto* h = static_cast<Parcel*>(parcel);
282   return h->ReadInt16(val);
283 }
284
285 extern "C" EXPORT int parcel_read_int32(parcel_h parcel, int32_t* val) {
286   if (parcel == nullptr || val == nullptr) {
287     _E("Invalid parameter");
288     return PARCEL_ERROR_INVALID_PARAMETER;
289   }
290
291   auto* h = static_cast<Parcel*>(parcel);
292   return h->ReadInt32(val);
293 }
294
295 extern "C" EXPORT int parcel_read_int64(parcel_h parcel, int64_t* val) {
296   if (parcel == nullptr || val == nullptr) {
297     _E("Invalid parameter");
298     return PARCEL_ERROR_INVALID_PARAMETER;
299   }
300
301   auto* h = static_cast<Parcel*>(parcel);
302   return h->ReadInt64(val);
303 }
304
305 extern "C" EXPORT int parcel_read_float(parcel_h parcel, float* val) {
306   if (parcel == nullptr || val == nullptr) {
307     _E("Invalid parameter");
308     return PARCEL_ERROR_INVALID_PARAMETER;
309   }
310
311   auto* h = static_cast<Parcel*>(parcel);
312   return h->ReadFloat(val);
313 }
314
315 extern "C" EXPORT int parcel_read_double(parcel_h parcel, double* val) {
316   if (parcel == nullptr || val == nullptr) {
317     _E("Invalid parameter");
318     return PARCEL_ERROR_INVALID_PARAMETER;
319   }
320
321   auto* h = static_cast<Parcel*>(parcel);
322   return h->ReadDouble(val);
323 }
324
325 extern "C" EXPORT int parcel_read_string(parcel_h parcel, char** str) {
326   if (parcel == nullptr || str == nullptr) {
327     _E("Invalid parameter");
328     return PARCEL_ERROR_INVALID_PARAMETER;
329   }
330
331   auto* h = static_cast<Parcel*>(parcel);
332   return h->ReadCString(str);
333 }
334
335 extern "C" EXPORT int parcel_read_bundle(parcel_h parcel, bundle** b) {
336   if (parcel == nullptr || b == nullptr) {
337     _E("Invalid parameter");
338     return PARCEL_ERROR_INVALID_PARAMETER;
339   }
340
341   auto* h = static_cast<Parcel*>(parcel);
342   return h->ReadCBundle(b);
343 }
344
345 extern "C" EXPORT int parcel_reset_reader(parcel_h parcel) {
346   if (parcel == nullptr) {
347     _E("Invalid parameter");
348     return PARCEL_ERROR_INVALID_PARAMETER;
349   }
350
351   auto* h = static_cast<Parcel*>(parcel);
352   h->ResetReader();
353   return PARCEL_ERROR_NONE;
354 }
355
356 extern "C" EXPORT int parcel_clear(parcel_h parcel) {
357   if (parcel == nullptr) {
358     _E("Invalid parameter");
359     return PARCEL_ERROR_INVALID_PARAMETER;
360   }
361
362   auto* h = static_cast<Parcel*>(parcel);
363   h->Clear();
364   return PARCEL_ERROR_NONE;
365 }
366
367 extern "C" EXPORT int parcel_reset(parcel_h parcel, const void* buf,
368     uint32_t size) {
369   if (parcel == nullptr || buf == nullptr || size == 0) {
370     _E("Invalid parameter");
371     return PARCEL_ERROR_INVALID_PARAMETER;
372   }
373
374   auto* h = static_cast<Parcel*>(parcel);
375   h->Reset(buf, size);
376   return PARCEL_ERROR_NONE;
377 }
378
379 extern "C" EXPORT int parcel_write(parcel_h parcel, parcelable_t* parcelable,
380     void* data) {
381   if (parcel == nullptr || parcelable == nullptr || parcelable->to == nullptr) {
382     _E("Invalid parameter");
383     return PARCEL_ERROR_INVALID_PARAMETER;
384   }
385
386   parcelable->to(parcel, data);
387   return PARCEL_ERROR_NONE;
388 }
389
390 extern "C" EXPORT int parcel_read(parcel_h parcel, parcelable_t* parcelable,
391     void* data) {
392   if (parcel == nullptr || parcelable == nullptr ||
393       parcelable->from == nullptr) {
394     _E("Invalid parameter");
395     return PARCEL_ERROR_INVALID_PARAMETER;
396   }
397
398   parcelable->from(parcel, data);
399   return PARCEL_ERROR_NONE;
400 }
401
402 extern "C" EXPORT int parcel_get_raw(parcel_h parcel, void** raw,
403     uint32_t* size) {
404   if (parcel == nullptr || raw == nullptr || size == nullptr) {
405     _E("Invalid parameter");
406     return PARCEL_ERROR_INVALID_PARAMETER;
407   }
408
409   auto* h = static_cast<Parcel*>(parcel);
410   auto& raw_data = h->GetRaw();
411   *raw = reinterpret_cast<void*>(const_cast<uint8_t*>(&raw_data[0]));
412   *size = raw_data.size();
413   return PARCEL_ERROR_NONE;
414 }