From fd2d00ccf558b1ac1184967d8702ef01cd60bf60 Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Mon, 29 Feb 2016 14:09:43 +0100 Subject: [PATCH] gpg: Refactor the printing of binary notations. * g10/build-packet.c (sig_to_notation): Break printing of binary notations into... (notation_value_to_human_readable_string): ... this new function. Provide a small preview of the binary data substituting non-printable characters with '?'. -- Signed-off-by: Neal H. Walfield --- g10/build-packet.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/g10/build-packet.c b/g10/build-packet.c index feb7b44cd..8388ce3e4 100644 --- a/g10/build-packet.c +++ b/g10/build-packet.c @@ -1062,6 +1062,33 @@ build_attribute_subpkt(PKT_user_id *uid,byte type, uid->attrib_len+=idx+headerlen+buflen; } +/* Returns a human-readable string corresponding to the notation. + This ignores notation->value. The caller must free the result. */ +static char * +notation_value_to_human_readable_string (struct notation *notation) +{ + if(notation->bdat) + /* Binary data. */ + { + size_t len = notation->blen; + int i; + char preview[20]; + + for (i = 0; i < len && i < sizeof (preview) - 1; i ++) + if (isprint (notation->bdat[i])) + preview[i] = notation->bdat[i]; + else + preview[i] = '?'; + preview[i] = 0; + + return xasprintf (_("[ not human readable (%zd bytes: %s%s) ]"), + len, preview, i < len ? "..." : ""); + } + else + /* The value is human-readable. */ + return xstrdup (notation->value); +} + /* Turn the notation described by the string STRING into a notation. STRING has the form: @@ -1222,10 +1249,7 @@ sig_to_notation(PKT_signature *sig) n->blen=n2; memcpy(n->bdat,&p[8+n1],n2); - n->value=xmalloc(2+strlen(_("not human readable"))+2+1); - strcpy(n->value,"[ "); - strcat(n->value,_("not human readable")); - strcat(n->value," ]"); + n->value = notation_value_to_human_readable_string (n); } n->flags.critical=crit;