]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
utils: chunk_from_hex() skips optional 0x prefix
authorAndreas Steffen <andreas.steffen@strongswan.org>
Mon, 6 Mar 2017 10:51:10 +0000 (11:51 +0100)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Mon, 6 Mar 2017 17:54:09 +0000 (18:54 +0100)
src/libstrongswan/tests/suites/test_chunk.c
src/libstrongswan/utils/chunk.c

index b82b1436f52c1971f5751d685843661f0e421ca5..9b2e48b0ec174513de98517a7360dc2567d188a6 100644 (file)
@@ -397,15 +397,15 @@ START_TEST(test_base16)
                {FALSE, "fooba", "666f6f6261"},
                {FALSE, "foobar", "666f6f626172"},
        };
-       testdata_t test_colon[] = {
-               {TRUE,  "", ""},
-               {TRUE,  "f", "66"},
+       testdata_t test_prefix_colon[] = {
+               {TRUE,  "", "0x"},
+               {TRUE,  "f", "0x66"},
                {TRUE,  "fo", "66:6F"},
-               {TRUE,  "foo", "66:6F:6F"},
+               {TRUE,  "foo", "0x66:6F:6F"},
                {FALSE, "foob", "66:6f:6f:62"},
-               {FALSE, "fooba", "66:6f:6f:62:61"},
+               {FALSE, "fooba", "0x66:6f:6f:62:61"},
                {FALSE, "foobar", "66:6f:6f:62:61:72"},
-               {FALSE, "foobar", "66:6f6f:6261:72"},
+               {FALSE, "foobar", "0x66:6f6f:6261:72"},
        };
        int i;
 
@@ -430,14 +430,15 @@ START_TEST(test_base16)
                free(out.ptr);
        }
 
-       for (i = 0; i < countof(test_colon); i++)
+       for (i = 0; i < countof(test_prefix_colon); i++)
        {
                chunk_t out;
 
-               out = chunk_from_hex(chunk_create(test_colon[i].out, strlen(test_colon[i].out)), NULL);
-               fail_unless(strneq(out.ptr, test_colon[i].in, out.len),
+               out = chunk_from_hex(chunk_create(test_prefix_colon[i].out,
+                                                        strlen(test_prefix_colon[i].out)), NULL);
+               fail_unless(strneq(out.ptr, test_prefix_colon[i].in, out.len),
                                        "base16 conversion error - should '%s', is %#B",
-                                       test_colon[i].in, &out);
+                                       test_prefix_colon[i].in, &out);
                free(out.ptr);
        }
 }
index 2f824a259044b6b4fe9b198b59cf056949f8d559..0c50ab788433e3e9ba642199361cd607e8f4eede 100644 (file)
@@ -504,7 +504,13 @@ chunk_t chunk_from_hex(chunk_t hex, char *buf)
        u_char *ptr;
        bool odd = FALSE;
 
-   /* subtract the number of optional ':' separation characters */
+       /* skip an optional 0x prefix */
+       if (hex.len > 1 && hex.ptr[1] == 'x' && hex.ptr[0] == '0')
+       {
+               hex = chunk_skip(hex, 2);
+       }
+
+       /* subtract the number of optional ':' separation characters */
        len = hex.len;
        ptr = hex.ptr;
        for (i = 0; i < hex.len; i++)