You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

misc.c 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**************************************************************************
  2. MISC Support Routines
  3. **************************************************************************/
  4. FILE_LICENCE ( GPL2_OR_LATER );
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <byteswap.h>
  8. #include <ipxe/in.h>
  9. #include <ipxe/timer.h>
  10. /**************************************************************************
  11. INET_ATON - Convert an ascii x.x.x.x to binary form
  12. **************************************************************************/
  13. int inet_aton ( const char *cp, struct in_addr *inp ) {
  14. const char *p = cp;
  15. const char *digits_start;
  16. unsigned long ip = 0;
  17. unsigned long val;
  18. int j;
  19. for(j = 0; j <= 3; j++) {
  20. digits_start = p;
  21. val = strtoul(p, ( char ** ) &p, 10);
  22. if ((p == digits_start) || (val > 255)) return 0;
  23. if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
  24. ip = (ip << 8) | val;
  25. }
  26. if ( *p == '\0' ) {
  27. inp->s_addr = htonl(ip);
  28. return 1;
  29. }
  30. return 0;
  31. }
  32. unsigned int strtoul_charval ( unsigned int charval ) {
  33. if ( charval >= 'a' ) {
  34. charval = ( charval - 'a' + 10 );
  35. } else if ( charval >= 'A' ) {
  36. charval = ( charval - 'A' + 10 );
  37. } else if ( charval <= '9' ) {
  38. charval = ( charval - '0' );
  39. }
  40. return charval;
  41. }
  42. unsigned long strtoul ( const char *p, char **endp, int base ) {
  43. unsigned long ret = 0;
  44. int negative = 0;
  45. unsigned int charval;
  46. while ( isspace ( *p ) )
  47. p++;
  48. if ( *p == '-' ) {
  49. negative = 1;
  50. p++;
  51. }
  52. base = strtoul_base ( &p, base );
  53. while ( 1 ) {
  54. charval = strtoul_charval ( *p );
  55. if ( charval >= ( unsigned int ) base )
  56. break;
  57. ret = ( ( ret * base ) + charval );
  58. p++;
  59. }
  60. if ( negative )
  61. ret = -ret;
  62. if ( endp )
  63. *endp = ( char * ) p;
  64. return ( ret );
  65. }
  66. /*
  67. * Local variables:
  68. * c-basic-offset: 8
  69. * End:
  70. */