tizen 2.4 release
[framework/security/key-manager.git] / src / manager / dpl / core / include / dpl / serialization.h
1 /*
2  * Copyright (c) 2011 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  * @file    serialization.h
18  * @author  Tomasz Swierczek (t.swierczek@samsung.com)
19  * @version 1.0
20  * @brief   Interfaces and templates used for data serialization.
21  */
22 #ifndef CENT_KEY_SERIALIZATION_H
23 #define CENT_KEY_SERIALIZATION_H
24
25 #include <stdint.h>
26
27 #include <string>
28 #include <vector>
29 #include <list>
30 #include <map>
31 #include <memory>
32
33 namespace CKM {
34 // Abstract data stream buffer
35 class IStream
36 {
37   public:
38     virtual void Read(size_t num, void * bytes) = 0;
39     virtual void Write(size_t num, const void * bytes) = 0;
40     virtual ~IStream(){}
41 };
42
43 // Serializable interface
44 class ISerializable
45 {
46   public:
47     /*    ISerializable(){};
48      *    ISerializable(IStream&){}; */
49     virtual void Serialize(IStream &) const = 0;
50     virtual void Deserialize(IStream &) = 0;
51     virtual ~ISerializable(){}
52 };
53
54 struct Serialization {
55     // serialization
56     // normal functions
57
58     // ISerializable objects
59     static void Serialize(IStream& stream, const ISerializable& object)
60     {
61         object.Serialize(stream);
62     }
63
64     static void Serialize(IStream& stream, const ISerializable* const object)
65     {
66         object->Serialize(stream);
67     }
68
69     // char
70     static void Serialize(IStream& stream, const char value)
71     {
72         stream.Write(sizeof(value), &value);
73     }
74     static void Serialize(IStream& stream, const char* const value)
75     {
76         stream.Write(sizeof(*value), value);
77     }
78
79     // unsigned char
80     static void Serialize(IStream& stream, const unsigned char value)
81     {
82         stream.Write(sizeof(value), &value);
83     }
84     static void Serialize(IStream& stream, const unsigned char* const value)
85     {
86         stream.Write(sizeof(*value), value);
87     }
88
89     // unsigned int32
90     static void Serialize(IStream& stream, const uint32_t value)
91     {
92         stream.Write(sizeof(value), &value);
93     }
94     static void Serialize(IStream& stream, const uint32_t* const value)
95     {
96         stream.Write(sizeof(*value), value);
97     }
98
99     // int32
100     static void Serialize(IStream& stream, const int32_t value)
101     {
102         stream.Write(sizeof(value), &value);
103     }
104     static void Serialize(IStream& stream, const int32_t* const value)
105     {
106         stream.Write(sizeof(*value), value);
107     }
108
109     // unsigned int64
110     static void Serialize(IStream& stream, const uint64_t value)
111     {
112         stream.Write(sizeof(value), &value);
113     }
114     static void Serialize(IStream& stream, const uint64_t* const value)
115     {
116         stream.Write(sizeof(*value), value);
117     }
118
119     // int64
120     static void Serialize(IStream& stream, const int64_t value)
121     {
122         stream.Write(sizeof(value), &value);
123     }
124     static void Serialize(IStream& stream, const int64_t* const value)
125     {
126         stream.Write(sizeof(*value), value);
127     }
128
129     // bool
130     static void Serialize(IStream& stream, const bool value)
131     {
132         stream.Write(sizeof(value), &value);
133     }
134     static void Serialize(IStream& stream, const bool* const value)
135     {
136         stream.Write(sizeof(*value), value);
137     }
138
139     // std::string
140     template <typename T, typename R, typename A>
141     static void Serialize(IStream& stream, const std::basic_string<T,R,A>& str)
142     {
143         int length = str.size();
144         stream.Write(sizeof(length), &length);
145         stream.Write(length*sizeof(T), str.data());
146     }
147
148     template<typename T, typename R, typename A>
149     static void Serialize(IStream& stream, const std::basic_string<T,R,A>* const str)
150     {
151         int length = str->size();
152         stream.Write(sizeof(length), &length);
153         stream.Write(length*sizeof(T), str->data());
154     }
155
156     // STL templates
157
158     // std::list
159     template <typename T>
160     static void Serialize(IStream& stream, const std::list<T>& list)
161     {
162         int length = list.size();
163         stream.Write(sizeof(length), &length);
164         for (typename std::list<T>::const_iterator list_iter = list.begin();
165              list_iter != list.end(); list_iter++)
166         {
167             Serialize(stream, *list_iter);
168         }
169     }
170     template <typename T>
171     static void Serialize(IStream& stream, const std::list<T>* const list)
172     {
173         Serialize(stream, *list);
174     }
175
176     // RawBuffer
177     template <typename A>
178     static void Serialize(IStream& stream, const std::vector<unsigned char, A>& vec)
179     {
180         int length = vec.size();
181         stream.Write(sizeof(length), &length);
182         stream.Write(length, vec.data());
183     }
184
185     template <typename A>
186     static void Serialize(IStream& stream, const std::vector<unsigned char, A>* const vec)
187     {
188         Serialize(stream, *vec);
189     }
190
191     // std::vector
192     template <typename T, typename A>
193     static void Serialize(IStream& stream, const std::vector<T, A>& vec)
194     {
195         int length = vec.size();
196         stream.Write(sizeof(length), &length);
197         for (const auto &i : vec)
198         {
199             Serialize(stream, i);
200         }
201     }
202     template <typename T, typename A>
203     static void Serialize(IStream& stream, const std::vector<T, A>* const vec)
204     {
205         Serialize(stream, *vec);
206     }
207
208     // std::pair
209     template <typename A, typename B>
210     static void Serialize(IStream& stream, const std::pair<A, B>& p)
211     {
212         Serialize(stream, p.first);
213         Serialize(stream, p.second);
214     }
215     template <typename A, typename B>
216     static void Serialize(IStream& stream, const std::pair<A, B>* const p)
217     {
218         Serialize(stream, *p);
219     }
220
221     // std::map
222     template <typename K, typename T>
223     static void Serialize(IStream& stream, const std::map<K, T>& map)
224     {
225         int length = map.size();
226         stream.Write(sizeof(length), &length);
227         typename std::map<K, T>::const_iterator it;
228         for (it = map.begin(); it != map.end(); ++it) {
229             Serialize(stream, (*it).first);
230             Serialize(stream, (*it).second);
231         }
232     }
233     template <typename K, typename T>
234     static void Serialize(IStream& stream, const std::map<K, T>* const map)
235     {
236         Serialize(stream, *map);
237     }
238
239     // std::unique_ptr
240     template <typename T>
241     static void Serialize(IStream& stream, const std::unique_ptr<T>& p)
242     {
243         Serialize(stream, *p);
244     }
245
246 }; // struct Serialization
247
248 struct Deserialization {
249     // deserialization
250     // normal functions
251
252     // ISerializable objects
253     // T instead of ISerializable is needed to call proper constructor
254     template <typename T>
255     static void Deserialize(IStream& stream, T& object)
256     {
257         object.Deserialize(stream);
258     }
259     template <typename T>
260     static void Deserialize(IStream& stream, T*& object)
261     {
262         object->Deserialize(stream);
263     }
264
265     // char
266     static void Deserialize(IStream& stream, char& value)
267     {
268         stream.Read(sizeof(value), &value);
269     }
270     static void Deserialize(IStream& stream, char*& value)
271     {
272         value = new char;
273         stream.Read(sizeof(*value), value);
274     }
275
276     // unsigned char
277     static void Deserialize(IStream& stream, unsigned char& value)
278     {
279         stream.Read(sizeof(value), &value);
280     }
281     static void Deserialize(IStream& stream, unsigned char*& value)
282     {
283         value = new unsigned char;
284         stream.Read(sizeof(*value), value);
285     }
286
287     // unsigned int32
288     static void Deserialize(IStream& stream, uint32_t& value)
289     {
290         stream.Read(sizeof(value), &value);
291     }
292     static void Deserialize(IStream& stream, uint32_t*& value)
293     {
294         value = new uint32_t;
295         stream.Read(sizeof(*value), value);
296     }
297
298     // int32
299     static void Deserialize(IStream& stream, int32_t& value)
300     {
301         stream.Read(sizeof(value), &value);
302     }
303     static void Deserialize(IStream& stream, int32_t*& value)
304     {
305         value = new int32_t;
306         stream.Read(sizeof(*value), value);
307     }
308
309     // unsigned int64
310     static void Deserialize(IStream& stream, uint64_t& value)
311     {
312         stream.Read(sizeof(value), &value);
313     }
314     static void Deserialize(IStream& stream, uint64_t*& value)
315     {
316         value = new uint64_t;
317         stream.Read(sizeof(*value), value);
318     }
319
320     // int64
321     static void Deserialize(IStream& stream, int64_t& value)
322     {
323         stream.Read(sizeof(value), &value);
324     }
325     static void Deserialize(IStream& stream, int64_t*& value)
326     {
327         value = new int64_t;
328         stream.Read(sizeof(*value), value);
329     }
330
331     // bool
332     static void Deserialize(IStream& stream, bool& value)
333     {
334         stream.Read(sizeof(value), &value);
335     }
336     static void Deserialize(IStream& stream, bool*& value)
337     {
338         value = new bool;
339         stream.Read(sizeof(*value), value);
340     }
341
342     template <typename T, typename R, typename A>
343     static void Deserialize(IStream& stream, std::basic_string<T,R,A>& str)
344     {
345         int length;
346         stream.Read(sizeof(length), &length);
347         std::vector<T> buf(length);
348         stream.Read(length*sizeof(T), buf.data());
349         str = std::basic_string<T,R,A>(buf.data(), buf.data()+length);
350     }
351
352     template <typename T, typename R, typename A>
353     static void Deserialize(IStream& stream, std::basic_string<T,R,A>*& str)
354     {
355         int length;
356         stream.Read(sizeof(length), &length);
357         std::vector<T> buf(length);
358         stream.Read(length*sizeof(T), buf.data());
359         str = new std::basic_string<T,R,A>(buf.data(), buf.data()+length);
360     }
361
362     // STL templates
363
364     // std::list
365     template <typename T>
366     static void Deserialize(IStream& stream, std::list<T>& list)
367     {
368         int length;
369         stream.Read(sizeof(length), &length);
370         for (int i = 0; i < length; ++i) {
371             T obj;
372             Deserialize(stream, obj);
373             list.push_back(std::move(obj));
374         }
375     }
376     template <typename T>
377     static void Deserialize(IStream& stream, std::list<T>*& list)
378     {
379         list = new std::list<T>;
380         Deserialize(stream, *list);
381     }
382
383     // RawBuffer
384     template <typename A>
385     static void Deserialize(IStream& stream, std::vector<unsigned char, A>& vec)
386     {
387         int length;
388         stream.Read(sizeof(length), &length);
389         vec.resize(length);
390         stream.Read(length, vec.data());
391     }
392
393     template <typename A>
394     static void Deserialize(IStream& stream, std::vector<unsigned char, A>*& vec)
395     {
396         vec = new std::vector<unsigned char,A>;
397         Deserialize(stream, *vec);
398     }
399
400     // std::vector
401     template <typename T, typename A>
402     static void Deserialize(IStream& stream, std::vector<T,A>& vec)
403     {
404         int length;
405         stream.Read(sizeof(length), &length);
406         for (int i = 0; i < length; ++i) {
407             T obj;
408             Deserialize(stream, obj);
409             vec.push_back(std::move(obj));
410         }
411     }
412     template <typename T, typename A>
413     static void Deserialize(IStream& stream, std::vector<T,A>*& vec)
414     {
415         vec = new std::vector<T,A>;
416         Deserialize(stream, *vec);
417     }
418
419     // std::pair
420     template <typename A, typename B>
421     static void Deserialize(IStream& stream, std::pair<A, B>& p)
422     {
423         Deserialize(stream, p.first);
424         Deserialize(stream, p.second);
425     }
426     template <typename A, typename B>
427     static void Deserialize(IStream& stream, std::pair<A, B>*& p)
428     {
429         p = new std::pair<A, B>;
430         Deserialize(stream, *p);
431     }
432
433     // std::map
434     template <typename K, typename T>
435     static void Deserialize(IStream& stream, std::map<K, T>& map)
436     {
437         int length;
438         stream.Read(sizeof(length), &length);
439         for (int i = 0; i < length; ++i) {
440             K key;
441             T obj;
442             Deserialize(stream, key);
443             Deserialize(stream, obj);
444             map[key] = std::move(obj);
445         }
446     }
447     template <typename K, typename T>
448     static void Deserialize(IStream& stream, std::map<K, T>*& map)
449     {
450         map = new std::map<K, T>;
451         Deserialize(stream, *map);
452     }
453 }; // struct Deserialization
454
455 // generic serialization
456 template <typename... Args>
457 struct Serializer;
458
459 template <typename First, typename... Args>
460 struct Serializer<First, Args...> : public Serializer<Args...> {
461     static void Serialize(IStream& stream, const First& f, const Args&... args) {
462         Serialization::Serialize(stream, f);
463         Serializer<Args...>::Serialize(stream, args...);
464     }
465 };
466
467 // end of recursion
468 template <>
469 struct Serializer<> {
470     static void Serialize(IStream&) {
471         return;
472     }
473 };
474
475 // generic deserialization
476 template <typename... Args>
477 struct Deserializer;
478
479 template <typename First, typename... Args>
480 struct Deserializer<First, Args...> : public Deserializer<Args...> {
481     static void Deserialize(IStream& stream, First& f, Args&... args) {
482         Deserialization::Deserialize(stream, f);
483         Deserializer<Args...>::Deserialize(stream, args...);
484     }
485 };
486
487 // end of recursion
488 template <>
489 struct Deserializer<> {
490     static void Deserialize(IStream&) {
491         return;
492     }
493 };
494
495 } // namespace CKM
496
497 #endif // CENT_KEY_SERIALIZATION_H