Bridge C++ bytes with Rust &[u8]

This commit is contained in:
topjohnwu
2023-06-07 16:49:40 -07:00
parent 5e2ef1b7f4
commit ee4dad7a13
7 changed files with 145 additions and 132 deletions
+44
View File
@@ -13,6 +13,50 @@
using namespace std;
bool byte_view::contains(byte_view pattern) const {
if (_buf == nullptr)
return false;
for (uint8_t *p = _buf, *eof = _buf + _sz; p < eof; ++p) {
if (memcmp(p, pattern.buf(), pattern.sz()) == 0) {
return true;
}
}
return false;
}
bool byte_view::equals(byte_view o) const {
return _sz == o._sz && memcmp(_buf, o._buf, _sz) == 0;
}
heap_data byte_view::clone() const {
heap_data copy(_sz);
memcpy(copy._buf, _buf, _sz);
return copy;
}
void byte_data::swap(byte_data &o) {
std::swap(_buf, o._buf);
std::swap(_sz, o._sz);
}
vector<size_t> byte_data::patch(byte_view from, byte_view to) {
vector<size_t> v;
if (_buf == nullptr)
return v;
auto p = _buf;
auto eof = _buf + _sz;
while (p < eof) {
p = static_cast<uint8_t *>(memmem(p, eof - p, from.buf(), from.sz()));
if (p == nullptr)
return v;
memset(p, 0, from.sz());
memcpy(p, to.buf(), to.sz());
v.push_back(p - _buf);
p += from.sz();
}
return v;
}
int fork_dont_care() {
if (int pid = xfork()) {
waitpid(pid, nullptr, 0);