Commit f643e022 authored by Balázs Scheidler's avatar Balázs Scheidler Committed by Niels Möller
Browse files

* src/format.c (format_hex_string): New function (Bazsi).

(ssh_vformat_length): Added hex 'x' modifier. (Bazsi).
(ssh_vformat_write): -"-

Rev: src/format.c:1.31
Rev: src/format.h:1.24
parent 5f0d529c
......@@ -73,6 +73,7 @@ void ssh_format_write(const char *format, UINT32 length, UINT8 *buffer, ...)
}
static int write_decimal_length(UINT8 *buffer, UINT32 n);
static void format_hex_string(UINT8 *buffer, UINT32 length, const UINT8 *data);
UINT32 ssh_vformat_length(const char *f, va_list args)
{
......@@ -85,6 +86,7 @@ UINT32 ssh_vformat_length(const char *f, va_list args)
int literal = 0;
int decimal = 0;
int unsigned_form = 0;
int hex = 0;
while(*++f)
{
......@@ -102,6 +104,9 @@ UINT32 ssh_vformat_length(const char *f, va_list args)
case 'u':
unsigned_form = 1;
break;
case 'x':
hex = 1;
break;
default:
goto end_options;
}
......@@ -135,9 +140,11 @@ end_options:
UINT32 l = va_arg(args, UINT32); /* String length */
(void) va_arg(args, UINT8 *); /* data */
length += l;
if (hex)
length += l;
if (decimal)
length += format_size_in_decimal(l) + 1;
else if (!literal)
......@@ -149,6 +156,9 @@ end_options:
{
struct lsh_string *s = va_arg(args, struct lsh_string *);
length += s->length;
if (hex)
length += s->length;
if (decimal)
length += format_size_in_decimal(s->length) + 1;
......@@ -279,6 +289,7 @@ void ssh_vformat_write(const char *f, UINT32 size, UINT8 *buffer, va_list args)
int literal = 0;
int do_free = 0;
int decimal = 0;
int hex = 0;
int unsigned_form = 0;
while(*++f)
......@@ -297,6 +308,9 @@ void ssh_vformat_write(const char *f, UINT32 size, UINT8 *buffer, va_list args)
case 'u':
unsigned_form = 1;
break;
case 'x':
hex = 1;
break;
default:
goto end_options;
}
......@@ -336,19 +350,24 @@ void ssh_vformat_write(const char *f, UINT32 size, UINT8 *buffer, va_list args)
case 's':
{
UINT32 length = va_arg(args, UINT32);
UINT32 size = va_arg(args, UINT32);
UINT8 *data = va_arg(args, UINT8 *);
UINT32 length = hex ? (2*size) : size;
if (decimal)
buffer += write_decimal_length(buffer, length);
else if (!literal)
{
WRITE_UINT32(buffer, length);
buffer += 4;
}
memcpy(buffer, data, length);
if (hex)
format_hex_string(buffer, size, data);
else
memcpy(buffer, data, size);
buffer += length;
break;
......@@ -356,18 +375,23 @@ void ssh_vformat_write(const char *f, UINT32 size, UINT8 *buffer, va_list args)
case 'S':
{
struct lsh_string *s = va_arg(args, struct lsh_string *);
UINT32 length = hex ? (2*size) : size;
if (decimal)
buffer += write_decimal_length(buffer, s->length);
buffer += write_decimal_length(buffer, length);
else if (!literal)
{
WRITE_UINT32(buffer, s->length);
WRITE_UINT32(buffer, length);
buffer += 4;
}
memcpy(buffer, s->data, s->length);
buffer += s->length;
if (hex)
format_hex_string(buffer, s->length, s->data);
else
memcpy(buffer, s->data, s->length);
buffer += length;
if (do_free)
lsh_string_free(s);
......@@ -538,6 +562,19 @@ unsigned format_size_in_decimal(UINT32 n)
return e+1;
}
static void format_hex_string(UINT8 *buffer, UINT32 length, const UINT8 *data)
{
static const UINT8 hexchars[16] = "0123456789abcdef";
UINT32 i;
for (i = 0; i < length; i++)
{
*buffer++ = hexchars[ (data[i] & 0xf0) >> 4 ];
*buffer++ = hexchars[ data[i] & 0x0f ];
}
}
void format_decimal(unsigned length, UINT8 *buffer, UINT32 n)
{
unsigned i;
......
......@@ -67,6 +67,10 @@
* digits. For strings, format the input string using sexp syntax;
* i.e. prefixed with the length in decimal.
*
* "x" (as in heXadecimal). For strings, format each character as two
* hexadecimal digits. Does not currently mean any thing for numbers.
* Note that this modifier is orthogonal to the decimal modifier.
*
* "f" (as in free). Frees the input string after it has been copied.
* Applicable to %S only.
*
......
Supports Markdown
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