Replace std::string with CKM::Password
[platform/core/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 <string>
26 #include <vector>
27 #include <list>
28 #include <map>
29 #include <memory>
30
31 #include <dpl/raw-buffer.h>
32 // temporary fix for tizen 2.3
33 #include <ckm/ckm-password.h>
34
35 namespace CKM {
36 // Abstract data stream buffer
37 class IStream
38 {
39   public:
40     virtual void Read(size_t num, void * bytes) = 0;
41     virtual void Write(size_t num, const void * bytes) = 0;
42     virtual ~IStream(){}
43 };
44
45 // Serializable interface
46 class ISerializable
47 {
48   public:
49     /*    ISerializable(){};
50      *    ISerializable(IStream&){}; */
51     virtual void Serialize(IStream &) const = 0;
52     virtual ~ISerializable(){}
53 };
54
55 struct Serialization {
56     // serialization
57     // normal functions
58
59     // ISerializable objects
60     static void Serialize(IStream& stream, const ISerializable& object)
61     {
62         object.Serialize(stream);
63     }
64
65     static void Serialize(IStream& stream, const ISerializable* const object)
66     {
67         object->Serialize(stream);
68     }
69
70     // char
71     static void Serialize(IStream& stream, const char value)
72     {
73         stream.Write(sizeof(value), &value);
74     }
75     static void Serialize(IStream& stream, const char* const value)
76     {
77         stream.Write(sizeof(*value), value);
78     }
79
80     // unsigned char
81     static void Serialize(IStream& stream, const unsigned char value)
82     {
83         stream.Write(sizeof(value), &value);
84     }
85     static void Serialize(IStream& stream, const unsigned char* const value)
86     {
87         stream.Write(sizeof(*value), value);
88     }
89
90     // unsigned int
91     static void Serialize(IStream& stream, const unsigned value)
92     {
93         stream.Write(sizeof(value), &value);
94     }
95     static void Serialize(IStream& stream, const unsigned* const value)
96     {
97         stream.Write(sizeof(*value), value);
98     }
99
100     // int
101     static void Serialize(IStream& stream, const int value)
102     {
103         stream.Write(sizeof(value), &value);
104     }
105     static void Serialize(IStream& stream, const int* const value)
106     {
107         stream.Write(sizeof(*value), value);
108     }
109
110     // bool
111     static void Serialize(IStream& stream, const bool value)
112     {
113         stream.Write(sizeof(value), &value);
114     }
115     static void Serialize(IStream& stream, const bool* const value)
116     {
117         stream.Write(sizeof(*value), value);
118     }
119
120     // time_t
121     static void Serialize(IStream& stream, const time_t value)
122     {
123         stream.Write(sizeof(value), &value);
124     }
125     static void Serialize(IStream& stream, const time_t* const value)
126     {
127         stream.Write(sizeof(*value), value);
128     }
129
130     // std::string
131     static void Serialize(IStream& stream, const std::string& str)
132     {
133         int length = str.size();
134         stream.Write(sizeof(length), &length);
135         stream.Write(length, str.c_str());
136     }
137     static void Serialize(IStream& stream, const std::string* const str)
138     {
139         int length = str->size();
140         stream.Write(sizeof(length), &length);
141         stream.Write(length, str->c_str());
142     }
143
144     // Password
145     static void Serialize(IStream& stream, const Password& str)
146     {
147         int length = str.size();
148         stream.Write(sizeof(length), &length);
149         stream.Write(length, str.c_str());
150     }
151     static void Serialize(IStream& stream, const Password* const str)
152     {
153         int length = str->size();
154         stream.Write(sizeof(length), &length);
155         stream.Write(length, str->c_str());
156     }
157
158     // STL templates
159
160     // std::list
161     template <typename T>
162     static void Serialize(IStream& stream, const std::list<T>& list)
163     {
164         int length = list.size();
165         stream.Write(sizeof(length), &length);
166         for (typename std::list<T>::const_iterator list_iter = list.begin();
167              list_iter != list.end(); list_iter++)
168         {
169             Serialize(stream, *list_iter);
170         }
171     }
172     template <typename T>
173     static void Serialize(IStream& stream, const std::list<T>* const list)
174     {
175         Serialize(stream, *list);
176     }
177
178     // std::vector
179     template <typename T>
180     static void Serialize(IStream& stream, const std::vector<T>& vec)
181     {
182         int length = vec.size();
183         stream.Write(sizeof(length), &length);
184         for (typename std::vector<T>::const_iterator vec_iter = vec.begin();
185              vec_iter != vec.end(); vec_iter++)
186         {
187             Serialize(stream, *vec_iter);
188         }
189     }
190     template <typename T>
191     static void Serialize(IStream& stream, const std::vector<T>* const vec)
192     {
193         Serialize(stream, *vec);
194     }
195
196     // std::pair
197     template <typename A, typename B>
198     static void Serialize(IStream& stream, const std::pair<A, B>& p)
199     {
200         Serialize(stream, p.first);
201         Serialize(stream, p.second);
202     }
203     template <typename A, typename B>
204     static void Serialize(IStream& stream, const std::pair<A, B>* const p)
205     {
206         Serialize(stream, *p);
207     }
208
209     // std::map
210     template <typename K, typename T>
211     static void Serialize(IStream& stream, const std::map<K, T>& map)
212     {
213         int length = map.size();
214         stream.Write(sizeof(length), &length);
215         typename std::map<K, T>::const_iterator it;
216         for (it = map.begin(); it != map.end(); ++it) {
217             Serialize(stream, (*it).first);
218             Serialize(stream, (*it).second);
219         }
220     }
221     template <typename K, typename T>
222     static void Serialize(IStream& stream, const std::map<K, T>* const map)
223     {
224         Serialize(stream, *map);
225     }
226
227     // std::unique_ptr
228     template <typename T>
229     static void Serialize(IStream& stream, const std::unique_ptr<T>& p)
230     {
231         Serialize(stream, *p);
232     }
233
234     static void Serialize(IStream& stream, const RawBuffer& vec)
235     {
236         int length = vec.size();
237         stream.Write(sizeof(length), &length);
238         stream.Write(length, vec.data());
239     }
240
241     static void Serialize(IStream& stream, const RawBuffer* const vec)
242     {
243         Serialize(stream, *vec);
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 = T(stream);
258     }
259     template <typename T>
260     static void Deserialize(IStream& stream, T*& object)
261     {
262         object = new T(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 int
288     static void Deserialize(IStream& stream, unsigned& value)
289     {
290         stream.Read(sizeof(value), &value);
291     }
292     static void Deserialize(IStream& stream, unsigned*& value)
293     {
294         value = new unsigned;
295         stream.Read(sizeof(*value), value);
296     }
297
298     // int
299     static void Deserialize(IStream& stream, int& value)
300     {
301         stream.Read(sizeof(value), &value);
302     }
303     static void Deserialize(IStream& stream, int*& value)
304     {
305         value = new int;
306         stream.Read(sizeof(*value), value);
307     }
308
309     // bool
310     static void Deserialize(IStream& stream, bool& value)
311     {
312         stream.Read(sizeof(value), &value);
313     }
314     static void Deserialize(IStream& stream, bool*& value)
315     {
316         value = new bool;
317         stream.Read(sizeof(*value), value);
318     }
319
320     // time_t
321     static void Deserialize(IStream& stream, time_t& value)
322     {
323         stream.Read(sizeof(value), &value);
324     }
325     static void Deserialize(IStream& stream, time_t*& value)
326     {
327         value = new time_t;
328         stream.Read(sizeof(*value), value);
329     }
330
331     // std::string
332     static void Deserialize(IStream& stream, std::string& str)
333     {
334         int length;
335         stream.Read(sizeof(length), &length);
336         char * buf = new char[length + 1];
337         stream.Read(length, buf);
338         buf[length] = 0;
339         str = std::string(buf);
340         delete[] buf;
341     }
342     static void Deserialize(IStream& stream, std::string*& str)
343     {
344         int length;
345         stream.Read(sizeof(length), &length);
346         char * buf = new char[length + 1];
347         stream.Read(length, buf);
348         buf[length] = 0;
349         str = new std::string(buf);
350         delete[] buf;
351     }
352
353     // Password
354     static void Deserialize(IStream& stream, Password& str)
355     {
356         int length;
357         stream.Read(sizeof(length), &length);
358         char * buf = new char[length + 1];
359         stream.Read(length, buf);
360         buf[length] = 0;
361         str = Password(buf);
362         delete[] buf;
363     }
364     static void Deserialize(IStream& stream, Password*& str)
365     {
366         int length;
367         stream.Read(sizeof(length), &length);
368         char * buf = new char[length + 1];
369         stream.Read(length, buf);
370         buf[length] = 0;
371         str = new Password(buf);
372         delete[] buf;
373     }
374
375     // STL templates
376
377     // std::list
378     template <typename T>
379     static void Deserialize(IStream& stream, std::list<T>& list)
380     {
381         int length;
382         stream.Read(sizeof(length), &length);
383         for (int i = 0; i < length; ++i) {
384             T obj;
385             Deserialize(stream, obj);
386             list.push_back(std::move(obj));
387         }
388     }
389     template <typename T>
390     static void Deserialize(IStream& stream, std::list<T>*& list)
391     {
392         list = new std::list<T>;
393         Deserialize(stream, *list);
394     }
395
396     // std::vector
397     template <typename T>
398     static void Deserialize(IStream& stream, std::vector<T>& vec)
399     {
400         int length;
401         stream.Read(sizeof(length), &length);
402         for (int i = 0; i < length; ++i) {
403             T obj;
404             Deserialize(stream, obj);
405             vec.push_back(std::move(obj));
406         }
407     }
408     template <typename T>
409     static void Deserialize(IStream& stream, std::vector<T>*& vec)
410     {
411         vec = new std::vector<T>;
412         Deserialize(stream, *vec);
413     }
414
415     // std::pair
416     template <typename A, typename B>
417     static void Deserialize(IStream& stream, std::pair<A, B>& p)
418     {
419         Deserialize(stream, p.first);
420         Deserialize(stream, p.second);
421     }
422     template <typename A, typename B>
423     static void Deserialize(IStream& stream, std::pair<A, B>*& p)
424     {
425         p = new std::pair<A, B>;
426         Deserialize(stream, *p);
427     }
428
429     // std::map
430     template <typename K, typename T>
431     static void Deserialize(IStream& stream, std::map<K, T>& map)
432     {
433         int length;
434         stream.Read(sizeof(length), &length);
435         for (int i = 0; i < length; ++i) {
436             K key;
437             T obj;
438             Deserialize(stream, key);
439             Deserialize(stream, obj);
440             map[key] = std::move(obj);
441         }
442     }
443     template <typename K, typename T>
444     static void Deserialize(IStream& stream, std::map<K, T>*& map)
445     {
446         map = new std::map<K, T>;
447         Deserialize(stream, *map);
448     }
449
450     static void Deserialize(IStream& stream, RawBuffer& vec)
451     {
452         int length;
453         stream.Read(sizeof(length), &length);
454         vec.resize(length);
455         stream.Read(length, vec.data());
456     }
457
458     static void Deserialize(IStream& stream, RawBuffer*& vec)
459     {
460         vec = new RawBuffer;
461         Deserialize(stream, *vec);
462     }
463
464 }; // struct Deserialization
465 } // namespace CKM
466
467 #endif // CENT_KEY_SERIALIZATION_H