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