aboutsummaryrefslogtreecommitdiffstats
path: root/common/convert.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2019-10-01 08:32:31 +0000
committerWerner Koch <[email protected]>2019-10-01 08:32:31 +0000
commit61765136cf92be2884603bc3fac020a1c6ed91f4 (patch)
tree2767becfb20c72a87ad5b2a5b811226b4dc4b8ac /common/convert.c
parentgpg: Fix --recv-key in case of a given fingerprint. (diff)
downloadgnupg-61765136cf92be2884603bc3fac020a1c6ed91f4.tar.gz
gnupg-61765136cf92be2884603bc3fac020a1c6ed91f4.zip
common: New function hex2fixedbuf.
* common/convert.c (hex2fixedbuf): New. -- This function is useful for converting hex strings received via assuan if they have a known length. For example keygrips or the new UBID. Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'common/convert.c')
-rw-r--r--common/convert.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/common/convert.c b/common/convert.c
index 40fb4eecf..54182e15b 100644
--- a/common/convert.c
+++ b/common/convert.c
@@ -191,7 +191,7 @@ bin2hexcolon (const void *buffer, size_t length, char *stringbuf)
HEXSTRING.
On success the function returns a pointer to the next character
- after HEXSTRING (which is either end-of-string or a the next white
+ after HEXSTRING (which is either end-of-string or the next white
space). If BUFLEN is not NULL the number of valid vytes in BUFFER
is stored there (an extra Nul byte is not counted); this will even
be done if BUFFER has been passed as NULL. */
@@ -264,3 +264,34 @@ hex2str_alloc (const char *hexstring, size_t *r_count)
BUG ();
return result;
}
+
+
+
+/* Take the hex-encoded string HEXSTR and put it into the provided
+ * BUFFER in binary format. The length of the buffer is BUFEFR_SIZE
+ * and the expected size of the hex-string (sans leading and trailing
+ * spaces) is 2*BUFFER_SIZE. Returns the actual scanned length of
+ * HEXSTR including any leading and trailing spaces on success or 0 on
+ * error. The HEXSTR must be terminated by a Space or a Nul and may
+ * have leading spaces. */
+unsigned int
+hex2fixedbuf (const char *hexstr, void *buffer_arg, size_t buffer_size)
+{
+ unsigned char *buffer = buffer_arg;
+ const char *s;
+ unsigned int leading_spaces, n;
+
+ for (leading_spaces = 0; *hexstr && *hexstr == ' '; hexstr++)
+ leading_spaces++;
+
+ for (s=hexstr, n=0; hexdigitp (s); s++, n++)
+ ;
+ if ((*s && *s != ' ') || !(n == 2*buffer_size))
+ return 0; /* Invalid or wrong length. */
+ for (s=hexstr, n=0; *s && n < buffer_size; s += 2, n++)
+ buffer[n] = xtoi_2 (s);
+ while (*s && *s == ' ')
+ s++;
+
+ return leading_spaces + (s - hexstr);
+}