22d3231076e282f07c1c18810f793363bb66c6b1
[platform/core/security/cynara.git] / src / storage / StorageDeserializer.cpp
1 /*
2  * Copyright (c) 2014 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        src/storage/StorageDeserializer.cpp
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       Implementation for Cynara::StorageDeserializer
21  */
22
23 #include <istream>
24 #include <memory>
25 #include <string>
26
27 #include <exceptions/BucketDeserializationException.h>
28 #include <exceptions/BucketRecordCorruptedException.h>
29 #include <types/PolicyType.h>
30
31 #include <storage/BucketDeserializer.h>
32 #include <storage/Buckets.h>
33 #include <storage/StorageSerializer.h>
34
35 #include "StorageDeserializer.h"
36
37 namespace Cynara {
38
39 StorageDeserializer::StorageDeserializer(std::shared_ptr<std::istream> inStream,
40                                          BucketStreamOpener bucketStreamOpener)
41         : m_inStream(inStream), m_bucketStreamOpener(bucketStreamOpener) {
42 }
43
44 void StorageDeserializer::initBuckets(Buckets &buckets) {
45     buckets.clear();
46
47     for (std::size_t lineNum = 1; !m_inStream->eof(); ++lineNum) {
48         std::string line;
49         std::getline(*m_inStream, line, StorageSerializer::recordSeparator());
50
51         if (line.empty())
52             break;
53
54         std::size_t beginToken = 0;
55         auto bucketId = parseBucketId(line, beginToken);
56         auto policyType = parsePolicyType(line, beginToken);
57         auto metadata = parseMetadata(line, beginToken);
58
59         buckets[bucketId] = PolicyBucket(bucketId, PolicyResult(policyType, metadata));
60     }
61 }
62
63 void StorageDeserializer::loadBuckets(Buckets &buckets) {
64     for (auto &bucketIter : buckets) {
65         const auto &bucketId = bucketIter.first;
66         auto &bucket = bucketIter.second;
67
68         auto bucketDeserializer = m_bucketStreamOpener(bucketId);
69         if (bucketDeserializer != nullptr) {
70             const auto policies = bucketDeserializer->loadPolicies();
71             for (const auto policy : policies) {
72                 bucket.insertPolicy(policy);
73             }
74         } else {
75             throw BucketDeserializationException(bucketId);
76         }
77     }
78 }
79
80 PolicyBucketId StorageDeserializer::parseBucketId(const std::string &line,
81                                                     std::size_t &beginToken) {
82     auto bucketNameEndToken = line.find(StorageSerializer::fieldSeparator(), beginToken);
83     if (bucketNameEndToken != std::string::npos) {
84         auto bucketName = line.substr(beginToken, bucketNameEndToken - beginToken);
85         beginToken = bucketNameEndToken + 1;
86         return bucketName;
87     }
88
89     // TODO: Should throw other exception
90     throw BucketRecordCorruptedException(line);
91 }
92
93 PolicyType StorageDeserializer::parsePolicyType(const std::string &line, std::size_t &beginToken) {
94     PolicyType policyType;
95     try {
96         std::size_t newBegin = 0;
97         policyType = std::stoi(line.substr(beginToken), &newBegin, 16);
98         beginToken += newBegin;
99     } catch (...) {
100         throw BucketRecordCorruptedException(line);
101     }
102
103     return policyType;
104 }
105
106 PolicyResult::PolicyMetadata StorageDeserializer::parseMetadata(const std::string &line,
107                                                                 std::size_t &beginToken) {
108     if (beginToken < line.size()) {
109         auto ret = line.substr(beginToken + 1);
110         beginToken = line.size();
111         return ret;
112     }
113
114     return std::string();
115 }
116
117 } /* namespace Cynara */