util: strhex_to_str() fixed to handle '0x' correctly
[ira/wip.git] / lib / util / util.c
index 2a809d3ccb021d077e99e0c8a08b475ba2eb6dee..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;
 
@@ -804,8 +804,8 @@ static bool next_token_internal_talloc(TALLOC_CTX *ctx,
                                 const char *sep,
                                 bool ltrim)
 {
-       char *s;
-       char *saved_s;
+       const char *s;
+       const char *saved_s;
        char *pbuf;
        bool quoted;
        size_t len=1;
@@ -815,7 +815,7 @@ static bool next_token_internal_talloc(TALLOC_CTX *ctx,
                return(false);
        }
 
-       s = (char *)*ptr;
+       s = *ptr;
 
        /* default to simple separators */
        if (!sep) {