Leaked source code of windows server 2003
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.

127 lines
3.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: N C I P A D D R . C P P
  7. //
  8. // Contents: WCHAR support for Winsock inet_ functions.
  9. //
  10. // Notes:
  11. //
  12. // Author: shaunco 24 Mar 1997
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <pch.h>
  16. #pragma hdrstop
  17. #include "ncipaddr.h"
  18. VOID
  19. IpHostAddrToPsz(
  20. IN DWORD dwAddr,
  21. OUT PWSTR pszBuffer)
  22. {
  23. BYTE* pb = (BYTE*)&dwAddr;
  24. static const WCHAR c_szIpAddr [] = L"%d.%d.%d.%d";
  25. wsprintfW (pszBuffer, c_szIpAddr, pb[3], pb[2], pb[1], pb[0]);
  26. }
  27. DWORD
  28. IpPszToHostAddr(
  29. IN PCWSTR cp)
  30. {
  31. DWORD val, base, n;
  32. WCHAR c;
  33. DWORD parts[4], *pp = parts;
  34. again:
  35. // Collect number up to ``.''.
  36. // Values are specified as for C:
  37. // 0x=hex, 0=octal, other=decimal.
  38. //
  39. val = 0; base = 10;
  40. if (*cp == L'0')
  41. {
  42. base = 8, cp++;
  43. }
  44. if (*cp == L'x' || *cp == L'X')
  45. {
  46. base = 16, cp++;
  47. }
  48. while (c = *cp)
  49. {
  50. if ((c >= L'0') && (c <= L'9'))
  51. {
  52. val = (val * base) + (c - L'0');
  53. cp++;
  54. continue;
  55. }
  56. if ((base == 16) &&
  57. ( ((c >= L'0') && (c <= L'9')) ||
  58. ((c >= L'A') && (c <= L'F')) ||
  59. ((c >= L'a') && (c <= L'f')) ))
  60. {
  61. val = (val << 4) + (c + 10 - (
  62. ((c >= L'a') && (c <= L'f'))
  63. ? L'a'
  64. : L'A' ) );
  65. cp++;
  66. continue;
  67. }
  68. break;
  69. }
  70. if (*cp == L'.')
  71. {
  72. // Internet format:
  73. // a.b.c.d
  74. // a.b.c (with c treated as 16-bits)
  75. // a.b (with b treated as 24 bits)
  76. //
  77. if (pp >= parts + 3)
  78. {
  79. return (DWORD) -1;
  80. }
  81. *pp++ = val, cp++;
  82. goto again;
  83. }
  84. // Check for trailing characters.
  85. //
  86. if (*cp && (*cp != L' '))
  87. {
  88. return 0xffffffff;
  89. }
  90. *pp++ = val;
  91. // Concoct the address according to
  92. // the number of parts specified.
  93. //
  94. n = (DWORD)(pp - parts);
  95. switch (n)
  96. {
  97. case 1: // a -- 32 bits
  98. val = parts[0];
  99. break;
  100. case 2: // a.b -- 8.24 bits
  101. val = (parts[0] << 24) | (parts[1] & 0xffffff);
  102. break;
  103. case 3: // a.b.c -- 8.8.16 bits
  104. val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
  105. (parts[2] & 0xffff);
  106. break;
  107. case 4: // a.b.c.d -- 8.8.8.8 bits
  108. val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
  109. ((parts[2] & 0xff) << 8) | (parts[3] & 0xff);
  110. break;
  111. default:
  112. return 0xffffffff;
  113. }
  114. return val;
  115. }