mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-07 04:32:18 +00:00
Use mmap_data more widely
This commit is contained in:
@@ -272,28 +272,6 @@ void fclone_attr(int src, int dest) {
|
||||
fsetattr(dest, &a);
|
||||
}
|
||||
|
||||
void *__mmap(const char *filename, size_t *size, bool rw) {
|
||||
int fd = xopen(filename, (rw ? O_RDWR : O_RDONLY) | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
*size = 0;
|
||||
return nullptr;
|
||||
}
|
||||
struct stat st;
|
||||
if (fstat(fd, &st)) {
|
||||
*size = 0;
|
||||
return nullptr;
|
||||
}
|
||||
if (S_ISBLK(st.st_mode))
|
||||
ioctl(fd, BLKGETSIZE64, size);
|
||||
else
|
||||
*size = st.st_size;
|
||||
void *buf = *size > 0 ?
|
||||
xmmap(nullptr, *size, PROT_READ | PROT_WRITE, rw ? MAP_SHARED : MAP_PRIVATE, fd, 0) :
|
||||
nullptr;
|
||||
close(fd);
|
||||
return buf;
|
||||
}
|
||||
|
||||
void fd_full_read(int fd, void **buf, size_t *size) {
|
||||
*size = lseek(fd, 0, SEEK_END);
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
@@ -439,15 +417,58 @@ sFILE make_file(FILE *fp) {
|
||||
return sFILE(fp, [](FILE *fp){ return fp ? fclose(fp) : 1; });
|
||||
}
|
||||
|
||||
raw_file::raw_file(raw_file &&o) {
|
||||
path.swap(o.path);
|
||||
attr = o.attr;
|
||||
buf = o.buf;
|
||||
sz = o.sz;
|
||||
o.buf = nullptr;
|
||||
o.sz = 0;
|
||||
int byte_data::patch(bool log, str_pairs list) {
|
||||
if (buf == nullptr)
|
||||
return 0;
|
||||
int count = 0;
|
||||
for (uint8_t *p = buf, *eof = buf + sz; p < eof; ++p) {
|
||||
for (auto [from, to] : list) {
|
||||
if (memcmp(p, from.data(), from.length() + 1) == 0) {
|
||||
if (log) LOGD("Replace [%s] -> [%s]\n", from.data(), to.data());
|
||||
memset(p, 0, from.length());
|
||||
memcpy(p, to.data(), to.length());
|
||||
++count;
|
||||
p += from.length();
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
raw_file::~raw_file() {
|
||||
free(buf);
|
||||
bool byte_data::contains(string_view pattern, bool log) const {
|
||||
if (buf == nullptr)
|
||||
return false;
|
||||
for (uint8_t *p = buf, *eof = buf + sz; p < eof; ++p) {
|
||||
if (memcmp(p, pattern.data(), pattern.length() + 1) == 0) {
|
||||
if (log) LOGD("Found pattern [%s]\n", pattern.data());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void byte_data::swap(byte_data &o) {
|
||||
std::swap(buf, o.buf);
|
||||
std::swap(sz, o.sz);
|
||||
}
|
||||
|
||||
mmap_data::mmap_data(const char *name, bool rw) {
|
||||
int fd = xopen(name, (rw ? O_RDWR : O_RDONLY) | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return;
|
||||
struct stat st;
|
||||
if (fstat(fd, &st))
|
||||
return;
|
||||
if (S_ISBLK(st.st_mode)) {
|
||||
uint64_t size;
|
||||
ioctl(fd, BLKGETSIZE64, &size);
|
||||
sz = size;
|
||||
} else {
|
||||
sz = st.st_size;
|
||||
}
|
||||
void *b = sz > 0
|
||||
? xmmap(nullptr, sz, PROT_READ | PROT_WRITE, rw ? MAP_SHARED : MAP_PRIVATE, fd, 0)
|
||||
: nullptr;
|
||||
close(fd);
|
||||
buf = static_cast<uint8_t *>(b);
|
||||
}
|
||||
|
||||
@@ -26,16 +26,36 @@ struct file_attr {
|
||||
char con[128];
|
||||
};
|
||||
|
||||
struct raw_file {
|
||||
struct byte_data {
|
||||
using str_pairs = std::initializer_list<std::pair<std::string_view, std::string_view>>;
|
||||
|
||||
uint8_t *buf = nullptr;
|
||||
size_t sz = 0;
|
||||
|
||||
int patch(str_pairs list) { return patch(true, list); }
|
||||
int patch(bool log, str_pairs list);
|
||||
bool contains(std::string_view pattern, bool log = true) const;
|
||||
protected:
|
||||
void swap(byte_data &o);
|
||||
};
|
||||
|
||||
struct raw_file : public byte_data {
|
||||
std::string path;
|
||||
file_attr attr;
|
||||
uint8_t *buf;
|
||||
size_t sz;
|
||||
|
||||
raw_file() : attr({}), buf(nullptr), sz(0) {}
|
||||
raw_file() : attr{} {}
|
||||
raw_file(const raw_file&) = delete;
|
||||
raw_file(raw_file &&o);
|
||||
~raw_file();
|
||||
raw_file(raw_file &&o) : path(std::move(o.path)), attr(o.attr) { swap(o); }
|
||||
~raw_file() { free(buf); }
|
||||
};
|
||||
|
||||
struct mmap_data : public byte_data {
|
||||
mmap_data() = default;
|
||||
mmap_data(const mmap_data&) = delete;
|
||||
mmap_data(mmap_data &&o) { swap(o); }
|
||||
mmap_data(const char *name, bool rw = false);
|
||||
~mmap_data() { if (buf) munmap(buf, sz); }
|
||||
mmap_data& operator=(mmap_data &&other) { swap(other); return *this; }
|
||||
};
|
||||
|
||||
ssize_t fd_path(int fd, char *path, size_t size);
|
||||
@@ -67,7 +87,6 @@ static inline void file_readline(const char *file,
|
||||
}
|
||||
void parse_prop_file(const char *file,
|
||||
const std::function<bool(std::string_view, std::string_view)> &fn);
|
||||
void *__mmap(const char *filename, size_t *size, bool rw);
|
||||
void frm_rf(int dirfd);
|
||||
void clone_dir(int src, int dest);
|
||||
void parse_mnt(const char *file, const std::function<bool(mntent*)> &fn);
|
||||
@@ -86,30 +105,6 @@ void fd_full_read(int fd, T &buf, size_t &size) {
|
||||
fd_full_read(fd, reinterpret_cast<void**>(&buf), &size);
|
||||
}
|
||||
|
||||
template <typename B>
|
||||
void mmap_ro(const char *filename, B &buf, size_t &sz) {
|
||||
buf = (B) __mmap(filename, &sz, false);
|
||||
}
|
||||
|
||||
template <typename B, typename L>
|
||||
void mmap_ro(const char *filename, B &buf, L &sz) {
|
||||
size_t __sz;
|
||||
buf = (B) __mmap(filename, &__sz, false);
|
||||
sz = __sz;
|
||||
}
|
||||
|
||||
template <typename B>
|
||||
void mmap_rw(const char *filename, B &buf, size_t &sz) {
|
||||
buf = (B) __mmap(filename, &sz, true);
|
||||
}
|
||||
|
||||
template <typename B, typename L>
|
||||
void mmap_rw(const char *filename, B &buf, L &sz) {
|
||||
size_t __sz;
|
||||
buf = (B) __mmap(filename, &__sz, true);
|
||||
sz = __sz;
|
||||
}
|
||||
|
||||
using sFILE = std::unique_ptr<FILE, decltype(&fclose)>;
|
||||
using sDIR = std::unique_ptr<DIR, decltype(&closedir)>;
|
||||
sDIR make_dir(DIR *dp);
|
||||
|
||||
Reference in New Issue
Block a user