1
0
Fork 0
mirror of git://git.gnupg.org/gnupg.git synced 2025-07-03 22:56:33 +02:00

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 <wk@gnupg.org>
This commit is contained in:
Werner Koch 2019-10-01 10:32:31 +02:00
parent a605dbb430
commit 61765136cf
No known key found for this signature in database
GPG key ID: E3FDFF218E45B72B
3 changed files with 71 additions and 1 deletions

View file

@ -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);
}