Skip to content
Snippets Groups Projects
Commit cae7cd67 authored by Niels Möller's avatar Niels Möller
Browse files

(sexp_put_string): Print binary strings using

either hex or base 64 (in advanced mode).
(parse_options): Implemented -s hex, for output using hex rather
than base64.

Rev: src/nettle/tools/sexp-conv.c:1.5
parent f282695d
Branches
Tags
No related merge requests found
...@@ -569,6 +569,8 @@ struct sexp_output ...@@ -569,6 +569,8 @@ struct sexp_output
const struct nettle_armor *coding; const struct nettle_armor *coding;
unsigned coding_indent; unsigned coding_indent;
int prefer_hex;
const struct nettle_hash *hash; const struct nettle_hash *hash;
void *ctx; void *ctx;
...@@ -581,11 +583,13 @@ struct sexp_output ...@@ -581,11 +583,13 @@ struct sexp_output
}; };
static void static void
sexp_output_init(struct sexp_output *output, FILE *f, unsigned width) sexp_output_init(struct sexp_output *output, FILE *f,
unsigned width, int prefer_hex)
{ {
output->f = f; output->f = f;
output->line_width = width; output->line_width = width;
output->coding = NULL; output->coding = NULL;
output->prefer_hex = prefer_hex;
output->hash = NULL; output->hash = NULL;
output->ctx = NULL; output->ctx = NULL;
...@@ -777,11 +781,25 @@ sexp_put_string(struct sexp_output *output, enum sexp_mode mode, ...@@ -777,11 +781,25 @@ sexp_put_string(struct sexp_output *output, enum sexp_mode mode,
} }
else else
{ {
sexp_put_char(output, '|'); uint8_t delimiter;
sexp_put_code_start(output, &nettle_base64); const struct nettle_armor *coding;
if (output->prefer_hex)
{
delimiter = '#';
coding = &nettle_base16;
}
else
{
delimiter = '|';
coding = &nettle_base64;
}
sexp_put_char(output, delimiter);
sexp_put_code_start(output, coding);
sexp_put_data(output, string->size, string->contents); sexp_put_data(output, string->size, string->contents);
sexp_put_code_end(output); sexp_put_code_end(output);
sexp_put_char(output, '|'); sexp_put_char(output, delimiter);
} }
} }
else else
...@@ -978,6 +996,7 @@ struct conv_options ...@@ -978,6 +996,7 @@ struct conv_options
{ {
/* Output mode */ /* Output mode */
enum sexp_mode mode; enum sexp_mode mode;
int prefer_hex;
int once; int once;
unsigned width; unsigned width;
const struct nettle_hash *hash; const struct nettle_hash *hash;
...@@ -1058,6 +1077,11 @@ parse_options(struct conv_options *o, ...@@ -1058,6 +1077,11 @@ parse_options(struct conv_options *o,
o->mode = SEXP_TRANSPORT; o->mode = SEXP_TRANSPORT;
else if (match_argument(optarg, "canonical")) else if (match_argument(optarg, "canonical"))
o->mode = SEXP_CANONICAL; o->mode = SEXP_CANONICAL;
else if (match_argument(optarg, "hex"))
{
o->mode = SEXP_ADVANCED;
o->prefer_hex = 1;
}
else else
die("Available syntax variants: advanced, transport, canonical\n"); die("Available syntax variants: advanced, transport, canonical\n");
break; break;
...@@ -1101,7 +1125,7 @@ parse_options(struct conv_options *o, ...@@ -1101,7 +1125,7 @@ parse_options(struct conv_options *o,
} }
printf(" (default is sha1).\n" printf(" (default is sha1).\n"
" -s, --syntax=SYNTAX The syntax used for the output. Available\n" " -s, --syntax=SYNTAX The syntax used for the output. Available\n"
" variants: advanced, transport, canonical\n" " variants: advanced, hex, transport, canonical\n"
" --once Process only the first s-expression.\n" " --once Process only the first s-expression.\n"
" -w, --width=WIDTH Linewidth for base64 encoded data.\n" " -w, --width=WIDTH Linewidth for base64 encoded data.\n"
" Zero means no limit.\n\n" " Zero means no limit.\n\n"
...@@ -1125,7 +1149,8 @@ main(int argc, char **argv) ...@@ -1125,7 +1149,8 @@ main(int argc, char **argv)
parse_options(&options, argc, argv); parse_options(&options, argc, argv);
sexp_input_init(&input, stdin); sexp_input_init(&input, stdin);
sexp_output_init(&output, stdout, options.width); sexp_output_init(&output, stdout,
options.width, options.prefer_hex);
if (options.hash) if (options.hash)
sexp_output_hash_init(&output, sexp_output_hash_init(&output,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment