summaryrefslogtreecommitdiff
path: root/sexp-format.c
diff options
context:
space:
mode:
authorNiels Möller <nisse@lysator.liu.se>2002-11-15 09:01:07 +0100
committerNiels Möller <nisse@lysator.liu.se>2002-11-15 09:01:07 +0100
commitf75832a135bc78083544325edf5d477927b2af08 (patch)
tree9ee4151e0fa0dd1b548e153be0a7563e99640905 /sexp-format.c
parent43152353176dfadc83a880dcec2f21312bf6fa19 (diff)
downloadnettle-f75832a135bc78083544325edf5d477927b2af08.tar.gz
(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
Diffstat (limited to 'sexp-format.c')
-rw-r--r--sexp-format.c21
1 files changed, 13 insertions, 8 deletions
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;