X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=lib%2Futils_crypt.c;h=dd7496a3aa490e18903279ed9d0de74526d9ea6a;hb=a9d9a2ad4466432323226f482d4e1d53844c12a3;hp=1fe9555ca56b45dcb51445e20bfa9e08983c67d0;hpb=c0a5293435e44863f95a39dfeb5a0b0c46608b91;p=platform%2Fupstream%2Fcryptsetup.git diff --git a/lib/utils_crypt.c b/lib/utils_crypt.c index 1fe9555..dd7496a 100644 --- a/lib/utils_crypt.c +++ b/lib/utils_crypt.c @@ -397,3 +397,29 @@ out_err: crypt_safe_free(pass); return r; } + +ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc) +{ + char buf[3] = "xx\0", *endp, *bytes; + size_t i, len; + + len = strlen(hex); + if (len % 2) + return -EINVAL; + len /= 2; + + bytes = safe_alloc ? crypt_safe_alloc(len) : malloc(len); + if (!bytes) + return -ENOMEM; + + for (i = 0; i < len; i++) { + memcpy(buf, &hex[i * 2], 2); + bytes[i] = strtoul(buf, &endp, 16); + if (endp != &buf[2]) { + safe_alloc ? crypt_safe_free(bytes) : free(bytes); + return -EINVAL; + } + } + *result = bytes; + return i; +}