Cvirtual_binary_source::Cvirtual_binary_source(const_memory_range d) { m_range.begin = new unsigned char[d.size()]; m_range.end = m_range.begin + d.size(); if (d) memcpy(m_range, d, d.size()); }
bool Cmerkle_tree::test(int i, const_memory_range v, const_memory_range w) { assert(i >= 0); assert(i < m_size); int a = 0; int b = m_size; unsigned int z = 0; const_memory_range h = v; while (1) { if (*d(a + i)) return h.string() == get0(a + i).string(); if (b - a < 2 || z + 20 > w.size()) return false; int j = i ^ 1; if (a + j < b) { h = i < j ? internal_hash(h, w.sub_range(z, 20)) : internal_hash(w.sub_range(z, 20), h); z += 20; } int c = a; a = b; b += b - c + 1 >> 1; i >>= 1; } }
std::string Cmerkle_tree::compute_root(const_memory_range r) { typedef std::map<int, std::string> t_map; t_map map; char d[1025]; for (; r.size(); r += 1024) { *d = 0; memcpy(d + 1, r, min(r.size(), 1024)); std::string h = Csha1(const_memory_range(d, min(r.size(), 1024) + 1)).read(); *d = 1; int i; for (i = 0; map.find(i) != map.end(); i++) { memcpy(d + 1, map.find(i)->second.c_str(), 20); memcpy(d + 21, h.c_str(), 20); h = Csha1(const_memory_range(d, 41)).read(); map.erase(i); } map[i] = h; } *d = 1; while (map.size() > 1) { memcpy(d + 21, map.begin()->second.c_str(), 20); map.erase(map.begin()); memcpy(d + 1, map.begin()->second.c_str(), 20); map.erase(map.begin()); map[0] = Csha1(const_memory_range(d, 41)).read(); } return map.empty() ? "" : map.begin()->second; }
std::string internal_hash(const_memory_range a, const_memory_range b) { assert(a.size() == 20); assert(b.size() == 20); char d[41]; *d = 1; memcpy(d + 1, a, 20); memcpy(d + 21, b, 20); return Csha1(const_memory_range(d, 41)).read(); }
void Cring_buffer::write(const_memory_range d) { size_t cb = min(d.size(), cb_w()); memcpy(w(), d, cb); cb_w(cb); d += cb; if (d.size()) { memcpy(w(), d, d.size()); cb_w(d.size()); } }
void Cmerkle_tree::set(int i, const_memory_range v, const_memory_range w) { assert(i >= 0); assert(i < m_size); int a = 0; int b = m_size; unsigned int z = 0; set0(a + i, v); while (b - a >= 2 && z + 20 <= w.size()) { int j = i ^ 1; if (a + j < b) { if (*d(a + j)) return; set0(a + j, w.sub_range(z, 20)); z += 20; } int c = a; a = b; b += b - c + 1 >> 1; i >>= 1; } }
void Cvirtual_binary::write(const_memory_range d) { memcpy(write_start(d.size()), d, d.size()); }
int Cbvalue::write(const_memory_range s) { return write(reinterpret_cast<const char*>(s.begin), s.size()); }
void Cmerkle_tree::set0(int i, const_memory_range v) { assert(v.size() == 20); *d(i) = true; memcpy(d(i) + 1, v, 20); }
int Csocket::send(const_memory_range s) const { return ::send(*this, reinterpret_cast<const char*>(s.begin), s.size(), MSG_NOSIGNAL); }
int Csocket::sendto(const_memory_range s, const sockaddr* a, socklen_t cb_a) const { return ::sendto(*this, reinterpret_cast<const char*>(s.begin), s.size(), MSG_NOSIGNAL, a, cb_a); }