int binary_reader::read(blob& blob) { int len; if (0 == read(len)) return 0; if (len <= get_remaining_size()) { blob = _blob.range(static_cast<int>(_ptr - _blob.data()), len); // optimization: zero-copy if (!blob.buffer_ptr()) { std::shared_ptr<char> buffer(new char[len]); memcpy(buffer.get(), blob.data(), blob.length()); blob = ::dsn::blob(buffer, 0, blob.length()); } _ptr += len; _remaining_size -= len; return len + sizeof(len); } else { dassert(false, "read beyond the end of buffer"); return 0; } }
binary_reader::binary_reader(blob& blob) { _blob = blob; _size = blob.length(); _ptr = blob.data(); _remaining_size = _size; }
void binary_reader::init(blob& bb) { _blob = bb; _size = bb.length(); _ptr = bb.data(); _remaining_size = _size; }
/** * Create a binary from the given data. * Data is shared between all cloned binaries by using reference counting. * @param data pointer to data. * @param size binary size in bytes * @param a_alloc is the allocator to use **/ binary(const char* data, size_t size, const Alloc& a_alloc = Alloc()) { if (size == 0) { m_blob = nullptr; return; } m_blob = new blob<char, Alloc>(size, a_alloc); memcpy(m_blob->data(), data, size); }
blob Base64::from(const blob& data) const { std::string transformed; CryptoPP::StringSource(data.data(), data.size(), true, new CryptoPP::Base64Decoder( new CryptoPP::StringSink(transformed) ) ); return blob(std::make_move_iterator(transformed.begin()), std::make_move_iterator(transformed.end())); }
binary_writer::binary_writer(blob& buffer) { _total_size = 0; _buffers.reserve(1); _reserved_size_per_buffer = _reserved_size_per_buffer_static; _buffers.push_back(buffer); _current_buffer = (char*)buffer.data(); _current_offset = 0; _current_buffer_length = buffer.length(); }
void Downloader::NeededChunk::put_block(uint32_t offset, const blob& content) { auto inserted = file_map_.insert({offset, content.size()}).second; if(inserted) { #ifndef FOPEN_BACKEND std::copy(content.begin(), content.end(), mapped_file_.data()+offset); #else if(wrapped_file_.ios().tellp() != offset) wrapped_file_.ios().seekp(offset); wrapped_file_.ios().write((char*)content.data(), content.size()); #endif } }
/** Get the data's binary buffer */ const char* data() const { return m_blob ? m_blob->data() : ""; }
// Must only be called from constructor! void init(const atom& node, int id, uint8_t creation, const Alloc& alloc) throw(err_bad_argument) { m_blob = new blob<port_blob, Alloc>(1, alloc); new (m_blob->data()) port_blob(node, id & 0x0fffffff, creation & 0x03); }
/** * Get the creation number from the PORT. * @return the creation number from the PORT. **/ int creation() const { return m_blob ? m_blob->data()->creation : 0; }
/** * Get the id number from the PORT. * @return the id number from the PORT. **/ int id() const { return m_blob ? m_blob->data()->id : 0; }
/** * Get the node name from the PORT. * @return the node name from the PORT. **/ atom node() const { return m_blob ? m_blob->data()->node : atom::null; }