154359Sroberto/* 254359Sroberto * hextoint - convert an ascii string in hex to an unsigned 354359Sroberto * long, with error checking 454359Sroberto */ 5285612Sdelphij#include <config.h> 654359Sroberto#include <ctype.h> 754359Sroberto 854359Sroberto#include "ntp_stdlib.h" 954359Sroberto 1054359Srobertoint 1154359Srobertohextoint( 1254359Sroberto const char *str, 13285612Sdelphij u_long *pu 1454359Sroberto ) 1554359Sroberto{ 1654359Sroberto register u_long u; 1754359Sroberto register const char *cp; 1854359Sroberto 1954359Sroberto cp = str; 2054359Sroberto 2154359Sroberto if (*cp == '\0') 22285612Sdelphij return 0; 2354359Sroberto 2454359Sroberto u = 0; 2554359Sroberto while (*cp != '\0') { 26285612Sdelphij if (!isxdigit((unsigned char)*cp)) 27285612Sdelphij return 0; 28285612Sdelphij if (u & 0xF0000000) 29285612Sdelphij return 0; /* overflow */ 3054359Sroberto u <<= 4; 31285612Sdelphij if ('0' <= *cp && *cp <= '9') 32285612Sdelphij u += *cp++ - '0'; 33285612Sdelphij else if ('a' <= *cp && *cp <= 'f') 34285612Sdelphij u += *cp++ - 'a' + 10; 35285612Sdelphij else if ('A' <= *cp && *cp <= 'F') 36285612Sdelphij u += *cp++ - 'A' + 10; 3754359Sroberto else 38285612Sdelphij return 0; 3954359Sroberto } 40285612Sdelphij *pu = u; 4154359Sroberto return 1; 4254359Sroberto} 43