Add initial values support - values to feed the shared database on first startup.
[platform/core/security/key-manager.git] / src / manager / initial-values / BufferHandler.cpp
1 /*
2  *  Copyright (c) 2000 - 2015 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        BufferHandler.cpp
18  * @author      Maciej Karpiuk (m.karpiuk2@samsung.com)
19  * @version     1.0
20  * @brief       BufferHandler class implementation.
21  */
22
23 #include <string>
24 #include <algorithm>
25 #include <cctype>
26 #include <BufferHandler.h>
27 #include <xml-utils.h>
28 #include <base64.h>
29
30 namespace CKM {
31 namespace InitialValues {
32
33 BufferHandler::BufferHandler(EncodingType type) : m_encoding(type) {}
34 BufferHandler::~BufferHandler() {}
35
36 void BufferHandler::Start(const XML::Parser::Attributes &)
37 {
38 }
39
40
41 void BufferHandler::Characters(const std::string & data)
42 {
43     m_data.reserve(m_data.size() + data.size());
44     m_data.insert(m_data.end(), data.begin(), data.end());
45 }
46
47 void BufferHandler::End()
48 {
49     switch(m_encoding)
50     {
51         // PEM requires that "----- END" section comes right after "\n" character
52         case PEM:
53         {
54             std::string trimmed = XML::trimEachLine(std::string(m_data.begin(), m_data.end()));
55             m_data = RawBuffer(trimmed.begin(), trimmed.end());
56             break;
57         }
58
59         // Base64 decoder also does not accept any whitespaces
60         case DER:
61         case BASE64:
62         {
63             std::string trimmed = XML::trimEachLine(std::string(m_data.begin(), m_data.end()));
64             Base64Decoder base64;
65             base64.reset();
66             base64.append(RawBuffer(trimmed.begin(), trimmed.end()));
67             base64.finalize();
68             m_data = base64.get();
69             break;
70         }
71
72         default:
73             break;
74     }
75 }
76
77 }
78 }