From 2087e4730044fc0ce6676595e50e330ecdfd7ec4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 3 Apr 2023 18:20:12 -0700 Subject: [PATCH] Get random separately --- native/src/base/misc.cpp | 7 ++++++- native/src/base/misc.hpp | 4 +++- native/src/init/rootdir.cpp | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/native/src/base/misc.cpp b/native/src/base/misc.cpp index 267ea9404..a4c692ec7 100644 --- a/native/src/base/misc.cpp +++ b/native/src/base/misc.cpp @@ -33,7 +33,7 @@ int fork_no_orphan() { return 0; } -int gen_rand_str(char *buf, int len, const void *seed_buf, bool varlen) { +mt19937_64 &get_rand(const void *seed_buf) { static mt19937_64 gen([&] { mt19937_64::result_type seed; if (seed_buf == nullptr) { @@ -45,6 +45,11 @@ int gen_rand_str(char *buf, int len, const void *seed_buf, bool varlen) { } return seed; }()); + return gen; +} + +int gen_rand_str(char *buf, int len, bool varlen) { + auto gen = get_rand(); if (len == 0) return 0; diff --git a/native/src/base/misc.hpp b/native/src/base/misc.hpp index dc47cd3a4..824109a90 100644 --- a/native/src/base/misc.hpp +++ b/native/src/base/misc.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -159,7 +160,8 @@ void init_argv0(int argc, char **argv); void set_nice_name(const char *name); uint32_t binary_gcd(uint32_t u, uint32_t v); int switch_mnt_ns(int pid); -int gen_rand_str(char *buf, int len, const void *seed_buf = nullptr, bool varlen = true); +std::mt19937_64 &get_rand(const void *seed_buf = nullptr); +int gen_rand_str(char *buf, int len, bool varlen = true); std::string &replace_all(std::string &str, std::string_view from, std::string_view to); std::vector split(const std::string &s, const std::string &delims); std::vector split_ro(std::string_view, std::string_view delims); diff --git a/native/src/init/rootdir.cpp b/native/src/init/rootdir.cpp index 73056dfcc..a7e1211d5 100644 --- a/native/src/init/rootdir.cpp +++ b/native/src/init/rootdir.cpp @@ -209,7 +209,7 @@ void MagiskInit::parse_config_file() { } return true; }); - gen_rand_str(nullptr, 0, &seed); + get_rand(&seed); } #define ROOTMIR MIRRDIR "/system_root"