Commit fc8a3c5f authored by topjohnwu's avatar topjohnwu

Migrate MagiskBoot to C++

parent 01e7dff1
......@@ -100,15 +100,15 @@ LOCAL_C_INCLUDES := \
$(LIBUTILS)
LOCAL_SRC_FILES := \
magiskboot/cpio.c \
magiskboot/main.c \
magiskboot/main.cpp \
magiskboot/cpio.cpp \
magiskboot/bootimg.cpp \
magiskboot/hexpatch.c \
magiskboot/compress.c \
magiskboot/format.c \
magiskboot/dtb.c \
magiskboot/ramdisk.c \
magiskboot/pattern.c
magiskboot/hexpatch.cpp \
magiskboot/compress.cpp \
magiskboot/format.cpp \
magiskboot/dtb.cpp \
magiskboot/ramdisk.cpp \
magiskboot/pattern.cpp
LOCAL_LDLIBS := -lz
include $(BUILD_EXECUTABLE)
......
......@@ -7,12 +7,10 @@
#include <mincrypt/sha.h>
#include <mincrypt/sha256.h>
extern "C" {
#include "bootimg.h"
#include "magiskboot.h"
#include "utils.h"
#include "logging.h"
}
static void dump(void *buf, size_t size, const char *filename) {
if (size == 0)
......@@ -259,7 +257,7 @@ int unpack(const char *image) {
// Dump kernel
if (COMPRESSED(boot.k_fmt)) {
fd = creat(KERNEL_FILE, 0644);
decomp(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size);
decompress(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size);
close(fd);
} else {
dump(boot.kernel, boot.hdr->kernel_size, KERNEL_FILE);
......@@ -271,7 +269,7 @@ int unpack(const char *image) {
// Dump ramdisk
if (COMPRESSED(boot.r_fmt)) {
fd = creat(RAMDISK_FILE, 0644);
decomp(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size);
decompress(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size);
close(fd);
} else {
dump(boot.ramdisk, boot.hdr->ramdisk_size, RAMDISK_FILE);
......@@ -335,7 +333,7 @@ void repack(const char* orig_image, const char* out_image) {
size_t raw_size;
void *kernel_raw;
mmap_ro(KERNEL_FILE, &kernel_raw, &raw_size);
boot.hdr->kernel_size = comp(boot.k_fmt, fd, kernel_raw, raw_size);
boot.hdr->kernel_size = compress(boot.k_fmt, fd, kernel_raw, raw_size);
munmap(kernel_raw, raw_size);
} else {
boot.hdr->kernel_size = restore(KERNEL_FILE, fd);
......@@ -358,7 +356,7 @@ void repack(const char* orig_image, const char* out_image) {
size_t cpio_size;
void *cpio;
mmap_ro(RAMDISK_FILE, &cpio, &cpio_size);
boot.hdr->ramdisk_size = comp(boot.r_fmt, fd, cpio, cpio_size);
boot.hdr->ramdisk_size = compress(boot.r_fmt, fd, cpio, cpio_size);
munmap(cpio, cpio_size);
} else {
boot.hdr->ramdisk_size = restore(RAMDISK_FILE, fd);
......
......@@ -39,7 +39,7 @@ size_t gzip(int mode, int fd, const void *buf, size_t size) {
if (ret != Z_OK)
LOGE("Unable to init zlib stream\n");
strm.next_in = (void *) buf;
strm.next_in = (Bytef *) buf;
strm.avail_in = size;
do {
......@@ -73,7 +73,7 @@ size_t gzip(int mode, int fd, const void *buf, size_t size) {
// Mode: 0 = decode xz/lzma; 1 = encode xz; 2 = encode lzma
size_t lzma(int mode, int fd, const void *buf, size_t size) {
size_t have, total = 0;
lzma_ret ret = 0;
lzma_ret ret = LZMA_OK;
lzma_stream strm = LZMA_STREAM_INIT;
lzma_options_lzma opt;
unsigned char out[CHUNK];
......@@ -82,7 +82,7 @@ size_t lzma(int mode, int fd, const void *buf, size_t size) {
lzma_lzma_preset(&opt, 9);
lzma_filter filters[] = {
{ .id = LZMA_FILTER_LZMA2, .options = &opt },
{ .id = LZMA_VLI_UNKNOWN, .options = NULL },
{ .id = LZMA_VLI_UNKNOWN, .options = nullptr },
};
switch(mode) {
......@@ -101,7 +101,7 @@ size_t lzma(int mode, int fd, const void *buf, size_t size) {
if (ret != LZMA_OK)
LOGE("Unable to init lzma stream\n");
strm.next_in = buf;
strm.next_in = static_cast<const uint8_t *>(buf);
strm.avail_in = size;
do {
......@@ -119,14 +119,14 @@ size_t lzma(int mode, int fd, const void *buf, size_t size) {
}
// Mode: 0 = decode; 1 = encode
size_t lz4(int mode, int fd, const void *buf, size_t size) {
size_t lz4(int mode, int fd, const uint8_t *buf, size_t size) {
LZ4F_decompressionContext_t dctx;
LZ4F_compressionContext_t cctx;
LZ4F_frameInfo_t info;
size_t blockSize, outCapacity, avail_in, ret = 0, pos = 0, total = 0;
size_t have, read;
void *out = NULL;
uint8_t *out = nullptr;
// Initialize context
switch(mode) {
......@@ -162,21 +162,21 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) {
pos += read;
break;
case 1:
outCapacity = LZ4F_compressFrameBound(blockSize, NULL);
outCapacity = LZ4F_compressFrameBound(blockSize, nullptr);
break;
}
out = xmalloc(outCapacity);
out = new uint8_t[outCapacity];
// Write header
if (mode == 1) {
LZ4F_preferences_t prefs;
memset(&prefs, 0, sizeof(prefs));
LZ4F_preferences_t prefs = LZ4F_preferences_t();
prefs.autoFlush = 1;
prefs.compressionLevel = 9;
prefs.frameInfo.blockMode = 1;
prefs.frameInfo.blockSizeID = 7;
prefs.frameInfo.contentChecksumFlag = 1;
prefs.frameInfo.blockMode = LZ4F_blockIndependent;
prefs.frameInfo.blockSizeID = LZ4F_max4MB;
prefs.frameInfo.blockChecksumFlag = LZ4F_noBlockChecksum;
prefs.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled;
have = ret = LZ4F_compressBegin(cctx, out, size, &prefs);
if (LZ4F_isError(ret))
LOGE("Failed to start compression: error %s\n", LZ4F_getErrorName(ret));
......@@ -195,11 +195,11 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) {
case 0:
have = outCapacity;
read = avail_in;
ret = LZ4F_decompress(dctx, out, &have, buf + pos, &read, NULL);
ret = LZ4F_decompress(dctx, out, &have, buf + pos, &read, nullptr);
break;
case 1:
read = avail_in;
have = ret = LZ4F_compressUpdate(cctx, out, outCapacity, buf + pos, avail_in, NULL);
have = ret = LZ4F_compressUpdate(cctx, out, outCapacity, buf + pos, avail_in, nullptr);
break;
}
if (LZ4F_isError(ret))
......@@ -218,7 +218,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) {
LZ4F_freeDecompressionContext(dctx);
break;
case 1:
have = ret = LZ4F_compressEnd(cctx, out, outCapacity, NULL);
have = ret = LZ4F_compressEnd(cctx, out, outCapacity, nullptr);
if (LZ4F_isError(ret))
LOGE("Failed to end compression: error %s\n", LZ4F_getErrorName(ret));
......@@ -228,7 +228,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) {
break;
}
free(out);
delete[] out;
return total;
}
......@@ -238,9 +238,9 @@ size_t bzip2(int mode, int fd, const void* buf, size_t size) {
bz_stream strm;
char out[CHUNK];
strm.bzalloc = NULL;
strm.bzfree = NULL;
strm.opaque = NULL;
strm.bzalloc = nullptr;
strm.bzfree = nullptr;
strm.opaque = nullptr;
switch(mode) {
case 0:
......@@ -254,7 +254,7 @@ size_t bzip2(int mode, int fd, const void* buf, size_t size) {
if (ret != BZ_OK)
LOGE("Unable to init bzlib stream\n");
strm.next_in = (void *) buf;
strm.next_in = (char *) buf;
strm.avail_in = size;
do {
......@@ -286,7 +286,7 @@ size_t bzip2(int mode, int fd, const void* buf, size_t size) {
#define LZ4_LEGACY_BLOCKSIZE 0x800000
// Mode: 0 = decode; 1 = encode
size_t lz4_legacy(int mode, int fd, const void* buf, size_t size) {
size_t lz4_legacy(int mode, int fd, const uint8_t *buf, size_t size) {
size_t pos = 0;
int have;
char *out;
......@@ -294,12 +294,12 @@ size_t lz4_legacy(int mode, int fd, const void* buf, size_t size) {
switch(mode) {
case 0:
out = xmalloc(LZ4_LEGACY_BLOCKSIZE);
out = new char[LZ4_LEGACY_BLOCKSIZE];
// Skip magic
pos += 4;
break;
case 1:
out = xmalloc(LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE));
out = new char[LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE)];
// Write magic
total += xwrite(fd, "\x02\x21\x4c\x18", 4);
break;
......@@ -313,7 +313,7 @@ size_t lz4_legacy(int mode, int fd, const void* buf, size_t size) {
pos += 4;
if (block_size > LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE))
goto done;
have = LZ4_decompress_safe(buf + pos, out, block_size, LZ4_LEGACY_BLOCKSIZE);
have = LZ4_decompress_safe((const char *) buf + pos, out, block_size, LZ4_LEGACY_BLOCKSIZE);
if (have < 0)
LOGE("Cannot decode lz4_legacy block\n");
pos += block_size;
......@@ -323,7 +323,7 @@ size_t lz4_legacy(int mode, int fd, const void* buf, size_t size) {
insize = size - pos;
else
insize = LZ4_LEGACY_BLOCKSIZE;
have = LZ4_compress_HC(buf + pos, out, insize, LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE), 9);
have = LZ4_compress_HC((const char *) buf + pos, out, insize, LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE), 9);
if (have == 0)
LOGE("lz4_legacy compression error\n");
pos += insize;
......@@ -341,44 +341,46 @@ done:
unsigned uncomp = size;
xwrite(fd, &uncomp, sizeof(uncomp));
}
free(out);
delete[] out;
return total;
}
long long decomp(format_t type, int to, const void *from, size_t size) {
long long decompress(format_t type, int fd, const void *from, size_t size) {
const uint8_t *buf = (uint8_t *) from;
switch (type) {
case GZIP:
return gzip(0, to, from, size);
return gzip(0, fd, buf, size);
case XZ:
return lzma(0, to, from, size);
return lzma(0, fd, buf, size);
case LZMA:
return lzma(0, to, from, size);
return lzma(0, fd, buf, size);
case BZIP2:
return bzip2(0, to, from, size);
return bzip2(0, fd, buf, size);
case LZ4:
return lz4(0, to, from, size);
return lz4(0, fd, buf, size);
case LZ4_LEGACY:
return lz4_legacy(0, to, from, size);
return lz4_legacy(0, fd, buf, size);
default:
// Unsupported
return -1;
}
}
long long comp(format_t type, int to, const void *from, size_t size) {
long long compress(format_t type, int fd, const void *from, size_t size) {
const uint8_t *buf = (uint8_t *) from;
switch (type) {
case GZIP:
return gzip(1, to, from, size);
return gzip(1, fd, buf, size);
case XZ:
return lzma(1, to, from, size);
return lzma(1, fd, buf, size);
case LZMA:
return lzma(2, to, from, size);
return lzma(2, fd, buf, size);
case BZIP2:
return bzip2(1, to, from, size);
return bzip2(1, fd, buf, size);
case LZ4:
return lz4(1, to, from, size);
return lz4(1, fd, buf, size);
case LZ4_LEGACY:
return lz4_legacy(1, to, from, size);
return lz4_legacy(1, fd, buf, size);
default:
// Unsupported
return -1;
......@@ -389,7 +391,7 @@ long long comp(format_t type, int to, const void *from, size_t size) {
* Below are utility functions for commandline
*/
void decomp_file(char *from, const char *to) {
void decompress(char *from, const char *to) {
int strip = 1;
void *file;
size_t size = 0;
......@@ -400,9 +402,9 @@ void decomp_file(char *from, const char *to) {
format_t type = check_fmt(file, size);
char *ext;
ext = strrchr(from, '.');
if (to == NULL)
if (to == nullptr)
to = from;
if (ext != NULL) {
if (ext != nullptr) {
// Strip out a matched file extension
switch (type) {
case GZIP:
......@@ -442,9 +444,9 @@ void decomp_file(char *from, const char *to) {
fprintf(stderr, "Decompressing to [%s]\n", to);
}
decomp(type, fd, file, size);
decompress(type, fd, file, size);
close(fd);
if (to == from && ext != NULL) {
if (to == from && ext != nullptr) {
*ext = '.';
unlink(from);
}
......@@ -454,9 +456,10 @@ void decomp_file(char *from, const char *to) {
munmap(file, size);
}
void comp_file(const char *method, const char *from, const char *to) {
void compress(const char *method, const char *from, const char *to) {
format_t type;
char *ext, dest[PATH_MAX];
const char *ext;
char dest[PATH_MAX];
if (strcmp(method, "gzip") == 0) {
type = GZIP;
ext = "gz";
......@@ -488,7 +491,7 @@ void comp_file(const char *method, const char *from, const char *to) {
stream_full_read(STDIN_FILENO, &file, &size);
else
mmap_ro(from, &file, &size);
if (to == NULL) {
if (to == nullptr) {
if (strcmp(from, "-") == 0)
strcpy(dest, "-");
else
......@@ -502,13 +505,13 @@ void comp_file(const char *method, const char *from, const char *to) {
fd = creat(dest, 0644);
fprintf(stderr, "Compressing to [%s]\n", dest);
}
comp(type, fd, file, size);
compress(type, fd, file, size);
close(fd);
if (strcmp(from, "-") == 0)
free(file);
else
munmap(file, size);
if (to == NULL)
if (to == nullptr)
unlink(from);
}
......@@ -3,9 +3,26 @@
#include <stdint.h>
#include "vector.h"
#include "array.h"
typedef struct cpio_entry {
struct cpio_newc_header {
char magic[6];
char ino[8];
char mode[8];
char uid[8];
char gid[8];
char nlink[8];
char mtime[8];
char filesize[8];
char devmajor[8];
char devminor[8];
char rdevmajor[8];
char rdevminor[8];
char namesize[8];
char check[8];
} __attribute__((packed));
struct cpio_entry {
// uint32_t ino;
uint32_t mode;
uint32_t uid;
......@@ -21,40 +38,31 @@ typedef struct cpio_entry {
// uint32_t check;
char *filename;
void *data;
int remove;
} cpio_entry;
typedef struct cpio_newc_header {
char magic[6];
char ino[8];
char mode[8];
char uid[8];
char gid[8];
char nlink[8];
char mtime[8];
char filesize[8];
char devmajor[8];
char devminor[8];
char rdevmajor[8];
char rdevminor[8];
char namesize[8];
char check[8];
} cpio_newc_header;
// Basic cpio functions
void cpio_free(cpio_entry *e);
int cpio_find(struct vector *v, const char *entry);
int cpio_cmp(const void *a, const void *b);
void parse_cpio(struct vector *v, const char *filename);
void dump_cpio(struct vector *v, const char *filename);
void cpio_vec_insert(struct vector *v, cpio_entry *n);
void cpio_vec_destroy(struct vector *v);
void cpio_rm(struct vector *v, int recursive, const char *entry);
void cpio_mkdir(struct vector *v, mode_t mode, const char *entry);
void cpio_ln(struct vector *v, const char *target, const char *entry);
void cpio_add(struct vector *v, mode_t mode, const char *entry, const char *filename);
int cpio_mv(struct vector *v, const char *from, const char *to);
int cpio_extract(struct vector *v, const char *entry, const char *filename);
void cpio_extract_all(struct vector *v);
cpio_entry() {}
cpio_entry(int fd, cpio_newc_header &header);
~cpio_entry();
};
class cpio {
public:
cpio(const char *filename);
~cpio();
void dump(const char *file);
int find(const char *name);
void insert(cpio_entry *e);
void rm(int recur, const char *name);
void makedir(mode_t mode, const char *name);
void ln(const char *target, const char *name);
void add(mode_t mode, const char *name, const char *file);
void insert(Array<cpio_entry *> &arr);
bool mv(const char *from, const char *to);
void extract();
bool extract(const char *name, const char *file);
void sort();
protected:
Array<cpio_entry *> arr;
};
#endif
#include <libfdt.h>
#include <unistd.h>
#include <sys/mman.h>
extern "C" {
#include <libfdt.h>
}
#include "magiskboot.h"
#include "utils.h"
......@@ -12,7 +14,7 @@ static void print_props(const void *fdt, int node, int depth) {
printf(" ");
int size;
const char *name;
const char *value = fdt_getprop_by_offset(fdt, prop, &name, &size);
const char *value = (char *) fdt_getprop_by_offset(fdt, prop, &name, &size);
printf("[%s]: [%s]\n", name, value);
}
}
......@@ -41,9 +43,9 @@ static int find_fstab(const void *fdt, int parent) {
static void dtb_dump(const char *file) {
size_t size ;
void *dtb, *fdt;
uint8_t *dtb, *fdt;
fprintf(stderr, "Loading dtbs from [%s]\n", file);
mmap_ro(file, &dtb, &size);
mmap_ro(file, (void **) &dtb, &size);
// Loop through all the dtbs
int dtb_num = 0;
for (int i = 0; i < size; ++i) {
......@@ -60,12 +62,12 @@ static void dtb_dump(const char *file) {
static void dtb_patch(const char *file, int patch) {
size_t size ;
void *dtb, *fdt;
uint8_t *dtb, *fdt;
fprintf(stderr, "Loading dtbs from [%s]\n", file);
if (patch)
mmap_rw(file, &dtb, &size);
mmap_rw(file, (void **) &dtb, &size);
else
mmap_ro(file, &dtb, &size);
mmap_ro(file, (void **) &dtb, &size);
// Loop through all the dtbs
int dtb_num = 0, found = 0;
for (int i = 0; i < size; ++i) {
......
......@@ -42,7 +42,7 @@ format_t check_fmt(const void *buf, size_t len) {
}
void get_fmt_name(format_t fmt, char *name) {
char *s;
const char *s;
switch (fmt) {
case CHROMEOS:
s = "chromeos";
......
......@@ -44,8 +44,8 @@ typedef enum {
#define ACCLAIM_MAGIC "BauwksBoot"
#define ACCLAIM_PRE_HEADER_SZ 262144
#define SUP_LIST ((char *[]) { "gzip", "xz", "lzma", "bzip2", "lz4", "lz4_legacy", NULL })
#define SUP_EXT_LIST ((char *[]) { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL })
#define SUP_LIST ((const char *[]) { "gzip", "xz", "lzma", "bzip2", "lz4", "lz4_legacy", NULL })
#define SUP_EXT_LIST ((const char *[]) { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL })
format_t check_fmt(const void *buf, size_t len);
void get_fmt_name(format_t fmt, char *name);
......
......@@ -6,9 +6,9 @@
#include "magiskboot.h"
#include "utils.h"
static void hex2byte(const char *hex, unsigned char *str) {
static void hex2byte(uint8_t *hex, uint8_t *str) {
char high, low;
for (int i = 0, length = strlen(hex); i < length; i += 2) {
for (int i = 0, length = strlen((char *) hex); i < length; i += 2) {
high = toupper(hex[i]) - '0';
low = toupper(hex[i + 1]) - '0';
str[i / 2] = ((high > 9 ? high - 7 : high) << 4) + (low > 9 ? low - 7 : low);
......@@ -18,12 +18,12 @@ static void hex2byte(const char *hex, unsigned char *str) {
void hexpatch(const char *image, const char *from, const char *to) {
int patternsize = strlen(from) / 2, patchsize = strlen(to) / 2;
size_t filesize;
void *file, *pattern, *patch;
mmap_rw(image, &file, &filesize);
pattern = xmalloc(patternsize);
patch = xmalloc(patchsize);
hex2byte(from, pattern);
hex2byte(to, patch);
uint8_t *file, *pattern, *patch;
mmap_rw(image, (void **) &file, &filesize);
pattern = (uint8_t *) xmalloc(patternsize);
patch = (uint8_t *) xmalloc(patchsize);
hex2byte((uint8_t *) from, pattern);
hex2byte((uint8_t *) to, patch);
for (size_t i = 0; filesize > 0 && i < filesize - patternsize; ++i) {
if (memcmp(file + i, pattern, patternsize) == 0) {
fprintf(stderr, "Patch @ %08X [%s]->[%s]\n", (unsigned) i, from, to);
......
......@@ -3,7 +3,6 @@
#include <sys/types.h>
#include "logging.h"
#include "format.h"
#define KERNEL_FILE "kernel"
......@@ -19,18 +18,18 @@ int unpack(const char *image);
void repack(const char* orig_image, const char* out_image);
void hexpatch(const char *image, const char *from, const char *to);
int cpio_commands(int argc, char *argv[]);
void comp_file(const char *method, const char *from, const char *to);
void decomp_file(char *from, const char *to);
void compress(const char *method, const char *from, const char *to);
void decompress(char *from, const char *to);
int dtb_commands(const char *cmd, int argc, char *argv[]);
// Compressions
size_t gzip(int mode, int fd, const void *buf, size_t size);
size_t lzma(int mode, int fd, const void *buf, size_t size);
size_t lz4(int mode, int fd, const void *buf, size_t size);
size_t lz4(int mode, int fd, const uint8_t *buf, size_t size);
size_t bzip2(int mode, int fd, const void *buf, size_t size);
size_t lz4_legacy(int mode, int fd, const void *buf, size_t size);
long long comp(format_t type, int to, const void *from, size_t size);
long long decomp(format_t type, int to, const void *from, size_t size);
size_t lz4_legacy(int mode, int fd, const uint8_t *buf, size_t size);
long long compress(format_t type, int fd, const void *from, size_t size);
long long decompress(format_t type, int fd, const void *from, size_t size);
// Pattern
int patch_verity(void **buf, uint32_t *size, int patch);
......
......@@ -4,9 +4,11 @@
#include <unistd.h>
#include <sys/mman.h>
#include <mincrypt/sha.h>
#include "magiskboot.h"
#include "logging.h"
#include "utils.h"
#include "mincrypt/sha.h"
/********************
Patch Boot Image
......@@ -107,6 +109,7 @@ static void usage(char *arg0) {
}
int main(int argc, char *argv[]) {
cmdline_logging();
fprintf(stderr, "MagiskBoot v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Boot Image Modification Tool\n");
umask(0);
......@@ -138,13 +141,13 @@ int main(int argc, char *argv[]) {
} else if (argc > 2 && strcmp(argv[1], "--repack") == 0) {
repack(argv[2], argc > 3 ? argv[3] : NEW_BOOT);
} else if (argc > 2 && strcmp(argv[1], "--decompress") == 0) {
decomp_file(argv[2], argc > 3 ? argv[3] : NULL);
decompress(argv[2], argc > 3 ? argv[3] : NULL);
} else if (argc > 2 && strncmp(argv[1], "--compress", 10) == 0) {
char *method;
const char *method;
method = strchr(argv[1], '=');
if (method == NULL) method = "gzip";
else method++;
comp_file(method, argv[2], argc > 3 ? argv[3] : NULL);
compress(method, argv[2], argc > 3 ? argv[3] : NULL);
} else if (argc > 4 && strcmp(argv[1], "--hexpatch") == 0) {
hexpatch(argv[2], argv[3], argv[4]);
} else if (argc > 2 && strcmp(argv[1], "--cpio") == 0) {
......
#include <malloc.h>
#include <string.h>
#include "utils.h"
#include "magiskboot.h"
#include "utils.h"
static int check_verity_pattern(const char *s) {
int skip = 0;
......@@ -32,7 +32,7 @@ static int check_encryption_pattern(const char *s) {
int patch_verity(void **buf, uint32_t *size, int patch) {
int skip, src_size = *size, found = 0;
char *src = *buf, *patched = patch ? xcalloc(src_size, 1) : NULL;
char *src = (char *) *buf, *patched = patch ? (char *) xcalloc(src_size, 1) : nullptr;
for (int read = 0, write = 0; read < src_size; ++read, ++write) {
if ((skip = check_verity_pattern(src + read)) > 0) {
if (patch) {
......@@ -56,7 +56,7 @@ int patch_verity(void **buf, uint32_t *size, int patch) {
void patch_encryption(void **buf, uint32_t *size) {
int skip, src_size = *size;
char *src = *buf, *patched = xcalloc(src_size, 1);
char *src = (char *) *buf, *patched = (char *) xcalloc(src_size, 1);
for (int read = 0, write = 0; read < src_size; ++read, ++write) {
if ((skip = check_encryption_pattern(src + read)) > 0) {
fprintf(stderr, "Replace pattern [%.*s] with [encryptable]\n", skip, src + read);
......
#pragma once
#include <stdlib.h>
template <class T>
class Array {
public:
Array() : _data(0), _size(0), _capacity(0) {}
~Array() { delete []_data; }
class iterator {
friend class Array;
public:
iterator(T* n= 0): _node(n) {}
iterator(const iterator& i): _node(i._node) {}
~iterator() {} // Should NOT delete _node
const T& operator * () const { return (*_node); }
T& operator * () { return (*_node); }
iterator& operator ++ () {
++_node;
return (*this);
}
iterator operator ++ (int) {
iterator temp = *this;
++_node;
return temp;
}
iterator& operator -- () {
--_node;
return (*this);
}
iterator operator -- (int) {
iterator temp = *this;
--_node;
return temp;
}
iterator operator + (int i) const {
iterator temp = *this;
temp += i;
return temp;
}
iterator& operator += (int i) {
_node += i;
return (*this);
}
iterator& operator = (const iterator& i) {
_node = i._node;
return (*this);
}
bool operator != (const iterator& i) const {
return _node != i._node;
}
bool operator == (const iterator& i) const { return !(*this != i); }
private:
T* _node;
};
iterator begin() const { return iterator(_data); }
iterator end() const { return iterator(_data + _size); }
bool empty() const { return !_size; }
size_t size() const { return _size; }
T& operator [] (size_t i) { return _data[i]; }
const T& operator [] (size_t i) const { return _data[i]; }
const T& back() const { return _data[_size - 1]; }
void push_back(const T& x) {
if(_size == _capacity)
expand();
_data[_size] = x;
++_size;
}
void pop_front() { erase(begin()); }
void pop_back() { if(_size) --_size; }
bool erase(iterator pos) {
T* d = pos._node;
if (_size == 0 || d < _data || d >= _data + _size)
return false;
for (; d < _data + _size - 1; ++d)
*d = *(d + 1);
--_size;
return true;
}
bool erase(const T& x) {
for (T* i = _data; i < _data + _size; ++i) {
if(*i == x) {
erase(iterator(i));
return true;
}
}
return false;
}
void clear() { _size = 0; }
void sort() const {
qsort(_data, _size, sizeof(T), compare);
}
// void reserve(size_t n) { ... }
// void resize(size_t n) { ... }
private:
T* _data;
size_t _size; // number of valid elements
size_t _capacity; // max number of elements
static int(*_cmp)(T&, T&);
static int compare(const void *a, const void *b) {
return _cmp ? _cmp(*((T*) a), *((T*) b)) : 0;
}
void expand() {
if (_capacity == 0)
_capacity = 1;
else
_capacity *= 2;
T* temp = _data;
_data = new T[_capacity];
for(int i = 0; i < _size; ++i) _data[i] = temp[i];
delete [] temp;
}
};
template<class T>
int(* Array<T>::_cmp)(T&, T&) = nullptr;
......@@ -7,6 +7,10 @@
#include <stdarg.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#define str(a) #a
#define xstr(a) str(a)
......@@ -42,3 +46,6 @@ void cmdline_logging();
int log_handler(log_type t, const char *fmt, ...);
#ifdef __cplusplus
}
#endif
......@@ -11,6 +11,10 @@
#include <sys/socket.h>
#include <sys/stat.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "vector.h"
#define UID_SHELL (get_shell_uid())
......@@ -140,4 +144,8 @@ void full_read_at(int dirfd, const char *filename, void **buf, size_t *size);
void stream_full_read(int fd, void **buf, size_t *size);
void write_zero(int fd, size_t size);
#ifdef __cplusplus
}
#endif
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment