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.

120 lines
2.9 KiB

  1. #pragma once
  2. #include <comutil.h>
  3. //----------------------------------------------------------------------------
  4. // Function: EscapeSpecialChars
  5. //
  6. // Synopsis: This function escapes special characters '<' and '>' in HTML.
  7. // It replaces '<' with "&#60" and '>' with "&#62". The purpose
  8. // is to prevent embedded active content.
  9. //
  10. // Arguments:
  11. //
  12. // pszOrig the original unicode string which could contain '<' and '>'.
  13. //
  14. // Returns: returns an escaped sequence; if out of memory, an empty string
  15. // will be returned.
  16. //
  17. // Modifies:
  18. //
  19. //----------------------------------------------------------------------------
  20. _bstr_t EscapeSpecialChars(LPCWSTR pszOrig)
  21. {
  22. _bstr_t result = L"";
  23. if (pszOrig != NULL)
  24. {
  25. static WCHAR specialChars[] = L"<>";
  26. static WCHAR* replacements[] = { L"&#60", L"&#62" };
  27. const int increments = 3;
  28. const int copyLen = increments + 1;
  29. int origLen = wcslen(pszOrig);
  30. const WCHAR* pWChar = pszOrig;
  31. int numOfSpecialChars = 0;
  32. // find out how many special characters we have
  33. while (*pWChar)
  34. {
  35. if (wcschr(specialChars, *pWChar))
  36. numOfSpecialChars++;
  37. pWChar++;
  38. }
  39. // replace each angle bracket with the corresponding special sequence
  40. WCHAR* outputBuffer = new WCHAR[origLen + increments * numOfSpecialChars + 1];
  41. WCHAR* outputString = outputBuffer;
  42. if (outputString)
  43. {
  44. pWChar = pszOrig;
  45. WCHAR* pMatch;
  46. while (*pWChar)
  47. {
  48. if (pMatch = wcschr(specialChars, *pWChar))
  49. {
  50. wcscpy(outputString, replacements[pMatch-specialChars]);
  51. outputString += copyLen;
  52. }
  53. else
  54. {
  55. *outputString = *pWChar;
  56. outputString++;
  57. }
  58. pWChar++;
  59. }
  60. *outputString = L'\0';
  61. result = outputBuffer;
  62. delete[] outputBuffer;
  63. }
  64. }
  65. return result;
  66. }
  67. //---------------------------------------------------------------------------
  68. // CStringUTF8
  69. //---------------------------------------------------------------------------
  70. class CStringUTF8
  71. {
  72. public:
  73. CStringUTF8(LPCWSTR pszOld) :
  74. m_pchNew(NULL)
  75. {
  76. if (pszOld)
  77. {
  78. int cchNew = WideCharToMultiByte(CP_UTF8, 0, pszOld, -1, NULL, 0, NULL, NULL);
  79. m_pchNew = new CHAR[cchNew];
  80. if (m_pchNew)
  81. {
  82. WideCharToMultiByte(CP_UTF8, 0, pszOld, -1, m_pchNew, cchNew, NULL, NULL);
  83. }
  84. }
  85. }
  86. ~CStringUTF8()
  87. {
  88. delete [] m_pchNew;
  89. }
  90. operator LPCSTR()
  91. {
  92. return m_pchNew;
  93. }
  94. protected:
  95. LPSTR m_pchNew;
  96. };
  97. #define WTUTF8(s) static_cast<LPCSTR>(CStringUTF8(s))