| 1 | // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer in the |
| 10 | // documentation and/or other materials provided with the distribution. |
| 11 | // * Neither the name of the Andrey N. Sabelnikov nor the |
| 12 | // names of its contributors may be used to endorse or promote products |
| 13 | // derived from this software without specific prior written permission. |
| 14 | // |
| 15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY |
| 19 | // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | // |
| 26 | |
| 27 | #include "byte_slice.h" |
| 28 | #include "byte_stream.h" |
| 29 | #include "misc_log_ex.h" |
| 30 | #include "span.h" |
| 31 | #include "storages/portable_storage.h" |
| 32 | #include "storages/portable_storage_to_json.h" |
| 33 | #include "storages/portable_storage_from_json.h" |
| 34 | #include "storages/portable_storage_to_bin.h" |
| 35 | #include "storages/portable_storage_from_bin.h" |
| 36 | |
| 37 | #include <boost/utility/string_ref.hpp> |
| 38 | |
| 39 | #include <string> |
| 40 | #include <sstream> |
| 41 | |
| 42 | namespace epee |
| 43 | { |
| 44 | namespace serialization |
| 45 | { |
| 46 | bool portable_storage::store_to_binary(byte_slice& target, const std::size_t initial_buffer_size) |
| 47 | { |
| 48 | TRY_ENTRY(); |
| 49 | byte_stream ss; |
| 50 | ss.reserve(initial_buffer_size); |
| 51 | store_to_binary(ss); |
| 52 | target = epee::byte_slice{std::move(ss), false}; |
| 53 | return true; |
| 54 | CATCH_ENTRY("portable_storage::store_to_binary", false); |
| 55 | } |
| 56 | |
| 57 | bool portable_storage::store_to_binary(byte_stream& ss) |
| 58 | { |
| 59 | TRY_ENTRY(); |
| 60 | storage_block_header sbh{}; |
| 61 | sbh.m_signature_a = SWAP32LE(PORTABLE_STORAGE_SIGNATUREA); |
| 62 | sbh.m_signature_b = SWAP32LE(PORTABLE_STORAGE_SIGNATUREB); |
| 63 | sbh.m_ver = PORTABLE_STORAGE_FORMAT_VER; |
| 64 | ss.write(epee::as_byte_span(sbh)); |
| 65 | pack_entry_to_buff(ss, m_root); |
| 66 | return true; |
| 67 | CATCH_ENTRY("portable_storage::store_to_binary", false); |
| 68 | } |
| 69 | |
| 70 | bool portable_storage::dump_as_json(std::string& buff, size_t indent, bool insert_newlines) |
| 71 | { |
| 72 | TRY_ENTRY(); |
| 73 | std::stringstream ss; |
| 74 | epee::serialization::dump_as_json(ss, m_root, indent, insert_newlines); |
| 75 | buff = ss.str(); |
| 76 | return true; |
| 77 | CATCH_ENTRY("portable_storage::dump_as_json", false) |
| 78 | } |
| 79 | |
| 80 | bool portable_storage::load_from_json(const std::string& source) |
| 81 | { |
| 82 | TRY_ENTRY(); |
| 83 | return json::load_from_json(source, *this); |
| 84 | CATCH_ENTRY("portable_storage::load_from_json", false) |
| 85 | } |
| 86 | |
| 87 | bool portable_storage::load_from_binary(const epee::span<const uint8_t> source, const limits_t *limits) |
| 88 | { |
| 89 | m_root.m_entries.clear(); |
| 90 | if(source.size() < sizeof(storage_block_header)) |
| 91 | { |
| 92 | LOG_ERROR("portable_storage: wrong binary format, packet size = " << source.size() << " less than expected sizeof(storage_block_header)=" << sizeof(storage_block_header)); |
| 93 | return false; |
| 94 | } |
| 95 | storage_block_header* pbuff = (storage_block_header*)source.data(); |
| 96 | if(pbuff->m_signature_a != SWAP32LE(PORTABLE_STORAGE_SIGNATUREA) || |
| 97 | pbuff->m_signature_b != SWAP32LE(PORTABLE_STORAGE_SIGNATUREB) |
| 98 | ) |
| 99 | { |
| 100 | LOG_ERROR("portable_storage: wrong binary format - signature mismatch"); |
| 101 | return false; |
| 102 | } |
| 103 | if(pbuff->m_ver != PORTABLE_STORAGE_FORMAT_VER) |
| 104 | { |
| 105 | LOG_ERROR("portable_storage: wrong binary format - unknown format ver = " << pbuff->m_ver); |
| 106 | return false; |
| 107 | } |
| 108 | TRY_ENTRY(); |
| 109 | throwable_buffer_reader buf_reader(source.data()+sizeof(storage_block_header), source.size()-sizeof(storage_block_header)); |
| 110 | if (limits) |
| 111 | buf_reader.set_limits(limits->n_objects, limits->n_fields, limits->n_strings); |
| 112 | buf_reader.read(m_root); |
| 113 | return true;//TODO: |
| 114 | CATCH_ENTRY("portable_storage::load_from_binary", false); |
| 115 | } |
| 116 | |
| 117 | hsection portable_storage::open_section(const std::string& section_name, hsection hparent_section, bool create_if_notexist) |
| 118 | { |
| 119 | TRY_ENTRY(); |
| 120 | hparent_section = hparent_section ? hparent_section:&m_root; |
| 121 | storage_entry* pentry = find_storage_entry(section_name, hparent_section); |
| 122 | if(!pentry) |
| 123 | { |
| 124 | if(!create_if_notexist) |
| 125 | return nullptr; |
| 126 | return insert_new_section(section_name, hparent_section); |
| 127 | } |
| 128 | CHECK_AND_ASSERT(pentry , nullptr); |
| 129 | //check that section_entry we find is real "CSSection" |
| 130 | if(pentry->type() != typeid(section)) |
| 131 | { |
| 132 | if(create_if_notexist) |
| 133 | *pentry = storage_entry(section());//replace |
| 134 | else |
| 135 | return nullptr; |
| 136 | } |
| 137 | return &boost::get<section>(*pentry); |
| 138 | CATCH_ENTRY("portable_storage::open_section", nullptr); |
| 139 | } |
| 140 | |
| 141 | bool portable_storage::get_value(const std::string& value_name, storage_entry& val, hsection hparent_section) |
| 142 | { |
| 143 | //TRY_ENTRY(); |
| 144 | if(!hparent_section) hparent_section = &m_root; |
| 145 | storage_entry* pentry = find_storage_entry(value_name, hparent_section); |
| 146 | if(!pentry) |
| 147 | return false; |
| 148 | |
| 149 | val = *pentry; |
| 150 | return true; |
| 151 | //CATCH_ENTRY("portable_storage::template<>get_value", false); |
| 152 | } |
| 153 | |
| 154 | storage_entry* portable_storage::find_storage_entry(const std::string& pentry_name, hsection psection) |
| 155 | { |
| 156 | TRY_ENTRY(); |
| 157 | CHECK_AND_ASSERT(psection, nullptr); |
| 158 | auto it = psection->m_entries.find(pentry_name); |
| 159 | if(it == psection->m_entries.end()) |
| 160 | return nullptr; |
| 161 | |
| 162 | return &it->second; |
| 163 | CATCH_ENTRY("portable_storage::find_storage_entry", nullptr); |
| 164 | } |
| 165 | |
| 166 | hsection portable_storage::insert_new_section(const std::string& pentry_name, hsection psection) |
| 167 | { |
| 168 | TRY_ENTRY(); |
| 169 | storage_entry* pse = insert_new_entry_get_storage_entry(pentry_name, psection, section()); |
| 170 | if(!pse) return nullptr; |
| 171 | return &boost::get<section>(*pse); |
| 172 | CATCH_ENTRY("portable_storage::insert_new_section", nullptr); |
| 173 | } |
| 174 | |
| 175 | harray portable_storage::get_first_section(const std::string& sec_name, hsection& h_child_section, hsection hparent_section) |
| 176 | { |
| 177 | TRY_ENTRY(); |
| 178 | if(!hparent_section) hparent_section = &m_root; |
| 179 | storage_entry* pentry = find_storage_entry(sec_name, hparent_section); |
| 180 | if(!pentry) |
| 181 | return nullptr; |
| 182 | if(pentry->type() != typeid(array_entry)) |
| 183 | return nullptr; |
| 184 | array_entry& ar_entry = boost::get<array_entry>(*pentry); |
| 185 | if(ar_entry.type() != typeid(array_entry_t<section>)) |
| 186 | return nullptr; |
| 187 | array_entry_t<section>& sec_array = boost::get<array_entry_t<section>>(ar_entry); |
| 188 | section* psec = sec_array.get_first_val(); |
| 189 | if(!psec) |
| 190 | return nullptr; |
| 191 | h_child_section = psec; |
| 192 | return &ar_entry; |
| 193 | CATCH_ENTRY("portable_storage::get_first_section", nullptr); |
| 194 | } |
| 195 | |
| 196 | bool portable_storage::get_next_section(harray hsec_array, hsection& h_child_section) |
| 197 | { |
| 198 | TRY_ENTRY(); |
| 199 | CHECK_AND_ASSERT(hsec_array, false); |
| 200 | if(hsec_array->type() != typeid(array_entry_t<section>)) |
| 201 | return false; |
| 202 | array_entry_t<section>& sec_array = boost::get<array_entry_t<section>>(*hsec_array); |
| 203 | h_child_section = sec_array.get_next_val(); |
| 204 | if(!h_child_section) |
| 205 | return false; |
| 206 | return true; |
| 207 | CATCH_ENTRY("portable_storage::get_next_section", false); |
| 208 | } |
| 209 | |
| 210 | harray portable_storage::insert_first_section(const std::string& sec_name, hsection& hinserted_childsection, hsection hparent_section) |
| 211 | { |
| 212 | TRY_ENTRY(); |
| 213 | if(!hparent_section) hparent_section = &m_root; |
| 214 | storage_entry* pentry = find_storage_entry(sec_name, hparent_section); |
| 215 | if(!pentry) |
| 216 | { |
| 217 | pentry = insert_new_entry_get_storage_entry(sec_name, hparent_section, array_entry(array_entry_t<section>())); |
| 218 | if(!pentry) |
| 219 | return nullptr; |
| 220 | } |
| 221 | if(pentry->type() != typeid(array_entry)) |
| 222 | *pentry = storage_entry(array_entry(array_entry_t<section>())); |
| 223 | |
| 224 | array_entry& ar_entry = boost::get<array_entry>(*pentry); |
| 225 | if(ar_entry.type() != typeid(array_entry_t<section>)) |
| 226 | ar_entry = array_entry(array_entry_t<section>()); |
| 227 | |
| 228 | array_entry_t<section>& sec_array = boost::get<array_entry_t<section>>(ar_entry); |
| 229 | hinserted_childsection = &sec_array.insert_first_val(section()); |
| 230 | return &ar_entry; |
| 231 | CATCH_ENTRY("portable_storage::insert_first_section", nullptr); |
| 232 | } |
| 233 | |
| 234 | bool portable_storage::insert_next_section(harray hsec_array, hsection& hinserted_childsection) |
| 235 | { |
| 236 | TRY_ENTRY(); |
| 237 | CHECK_AND_ASSERT(hsec_array, false); |
| 238 | CHECK_AND_ASSERT_MES(hsec_array->type() == typeid(array_entry_t<section>), |
| 239 | false, "unexpected type(not 'section') in insert_next_section, type: " << hsec_array->type().name()); |
| 240 | |
| 241 | array_entry_t<section>& sec_array = boost::get<array_entry_t<section>>(*hsec_array); |
| 242 | hinserted_childsection = &sec_array.insert_next_value(section()); |
| 243 | return true; |
| 244 | CATCH_ENTRY("portable_storage::insert_next_section", false); |
| 245 | } |
| 246 | } |
| 247 | } |