mirror of
git://git.gnupg.org/gnupg.git
synced 2025-07-03 22:56:33 +02:00
common: Minor change of hex2str to allow for embedded nul.
* common/convert.c (hex2str): Set ERRNO. Return adjusted COUNT. -- hex2str is only used at one place for in-place converting an hex encoded passphrase. This change does not affect this use. The change is however useful to use the function for in-place conversion of arbitrary hex encoded strings. Take care for in-place conversion of a hex string encoding binary data you need to use it this way: if (hex2str (string, string, strlen (string) + 1, &length) oops ("probably out of memory but see ERRNO"); for (i=0; i < length; i++) foo (string[i)); Note that strlen() + 1. Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
parent
a7264e3a6a
commit
ce11cc39ea
2 changed files with 56 additions and 45 deletions
|
@ -175,21 +175,26 @@ bin2hexcolon (const void *buffer, size_t length, char *stringbuf)
|
|||
/* Convert HEXSTRING consisting of hex characters into string and
|
||||
store that at BUFFER. HEXSTRING is either delimited by end of
|
||||
string or a white space character. The function makes sure that
|
||||
the resulting string in BUFFER is terminated by a Nul character.
|
||||
the resulting string in BUFFER is terminated by a Nul byte. Note
|
||||
that the retruned string may include embedded Nul bytes; the extra
|
||||
Nul byte at the end is used to make sure tha the result can always
|
||||
be used as a C-string.
|
||||
|
||||
BUFSIZE is the availabe length of BUFFER; if the converted result
|
||||
plus a possible required Nul character does not fit into this
|
||||
plus a possible required extra Nul character does not fit into this
|
||||
buffer, the function returns NULL and won't change the existing
|
||||
conent of buffer. In-place conversion is possible as long as
|
||||
content of BUFFER. In-place conversion is possible as long as
|
||||
BUFFER points to HEXSTRING.
|
||||
|
||||
If BUFFER is NULL and bufsize is 0 the function scans HEXSTRING but
|
||||
If BUFFER is NULL and BUFSIZE is 0 the function scans HEXSTRING but
|
||||
does not store anything. This may be used to find the end of
|
||||
hexstring.
|
||||
HEXSTRING.
|
||||
|
||||
On sucess the function returns a pointer to the next character
|
||||
after HEXSTRING (which is either end-of-string or a the next white
|
||||
space). If BUFLEN is not NULL the strlen of buffer is stored
|
||||
there; this will even be done if BUFFER has been passed as NULL. */
|
||||
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. */
|
||||
const char *
|
||||
hex2str (const char *hexstring, char *buffer, size_t bufsize, size_t *buflen)
|
||||
{
|
||||
|
@ -203,7 +208,10 @@ hex2str (const char *hexstring, char *buffer, size_t bufsize, size_t *buflen)
|
|||
for (s=hexstring, count=0; hexdigitp (s) && hexdigitp (s+1); s += 2, count++)
|
||||
;
|
||||
if (*s && (!isascii (*s) || !isspace (*s)) )
|
||||
return NULL; /* Not followed by Nul or white space. */
|
||||
{
|
||||
gpg_err_set_errno (EINVAL);
|
||||
return NULL; /* Not followed by Nul or white space. */
|
||||
}
|
||||
/* We need to append a nul character. However we don't want that if
|
||||
the hexstring already ends with "00". */
|
||||
need_nul = ((s == hexstring) || !(s[-2] == '0' && s[-1] == '0'));
|
||||
|
@ -213,7 +221,10 @@ hex2str (const char *hexstring, char *buffer, size_t bufsize, size_t *buflen)
|
|||
if (buffer)
|
||||
{
|
||||
if (count > bufsize)
|
||||
return NULL; /* Too long. */
|
||||
{
|
||||
gpg_err_set_errno (EINVAL);
|
||||
return NULL; /* Too long. */
|
||||
}
|
||||
|
||||
for (s=hexstring, idx=0; hexdigitp (s) && hexdigitp (s+1); s += 2)
|
||||
((unsigned char*)buffer)[idx++] = xtoi_2 (s);
|
||||
|
@ -222,7 +233,7 @@ hex2str (const char *hexstring, char *buffer, size_t bufsize, size_t *buflen)
|
|||
}
|
||||
|
||||
if (buflen)
|
||||
*buflen = count - 1;
|
||||
*buflen = count - need_nul;
|
||||
return s;
|
||||
}
|
||||
|
||||
|
@ -242,7 +253,6 @@ hex2str_alloc (const char *hexstring, size_t *r_count)
|
|||
{
|
||||
if (r_count)
|
||||
*r_count = 0;
|
||||
gpg_err_set_errno (EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
if (r_count)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue