diff options
Diffstat (limited to 'src/libstrongswan/chunk.c')
-rw-r--r-- | src/libstrongswan/chunk.c | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/src/libstrongswan/chunk.c b/src/libstrongswan/chunk.c index 7e7a7c69d..565f48fb0 100644 --- a/src/libstrongswan/chunk.c +++ b/src/libstrongswan/chunk.c @@ -13,7 +13,7 @@ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * - * $Id: chunk.c 3868 2008-04-24 13:26:22Z martin $ + * $Id: chunk.c 4276 2008-08-22 10:44:51Z martin $ */ #include <stdio.h> @@ -298,16 +298,28 @@ static char hex2bin(char hex) chunk_t chunk_from_hex(chunk_t hex, char *buf) { int i, len; + bool odd = FALSE; - len = hex.len / 2; + len = (hex.len / 2); + if (hex.len % 2) + { + odd = TRUE; + len++; + } if (!buf) { buf = malloc(len); } - for (i = 0; i < len; i++) + /* buffer is filled from the right */ + memset(buf, 0, len); + hex.ptr += hex.len; + for (i = len - 1; i >= 0; i--) { - buf[i] = hex2bin(*hex.ptr++) << 4; - buf[i] |= hex2bin(*hex.ptr++); + buf[i] = hex2bin(*(--hex.ptr)); + if (i > 0 || !odd) + { + buf[i] |= hex2bin(*(--hex.ptr)) << 4; + } } return chunk_create(buf, len); } |