aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2006-10-20 11:38:48 +0000
committerWerner Koch <[email protected]>2006-10-20 11:38:48 +0000
commit58785c880d4540e0f75738b3b10a8a03c35d5ee1 (patch)
tree3b1a8732c3d3f8132bfaab9447a9252d4235b852 /common
parent* gpgkeys_hkp.c (curl_mrindex_writer): Print a warning if we see HTML (diff)
downloadgnupg-58785c880d4540e0f75738b3b10a8a03c35d5ee1.tar.gz
gnupg-58785c880d4540e0f75738b3b10a8a03c35d5ee1.zip
Allow to select X.509 certificates using the keygrip.
Diffstat (limited to 'common')
-rw-r--r--common/ChangeLog4
-rw-r--r--common/convert.c30
-rw-r--r--common/t-convert.c88
-rw-r--r--common/util.h1
4 files changed, 122 insertions, 1 deletions
diff --git a/common/ChangeLog b/common/ChangeLog
index 68cdb99e4..6296cbc25 100644
--- a/common/ChangeLog
+++ b/common/ChangeLog
@@ -1,3 +1,7 @@
+2006-10-20 Werner Koch <[email protected]>
+
+ * convert.c (hex2bin): New.
+
2006-10-17 Werner Koch <[email protected]>
* estream.c (struct estream_internal, es_initialize)
diff --git a/common/convert.c b/common/convert.c
index 66f612063..d5301b8e7 100644
--- a/common/convert.c
+++ b/common/convert.c
@@ -30,6 +30,35 @@
#define tohex(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'A'))
+/* Convert STRING consisting of hex characters into its binary
+ representation and store that at BUFFER. BUFFER needs to be of
+ LENGTH bytes. The function check that the STRING will convert
+ exactly to LENGTH bytes. The string is delimited by either end of
+ string or a white space character. The function returns -1 on
+ error or the length of the parsed string. */
+int
+hex2bin (const char *string, void *buffer, size_t length)
+{
+ int i;
+ const char *s = string;
+
+ for (i=0; i < length; )
+ {
+ if (!hexdigitp (s) || !hexdigitp (s+1))
+ return -1; /* Invalid hex digits. */
+ ((unsigned char*)buffer)[i++] = xtoi_2 (s);
+ s += 2;
+ }
+ if (*s && (!isascii (*s) || !isspace (*s)) )
+ return -1; /* Not followed by Nul or white space. */
+ if (i != length)
+ return -1; /* Not of expected length. */
+ if (*s)
+ s++; /* Skip the delimiter. */
+ return s - string;
+}
+
+
/* Convert STRING consisting of hex characters into its binary representation
and store that at BUFFER. BUFFER needs to be of LENGTH bytes. The
function check that the STRING will convert exactly to LENGTH
@@ -73,7 +102,6 @@ hexcolon2bin (const char *string, void *buffer, size_t length)
}
-
static char *
do_bin2hex (const void *buffer, size_t length, char *stringbuf, int with_colon)
{
diff --git a/common/t-convert.c b/common/t-convert.c
index 52647b085..ef295985a 100644
--- a/common/t-convert.c
+++ b/common/t-convert.c
@@ -33,6 +33,93 @@
static void
+test_hex2bin (void)
+{
+ static const char *valid[] = {
+ "00112233445566778899aabbccddeeff11223344",
+ "00112233445566778899AABBCCDDEEFF11223344",
+ "00112233445566778899AABBCCDDEEFF11223344 blah",
+ "00112233445566778899AABBCCDDEEFF11223344\tblah",
+ "00112233445566778899AABBCCDDEEFF11223344\nblah",
+ NULL
+ };
+ static const char *invalid[] = {
+ "00112233445566778899aabbccddeeff1122334",
+ "00112233445566778899AABBCCDDEEFF1122334",
+ "00112233445566778899AABBCCDDEEFG11223344",
+ "00 112233445566778899aabbccddeeff11223344",
+ "00:112233445566778899aabbccddeeff11223344",
+ ":00112233445566778899aabbccddeeff11223344",
+ "0:0112233445566778899aabbccddeeff11223344",
+ "00112233445566778899aabbccddeeff11223344:",
+ "00112233445566778899aabbccddeeff112233445",
+ "00112233445566778899aabbccddeeff1122334455",
+ "00112233445566778899aabbccddeeff11223344blah",
+ NULL
+ };
+ static const char *valid2[] = {
+ "00",
+ "00 x",
+ NULL
+ };
+ static const char *invalid2[] = {
+ "",
+ "0",
+ "00:",
+ "00x",
+ " 00",
+ NULL
+ };
+ unsigned char buffer[20];
+ int len;
+ int i;
+
+
+ for (i=0; valid[i]; i++)
+ {
+ len = hex2bin (valid[i], buffer, sizeof buffer);
+ if (len < 0)
+ fail (i);
+ if (memcmp (buffer, ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
+ "\xbb\xcc\xdd\xee\xff\x11\x22\x33\x44"), 20))
+ fail (i);
+ }
+ if (hex2bin (valid[0], buffer, sizeof buffer) != 40)
+ fail (0);
+ if (hex2bin (valid[2], buffer, sizeof buffer) != 41)
+ fail (0);
+
+ for (i=0; invalid[i]; i++)
+ {
+ len = hex2bin (invalid[i], buffer, sizeof buffer);
+ if (!(len < 0))
+ fail (i);
+ }
+
+ for (i=0; valid2[i]; i++)
+ {
+ len = hex2bin (valid2[i], buffer, 1);
+ if (len < 0)
+ fail (i);
+ if (memcmp (buffer, "\x00", 1))
+ fail (i);
+ }
+ if (hex2bin (valid2[0], buffer, 1) != 2)
+ fail (0);
+ if (hex2bin (valid2[1], buffer, 1) != 3)
+ fail (0);
+
+ for (i=0; invalid2[i]; i++)
+ {
+ len = hex2bin (invalid2[i], buffer, 1);
+ if (!(len < 0))
+ fail (i);
+ }
+}
+
+
+
+static void
test_hexcolon2bin (void)
{
static const char *valid[] = {
@@ -195,6 +282,7 @@ int
main (int argc, char **argv)
{
+ test_hex2bin ();
test_hexcolon2bin ();
test_bin2hex ();
test_bin2hexcolon ();
diff --git a/common/util.h b/common/util.h
index 7a9738d69..11ff50503 100644
--- a/common/util.h
+++ b/common/util.h
@@ -157,6 +157,7 @@ unsigned char *make_simple_sexp_from_hexstr (const char *line,
size_t *nscanned);
/*-- convert.c --*/
+int hex2bin (const char *string, void *buffer, size_t length);
int hexcolon2bin (const char *string, void *buffer, size_t length);
char *bin2hex (const void *buffer, size_t length, char *stringbuf);
char *bin2hexcolon (const void *buffer, size_t length, char *stringbuf);