/nsprpub/pr/src/misc/praton.c

https://github.com/diogogmt/mozilla-central · C · 198 lines · 96 code · 12 blank · 90 comment · 38 complexity · bae07090d6dba794d129747d7fb6f85d MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*******************************************************************************
  3. * The following function pr_inet_aton is based on the BSD function inet_aton
  4. * with some modifications. The license and copyright notices applying to this
  5. * function appear below. Modifications are also according to the license below.
  6. ******************************************************************************/
  7. #include "prnetdb.h"
  8. /*
  9. * Copyright (c) 1983, 1990, 1993
  10. * The Regents of the University of California. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. /*
  37. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  38. *
  39. * Permission to use, copy, modify, and distribute this software for any
  40. * purpose with or without fee is hereby granted, provided that the above
  41. * copyright notice and this permission notice appear in all copies, and that
  42. * the name of Digital Equipment Corporation not be used in advertising or
  43. * publicity pertaining to distribution of the document or software without
  44. * specific, written prior permission.
  45. *
  46. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  47. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  48. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  49. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  50. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  51. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  52. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  53. * SOFTWARE.
  54. */
  55. /*
  56. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  57. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  58. *
  59. * Permission to use, copy, modify, and distribute this software for any
  60. * purpose with or without fee is hereby granted, provided that the above
  61. * copyright notice and this permission notice appear in all copies.
  62. *
  63. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  64. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  65. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  66. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  67. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  68. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  69. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  70. */
  71. #define XX 127
  72. static const unsigned char index_hex[256] = {
  73. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  74. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  75. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  76. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,XX,XX, XX,XX,XX,XX,
  77. XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  78. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  79. XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  80. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  81. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  82. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  83. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  84. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  85. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  86. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  87. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  88. XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
  89. };
  90. static PRBool _isdigit(char c) { return c >= '0' && c <= '9'; }
  91. static PRBool _isxdigit(char c) { return index_hex[(unsigned char) c] != XX; }
  92. static PRBool _isspace(char c) { return c == ' ' || (c >= '\t' && c <= '\r'); }
  93. #undef XX
  94. int
  95. pr_inet_aton(const char *cp, PRUint32 *addr)
  96. {
  97. PRUint32 val;
  98. int base, n;
  99. char c;
  100. PRUint8 parts[4];
  101. PRUint8 *pp = parts;
  102. int digit;
  103. c = *cp;
  104. for (;;) {
  105. /*
  106. * Collect number up to ``.''.
  107. * Values are specified as for C:
  108. * 0x=hex, 0=octal, isdigit=decimal.
  109. */
  110. if (!_isdigit(c))
  111. return (0);
  112. val = 0; base = 10; digit = 0;
  113. if (c == '0') {
  114. c = *++cp;
  115. if (c == 'x' || c == 'X')
  116. base = 16, c = *++cp;
  117. else {
  118. base = 8;
  119. digit = 1;
  120. }
  121. }
  122. for (;;) {
  123. if (_isdigit(c)) {
  124. if (base == 8 && (c == '8' || c == '9'))
  125. return (0);
  126. val = (val * base) + (c - '0');
  127. c = *++cp;
  128. digit = 1;
  129. } else if (base == 16 && _isxdigit(c)) {
  130. val = (val << 4) + index_hex[(unsigned char) c];
  131. c = *++cp;
  132. digit = 1;
  133. } else
  134. break;
  135. }
  136. if (c == '.') {
  137. /*
  138. * Internet format:
  139. * a.b.c.d
  140. * a.b.c (with c treated as 16 bits)
  141. * a.b (with b treated as 24 bits)
  142. */
  143. if (pp >= parts + 3 || val > 0xffU)
  144. return (0);
  145. *pp++ = val;
  146. c = *++cp;
  147. } else
  148. break;
  149. }
  150. /*
  151. * Check for trailing characters.
  152. */
  153. if (c != '\0' && !_isspace(c))
  154. return (0);
  155. /*
  156. * Did we get a valid digit?
  157. */
  158. if (!digit)
  159. return (0);
  160. /*
  161. * Concoct the address according to
  162. * the number of parts specified.
  163. */
  164. n = pp - parts + 1;
  165. switch (n) {
  166. case 1: /*%< a -- 32 bits */
  167. break;
  168. case 2: /*%< a.b -- 8.24 bits */
  169. if (val > 0xffffffU)
  170. return (0);
  171. val |= parts[0] << 24;
  172. break;
  173. case 3: /*%< a.b.c -- 8.8.16 bits */
  174. if (val > 0xffffU)
  175. return (0);
  176. val |= (parts[0] << 24) | (parts[1] << 16);
  177. break;
  178. case 4: /*%< a.b.c.d -- 8.8.8.8 bits */
  179. if (val > 0xffU)
  180. return (0);
  181. val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  182. break;
  183. }
  184. *addr = PR_htonl(val);
  185. return (1);
  186. }