From b3529cf43bba5c3c4cddefa7f5d0143d510fd3ec Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Thu, 8 Jan 2015 07:54:34 +0100 Subject: build: add --with-mini-gmp switch to disable linking libgmp This allows to disable linking the >400 KB big libgmp and replace it with the builtin mini-gmp which only increases size by ~30KB. Enabling this selectively decreases debugging verbosity (pr_debug). Signed-off-by: Steven Barth Signed-off-by: Pablo Neira Ayuso --- src/gmputil.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) (limited to 'src/gmputil.c') diff --git a/src/gmputil.c b/src/gmputil.c index cb464457..c7637927 100644 --- a/src/gmputil.c +++ b/src/gmputil.c @@ -14,11 +14,9 @@ #include #include #include -#include #include #include -#include #include void mpz_bitmask(mpz_t rop, unsigned int width) @@ -148,6 +146,62 @@ void mpz_switch_byteorder(mpz_t rop, unsigned int len) 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); -- cgit v1.2.3