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.

118 lines
3.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 2002
  6. //
  7. // File: ldaputil.h
  8. //
  9. // Summary:
  10. // Contains utility functions for working with LDAP.
  11. //
  12. // Notes:
  13. // You will need to build with 'USE_WTL=1' in your sources file.
  14. //
  15. //--------------------------------------------------------------------------
  16. #ifndef _LDAP_UTIL_
  17. #define _LDAP_UTIL_
  18. #include <string> // Need this for wstring.
  19. using namespace std;
  20. //
  21. // Forward declarations (list all functions declared in this file here).
  22. //
  23. void
  24. LdapEscape(IN wstring& input, OUT wstring& strFilter);
  25. //
  26. // Implementations.
  27. //
  28. //+--------------------------------------------------------------------------
  29. //
  30. // Function: LdapEscape
  31. //
  32. // Synopsis: Escape the characters in *[pszInput] as required by
  33. // RFC 2254.
  34. //
  35. // Arguments: [input] - string to escape
  36. // [strFilter] - input string but with special characters escaped;
  37. //
  38. // History: 06-23-2000 DavidMun Created
  39. // 2002/04/24 ArtM Changed interface to use wstring.
  40. //
  41. // Notes: RFC 2254
  42. //
  43. // If a value should contain any of the following characters
  44. //
  45. // Character ASCII value
  46. // ---------------------------
  47. // * 0x2a
  48. // ( 0x28
  49. // ) 0x29
  50. // \ 0x5c
  51. // NUL 0x00
  52. //
  53. // the character must be encoded as the backslash '\'
  54. // character (ASCII 0x5c) followed by the two hexadecimal
  55. // digits representing the ASCII value of the encoded
  56. // character. The case of the two hexadecimal digits is not
  57. // significant.
  58. //
  59. // More Notes: Passing LPCWSTR as input parameter.
  60. // The compiler will automatically create a temporary wstring
  61. // object if you pass a LPCWSTR for input. The wstring constructor
  62. // that takes a WCHAR* will croak most egregiously though if you pass
  63. // it NULL, so make sure you know a pointer is not NULL before passing
  64. // it to this function.
  65. //
  66. //---------------------------------------------------------------------------
  67. void
  68. LdapEscape(const wstring& input, OUT wstring& strFilter)
  69. {
  70. // Normally I would call strFilter.clear() to do this but for
  71. // some reason the compiler cannot find that function for
  72. // wstring (even though it is in the documentation).
  73. strFilter = L"";
  74. wstring::size_type iLen = input.length();
  75. for( int i = 0; i < iLen; ++i)
  76. {
  77. switch (input[i])
  78. {
  79. case L'*':
  80. strFilter += L"\\2a";
  81. break;
  82. case L'(':
  83. strFilter += L"\\28";
  84. break;
  85. case L')':
  86. strFilter += L"\\29";
  87. break;
  88. case L'\\':
  89. strFilter += L"\\5c";
  90. break;
  91. default:
  92. // If it is not a special character, simply append
  93. // it to the end of the filtered string.
  94. strFilter += input[i];
  95. break;
  96. } // end switch
  97. }
  98. } // LdapEscape()
  99. #endif //_LDAP_UTIL_