op-packages/mtproxy/patches/0001-musl-rand_r-function.patch
github-actions[bot] 32600acc60 🌴 Sync 2026-03-05 23:51:42
2026-03-05 23:51:42 +08:00

258 lines
7.8 KiB
Diff

Index: mtproxy/common/server-functions.c
===================================================================
--- mtproxy.orig/common/server-functions.c
+++ mtproxy/common/server-functions.c
@@ -35,7 +35,9 @@
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
+#ifdef __GLIBC__
#include <execinfo.h>
+#endif
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
@@ -168,6 +170,7 @@ const char *get_version_string (void) {
}
void print_backtrace (void) {
+#ifdef __GLIBC__
void *buffer[64];
int nptrs = backtrace (buffer, 64);
kwrite (2, "\n------- Stack Backtrace -------\n", 33);
@@ -178,6 +181,7 @@ void print_backtrace (void) {
kwrite (2, s, strlen (s));
kwrite (2, "\n", 1);
}
+#endif
}
pthread_t debug_main_pthread_id;
Index: mtproxy/jobs/jobs.h
===================================================================
--- mtproxy.orig/jobs/jobs.h
+++ mtproxy/jobs/jobs.h
@@ -27,6 +27,7 @@
#include "net/net-events.h"
#include "net/net-msg.h"
#include "net/net-timers.h"
+#include "common/randr_compat.h"
#define __joblocked
#define __jobref
Index: mtproxy/Makefile
===================================================================
--- mtproxy.orig/Makefile
+++ mtproxy/Makefile
@@ -56,6 +56,7 @@ DEPENDENCE_STRANGE := $(subst ${OBJ}/,${
DEPENDENCE_NORM := $(subst ${OBJ}/,${DEP}/,$(patsubst %.o,%.d,${OBJECTS}))
LIB_OBJS_NORMAL := \
+ ${OBJ}/common/randr_compat.o \
${OBJ}/common/crc32c.o \
${OBJ}/common/pid.o \
${OBJ}/common/sha1.o \
Index: mtproxy/common/randr_compat.c
===================================================================
--- /dev/null
+++ mtproxy/common/randr_compat.c
@@ -0,0 +1,121 @@
+/*
+ The GNU C Library is free software. See the file COPYING.LIB for copying
+ conditions, and LICENSES for notices about a few contributions that require
+ these additional notices to be distributed. License copyright years may be
+ listed using range notation, e.g., 2000-2011, indicating that every year in
+ the range, inclusive, is a copyrightable year that would otherwise be listed
+ individually.
+*/
+
+#include <stddef.h>
+#include "common/randr_compat.h"
+#include <stdint.h>
+
+int __drand48_iterate (unsigned short int xsubi[3], struct drand48_data *buffer) {
+ uint64_t X;
+ uint64_t result;
+
+ /* Initialize buffer, if not yet done. */
+ if (!buffer->__init == 0)
+ {
+ buffer->__a = 0x5deece66dull;
+ buffer->__c = 0xb;
+ buffer->__init = 1;
+ }
+
+ /* Do the real work. We choose a data type which contains at least
+ 48 bits. Because we compute the modulus it does not care how
+ many bits really are computed. */
+
+ X = (uint64_t) xsubi[2] << 32 | (uint32_t) xsubi[1] << 16 | xsubi[0];
+
+ result = X * buffer->__a + buffer->__c;
+
+ xsubi[0] = result & 0xffff;
+ xsubi[1] = (result >> 16) & 0xffff;
+ xsubi[2] = (result >> 32) & 0xffff;
+
+ return 0;
+}
+
+int __erand48_r (unsigned short int xsubi[3], struct drand48_data *buffer, double *result) {
+ union ieee754_double temp;
+
+ /* Compute next state. */
+ if (__drand48_iterate (xsubi, buffer) < 0)
+ return -1;
+
+ /* Construct a positive double with the 48 random bits distributed over
+ its fractional part so the resulting FP number is [0.0,1.0). */
+
+ temp.ieee.negative = 0;
+ temp.ieee.exponent = IEEE754_DOUBLE_BIAS;
+ temp.ieee.mantissa0 = (xsubi[2] << 4) | (xsubi[1] >> 12);
+ temp.ieee.mantissa1 = ((xsubi[1] & 0xfff) << 20) | (xsubi[0] << 4);
+
+ /* Please note the lower 4 bits of mantissa1 are always 0. */
+ *result = temp.d - 1.0;
+
+ return 0;
+}
+
+int __nrand48_r (unsigned short int xsubi[3], struct drand48_data *buffer, long int *result) {
+ /* Compute next state. */
+ if (__drand48_iterate (xsubi, buffer) < 0)
+ return -1;
+
+ /* Store the result. */
+ if (sizeof (unsigned short int) == 2)
+ *result = xsubi[2] << 15 | xsubi[1] >> 1;
+ else
+ *result = xsubi[2] >> 1;
+
+ return 0;
+}
+
+int __jrand48_r (unsigned short int xsubi[3], struct drand48_data *buffer, long int *result) {
+ /* Compute next state. */
+ if (__drand48_iterate (xsubi, buffer) < 0)
+ return -1;
+
+ /* Store the result. */
+ *result = (int32_t) ((xsubi[2] << 16) | xsubi[1]);
+
+ return 0;
+}
+
+int drand48_r (struct drand48_data *buffer, double *result) {
+ return __erand48_r (buffer->__x, buffer, result);
+}
+
+int lrand48_r (struct drand48_data *buffer, long int *result) {
+ /* Be generous for the arguments, detect some errors. */
+ if (buffer == NULL)
+ return -1;
+
+ return __nrand48_r (buffer->__x, buffer, result);
+}
+
+int mrand48_r (struct drand48_data *buffer, long int *result) {
+ /* Be generous for the arguments, detect some errors. */
+ if (buffer == NULL)
+ return -1;
+
+ return __jrand48_r (buffer->__x, buffer, result);
+}
+
+int srand48_r (long int seedval, struct drand48_data *buffer) {
+ /* The standards say we only have 32 bits. */
+ if (sizeof (long int) > 4)
+ seedval &= 0xffffffffl;
+
+ buffer->__x[2] = seedval >> 16;
+ buffer->__x[1] = seedval & 0xffffl;
+ buffer->__x[0] = 0x330e;
+
+ buffer->__a = 0x5deece66dull;
+ buffer->__c = 0xb;
+ buffer->__init = 1;
+
+ return 0;
+}
Index: mtproxy/common/randr_compat.h
===================================================================
--- /dev/null
+++ mtproxy/common/randr_compat.h
@@ -0,0 +1,72 @@
+/*
+ The GNU C Library is free software. See the file COPYING.LIB for copying
+ conditions, and LICENSES for notices about a few contributions that require
+ these additional notices to be distributed. License copyright years may be
+ listed using range notation, e.g., 2000-2011, indicating that every year in
+ the range, inclusive, is a copyrightable year that would otherwise be listed
+ individually.
+*/
+
+#pragma once
+
+#include <endian.h>
+#include <pthread.h>
+
+struct drand48_data {
+ unsigned short int __x[3]; /* Current state. */
+ unsigned short int __old_x[3]; /* Old state. */
+ unsigned short int __c; /* Additive const. in congruential formula. */
+ unsigned short int __init; /* Flag for initializing. */
+ unsigned long long int __a; /* Factor in congruential formula. */
+};
+
+union ieee754_double
+{
+ double d;
+
+ /* This is the IEEE 754 double-precision format. */
+ struct
+ {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ unsigned int negative:1;
+ unsigned int exponent:11;
+ /* Together these comprise the mantissa. */
+ unsigned int mantissa0:20;
+ unsigned int mantissa1:32;
+#endif /* Big endian. */
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+ /* Together these comprise the mantissa. */
+ unsigned int mantissa1:32;
+ unsigned int mantissa0:20;
+ unsigned int exponent:11;
+ unsigned int negative:1;
+#endif /* Little endian. */
+ } ieee;
+
+ /* This format makes it easier to see if a NaN is a signalling NaN. */
+ struct
+ {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ unsigned int negative:1;
+ unsigned int exponent:11;
+ unsigned int quiet_nan:1;
+ /* Together these comprise the mantissa. */
+ unsigned int mantissa0:19;
+ unsigned int mantissa1:32;
+#else
+ /* Together these comprise the mantissa. */
+ unsigned int mantissa1:32;
+ unsigned int mantissa0:19;
+ unsigned int quiet_nan:1;
+ unsigned int exponent:11;
+ unsigned int negative:1;
+#endif
+ } ieee_nan;
+};
+
+#define IEEE754_DOUBLE_BIAS 0x3ff /* Added to exponent. */
+
+int drand48_r (struct drand48_data *buffer, double *result);
+int lrand48_r (struct drand48_data *buffer, long int *result);
+int mrand48_r (struct drand48_data *buffer, long int *result);
+int srand48_r (long int seedval, struct drand48_data *buffer);