ipv4spec.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*====================================================================*
  2. *
  3. * size_t ipv4spec (char const *string, void * memory);
  4. *
  5. * memory.h
  6. *
  7. * encode a 4-byte memory region with the equivalent of an IPv4
  8. * dotted decimal string; all field delimiters must be present
  9. * but individual fields may have leading zeros or be empty;
  10. *
  11. * 0.0.0.0 0x00, 0x00, 0x00, 0x00
  12. * 127...1 0x7F, 0x00, 0x00, 0x01
  13. * 192.168.099.000 0xC0, 0xA8, 0x63, 0x00
  14. *
  15. *. released 2005 by charles maier associates ltd. for public use;
  16. *: compiled on debian gnu/linux with gcc 2.95 compiler;
  17. *; licensed under the gnu public license version two;
  18. *
  19. *--------------------------------------------------------------------*/
  20. #ifndef IPV4SPEC_SOURCE
  21. #define IPV4SPEC_SOURCE
  22. #include <ctype.h>
  23. #include "../tools/memory.h"
  24. #include "../tools/number.h"
  25. #include "../tools/error.h"
  26. size_t ipv4spec (char const * string, void * memory)
  27. {
  28. char const * number = string;
  29. byte * origin = (byte *)(memory);
  30. byte * offset = (byte *)(memory);
  31. byte * extent = offset + IPv4_LEN;
  32. unsigned radix = RADIX_DEC;
  33. unsigned digit = 0;
  34. while ((*number) && (offset < extent))
  35. {
  36. unsigned value = 0;
  37. if (offset > origin)
  38. {
  39. if (*number == DEC_EXTENDER)
  40. {
  41. number++;
  42. }
  43. }
  44. while ((digit = todigit (*number)) < radix)
  45. {
  46. value *= radix;
  47. value += digit;
  48. if (value >> 8)
  49. {
  50. error (1, ERANGE, "IPv4 '%s' octet %d exceeds 8 bits", string, (unsigned)(offset - origin) + 1);
  51. }
  52. number++;
  53. }
  54. *offset++ = value;
  55. }
  56. #if defined (WIN32)
  57. while (isspace (*number))
  58. {
  59. number++;
  60. }
  61. #endif
  62. if (offset < extent)
  63. {
  64. error (1, EINVAL, "IPv4 '%s' has only %d octets", string, (unsigned)(offset - origin));
  65. }
  66. if (*number)
  67. {
  68. error (1, EINVAL, "IPv4 '%s' contains trash '%s'", string, number);
  69. }
  70. return (offset - origin);
  71. }
  72. /*====================================================================*
  73. * demo/test program;
  74. *--------------------------------------------------------------------*/
  75. #if 0
  76. #include <stdio.h>
  77. char const * program_name = "ipv4spec";
  78. int main (int argc, char * argv [])
  79. {
  80. byte memory [4];
  81. char string [16];
  82. while (*++argv)
  83. {
  84. ipv4spec (* argv, memory);
  85. hexdecode (memory, sizeof (memory), string, sizeof (string));
  86. printf ("%s %s\n", string, * argv);
  87. }
  88. return (0);
  89. }
  90. #endif
  91. /*====================================================================*
  92. *
  93. *--------------------------------------------------------------------*/
  94. #endif