From ce11cc39ea7e011040debc9339a2310a714efe7e Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 23 Apr 2015 14:31:04 +0200 Subject: 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 --- common/t-convert.c | 71 +++++++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 35 deletions(-) (limited to 'common/t-convert.c') diff --git a/common/t-convert.c b/common/t-convert.c index d6056b9a6..a03c680ad 100644 --- a/common/t-convert.c +++ b/common/t-convert.c @@ -27,7 +27,7 @@ #define pass() do { ; } while(0) #define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\ __FILE__,__LINE__, (a)); \ - exit (1); \ + /*exit (1)*/; \ } while(0) @@ -282,73 +282,74 @@ test_hex2str (void) static struct { const char *hex; const char *str; + int len; /* Length of STR. This may included embedded nuls. */ int off; int no_alloc_test; } tests[] = { /* Simple tests. */ { "112233445566778899aabbccddeeff1122", "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22", - 34 }, + 17, 34 }, { "112233445566778899aabbccddeeff1122 blah", "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22", - 34 }, + 17, 34 }, { "112233445566778899aabbccddeeff1122\tblah", "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22", - 34 }, + 17, 34 }, { "112233445566778899aabbccddeeff1122\nblah", "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22", - 34 }, + 17, 34 }, /* Valid tests yielding an empty string. */ { "00", "", - 2 }, + 1, 2 }, { "00 x", "", - 2 }, + 1, 2 }, { "", "", - 0 }, + 0, 0 }, { " ", "", - 0 }, + 0, 0 }, /* Test trailing Nul feature. */ - { "112233445566778899aabbccddeeff112200", - "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22", - 36 }, - { "112233445566778899aabbccddeeff112200 ", - "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22", - 36 }, + { "112233445566778899aabbccddeeff1100", + "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x00", + 17, 34 }, + { "112233445566778899aabbccddeeff1100 ", + "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x00", + 17, 34 }, /* Test buffer size. (buffer is of length 20) */ { "6162636465666768696A6b6c6D6e6f70717273", "abcdefghijklmnopqrs", - 38 }, + 19, 38 }, { "6162636465666768696A6b6c6D6e6f7071727300", "abcdefghijklmnopqrs", - 40 }, + 20, 40 }, { "6162636465666768696A6b6c6D6e6f7071727374", NULL, - 0, 1 }, + 0, 0, 1 }, { "6162636465666768696A6b6c6D6e6f707172737400", NULL, - 0, 1 }, + 0, 0, 1 }, { "6162636465666768696A6b6c6D6e6f707172737475", NULL, - 0, 1 }, + 0, 0, 1 }, /* Invalid tests. */ - { "112233445566778899aabbccddeeff1122334", NULL, 0 }, - { "112233445566778899AABBCCDDEEFF1122334", NULL, 0 }, - { "112233445566778899AABBCCDDEEFG11223344", NULL, 0 }, - { "0:0112233445566778899aabbccddeeff11223344", NULL, 0 }, - { "112233445566778899aabbccddeeff11223344:", NULL, 0 }, - { "112233445566778899aabbccddeeff112233445", NULL, 0 }, - { "112233445566778899aabbccddeeff1122334455", NULL, 0, 1 }, - { "112233445566778899aabbccddeeff11223344blah", NULL, 0 }, - { "0", NULL, 0 }, - { "00:", NULL, 0 }, - { "00x", NULL, 0 }, - - { NULL, NULL, 0 } + { "112233445566778899aabbccddeeff1122334", NULL, 0, 0 }, + { "112233445566778899AABBCCDDEEFF1122334", NULL, 0, 0 }, + { "112233445566778899AABBCCDDEEFG11223344", NULL, 0, 0 }, + { "0:0112233445566778899aabbccddeeff11223344", NULL, 0, 0 }, + { "112233445566778899aabbccddeeff11223344:", NULL, 0, 0 }, + { "112233445566778899aabbccddeeff112233445", NULL, 0, 0 }, + { "112233445566778899aabbccddeeff1122334455", NULL, 0, 0, 1 }, + { "112233445566778899aabbccddeeff11223344blah", NULL, 0, 0 }, + { "0", NULL, 0, 0 }, + { "00:", NULL, 0, 0 }, + { "00x", NULL, 0, 0 }, + + { NULL, NULL, 0, 0 } }; int idx; @@ -369,7 +370,7 @@ test_hex2str (void) fail (idx); else if (tail - tests[idx].hex != tests[idx].off) fail (idx); - else if (strlen (buffer) != count) + else if (tests[idx].len != count) fail (idx); } else @@ -400,7 +401,7 @@ test_hex2str (void) fail (idx); else if (tail - tmpbuf != tests[idx].off) fail (idx); - else if (strlen (tmpbuf) != count) + else if (tests[idx].len != count) fail (idx); } else -- cgit v1.2.3