Commit 1c323788 authored by Niels Möller's avatar Niels Möller
Browse files

(format_length_string): Deleted function.

(format_string): Deleted function.
(sexp_vformat): New %t specifier, formatting an optional display
type. Deleted %z specifier. Instead, introduced a new modifier "0"
that can be used with %s, %l and %t, which says that the data is
NUL-terminated.

Rev: src/nettle/sexp-format.c:1.5
Rev: src/nettle/sexp.h:1.11
parent a38808d6
...@@ -71,27 +71,6 @@ format_prefix(struct nettle_buffer *buffer, ...@@ -71,27 +71,6 @@ format_prefix(struct nettle_buffer *buffer,
return prefix_length + 1; return prefix_length + 1;
} }
static unsigned
format_length_string(struct nettle_buffer *buffer,
unsigned length, const char *s)
{
unsigned done = format_prefix(buffer, length);
if (!done)
return 0;
if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
return done + length;
}
static unsigned
format_string(struct nettle_buffer *buffer,
const char *s)
{
return format_length_string(buffer, strlen(s), s);
}
unsigned unsigned
sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args) sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
{ {
...@@ -127,133 +106,200 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args) ...@@ -127,133 +106,200 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
break; break;
case '%': case '%':
switch (*format++) {
{ int nul_flag = 0;
default:
abort();
case 'z':
{
const char *s = va_arg(args, const char *);
unsigned length = format_string(buffer, s);
if (!length)
return 0;
done += length; if (*format == '0')
break; {
format++;
nul_flag = 1;
} }
case 's': switch (*format++)
{ {
unsigned length = va_arg(args, unsigned); default:
const char *s = va_arg(args, const char *); abort();
unsigned prefix_length = format_prefix(buffer, length);
if (!prefix_length)
return 0;
done += prefix_length; case 's':
{
const char *s;
unsigned length;
unsigned prefix_length;
if (nul_flag)
{
s = va_arg(args, const char *);
length = strlen(s);
}
else
{
length = va_arg(args, unsigned);
s = va_arg(args, const char *);
}
prefix_length = format_prefix(buffer, length);
if (!prefix_length)
return 0;
done += prefix_length;
if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
done += length;
break;
}
case 't':
{
const char *s;
unsigned length;
unsigned prefix_length;
if (nul_flag)
{
s = va_arg(args, const char *);
if (!s)
break;
length = strlen(s);
}
else
{
length = va_arg(args, unsigned);
s = va_arg(args, const char *);
if (!s)
break;
}
if (buffer && !NETTLE_BUFFER_PUTC(buffer, '['))
return 0;
done++;
prefix_length = format_prefix(buffer, length);
if (!prefix_length)
return 0;
if (buffer && !nettle_buffer_write(buffer, length, s)) done += prefix_length;
return 0;
done += length; if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
break; done += length;
}
case 'l':
{
unsigned length = va_arg(args, unsigned);
const char *s = va_arg(args, const char *);
if (buffer && !nettle_buffer_write(buffer, length, s)) if (buffer && !NETTLE_BUFFER_PUTC(buffer, ']'))
return 0; return 0;
done++;
break;
}
done += length; case 'l':
break; {
} const char *s;
case 'i': unsigned length;
{
uint32_t x = va_arg(args, uint32_t); if (nul_flag)
unsigned length; {
s = va_arg(args, const char *);
length = strlen(s);
}
else
{
length = va_arg(args, unsigned);
s = va_arg(args, const char *);
}
if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
if (x < 0x80) done += length;
length = 1; break;
else if (x < 0x8000L) }
length = 2; case 'i':
else if (x < 0x800000L) {
length = 3; uint32_t x = va_arg(args, uint32_t);
else if (x < 0x80000000L) unsigned length;
length = 4;
else
length = 5;
if (buffer && !(NETTLE_BUFFER_PUTC(buffer, '0' + length) if (x < 0x80)
&& NETTLE_BUFFER_PUTC(buffer, ':'))) length = 1;
return 0; else if (x < 0x8000L)
length = 2;
done += (2 + length); else if (x < 0x800000L)
length = 3;
if (buffer) else if (x < 0x80000000L)
switch(length) length = 4;
{ else
case 5: length = 5;
/* Leading byte needed for the sign. */
if (!NETTLE_BUFFER_PUTC(buffer, 0)) if (buffer && !(NETTLE_BUFFER_PUTC(buffer, '0' + length)
return 0; && NETTLE_BUFFER_PUTC(buffer, ':')))
/* Fall through */ return 0;
case 4:
if (!NETTLE_BUFFER_PUTC(buffer, x >> 24)) done += (2 + length);
return 0;
/* Fall through */ if (buffer)
case 3: switch(length)
if (!NETTLE_BUFFER_PUTC(buffer, (x >> 16) & 0xff)) {
return 0; case 5:
/* Fall through */ /* Leading byte needed for the sign. */
case 2: if (!NETTLE_BUFFER_PUTC(buffer, 0))
if (!NETTLE_BUFFER_PUTC(buffer, (x >> 8) & 0xff)) return 0;
return 0; /* Fall through */
/* Fall through */ case 4:
case 1: if (!NETTLE_BUFFER_PUTC(buffer, x >> 24))
if (!NETTLE_BUFFER_PUTC(buffer, x & 0xff)) return 0;
return 0; /* Fall through */
break; case 3:
default: if (!NETTLE_BUFFER_PUTC(buffer, (x >> 16) & 0xff))
abort(); return 0;
} /* Fall through */
break; case 2:
} if (!NETTLE_BUFFER_PUTC(buffer, (x >> 8) & 0xff))
case 'b': return 0;
{ /* Fall through */
case 1:
if (!NETTLE_BUFFER_PUTC(buffer, x & 0xff))
return 0;
break;
default:
abort();
}
break;
}
case 'b':
{
#if HAVE_LIBGMP #if HAVE_LIBGMP
const MP_INT *n = va_arg(args, const MP_INT *); const MP_INT *n = va_arg(args, const MP_INT *);
unsigned length; unsigned length;
unsigned prefix_length; unsigned prefix_length;
length = nettle_mpz_sizeinbase_256_s(n); length = nettle_mpz_sizeinbase_256_s(n);
prefix_length = format_prefix(buffer, length); prefix_length = format_prefix(buffer, length);
if (!prefix_length) if (!prefix_length)
return 0; return 0;
done += prefix_length; done += prefix_length;
if (buffer) if (buffer)
{ {
uint8_t *space = nettle_buffer_space(buffer, length); uint8_t *space = nettle_buffer_space(buffer, length);
if (!space) if (!space)
return 0; return 0;
nettle_mpz_get_str_256(length, space, n); nettle_mpz_get_str_256(length, space, n);
} }
done += length; done += length;
#else /* ! HAVE_LIBGMP */ #else /* ! HAVE_LIBGMP */
abort(); abort();
#endif /* ! HAVE_LIBGMP */ #endif /* ! HAVE_LIBGMP */
break; break;
}
} }
} }
} }
} }
......
...@@ -137,10 +137,12 @@ struct nettle_buffer; ...@@ -137,10 +137,12 @@ struct nettle_buffer;
* Format strings can contained matched parentheses, and the following * Format strings can contained matched parentheses, and the following
* formatting specifiers: * formatting specifiers:
* *
* %z NUL-terminated string, const uint8_t *.
*
* %s String represented as unsigned length, const uint8_t *data. * %s String represented as unsigned length, const uint8_t *data.
* *
* %t Optional display type, represented as
* unsigned display_length, const uint8_t *display,
* display == NULL means no display type.
*
* %i Non-negative small integer, uint32_t. * %i Non-negative small integer, uint32_t.
* *
* %b Non-negative bignum, mpz_t. * %b Non-negative bignum, mpz_t.
...@@ -148,7 +150,15 @@ struct nettle_buffer; ...@@ -148,7 +150,15 @@ struct nettle_buffer;
* %l Literal string (no length added), typically a balanced * %l Literal string (no length added), typically a balanced
* subexpression. Represented as unsigned length, const uint8_t * subexpression. Represented as unsigned length, const uint8_t
* *data. * *data.
*/ *
* Modifiers:
*
* %0 For %s, %t and %l, says that there's no length argument,
* instead the string is NUL-terminated, and there's only one
* const uint8_t * argument.
*
* FIXME: Allow literals, like "(x%b)". Allow "%(" for unbalanced
* parenthesis. */
unsigned unsigned
sexp_format(struct nettle_buffer *buffer, sexp_format(struct nettle_buffer *buffer,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment