From 00c67098964c075254f7bda38380af1d6ae8fdb7 Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Sat, 28 May 2005 14:30:43 +0000 Subject: [PATCH] expose macro(s) to produce string representations of arbitrary precision counters --- servers/slapd/proto-slap.h | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h index 12bce17bd1..f5ed80742f 100644 --- a/servers/slapd/proto-slap.h +++ b/servers/slapd/proto-slap.h @@ -1572,6 +1572,60 @@ LDAP_SLAPD_F (int) fe_op_unbind LDAP_P((Operation *op, SlapReply *rs)); #endif LDAP_SLAPD_F (int) fe_extended LDAP_P((Operation *op, SlapReply *rs)); +/* NOTE: this macro assumes that bv has been allocated + * by ber_* malloc functions or is { 0L, NULL } */ +#if defined(HAVE_BIGNUM) +#define UI2BVX(bv,ui,ctx) \ + do { \ + char *val; \ + ber_len_t len; \ + val = BN_bn2dec(ui); \ + if (val) { \ + len = strlen(val); \ + if ( len > (bv)->bv_len ) { \ + (bv)->bv_val = ber_memrealloc_x( (bv)->bv_val, len + 1, (ctx) ); \ + } \ + AC_MEMCPY((bv)->bv_val, val, len + 1); \ + (bv)->bv_len = len; \ + OPENSSL_free(val); \ + } else { \ + ber_memfree_x( (bv)->bv_val, (ctx) ); \ + BER_BVZERO( (bv) ); \ + } \ + } while ( 0 ) +#elif defined(HAVE_GMP) +/* NOTE: according to the documentation, the result + * of mpz_sizeinbase() can exceed the length of the + * string representation of the number by 1 + */ +#define UI2BVX(bv,ui,ctx) \ + do { \ + ber_len_t len = mpz_sizeinbase( (ui), 10 ); \ + if ( len > (bv)->bv_len ) { \ + (bv)->bv_val = ber_memrealloc_x( (bv)->bv_val, len + 1, (ctx) ); \ + } \ + (void)mpz_get_str( (bv)->bv_val, 10, (ui) ); \ + if ( (bv)->bv_val[ len - 1 ] == '\0' ) { \ + len--; \ + } \ + (bv)->bv_len = len; \ + } while ( 0 ) +#else /* ! HAVE_BIGNUM && ! HAVE_GMP */ +#define UI2BVX(bv,ui,ctx) \ + do { \ + char buf[] = "+9223372036854775807L"; \ + ber_len_t len; \ + snprintf( buf, sizeof( buf ), "%lu", (ui) ); \ + len = strlen( buf ); \ + if ( len > (bv)->bv_len ) { \ + (bv)->bv_val = ber_memrealloc_x( (bv)->bv_val, len + 1, (ctx) ); \ + } \ + AC_MEMCPY( (bv)->bv_val, buf, len + 1 ); \ + } while ( 0 ) +#endif /* ! HAVE_GMP */ + +#define UI2BV(bv,ui) UI2BVX(bv,ui,NULL) + LDAP_END_DECL #endif /* PROTO_SLAP_H */ -- 2.39.5