]> git.ozlabs.org Git - ccan/commitdiff
Merge remote-tracking branch 'origin/pr/45'
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 23 Aug 2016 03:33:10 +0000 (13:03 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 23 Aug 2016 03:33:10 +0000 (13:03 +0930)
Closes: 45
ccan/str/hex/hex.c

index fd4074f3d6a4f484e33674fa80b126b0fb1416db..6e031779fe87b325670040606fafefdef562c1f4 100644 (file)
@@ -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;
 }