Better bytes support in C++

This commit is contained in:
topjohnwu
2023-06-06 17:11:42 -07:00
parent f8c38eab74
commit 5e2ef1b7f4
12 changed files with 89 additions and 75 deletions

View File

@@ -45,7 +45,7 @@ static uint32_t x8u(const char *hex) {
cpio_entry::cpio_entry(uint32_t mode) : mode(mode), uid(0), gid(0), data(0) {}
cpio_entry::cpio_entry(uint32_t mode, const byte_view &data) :
cpio_entry::cpio_entry(uint32_t mode, byte_view data) :
mode(mode), uid(0), gid(0), data(data.clone()) {}
cpio_entry::cpio_entry(const cpio_newc_header *h) :
@@ -187,8 +187,7 @@ void cpio::mkdir(mode_t mode, const char *name) {
}
void cpio::ln(const char *target, const char *name) {
byte_view link(target);
auto e = new cpio_entry(S_IFLNK, link);
auto e = new cpio_entry(S_IFLNK, {target, false});
insert(name, e);
fprintf(stderr, "Create symlink [%s] -> [%s]\n", name, target);
}

View File

@@ -17,7 +17,7 @@ struct cpio_entry {
heap_data data;
explicit cpio_entry(uint32_t mode);
explicit cpio_entry(uint32_t mode, const byte_view &data);
explicit cpio_entry(uint32_t mode, byte_view data);
explicit cpio_entry(const cpio_newc_header *h);
};

View File

@@ -223,7 +223,7 @@ static bool fdt_patch(void *fdt) {
// Force remove AVB for 2SI since it may bootloop some devices
int len;
const void *value = fdt_getprop(fdt, node, "fsmgr_flags", &len);
heap_data copy(value, len);
heap_data copy = byte_view(value, len).clone();
if (patch_verity(copy)) {
modified = true;
fdt_setprop(fdt, node, "fsmgr_flags", copy.buf(), copy.sz());
@@ -291,8 +291,7 @@ static bool dt_table_patch(const Header *hdr, const char *out) {
total_size += xwrite(fd, buf, dtb_map.begin()->first);
// mmap rw to patch table values retroactively
auto mmap_sz = lseek(fd, 0, SEEK_CUR);
auto addr = (uint8_t *) xmmap(nullptr, mmap_sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
mmap_data m(fd, lseek(fd, 0, SEEK_CUR), true);
// Guess alignment using gcd
uint32_t align = 1;
@@ -319,17 +318,16 @@ static bool dt_table_patch(const Header *hdr, const char *out) {
// Patch headers
if constexpr (is_aosp) {
auto hdr_rw = reinterpret_cast<Header *>(addr);
auto hdr_rw = reinterpret_cast<Header *>(m.buf());
hdr_rw->total_size = le_to_be(total_size);
}
auto tables_rw = reinterpret_cast<Table *>(addr + sizeof(Header));
auto tables_rw = reinterpret_cast<Table *>(m.buf() + sizeof(Header));
for (int i = 0; i < num_dtb; ++i) {
auto &blob = dtb_map[be_to_le(tables_rw[i].offset)];
tables_rw[i].offset = le_to_be(blob.offset);
tables_rw[i].len = le_to_be(blob.len);
}
munmap(addr, mmap_sz);
close(fd);
return true;

View File

@@ -20,5 +20,9 @@ int hexpatch(const char *file, string_view from, string_view to) {
hex2byte(from, pattern.data());
hex2byte(to, patch.data());
return m.patch({ make_pair(pattern, patch) }) > 0 ? 0 : 1;
auto v = m.patch(pattern, patch);
for (size_t off : v) {
fprintf(stderr, "Patch @ %08zX [%s] -> [%s]\n", off, from.data(), to.data());
}
return v.empty() ? 1 : 0;
}

View File

@@ -186,8 +186,7 @@ void magisk_cpio::backup(const char *orig) {
}
if (!rm_list.empty()) {
byte_view rm(rm_list);
auto rm_list_file = new cpio_entry(S_IFREG, rm);
auto rm_list_file = new cpio_entry(S_IFREG, {rm_list, false});
backups.emplace(".backup/.rmlist", rm_list_file);
}