mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-13 14:01:48 +00:00
Bridge C++ bytes with Rust &[u8]
This commit is contained in:
@@ -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); }
|
||||
|
||||
Reference in New Issue
Block a user