From 31c9e3fb7e8cd19e343de57dafa510de9e07873c Mon Sep 17 00:00:00 2001 From: cpq Date: Thu, 29 Apr 2021 09:09:21 +0100 Subject: [PATCH] Make mg_random() weak, see #1269 --- docs/README.md | 5 +++++ mongoose.h | 28 ++++++++++++++-------------- src/util.h | 28 ++++++++++++++-------------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/docs/README.md b/docs/README.md index 72895e22..181a6655 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1021,6 +1021,11 @@ int mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts); Initialise TLS on a given connection. +IMPORTANT: mbedTLS implementation uses `mg_random` as RNG. The `mg_random` +is defined as weak, therefore it is possible to override Mongoose's `mg_random` +with a custom implementation. Just create your own `mg_random` function: +`void mg_random(void *buf, size_t len)`. + ## Timers diff --git a/mongoose.h b/mongoose.h index 26bbac40..aa3af003 100644 --- a/mongoose.h +++ b/mongoose.h @@ -477,11 +477,24 @@ void mg_timer_poll(unsigned long uptime_ms); +// WEAK symbol makes it possible to define a "default" function implementation, +// which could be overridden by the user who can define a function with the +// same name without linking conflict +#if !defined(WEAK) +#if (defined(__GNUC__) || defined(__clang__) || \ + defined(__TI_COMPILER_VERSION__)) && \ + !defined(_WIN32) && !defined(__CYGWIN__) +#define WEAK __attribute__((weak)) +#else +#define WEAK +#endif +#endif + char *mg_file_read(const char *path, size_t *size); int64_t mg_file_size(const char *path); bool mg_file_write(const char *path, const void *buf, size_t len); bool mg_file_printf(const char *path, const char *fmt, ...); -void mg_random(void *buf, size_t len); +void mg_random(void *buf, size_t len) WEAK; bool mg_globmatch(const char *pattern, int plen, const char *s, int n); bool mg_next_comma_entry(struct mg_str *s, struct mg_str *k, struct mg_str *v); uint16_t mg_ntohs(uint16_t net); @@ -511,19 +524,6 @@ FILE *mg_fopen(const char *fp, const char *mode); #define mg_htons(x) mg_ntohs(x) #define mg_htonl(x) mg_ntohl(x) -// WEAK symbol makes it possible to define a "default" function implementation, -// which could be overridden by the user who can define a function with the -// same name without linking conflict -#if !defined(WEAK) -#if (defined(__GNUC__) || defined(__clang__) || \ - defined(__TI_COMPILER_VERSION__)) && \ - !defined(_WIN32) && !defined(__CYGWIN__) -#define WEAK __attribute__((weak)) -#else -#define WEAK -#endif -#endif - // Expands to a string representation of its argument: e.g. // MG_STRINGIFY_LITERAL(5) expands to "5" #if !defined(_MSC_VER) || _MSC_VER >= 1900 diff --git a/src/util.h b/src/util.h index 3d202738..0f128eea 100644 --- a/src/util.h +++ b/src/util.h @@ -3,11 +3,24 @@ #include "arch.h" #include "str.h" +// WEAK symbol makes it possible to define a "default" function implementation, +// which could be overridden by the user who can define a function with the +// same name without linking conflict +#if !defined(WEAK) +#if (defined(__GNUC__) || defined(__clang__) || \ + defined(__TI_COMPILER_VERSION__)) && \ + !defined(_WIN32) && !defined(__CYGWIN__) +#define WEAK __attribute__((weak)) +#else +#define WEAK +#endif +#endif + char *mg_file_read(const char *path, size_t *size); int64_t mg_file_size(const char *path); bool mg_file_write(const char *path, const void *buf, size_t len); bool mg_file_printf(const char *path, const char *fmt, ...); -void mg_random(void *buf, size_t len); +void mg_random(void *buf, size_t len) WEAK; bool mg_globmatch(const char *pattern, int plen, const char *s, int n); bool mg_next_comma_entry(struct mg_str *s, struct mg_str *k, struct mg_str *v); uint16_t mg_ntohs(uint16_t net); @@ -37,19 +50,6 @@ FILE *mg_fopen(const char *fp, const char *mode); #define mg_htons(x) mg_ntohs(x) #define mg_htonl(x) mg_ntohl(x) -// WEAK symbol makes it possible to define a "default" function implementation, -// which could be overridden by the user who can define a function with the -// same name without linking conflict -#if !defined(WEAK) -#if (defined(__GNUC__) || defined(__clang__) || \ - defined(__TI_COMPILER_VERSION__)) && \ - !defined(_WIN32) && !defined(__CYGWIN__) -#define WEAK __attribute__((weak)) -#else -#define WEAK -#endif -#endif - // Expands to a string representation of its argument: e.g. // MG_STRINGIFY_LITERAL(5) expands to "5" #if !defined(_MSC_VER) || _MSC_VER >= 1900