lib/libc/inet/nsap_addr.c

http://www.minix3.org/ · C · 130 lines · 92 code · 20 blank · 18 comment · 33 complexity · 7154d04b3c21625b320925ba0440c9d7 MD5 · raw file

  1. /* $NetBSD: nsap_addr.c,v 1.6 2009/04/12 17:07:17 christos Exp $ */
  2. /*
  3. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  4. * Copyright (c) 1996-1999 by Internet Software Consortium.
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  16. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/cdefs.h>
  19. #if defined(LIBC_SCCS) && !defined(lint)
  20. #if 0
  21. static const char rcsid[] = "Id: nsap_addr.c,v 1.5 2005/07/28 06:51:48 marka Exp";
  22. #else
  23. __RCSID("$NetBSD: nsap_addr.c,v 1.6 2009/04/12 17:07:17 christos Exp $");
  24. #endif
  25. #endif /* LIBC_SCCS and not lint */
  26. #include "port_before.h"
  27. #include "namespace.h"
  28. #include <sys/types.h>
  29. #include <sys/param.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <arpa/nameser.h>
  34. #include <assert.h>
  35. #include <ctype.h>
  36. #include <resolv.h>
  37. #include <resolv_mt.h>
  38. #include "port_after.h"
  39. #ifdef __weak_alias
  40. __weak_alias(inet_nsap_addr,_inet_nsap_addr)
  41. __weak_alias(inet_nsap_ntoa,_inet_nsap_ntoa)
  42. #endif
  43. static char
  44. xtob(int c) {
  45. return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
  46. }
  47. u_int
  48. inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
  49. u_char c, nib;
  50. u_int len = 0;
  51. _DIAGASSERT(ascii != NULL);
  52. _DIAGASSERT(binary != NULL);
  53. if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
  54. return (0);
  55. ascii += 2;
  56. while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
  57. if (c == '.' || c == '+' || c == '/')
  58. continue;
  59. if (!isascii(c))
  60. return (0);
  61. if (islower(c))
  62. c = toupper(c);
  63. if (isxdigit(c)) {
  64. nib = xtob(c);
  65. c = *ascii++;
  66. if (c != '\0') {
  67. c = toupper(c);
  68. if (isxdigit(c)) {
  69. *binary++ = (nib << 4) | xtob(c);
  70. len++;
  71. } else
  72. return (0);
  73. }
  74. else
  75. return (0);
  76. }
  77. else
  78. return (0);
  79. }
  80. return (len);
  81. }
  82. char *
  83. inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
  84. int nib;
  85. int i;
  86. char *tmpbuf = inet_nsap_ntoa_tmpbuf;
  87. char *start;
  88. _DIAGASSERT(binary != NULL);
  89. if (ascii)
  90. start = ascii;
  91. else {
  92. ascii = tmpbuf;
  93. start = tmpbuf;
  94. }
  95. *ascii++ = '0';
  96. *ascii++ = 'x';
  97. if (binlen > 255)
  98. binlen = 255;
  99. for (i = 0; i < binlen; i++) {
  100. nib = (u_int32_t)*binary >> 4;
  101. *ascii++ = nib + (nib < 10 ? '0' : '7');
  102. nib = *binary++ & 0x0f;
  103. *ascii++ = nib + (nib < 10 ? '0' : '7');
  104. if (((i % 2) == 0 && (i + 1) < binlen))
  105. *ascii++ = '.';
  106. }
  107. *ascii = '\0';
  108. return (start);
  109. }
  110. /*! \file */