mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-10 07:42:06 +00:00
Update nanopb
This commit is contained in:
@@ -1,14 +1,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <vector>
|
||||
|
||||
#include <pb.h>
|
||||
#include <pb_decode.h>
|
||||
#include <pb_encode.h>
|
||||
|
||||
#include <utils.hpp>
|
||||
|
||||
#include "_resetprop.hpp"
|
||||
@@ -16,34 +9,28 @@
|
||||
using namespace std;
|
||||
|
||||
/* ***********************************************************************
|
||||
* Auto generated header and constant definitions compiled from
|
||||
* android/platform/system/core/master/init/persistent_properties.proto
|
||||
* using Nanopb's protoc
|
||||
* Nanopb: https://github.com/nanopb/nanopb
|
||||
* Auto generated header and field definitions compiled from
|
||||
* https://android.googlesource.com/platform/system/core/+/master/init/persistent_properties.proto
|
||||
* Generated with Nanopb: https://github.com/nanopb/nanopb
|
||||
* ***********************************************************************/
|
||||
|
||||
/* Automatically generated nanopb header */
|
||||
/* Generated by nanopb-0.3.9.1 at Sun Apr 22 14:36:22 2018. */
|
||||
/* Generated by nanopb-0.4.3 */
|
||||
|
||||
/* @@protoc_insertion_point(includes) */
|
||||
#if PB_PROTO_HEADER_VERSION != 30
|
||||
#if PB_PROTO_HEADER_VERSION != 40
|
||||
#error Regenerate this file with the current version of nanopb generator.
|
||||
#endif
|
||||
|
||||
/* Struct definitions */
|
||||
typedef struct _PersistentProperties {
|
||||
pb_callback_t properties;
|
||||
/* @@protoc_insertion_point(struct:PersistentProperties) */
|
||||
} PersistentProperties;
|
||||
struct PersistentProperties {
|
||||
pb_callback_t properties;
|
||||
};
|
||||
|
||||
typedef struct _PersistentProperties_PersistentPropertyRecord {
|
||||
pb_callback_t name;
|
||||
bool has_value;
|
||||
char value[92];
|
||||
/* @@protoc_insertion_point(struct:PersistentProperties_PersistentPropertyRecord) */
|
||||
} PersistentProperties_PersistentPropertyRecord;
|
||||
|
||||
/* Default values for struct fields */
|
||||
struct PersistentProperties_PersistentPropertyRecord {
|
||||
pb_callback_t name;
|
||||
bool has_value;
|
||||
char value[92];
|
||||
};
|
||||
|
||||
/* Initializer values for message structs */
|
||||
#define PersistentProperties_init_default {{{NULL}, NULL}}
|
||||
@@ -56,33 +43,33 @@ typedef struct _PersistentProperties_PersistentPropertyRecord {
|
||||
#define PersistentProperties_PersistentPropertyRecord_name_tag 1
|
||||
#define PersistentProperties_PersistentPropertyRecord_value_tag 2
|
||||
|
||||
/* Automatically generated nanopb constant definitions */
|
||||
/* Generated by nanopb-0.3.9.1 at Sun Apr 22 14:36:22 2018. */
|
||||
|
||||
/* Struct field encoding specification for nanopb */
|
||||
const pb_field_t PersistentProperties_PersistentPropertyRecord_fields[3] = {
|
||||
PB_FIELD( 1, STRING , OPTIONAL, CALLBACK, FIRST, PersistentProperties_PersistentPropertyRecord, name, name, 0),
|
||||
PB_FIELD( 2, STRING , OPTIONAL, STATIC , OTHER, PersistentProperties_PersistentPropertyRecord, value, name, 0),
|
||||
PB_LAST_FIELD
|
||||
};
|
||||
#define PersistentProperties_FIELDLIST(X, a) \
|
||||
X(a, CALLBACK, REPEATED, MESSAGE, properties, 1)
|
||||
#define PersistentProperties_CALLBACK pb_default_field_callback
|
||||
#define PersistentProperties_DEFAULT NULL
|
||||
#define PersistentProperties_properties_MSGTYPE PersistentProperties_PersistentPropertyRecord
|
||||
|
||||
const pb_field_t PersistentProperties_fields[2] = {
|
||||
PB_FIELD( 1, MESSAGE , REPEATED, CALLBACK, FIRST, PersistentProperties, properties, properties, &PersistentProperties_PersistentPropertyRecord_fields),
|
||||
PB_LAST_FIELD
|
||||
};
|
||||
#define PersistentProperties_PersistentPropertyRecord_FIELDLIST(X, a) \
|
||||
X(a, CALLBACK, OPTIONAL, STRING, name, 1) \
|
||||
X(a, STATIC, OPTIONAL, STRING, value, 2)
|
||||
#define PersistentProperties_PersistentPropertyRecord_CALLBACK pb_default_field_callback
|
||||
#define PersistentProperties_PersistentPropertyRecord_DEFAULT NULL
|
||||
|
||||
extern const pb_msgdesc_t PersistentProperties_msg;
|
||||
extern const pb_msgdesc_t PersistentProperties_PersistentPropertyRecord_msg;
|
||||
|
||||
/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
|
||||
#define PersistentProperties_fields &PersistentProperties_msg
|
||||
#define PersistentProperties_PersistentPropertyRecord_fields &PersistentProperties_PersistentPropertyRecord_msg
|
||||
|
||||
/* Maximum encoded size of messages (where known) */
|
||||
/* PersistentProperties_size depends on runtime parameters */
|
||||
/* PersistentProperties_PersistentPropertyRecord_size depends on runtime parameters */
|
||||
|
||||
/* Message IDs (where set with "msgid" option) */
|
||||
#ifdef PB_MSGID
|
||||
|
||||
#define PROPS_MESSAGES \
|
||||
|
||||
#endif
|
||||
/* @@protoc_insertion_point(eof) */
|
||||
PB_BIND(PersistentProperties, PersistentProperties, AUTO)
|
||||
|
||||
PB_BIND(PersistentProperties_PersistentPropertyRecord, PersistentProperties_PersistentPropertyRecord, AUTO)
|
||||
|
||||
/* ***************************
|
||||
* End of auto generated code
|
||||
@@ -90,27 +77,10 @@ const pb_field_t PersistentProperties_fields[2] = {
|
||||
|
||||
bool use_pb = false;
|
||||
|
||||
#ifdef APPLET_STUB_MAIN
|
||||
struct {
|
||||
void push_back(...){};
|
||||
} allocs;
|
||||
void persist_cleanup(){}
|
||||
#else
|
||||
static thread_local vector<void *> allocs;
|
||||
void persist_cleanup() {
|
||||
std::for_each(allocs.begin(), allocs.end(), [](auto p){ free(p); });
|
||||
allocs.clear();
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool name_decode(pb_istream_t *stream, const pb_field_t *field, void **arg) {
|
||||
auto name = (pb_byte_t *) malloc(stream->bytes_left + 1);
|
||||
allocs.push_back(name); /* Record all mallocs to cleanup */
|
||||
name[stream->bytes_left] = '\0';
|
||||
if (!pb_read(stream, name, stream->bytes_left))
|
||||
return false;
|
||||
*arg = name;
|
||||
return true;
|
||||
string &name = *static_cast<string *>(*arg);
|
||||
name.resize(stream->bytes_left);
|
||||
return pb_read(stream, (pb_byte_t *)(name.data()), stream->bytes_left);
|
||||
}
|
||||
|
||||
static bool name_encode(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
|
||||
@@ -119,25 +89,28 @@ static bool name_encode(pb_ostream_t *stream, const pb_field_t *field, void * co
|
||||
}
|
||||
|
||||
static bool prop_decode(pb_istream_t *stream, const pb_field_t *field, void **arg) {
|
||||
PersistentProperties_PersistentPropertyRecord prop = {};
|
||||
PersistentProperties_PersistentPropertyRecord prop{};
|
||||
string name;
|
||||
prop.name.funcs.decode = name_decode;
|
||||
if (!pb_decode(stream, PersistentProperties_PersistentPropertyRecord_fields, &prop))
|
||||
prop.name.arg = &name;
|
||||
if (!pb_decode(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
|
||||
return false;
|
||||
reinterpret_cast<prop_cb*>(*arg)->exec((const char *) prop.name.arg, prop.value);
|
||||
auto cb = reinterpret_cast<prop_cb*>(*arg);
|
||||
cb->exec(std::move(name), prop.value);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool prop_encode(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
|
||||
PersistentProperties_PersistentPropertyRecord prop = {};
|
||||
PersistentProperties_PersistentPropertyRecord prop{};
|
||||
prop.name.funcs.encode = name_encode;
|
||||
prop.has_value = true;
|
||||
auto &list = *(prop_list *) *arg;
|
||||
auto &list = *static_cast<prop_list *>(*arg);
|
||||
for (auto &p : list) {
|
||||
if (!pb_encode_tag_for_field(stream, field))
|
||||
return false;
|
||||
prop.name.arg = (void *) p.first.data();
|
||||
strcpy(prop.value, p.second.data());
|
||||
if (!pb_encode_submessage(stream, PersistentProperties_PersistentPropertyRecord_fields, &prop))
|
||||
if (!pb_encode_submessage(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -168,18 +141,18 @@ static void pb_getprop(prop_cb *prop_cb) {
|
||||
size_t size;
|
||||
mmap_ro(PERSISTENT_PROPERTY_DIR "/persistent_properties", buf, size);
|
||||
pb_istream_t stream = pb_istream_from_buffer(buf, size);
|
||||
pb_decode(&stream, PersistentProperties_fields, &props);
|
||||
pb_decode(&stream, &PersistentProperties_msg, &props);
|
||||
munmap(buf, size);
|
||||
}
|
||||
|
||||
static bool file_getprop(const char *name, char *value) {
|
||||
char path[PATH_MAX];
|
||||
char path[4096];
|
||||
snprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return false;
|
||||
LOGD("resetprop: read prop from [%s]\n", path);
|
||||
value[read(fd, value, PROP_VALUE_MAX)] = '\0'; // Null terminate the read value
|
||||
value[read(fd, value, PROP_VALUE_MAX - 1)] = '\0'; // Null terminate the read value
|
||||
close(fd);
|
||||
return value[0] != '\0';
|
||||
}
|
||||
@@ -188,11 +161,9 @@ void persist_getprops(prop_cb *prop_cb) {
|
||||
if (use_pb) {
|
||||
pb_getprop(prop_cb);
|
||||
} else {
|
||||
DIR *dir = opendir(PERSISTENT_PROPERTY_DIR);
|
||||
struct dirent *entry;
|
||||
while ((entry = readdir(dir))) {
|
||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0 )
|
||||
continue;
|
||||
auto dir = open_dir(PERSISTENT_PROPERTY_DIR);
|
||||
if (!dir) return;
|
||||
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
||||
char value[PROP_VALUE_MAX];
|
||||
if (file_getprop(entry->d_name, value))
|
||||
prop_cb->exec(entry->d_name, value);
|
||||
@@ -200,20 +171,21 @@ void persist_getprops(prop_cb *prop_cb) {
|
||||
}
|
||||
}
|
||||
|
||||
struct match_prop_name : prop_cb {
|
||||
explicit match_prop_name(const char *name) : _name(name) { value[0] = '\0'; }
|
||||
void exec(string &&name, const char *val) override {
|
||||
if (name == _name)
|
||||
strcpy(value, val);
|
||||
}
|
||||
char value[PROP_VALUE_MAX];
|
||||
private:
|
||||
const char *_name;
|
||||
};
|
||||
|
||||
string persist_getprop(const char *name) {
|
||||
if (use_pb) {
|
||||
run_finally f([]{ persist_cleanup(); });
|
||||
struct {
|
||||
const char *name;
|
||||
char value[PROP_VALUE_MAX];
|
||||
} prop;
|
||||
prop.name = name;
|
||||
prop.value[0] = '\0';
|
||||
auto reader = make_prop_cb(prop, [](auto name, auto value, auto prop) {
|
||||
if (strcmp(name, prop.name) == 0)
|
||||
strcpy(prop.value, value);
|
||||
});
|
||||
pb_getprop(&reader);
|
||||
auto prop = match_prop_name(name);
|
||||
pb_getprop(&prop);
|
||||
if (prop.value[0])
|
||||
return prop.value;
|
||||
} else {
|
||||
@@ -227,7 +199,6 @@ string persist_getprop(const char *name) {
|
||||
|
||||
bool persist_deleteprop(const char *name) {
|
||||
if (use_pb) {
|
||||
run_finally f([]{ persist_cleanup(); });
|
||||
prop_list list;
|
||||
prop_collector collector(list);
|
||||
persist_getprops(&collector);
|
||||
@@ -236,14 +207,14 @@ bool persist_deleteprop(const char *name) {
|
||||
if (it->first == name) {
|
||||
list.erase(it);
|
||||
// Dump the props back
|
||||
PersistentProperties props = PersistentProperties_init_zero;
|
||||
PersistentProperties props{};
|
||||
pb_ostream_t ostream = create_ostream(PERSISTENT_PROPERTY_DIR
|
||||
"/persistent_properties.tmp");
|
||||
props.properties.funcs.encode = prop_encode;
|
||||
props.properties.arg = &list;
|
||||
LOGD("resetprop: encode with protobuf [" PERSISTENT_PROPERTY_DIR
|
||||
"/persistent_properties.tmp]\n");
|
||||
if (!pb_encode(&ostream, PersistentProperties_fields, &props))
|
||||
if (!pb_encode(&ostream, &PersistentProperties_msg, &props))
|
||||
return false;
|
||||
clone_attr(PERSISTENT_PROPERTY_DIR "/persistent_properties",
|
||||
PERSISTENT_PROPERTY_DIR "/persistent_properties.tmp");
|
||||
@@ -254,7 +225,7 @@ bool persist_deleteprop(const char *name) {
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
char path[PATH_MAX];
|
||||
char path[4096];
|
||||
snprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
|
||||
if (unlink(path) == 0) {
|
||||
LOGD("resetprop: unlink [%s]\n", path);
|
||||
|
||||
Reference in New Issue
Block a user