1 /* 2 * hextolfp - convert an ascii hex string to an l_fp number 3 */ 4 #include <config.h> 5 #include <stdio.h> 6 #include <ctype.h> 7 8 #include "ntp_fp.h" 9 #include "ntp_string.h" 10 #include "ntp_stdlib.h" 11 12 int 13 hextolfp( 14 const char *str, 15 l_fp *lfp 16 ) 17 { 18 register const char *cp; 19 register const char *cpstart; 20 register u_long dec_i; 21 register u_long dec_f; 22 char *ind = NULL; 23 static const char *digits = "0123456789abcdefABCDEF"; 24 25 dec_i = dec_f = 0; 26 cp = str; 27 28 /* 29 * We understand numbers of the form: 30 * 31 * [spaces]8_hex_digits[.]8_hex_digits[spaces|\n|\0] 32 */ 33 while (isspace((unsigned char)*cp)) 34 cp++; 35 36 cpstart = cp; 37 while (*cp != '\0' && (cp - cpstart) < 8 && 38 (ind = strchr(digits, *cp)) != NULL) { 39 dec_i = dec_i << 4; /* multiply by 16 */ 40 dec_i += ((ind - digits) > 15) ? (ind - digits) - 6 41 : (ind - digits); 42 cp++; 43 } 44 45 if ((cp - cpstart) < 8 || ind == NULL) 46 return 0; 47 if (*cp == '.') 48 cp++; 49 50 cpstart = cp; 51 while (*cp != '\0' && (cp - cpstart) < 8 && 52 (ind = strchr(digits, *cp)) != NULL) { 53 dec_f = dec_f << 4; /* multiply by 16 */ 54 dec_f += ((ind - digits) > 15) ? (ind - digits) - 6 55 : (ind - digits); 56 cp++; 57 } 58 59 if ((cp - cpstart) < 8 || ind == NULL) 60 return 0; 61 62 if (*cp != '\0' && !isspace((unsigned char)*cp)) 63 return 0; 64 65 lfp->l_ui = dec_i; 66 lfp->l_uf = dec_f; 67 return 1; 68 } 69