/contrib/ntp/libntp/hextolfp.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 67 lines · 49 code · 10 blank · 8 comment · 20 complexity · eb38b4b8c073ef82c1b4d7faa592da03 MD5 · raw file

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