X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fstr%2Fhex%2Fhex.c;fp=ccan%2Fstr%2Fhex%2Fhex.c;h=6e031779fe87b325670040606fafefdef562c1f4;hb=578c4bfb22dd2df3c8133066b28397725b76734a;hp=fd4074f3d6a4f484e33674fa80b126b0fb1416db;hpb=be24e496578d7c03bc2fd6239887266e82c8d6da;p=ccan diff --git a/ccan/str/hex/hex.c b/ccan/str/hex/hex.c index fd4074f3..6e031779 100644 --- a/ccan/str/hex/hex.c +++ b/ccan/str/hex/hex.c @@ -50,21 +50,17 @@ static char hexchar(unsigned int val) bool hex_encode(const void *buf, size_t bufsize, char *dest, size_t destsize) { - size_t used = 0; + size_t i; - if (destsize < 1) + if (destsize < hex_str_size(bufsize)) return false; - while (used < bufsize) { - unsigned int c = ((const unsigned char *)buf)[used]; - if (destsize < 3) - return false; + for (i = 0; i < bufsize; i++) { + unsigned int c = ((const unsigned char *)buf)[i]; *(dest++) = hexchar(c >> 4); *(dest++) = hexchar(c & 0xF); - used++; - destsize -= 2; } *dest = '\0'; - return used + 1; + return true; }