Release version 0.9.10
[platform/core/base/bundle.git] / src / bundle-internal.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 "bundle-internal.h"
18
19 #include <errno.h>
20 #include <glib.h>
21
22 #include <cstring>
23
24 #include "include/bundle.h"
25
26 #include "exception-internal.h"
27
28 namespace tizen_base {
29 namespace internal {
30
31 static const int CHECKSUM_LENGTH = 32;
32
33 Bundle::Bundle() = default;
34
35 Bundle::Bundle(unsigned char* raw, int size, bool base64) {
36   int ret;
37   if (base64)
38     ret = Decode(raw, size);
39   else
40     ret = DecodeRaw(raw, size);
41   if (ret != BUNDLE_ERROR_NONE)
42     THROW(ret);
43 }
44
45 Bundle::Bundle(int argc, char** argv) {
46   int ret = Import(argc, argv);
47   if (ret != BUNDLE_ERROR_NONE)
48     THROW(ret);
49 }
50
51 Bundle::~Bundle() = default;
52
53 Bundle::Bundle(const Bundle& b) {
54   map_ = b.map_;
55   list_ = b.list_;
56 }
57
58 Bundle& Bundle::operator = (const Bundle& b) {
59   if (this != &b) {
60     map_ = b.map_;
61     list_ = b.list_;
62   }
63   return *this;
64 }
65
66 Bundle::Bundle(Bundle&& b) noexcept {
67   map_ = std::move(b.map_);
68   list_ = std::move(b.list_);
69 }
70
71 Bundle& Bundle::operator = (Bundle&& b) noexcept {
72   if (this != &b) {
73     map_ = std::move(b.map_);
74     list_ = std::move(b.list_);
75   }
76   return *this;
77 }
78
79 bool Bundle::operator == (const Bundle& b) {
80   if (this == &b)
81     return true;
82
83   if (map_.size() != b.map_.size())
84     return false;
85
86   for (const auto& kv : map_) {
87     auto& lhs = kv.second;
88     auto iter = b.map_.find(lhs->GetKey());
89     if (iter == b.map_.end())
90       return false;
91
92     auto& rhs = iter->second;
93     if (!(lhs == rhs))
94       return false;
95   }
96
97   return true;
98 }
99
100 void Bundle::Remove(const std::string& key) {
101   auto iter = map_.find(key);
102   if (iter == map_.end())
103     THROW(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
104
105   list_.remove(iter->second);
106   map_.erase(iter);
107 }
108
109 void Bundle::Add(std::shared_ptr<KeyInfo> key_info) {
110   auto iter = map_.find(key_info->GetKey());
111   if (iter != map_.end())
112     THROW(BUNDLE_ERROR_KEY_EXISTS);
113
114   map_[key_info->GetKey()] = key_info;
115   list_.push_back(std::move(key_info));
116 }
117
118 void Bundle::Set(const std::string& key, int index,
119     std::vector<unsigned char> value) {
120   auto iter = map_.find(key);
121   if (iter == map_.end())
122     THROW(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
123
124   int ret = iter->second->SetValue(index, std::move(value));
125   if (ret != BUNDLE_ERROR_NONE)
126     THROW(ret);
127 }
128
129 std::shared_ptr<KeyInfo>& Bundle::Get(const std::string& key) {
130   auto iter = map_.find(key.c_str());
131   if (iter == map_.end())
132     THROW(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
133
134   return iter->second;
135 }
136
137 int Bundle::GetSize() {
138   return map_.size();
139 }
140
141 int Bundle::GetType(const std::string& key) {
142   auto iter = map_.find(key);
143   if (iter == map_.end())
144     THROW(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
145
146   return iter->second->GetType();
147 }
148
149 unsigned char* Bundle::Encode() {
150   int size = 0;
151   unsigned char* raw;
152   try {
153     raw = EncodeRaw(&size);
154   } catch (const Exception& e) {
155     THROW(e.GetErrorCode());
156   }
157
158   std::unique_ptr<unsigned char, decltype(std::free)*> raw_ptr(raw, std::free);
159   char* encoded_data = reinterpret_cast<char*>(
160       g_base64_encode(reinterpret_cast<guchar*>(raw),
161         static_cast<gsize>(size)));
162   if (encoded_data == nullptr)
163     THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
164
165   return reinterpret_cast<unsigned char*>(encoded_data);
166 }
167
168 int Bundle::Decode(unsigned char* raw, int size) {
169   unsigned char* d_str = new (std::nothrow) unsigned char[(size / 4) * 3 + 3];
170   if (d_str == nullptr)
171     return BUNDLE_ERROR_OUT_OF_MEMORY;
172
173   std::unique_ptr<unsigned char[]> d_ptr(d_str);
174   gint state = 0;
175   guint save = 0;
176   unsigned int d_len_raw = g_base64_decode_step(reinterpret_cast<char*>(raw),
177       size, d_str, &state, &save);
178   if (d_len_raw < CHECKSUM_LENGTH)
179     return BUNDLE_ERROR_OUT_OF_MEMORY;
180
181   return DecodeRaw(d_str, d_len_raw);
182 }
183
184 unsigned char* Bundle::EncodeRaw(int* size) {
185   std::vector<unsigned char> bytes;
186   for (const auto& key_info : list_) {
187     auto encoded_bytes = key_info->Encode();
188     bytes.insert(bytes.end(), encoded_bytes.begin(), encoded_bytes.end());
189   }
190
191   gchar* checksum = g_compute_checksum_for_string(
192       G_CHECKSUM_MD5, reinterpret_cast<gchar*>(&bytes[0]),
193       static_cast<gssize>(bytes.size()));
194   if (checksum == nullptr)
195     THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
196
197   std::unique_ptr<gchar, decltype(g_free)*> ptr(checksum, g_free);
198   unsigned char* p = reinterpret_cast<unsigned char*>(checksum);
199   bytes.insert(bytes.begin(), p, p + CHECKSUM_LENGTH);
200
201   unsigned char* raw = static_cast<unsigned char*>(malloc(bytes.size()));
202   if (raw == nullptr)
203     THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
204
205   std::copy(bytes.begin(), bytes.end(), raw);
206   *size = static_cast<int>(bytes.size());
207   return raw;
208 }
209
210 int Bundle::DecodeRaw(unsigned char* raw, int size) {
211   char* extract_checksum = new (std::nothrow) char[CHECKSUM_LENGTH + 1];
212   if (extract_checksum == nullptr)
213     return BUNDLE_ERROR_OUT_OF_MEMORY;
214
215   std::unique_ptr<char[]> extract_ptr(extract_checksum);
216   unsigned char* d_str = raw;
217   unsigned int d_len_raw = size;
218   strncpy(extract_checksum, reinterpret_cast<char*>(d_str), CHECKSUM_LENGTH);
219   extract_checksum[CHECKSUM_LENGTH] = '\0';
220
221   gchar* compute_checksum = g_compute_checksum_for_string(G_CHECKSUM_MD5,
222       reinterpret_cast<gchar*>(d_str + CHECKSUM_LENGTH),
223       d_len_raw - CHECKSUM_LENGTH);
224   if (compute_checksum == nullptr)
225     return BUNDLE_ERROR_OUT_OF_MEMORY;
226
227   std::unique_ptr<gchar, decltype(g_free)*> compute_ptr(compute_checksum,
228       g_free);
229   if (strcmp(extract_checksum, static_cast<char*>(compute_checksum)) != 0)
230     return BUNDLE_ERROR_INVALID_PARAMETER;
231
232   unsigned char* d_r = d_str + CHECKSUM_LENGTH;
233   unsigned int d_len = d_len_raw - CHECKSUM_LENGTH;
234
235   unsigned int reader = 0;
236   std::vector<unsigned char> bytes(d_r, d_r + d_len);
237
238   while (reader < bytes.size()) {
239     std::size_t total_size = -1;
240     unsigned char* p = reinterpret_cast<unsigned char*>(&total_size);
241     std::copy(&bytes[reader], &bytes[reader] + sizeof(total_size), p);
242
243     std::vector<unsigned char> encoded_bytes;
244     std::copy(&bytes[reader], &bytes[reader] + total_size,
245         std::back_inserter(encoded_bytes));
246     reader += total_size;
247
248     KeyInfo* new_key_info;
249     try {
250       new_key_info = new KeyInfo(std::move(encoded_bytes));
251     } catch (const Exception& e) {
252       return e.GetErrorCode();
253     } catch (const std::bad_alloc& ba) {
254       return BUNDLE_ERROR_OUT_OF_MEMORY;
255     }
256
257     auto key_info = std::shared_ptr<KeyInfo>(new_key_info);
258     auto iter = map_.find(key_info->GetKey());
259     if (iter != map_.end())
260       continue;
261
262     map_[key_info->GetKey()] = key_info;
263     list_.push_back(std::move(key_info));
264   }
265
266   return BUNDLE_ERROR_NONE;
267 }
268
269 const std::unordered_map<std::string, std::shared_ptr<KeyInfo>>&
270 Bundle::GetMap() const {
271   return map_;
272 }
273
274 std::vector<std::string> Bundle::Export() {
275   std::vector<std::string> argv(2);
276   for (const auto& key_info : list_) {
277     argv.push_back(key_info->GetKey());
278
279     auto encoded_bytes = key_info->Encode();
280     auto* p = reinterpret_cast<unsigned char*>(&encoded_bytes[0]);
281     auto* base64_bytes = g_base64_encode(p, encoded_bytes.size());
282     if (base64_bytes == nullptr)
283       THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
284
285     std::unique_ptr<gchar, decltype(g_free)*> base64_bytes_ptr(base64_bytes,
286         g_free);
287     argv.push_back(std::move(base64_bytes));
288   }
289
290   return argv;
291 }
292
293 int Bundle::Import(int argc, char** argv) {
294   if (argc < 2)
295     return BUNDLE_ERROR_NONE;
296
297   if (!argv[1] || std::strcmp(argv[1], TAG_IMPORT_EXPORT_CHECK)) {
298     for (int idx = 1; idx + 1 < argc; idx += 2) {
299       auto* p = reinterpret_cast<unsigned char*>(argv[idx +1]);
300       auto len = strlen(argv[idx + 1]) + 1;
301       std::vector<unsigned char> value;
302       std::copy(p, p + len, std::back_inserter(value));
303
304       KeyInfo* new_key_info;
305       try {
306         new_key_info = new KeyInfo(Type::String, argv[idx], std::move(value));
307       } catch (const Exception& e) {
308         return e.GetErrorCode();
309       } catch (const std::bad_alloc& ba) {
310         return BUNDLE_ERROR_OUT_OF_MEMORY;
311       }
312
313       auto key_info = std::shared_ptr<KeyInfo>(new_key_info);
314       auto iter = map_.find(key_info->GetKey());
315       if (iter != map_.end())
316         continue;
317
318       map_[key_info->GetKey()] = key_info;
319       list_.push_back(key_info);
320     }
321     return BUNDLE_ERROR_NONE;
322   }
323
324   for (int idx = 2; idx + 1 < argc; idx += 2) {
325     gsize out_len = 0;
326     auto* bytes = g_base64_decode(argv[idx + 1], &out_len);
327     if (bytes == nullptr)
328       return BUNDLE_ERROR_OUT_OF_MEMORY;
329
330     std::unique_ptr<guchar, decltype(g_free)*> bytes_ptr(bytes, g_free);
331
332     if (out_len < sizeof(std::size_t))
333       continue;
334
335     auto* p = reinterpret_cast<unsigned char*>(bytes);
336     std::vector<unsigned char> decoded_bytes(p, p + (out_len + 1));
337
338     KeyInfo* new_key_info;
339     try {
340       new_key_info = new KeyInfo(std::move(decoded_bytes));
341     } catch (const Exception& e) {
342       return e.GetErrorCode();
343     } catch (const std::bad_alloc& ba) {
344       return BUNDLE_ERROR_OUT_OF_MEMORY;
345     }
346
347     auto key_info = std::shared_ptr<KeyInfo>(new_key_info);
348     auto iter = map_.find(key_info->GetKey());
349     if (iter != map_.end())
350       continue;
351
352     map_[key_info->GetKey()] = key_info;
353     list_.push_back(std::move(key_info));
354   }
355
356   return BUNDLE_ERROR_NONE;
357 }
358
359 }  // namespace internal
360 }  // namespace tizen_base