bool storage::write_data_block(core::tools::binary_stream& _data, int64_t& _offset) { if (!active_file_stream_) { assert(!"file stream not opened"); return false; } _offset = active_file_stream_->tellp(); uint32_t data_size = _data.available(); active_file_stream_->write((const char*) &data_size, sizeof(data_size)); active_file_stream_->write((const char*) &data_size, sizeof(data_size)); if (data_size) active_file_stream_->write((const char*) _data.read(data_size), data_size); active_file_stream_->write((const char*) &data_size, sizeof(data_size)); active_file_stream_->write((const char*) &data_size, sizeof(data_size)); return true; }
bool storage::read_data_block(int64_t _offset, core::tools::binary_stream& _data) { if (_offset != -1) active_file_stream_->seekp(_offset); if (active_file_stream_->peek() == EOF) { last_error_ = archive::error::end_of_file; return false; } uint32_t sz1 = 0, sz2 = 0; active_file_stream_->read((char*) &sz1, sizeof(sz1)); if (!active_file_stream_->good()) return false; active_file_stream_->read((char*) &sz2, sizeof(sz2)); if (!active_file_stream_->good()) return false; if (sz1 != sz2 || sz1 > max_data_block_size) return false; if (sz1 != 0) { active_file_stream_->read(_data.alloc_buffer(sz1), sz1); if (!active_file_stream_->good()) return false; } uint32_t sz3 = 0, sz4 = 0; active_file_stream_->read((char*) &sz3, sizeof(sz3)); if (!active_file_stream_->good()) return false; active_file_stream_->read((char*) &sz4, sizeof(sz4)); if (!active_file_stream_->good()) return false; if (sz1 != sz3 || sz1 != sz4) return false; return true; }
bool core::archive::image_cache::unserialize_block(core::tools::binary_stream& _stream, images_map_t& _images) const { while (_stream.available()) { core::tools::tlvpack block; if (!block.unserialize(_stream)) return false; for (auto item = block.get_first(); item; item = block.get_next()) { image_data img; auto stream = item->get_value<core::tools::binary_stream>(); if (!img.unserialize(stream)) return false; const auto id = img.get_msgid(); _images.emplace(id, std::move(img)); } } return true; }