aboutsummaryrefslogtreecommitdiffstats
path: root/common/convert.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2015-04-23 12:31:04 +0000
committerWerner Koch <[email protected]>2015-04-23 13:51:51 +0000
commitce11cc39ea7e011040debc9339a2310a714efe7e (patch)
treef53bc9939b00a857e70270b9cd4ac55303ba2933 /common/convert.c
parentcommon: removal of t-support.c from t_jnlib_src. (diff)
downloadgnupg-ce11cc39ea7e011040debc9339a2310a714efe7e.tar.gz
gnupg-ce11cc39ea7e011040debc9339a2310a714efe7e.zip
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 <[email protected]>
Diffstat (limited to 'common/convert.c')
-rw-r--r--common/convert.c32
1 files changed, 21 insertions, 11 deletions
diff --git a/common/convert.c b/common/convert.c
index e86ccecc7..6b0ff3548 100644
--- a/common/convert.c
+++ b/common/convert.c
@@ -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)