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

View File

@@ -6,7 +6,7 @@
#include <string>
#include <vector>
#include "xwrap.hpp"
#include "misc.hpp"
template <typename T>
static inline T align_to(T v, int a) {
@@ -41,68 +41,8 @@ struct mount_info {
std::string fs_option;
};
struct heap_data;
struct byte_view {
byte_view() : _buf(nullptr), _sz(0) {}
byte_view(const void *buf, size_t sz) : _buf((uint8_t *) buf), _sz(sz) {}
// byte_view, or any of its sub-type, can be copied as byte_view
byte_view(const byte_view &o) : _buf(o._buf), _sz(o._sz) {}
// String as bytes
byte_view(std::string_view s, bool with_nul = true);
byte_view(const char *s, bool with_nul = true)
: byte_view(std::string_view(s), with_nul) {}
byte_view(const std::string &s, bool with_nul = true)
: byte_view(std::string_view(s), with_nul) {}
// Vector as bytes
byte_view(const std::vector<uint8_t> &v) : byte_view(v.data(), v.size()) {}
const uint8_t *buf() const { return _buf; }
size_t sz() const { return _sz; }
bool contains(byte_view pattern) const;
bool equals(byte_view o) const;
heap_data clone() const;
protected:
uint8_t *_buf;
size_t _sz;
void swap(byte_view &o);
};
struct byte_data : public byte_view {
byte_data() = default;
byte_data(void *buf, size_t sz) : byte_view(buf, sz) {}
using byte_view::buf;
using byte_view::sz;
uint8_t *buf() { return _buf; }
size_t &sz() { return _sz; }
std::vector<size_t> patch(byte_view from, byte_view to);
};
#define MOVE_ONLY(clazz) \
clazz() = default; \
clazz(const clazz&) = delete; \
clazz(clazz &&o) { swap(o); } \
clazz& operator=(clazz &&o) { swap(o); return *this; }
struct heap_data : public byte_data {
MOVE_ONLY(heap_data)
explicit heap_data(size_t sz) : byte_data(malloc(sz), sz) {}
~heap_data() { free(_buf); }
void realloc(size_t sz);
};
struct mmap_data : public byte_data {
MOVE_ONLY(mmap_data)
ALLOW_MOVE_ONLY(mmap_data)
explicit mmap_data(const char *name, bool rw = false);
mmap_data(int fd, size_t sz, bool rw = false) { init(fd, sz, rw); }