summaryrefslogtreecommitdiffstats
path: root/package/network/utils/nftables/patches/205-build-add-without-libgmp-switch-to-disable-use-of-sh.patch
blob: 195c2daaada3faf66aec1cb93c82bba0de1d15d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
From d73f1b630848fb7d90f51938e3c75a42ad947c26 Mon Sep 17 00:00:00 2001
From: Steven Barth <cyrus@openwrt.org>
Date: Mon, 15 Dec 2014 14:26:34 +0100
Subject: [PATCH 5/5] build: add --without-libgmp switch to disable use of
 shared libgmp

This disables linking the >400 KB big libgmp and replace it with
the builtin mini-gmp which only increases size by ~30KB.

Signed-off-by: Steven Barth <cyrus@openwrt.org>
---
 configure.ac         | 17 +++++++++++++---
 include/expression.h |  2 +-
 include/gmputil.h    | 10 +++++++++
 include/utils.h      |  4 ++--
 src/Makefile.am      |  4 ++++
 src/gmputil.c        | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 6 files changed, 86 insertions(+), 8 deletions(-)

--- a/configure.ac
+++ b/configure.ac
@@ -73,8 +73,18 @@ AM_CONDITIONAL([BUILD_PDF], [test "$DBLA
 PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3])
 PKG_CHECK_MODULES([LIBNFTNL], [libnftnl >= 1.0.2])
 
-AC_CHECK_LIB([gmp], [__gmpz_init], ,
-	     AC_MSG_ERROR([No suitable version of libgmp found]))
+AC_ARG_WITH([libgmp], [AS_HELP_STRING([--without-libgmp],
+            [Disable libgmp support (use builtin mini-gmp)])], [],
+            [with_libgmp=yes])
+AS_IF([test "x$with_libgmp" != xno], [
+AC_CHECK_LIB([gmp],[__gmpz_init], , AC_MSG_ERROR([No suitable version of libgmp found]))
+])
+AM_CONDITIONAL([BUILD_MINIGMP], [test "x$with_libgmp" == xno])
+
+
+AS_IF([test "x$with_libgmp" != xyes -a "x$CONFIG_DEBUG" = xy], [
+AC_MSG_ERROR([--without-libgmp MUST be used with --disable-debug])
+])
 
 AC_ARG_WITH([cli], [AS_HELP_STRING([--without-cli],
             [disable interactive CLI (libreadline support)])],
@@ -130,4 +140,5 @@ AC_OUTPUT
 echo "
 nft configuration:
   cli support:			${with_cli}
-  enable debugging:		${with_debug}"
+  enable debugging:		${with_debug}
+  use shared libgmp:		${with_libgmp}"
--- a/include/expression.h
+++ b/include/expression.h
@@ -2,7 +2,7 @@
 #define NFTABLES_EXPRESSION_H
 
 #include <stdbool.h>
-#include <gmp.h>
+#include <gmputil.h>
 #include <linux/netfilter/nf_tables.h>
 
 #include <nftables.h>
--- a/include/gmputil.h
+++ b/include/gmputil.h
@@ -1,7 +1,17 @@
 #ifndef NFTABLES_GMPUTIL_H
 #define NFTABLES_GMPUTIL_H
 
+#include <config.h>
+
+#ifdef HAVE_LIBGMP
 #include <gmp.h>
+#else
+#include <mini-gmp.h>
+/* mini-gmp doesn't come with gmp_printf, so we use our own minimal variant */
+extern int mpz_printf(const char *format, const mpz_t value);
+#define gmp_printf mpz_printf
+#endif
+
 #include <asm/byteorder.h>
 
 enum mpz_word_order {
--- a/include/utils.h
+++ b/include/utils.h
@@ -9,14 +9,14 @@
 #include <unistd.h>
 #include <assert.h>
 #include <list.h>
-#include <gmp.h>
+#include <gmputil.h>
 
 #define BITS_PER_BYTE	8
 
 #ifdef DEBUG
 #define pr_debug(fmt, arg...) gmp_printf(fmt, ##arg)
 #else
-#define pr_debug(fmt, arg...) ({ if (false) gmp_printf(fmt, ##arg); 0; })
+#define pr_debug(fmt, arg...) ({ if (false) {}; 0; })
 #endif
 
 #define __fmtstring(x, y)	__attribute__((format(printf, x, y)))
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -51,4 +51,8 @@ if BUILD_CLI
 nft_SOURCES +=	cli.c
 endif
 
+if BUILD_MINIGMP
+nft_SOURCES +=	mini-gmp.c
+endif
+
 nft_LDADD	= ${LIBMNL_LIBS} ${LIBNFTNL_LIBS}
--- a/src/gmputil.c
+++ b/src/gmputil.c
@@ -14,11 +14,9 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
-#include <gmp.h>
 
 #include <nftables.h>
 #include <datatype.h>
-#include <gmputil.h>
 #include <utils.h>
 
 void mpz_bitmask(mpz_t rop, unsigned int width)
@@ -148,6 +146,61 @@ void mpz_switch_byteorder(mpz_t rop, uns
 	mpz_import_data(rop, data, BYTEORDER_HOST_ENDIAN, len);
 }
 
+#ifndef HAVE_LIBGMP
+/* mini-gmp doesn't have a gmp_printf so we use our own minimal
+ * variant here which is able to format a single mpz_t */
+int mpz_printf(const char *f, const mpz_t value)
+{
+	int n = 0;
+	while (*f) {
+		if (*f != '%') {
+			if (fputc(*f, stdout) != *f)
+				return -1;
+
+			++n;
+		} else {
+			unsigned long prec = 0;
+			int base;
+			size_t len;
+			char *str;
+			bool ok;
+
+			if (*++f == '.')
+				prec = strtoul(++f, (char**)&f, 10);
+
+			if (*f++ != 'Z')
+				return -1;
+
+			if (*f == 'u')
+				base = 10;
+			else if (*f == 'x')
+				base = 16;
+			else
+				return -1;
+
+			len = mpz_sizeinbase(value, base);
+			while (prec-- > len) {
+				if (fputc('0', stdout) != '0')
+					return -1;
+
+				++n;
+			}
+
+			str = mpz_get_str(NULL, base, value);
+			ok = str && fwrite(str, 1, len, stdout) == len;
+			free(str);
+
+			if (!ok)
+				return -1;
+
+			n += len;
+		}
+		++f;
+	}
+	return n;
+}
+#endif
+
 static void *gmp_xrealloc(void *ptr, size_t old_size, size_t new_size)
 {
 	return xrealloc(ptr, new_size);