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.

360 lines
7.5 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // File: SecStr.hxx
  4. //
  5. // Contents: C++ wrapper classes for SECURITY_STRING structures.
  6. //
  7. //
  8. // History: 24-Feb-93 WadeR Created
  9. //
  10. //------------------------------------------------------------------------
  11. #ifndef _INC_SECSTR_HXX
  12. #define _INC_SECSTR_HXX
  13. ////////////////////////////////////////////////////////////////////////////
  14. ////////////////////////////////////////////////////////////////////////////
  15. //
  16. // Support classes
  17. //
  18. ////////////////////////////////////////////////////////////////////////////
  19. ////////////////////////////////////////////////////////////////////////////
  20. class CIntSecString {
  21. friend class CSecString;
  22. SECURITY_STRING _ssString;
  23. LONG _cRefs;
  24. inline CIntSecString();
  25. };
  26. inline CIntSecString::CIntSecString()
  27. {
  28. _ssString.Buffer = 0;
  29. _ssString.Length = _ssString.MaximumLength = 0;
  30. _cRefs = 1;
  31. }
  32. ////////////////////////////////////////////////////////////////////
  33. //
  34. // Name: CSecString
  35. //
  36. // Synopsis: Wrapper for a SECURITY_STRING.
  37. //
  38. // Methods: ~CSecString()
  39. // CSecString()
  40. // CSecString(PWCHAR)
  41. // CSecString(PCHAR)
  42. // CSecString(CSecString&)
  43. // CSecString(SECURITY_STRING&)
  44. // ulong GetSizeToMarshall()
  45. // SCODE Marshall(PBYTE) - marshals into the buffer
  46. // SCODE Unmarshall(PBYTE)
  47. // implicit conversions to PWCHAR, PSECURITY_STRING, SECURITY_STRING
  48. // assignment operator from PWCHAR, SECURITY_STRING&, CSecString&
  49. // ==,>,< operators for above types.
  50. //
  51. // Notes: This class keeps a reference count to a security string, and
  52. // assignment increments the ref count. When the last CSecString
  53. // that refers to a particular SECURITY_STRING is destroyed, the
  54. // SECURITY_STRING is freed.
  55. //
  56. // This class will implicity convert to either a SECURITY_STRING or
  57. // to a PSECURITY_STRING. This means if you have a function:
  58. // Foo1 ( SECURITY_STRING ss, PSECURITY_STRING pss );
  59. // and two CSecStrings, SecStr1, SecStr2, you call it foo like:
  60. // Foo1 ( SecStr1, SecStr2 );
  61. // (ie. you don't use "&SecStr2" like you would for a SECURITY_STRING.)
  62. //
  63. class CSecString {
  64. private:
  65. struct MarshalledData {
  66. USHORT cbLength;
  67. BYTE abData[1];
  68. };
  69. CIntSecString *_iss;
  70. void Destructor();
  71. void Constructor( CHAR * );
  72. void Constructor( WCHAR * );
  73. void Constructor( CSecString& );
  74. void Constructor( SECURITY_STRING& );
  75. public:
  76. //
  77. // Destructor
  78. //
  79. inline ~CSecString();
  80. //
  81. // Constructors
  82. //
  83. inline CSecString();
  84. inline CSecString( PCHAR );
  85. inline CSecString( PWCHAR );
  86. inline CSecString( CSecString& );
  87. inline CSecString( SECURITY_STRING& );
  88. //
  89. // Marshalling and unmarshalling code.
  90. //
  91. SECURITY_STATUS Unmarshall( PBYTE pb );
  92. SECURITY_STATUS Marshall( PBYTE pb );
  93. ULONG GetSizeToMarshall();
  94. //
  95. // Some useful conversions
  96. //
  97. inline operator PWCHAR ();
  98. inline operator SECURITY_STRING ();
  99. inline operator PSECURITY_STRING ();
  100. //
  101. // Access to elements
  102. //
  103. inline USHORT GetLength();
  104. inline USHORT GetMaximumLength();
  105. inline PWCHAR GetBuffer();
  106. //
  107. // Finally, some operators
  108. //
  109. // &
  110. //inline PSECURITY_STRING operator&();
  111. // =
  112. inline CSecString& operator=( PWCHAR r );
  113. inline CSecString& operator=( CSecString& r );
  114. inline CSecString& operator=( SECURITY_STRING& r );
  115. // ==
  116. inline int operator==( SECURITY_STRING& r );
  117. inline int operator==( CSecString& r );
  118. //inline int operator==( PWCHAR r );
  119. inline int operator==( PVOID pv ); // used to compare to NULL
  120. inline int IsEqual( PWCHAR r );
  121. // <
  122. inline int operator<( SECURITY_STRING& r );
  123. inline int operator<( CSecString& r );
  124. inline int operator<( PWCHAR r );
  125. // >
  126. inline int operator>( SECURITY_STRING& r );
  127. inline int operator>( CSecString& r );
  128. inline int operator>( PWCHAR r );
  129. };
  130. //
  131. // Destructor
  132. //
  133. inline CSecString::~CSecString()
  134. {
  135. Destructor();
  136. }
  137. //
  138. // Constructors
  139. //
  140. inline CSecString::CSecString()
  141. {
  142. _iss = new CIntSecString;
  143. }
  144. inline CSecString::CSecString( CHAR *pc)
  145. {
  146. Constructor( pc );
  147. }
  148. inline CSecString::CSecString( WCHAR *pwc )
  149. {
  150. Constructor( pwc );
  151. }
  152. inline CSecString::CSecString( CSecString& css )
  153. {
  154. Constructor( css );
  155. }
  156. inline CSecString::CSecString( SECURITY_STRING& ss )
  157. {
  158. Constructor( ss );
  159. }
  160. //
  161. // Some useful conversions
  162. //
  163. inline CSecString::operator PWCHAR ()
  164. {
  165. return(_iss->_ssString.Buffer);
  166. }
  167. inline CSecString::operator SECURITY_STRING ()
  168. {
  169. return(_iss->_ssString);
  170. }
  171. inline CSecString::operator PSECURITY_STRING ()
  172. {
  173. return(&_iss->_ssString);
  174. }
  175. //
  176. // Access to elements
  177. //
  178. inline USHORT CSecString::GetLength()
  179. {
  180. return(_iss->_ssString.Length);
  181. }
  182. inline USHORT CSecString::GetMaximumLength()
  183. {
  184. return(_iss->_ssString.MaximumLength);
  185. }
  186. inline PWCHAR CSecString::GetBuffer()
  187. {
  188. return(_iss->_ssString.Buffer);
  189. }
  190. //
  191. // Finally, some operators
  192. //
  193. // &
  194. // This is a neat idea, but it's too strange for the '&' operator to return
  195. // anything but "pointer to CSecString".
  196. //inline PSECURITY_STRING operator&()
  197. //{
  198. // return(&_iss->_ssString);
  199. //}
  200. // =
  201. inline CSecString& CSecString::operator=( PWCHAR r )
  202. {
  203. Destructor(); // destroy this.
  204. Constructor( r );
  205. return(*this);
  206. }
  207. inline CSecString& CSecString::operator=( CSecString& r )
  208. {
  209. Destructor(); // destroy this.
  210. Constructor( r );
  211. return(*this);
  212. }
  213. inline CSecString& CSecString::operator=( SECURITY_STRING& r )
  214. {
  215. Destructor(); // destroy this.
  216. Constructor( r );
  217. return(*this);
  218. }
  219. // ==
  220. inline int CSecString::operator==( SECURITY_STRING& r )
  221. {
  222. return( SRtlCompareString( &_iss->_ssString, &r,
  223. SRTL_CASE_INSENSITIVE) == 0 );
  224. }
  225. inline int CSecString::operator==( CSecString& r )
  226. {
  227. return( SRtlCompareString( &_iss->_ssString, &r._iss->_ssString,
  228. SRTL_CASE_INSENSITIVE) == 0 );
  229. }
  230. inline int CSecString::IsEqual( PWCHAR r )
  231. {
  232. return( wcsnicmp(_iss->_ssString.Buffer, r, _iss->_ssString.Length ) == 0 );
  233. }
  234. inline int CSecString::operator==( PVOID pv )
  235. {
  236. return((PVOID)_iss == pv);
  237. }
  238. // <
  239. inline int CSecString::operator<( SECURITY_STRING& r )
  240. {
  241. return( SRtlCompareString( &_iss->_ssString, &r,
  242. SRTL_CASE_INSENSITIVE) < 0 );
  243. }
  244. inline int CSecString::operator<( CSecString& r )
  245. {
  246. return( SRtlCompareString( &_iss->_ssString, &r._iss->_ssString,
  247. SRTL_CASE_INSENSITIVE) < 0 );
  248. }
  249. inline int CSecString::operator<( PWCHAR r )
  250. {
  251. return( wcsnicmp(_iss->_ssString.Buffer, r, _iss->_ssString.Length ) < 0 );
  252. }
  253. // >
  254. inline int CSecString::operator>( SECURITY_STRING& r )
  255. {
  256. return( SRtlCompareString( &_iss->_ssString, &r,
  257. SRTL_CASE_INSENSITIVE) > 0 );
  258. }
  259. inline int CSecString::operator>( CSecString& r )
  260. {
  261. return( SRtlCompareString( &_iss->_ssString, &r._iss->_ssString,
  262. SRTL_CASE_INSENSITIVE) > 0 );
  263. }
  264. inline int CSecString::operator>( PWCHAR r )
  265. {
  266. return( wcsnicmp(_iss->_ssString.Buffer, r, _iss->_ssString.Length ) > 0 );
  267. }
  268. #endif // _INC_SECSTR_HXX