From f75832a135bc78083544325edf5d477927b2af08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Fri, 15 Nov 2002 09:01:07 +0100 Subject: (sexp_vformat): For %i, output a leading zero when needed to get a correct, positive, sign. For %b, use nettle_mpz_sizeinbase_256_s, to handle negative numbers properly. Rev: src/nettle/sexp-format.c:1.4 --- sexp-format.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'sexp-format.c') diff --git a/sexp-format.c b/sexp-format.c index d445f034..217ead31 100644 --- a/sexp-format.c +++ b/sexp-format.c @@ -177,15 +177,17 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args) uint32_t x = va_arg(args, uint32_t); unsigned length; - if (x < 0x100) + if (x < 0x80) length = 1; - else if (x < 0x10000L) + else if (x < 0x8000L) length = 2; - else if (x < 0x1000000L) + else if (x < 0x800000L) length = 3; - else + else if (x < 0x80000000L) length = 4; - + else + length = 5; + if (buffer && !(NETTLE_BUFFER_PUTC(buffer, '0' + length) && NETTLE_BUFFER_PUTC(buffer, ':'))) return 0; @@ -195,6 +197,11 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args) if (buffer) switch(length) { + case 5: + /* Leading byte needed for the sign. */ + if (!NETTLE_BUFFER_PUTC(buffer, 0)) + return 0; + /* Fall through */ case 4: if (!NETTLE_BUFFER_PUTC(buffer, x >> 24)) return 0; @@ -223,9 +230,7 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args) unsigned length; unsigned prefix_length; - assert(mpz_sgn(n) >= 0); - - length = nettle_mpz_sizeinbase_256(n); + length = nettle_mpz_sizeinbase_256_s(n); prefix_length = format_prefix(buffer, length); if (!prefix_length) return 0; -- cgit v1.2.1