Source code of Windows XP (NT5)
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.

182 lines
3.7 KiB

  1. //-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: A T M U T I L . C P P
  7. //
  8. // Contents: Utility function declaration
  9. //
  10. // Notes:
  11. //
  12. // Author: tongl 3 Feb 1997
  13. //
  14. //-----------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "atmutil.h"
  18. #include "ncmisc.h"
  19. //#include "ncreg.h"
  20. #include "ncstl.h"
  21. void GetLowerIp(tstring& strIpRange, tstring * pstrLowerIp)
  22. {
  23. size_t iSeparator = strIpRange.find(c_chSeparator);
  24. if(iSeparator != tstring::npos)
  25. {
  26. // get the first half of the string
  27. *pstrLowerIp = strIpRange.substr(0, iSeparator);
  28. }
  29. return;
  30. }
  31. void GetUpperIp(tstring& strIpRange, tstring * pstrUpperIp)
  32. {
  33. size_t iSeparator = strIpRange.find(c_chSeparator);
  34. if(iSeparator != tstring::npos)
  35. {
  36. // get the first half of the string
  37. *pstrUpperIp = strIpRange.substr(iSeparator+1);
  38. }
  39. return;
  40. }
  41. BOOL IsValidIpRange(tstring& strIpLower, tstring& strIpUpper)
  42. {
  43. if (strIpLower > strIpUpper)
  44. return FALSE;
  45. else
  46. return TRUE;
  47. }
  48. void MakeIpRange(tstring& strIpLower, tstring& strIpUpper, tstring * pstrNewIpRange)
  49. {
  50. tstring strNewIpRange = strIpLower;
  51. strNewIpRange += c_chSeparator;
  52. strNewIpRange += strIpUpper;
  53. *pstrNewIpRange = strNewIpRange;
  54. return;
  55. }
  56. void InitComboWithStringArray(HWND hDlg, int nIDDlgItem,
  57. int csid, const int* asid)
  58. {
  59. while (csid--)
  60. {
  61. SendDlgItemMessage(hDlg, nIDDlgItem, CB_ADDSTRING,
  62. 0, (LPARAM)((PWSTR) SzLoadIds(* asid++)));
  63. }
  64. }
  65. void ConvertBinaryToHexString(BYTE * pbData, DWORD cbData, tstring * pstrData)
  66. {
  67. Assert(pstrData);
  68. Assert(pbData);
  69. tstring strData = c_szEmpty;
  70. WCHAR szByte[3];
  71. while (cbData>0)
  72. {
  73. ConvertByteToSz(pbData, szByte);
  74. strData += szByte;
  75. pbData ++;
  76. cbData --;
  77. }
  78. *pstrData = strData;
  79. }
  80. void ConvertByteToSz(BYTE * pbData, PWSTR pszByte)
  81. {
  82. // high 4 bits
  83. BYTE bHighData = *pbData;
  84. bHighData &= 0xF0;
  85. bHighData >>= 4;
  86. pszByte[0] = (bHighData < 10) ? L'0'+bHighData : L'A'+(bHighData-10);
  87. // low 4 bits
  88. BYTE bLowData = *pbData;
  89. bLowData &= 0x0F;
  90. pszByte[1] = (bLowData < 10) ? L'0'+bLowData : L'A'+(bLowData-10);
  91. // terminater.
  92. pszByte[2] = L'\0';
  93. }
  94. void ConvertHexCharToByte(WCHAR ch, BYTE * pByte)
  95. {
  96. *pByte =0;
  97. if ((ch >= L'0') && (ch <= L'9'))
  98. *pByte = ch-L'0';
  99. else if ((ch >= L'A') && (ch <= L'F'))
  100. *pByte = ch-L'A'+10;
  101. else if ((ch >= L'a') && (ch <= L'f'))
  102. *pByte = ch-L'a'+10;
  103. else
  104. AssertSz(FALSE, "Invalid hex character.");
  105. }
  106. void ConvertHexStringToBinaryWithAlloc(PCWSTR pszData, LPBYTE * ppbData, LPDWORD pcbData)
  107. {
  108. // Initialize the output parameters.
  109. *ppbData = NULL;
  110. *pcbData = 0;
  111. LPBYTE pbBuffer = NULL;
  112. DWORD cbBuffer = wcslen(pszData)/2;
  113. const WCHAR * pChar = pszData;
  114. if ((*pszData == L'0') &&
  115. ((*(pszData+1) == L'x') || (*(pszData+1) == L'X')))
  116. {
  117. cbBuffer -=2;
  118. pChar +=2;
  119. }
  120. if (cbBuffer)
  121. {
  122. pbBuffer = new BYTE[cbBuffer];
  123. if (pbBuffer == NULL)
  124. {
  125. AssertSz(FALSE, "new returned a NULL pointer");
  126. return;
  127. }
  128. BYTE * pByte = pbBuffer;
  129. if (pByte != NULL)
  130. {
  131. while (*pChar)
  132. {
  133. *pByte=0;
  134. BYTE bData;
  135. // first 4 bits
  136. ConvertHexCharToByte(*pChar, &bData);
  137. bData <<= 4;
  138. *pByte |= bData;
  139. pChar++;
  140. // second 4 bits
  141. ConvertHexCharToByte(*pChar, &bData);
  142. *pByte |= bData;
  143. pChar++;
  144. pByte++;
  145. }
  146. }
  147. }
  148. *ppbData = pbBuffer;
  149. *pcbData = cbBuffer;
  150. }