util: strhex_to_str() fixed to handle '0x' correctly
authorKamen Mazdrashki <kamen.mazdrashki@postpath.com>
Fri, 25 Sep 2009 20:40:55 +0000 (23:40 +0300)
committerMatthias Dieter Wallnöfer <mwallnoefer@yahoo.de>
Thu, 1 Oct 2009 21:12:58 +0000 (23:12 +0200)
lib/util/util.c

index a07f50ae3b681e42b5d882fbc1d43ce3082a4927..fd0e6b8d799c717b0b704bdc95ac0f3b6dd4e84c 100644 (file)
@@ -579,18 +579,18 @@ _PUBLIC_ _PURE_ size_t count_chars(const char *s, char c)
 **/
 _PUBLIC_ size_t strhex_to_str(char *p, size_t p_len, const char *strhex, size_t strhex_len)
 {
-       size_t i;
+       size_t i = 0;
        size_t num_chars = 0;
        uint8_t   lonybble, hinybble;
        const char     *hexchars = "0123456789ABCDEF";
        char           *p1 = NULL, *p2 = NULL;
 
-       for (i = 0; i < strhex_len && strhex[i] != 0; i++) {
-               if (strncasecmp(hexchars, "0x", 2) == 0) {
-                       i++; /* skip two chars */
-                       continue;
-               }
+       /* skip leading 0x prefix */
+       if (strncasecmp(strhex, "0x", 2) == 0) {
+               i += 2; /* skip two chars */
+       }
 
+       for (; i < strhex_len && strhex[i] != 0; i++) {
                if (!(p1 = strchr(hexchars, toupper((unsigned char)strhex[i]))))
                        break;