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.

418 lines
9.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992.
  5. //
  6. // File: string.cxx
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 13-May-92 PeterWi Created string.c
  15. // 16-Mar-93 WadeR ported to C++
  16. //
  17. //--------------------------------------------------------------------------
  18. #include <secpch2.hxx>
  19. #pragma hdrstop
  20. // Local Headers
  21. //
  22. // String manipulation routines
  23. //+---------------------------------------------------------------------------
  24. //
  25. // Function: SRtlInitStringX
  26. //
  27. // Synopsis: Initializes a string to a passed pointer
  28. //
  29. // Arguments: [pDest] --
  30. // [pSrc] --
  31. //
  32. // History: 4-25-95 RichardW Created
  33. //
  34. // Notes: Here for Win32 compatibility
  35. //
  36. //----------------------------------------------------------------------------
  37. extern "C"
  38. void
  39. SRtlInitStringX(
  40. PSECURITY_STRING pDest,
  41. LPWSTR pSrc)
  42. {
  43. pDest->Buffer = pSrc;
  44. pDest->Length = wcslen(pSrc) * sizeof(WCHAR);
  45. pDest->MaximumLength = pDest->Length + sizeof(WCHAR);
  46. }
  47. //+-------------------------------------------------------------------------
  48. //
  49. // Function: SRtlDuplicateString
  50. //
  51. // Synopsis: Makes a copy of the string referenced by pSrc
  52. //
  53. // Effects: Memory is allocated from the process heap.
  54. // An exact copy is made, except that:
  55. // Buffer is zero'ed
  56. // Length bytes are copied
  57. //
  58. // Arguments:
  59. //
  60. // Requires:
  61. //
  62. // Returns:
  63. //
  64. // Notes: Will null terminate the string, growing it if needed.
  65. //
  66. //--------------------------------------------------------------------------
  67. void
  68. SRtlDuplicateString(PSECURITY_STRING pDest,
  69. PSECURITY_STRING pSrc)
  70. {
  71. ULONG cb = max( pSrc->MaximumLength, pSrc->Length + sizeof(WCHAR));
  72. pDest->Buffer = (PWSTR) LocalAlloc( LMEM_FIXED | LMEM_ZEROINIT, cb);
  73. if (pDest->Buffer)
  74. {
  75. pDest->MaximumLength = (USHORT) cb;
  76. CopyMemory(pDest->Buffer, pSrc->Buffer, pSrc->Length);
  77. pDest->Length = pSrc->Length;
  78. pDest->Buffer[pDest->Length/sizeof(WCHAR)] = L'\0';
  79. }
  80. else
  81. {
  82. pDest->MaximumLength = pDest->Length = 0;
  83. }
  84. }
  85. //+-------------------------------------------------------------------------
  86. //
  87. // Function: SRtlDuplicateBuffer
  88. //
  89. // Synopsis: Makes a copy of the buffer referenced by pSrc
  90. //
  91. // Effects: Memory is allocated from the process heap.
  92. // An exact copy is made, except that:
  93. // Buffer is zero'ed
  94. // Length bytes are copied
  95. //
  96. // Arguments:
  97. //
  98. // Requires:
  99. //
  100. // Returns:
  101. //
  102. // Notes:
  103. //
  104. //--------------------------------------------------------------------------
  105. void
  106. SRtlDuplicateBuffer(PSTRING pDest,
  107. PSTRING pSrc)
  108. {
  109. pDest->Length = 0;
  110. pDest->Buffer = (PCHAR) LocalAlloc( LMEM_FIXED | LMEM_ZEROINIT,
  111. pSrc->MaximumLength);
  112. if (pDest->Buffer)
  113. {
  114. ZeroMemory(pDest->Buffer, pSrc->MaximumLength);
  115. pDest->MaximumLength = pSrc->MaximumLength;
  116. pDest->Length = pSrc->Length;
  117. CopyMemory(pDest->Buffer, pSrc->Buffer, pSrc->Length);
  118. } else
  119. {
  120. pDest->MaximumLength = 0;
  121. }
  122. }
  123. //+-------------------------------------------------------------------------
  124. //
  125. // Function: SRtlNewString
  126. //
  127. // Synopsis: Creates a COPY of the wide character string pointer pSrc
  128. // and sets up a SECURITY_STRING appropriately
  129. //
  130. //
  131. // Effects: Memory is allocated off of the process heap
  132. // The string remains null terminated.
  133. //
  134. //
  135. // Arguments:
  136. //
  137. // Requires:
  138. //
  139. // Returns:
  140. //
  141. // Notes:
  142. //
  143. //--------------------------------------------------------------------------
  144. void
  145. SRtlNewString( PSECURITY_STRING pDest,
  146. LPWSTR pSrc)
  147. {
  148. SECURITY_STRING Source;
  149. Source.Length = wcslen((wchar_t *) pSrc) * sizeof(wchar_t);
  150. Source.MaximumLength = Source.Length + 2;
  151. Source.Buffer = pSrc;
  152. SRtlDuplicateString(pDest, &Source);
  153. }
  154. //+-------------------------------------------------------------------------
  155. //
  156. // Function: SRtlNewStringFromNarrow
  157. //
  158. // Synopsis: Takes a ansi string and (1) unicodes it, (2) copies it
  159. //
  160. // Effects:
  161. //
  162. // Arguments:
  163. //
  164. // Requires:
  165. //
  166. // Returns:
  167. //
  168. // Notes:
  169. //
  170. //--------------------------------------------------------------------------
  171. void
  172. SRtlNewStringFromNarrow(PSECURITY_STRING pDest,
  173. char * pszString)
  174. {
  175. int cbNewString;
  176. DWORD cbOriginalString;
  177. cbOriginalString = strlen(pszString);
  178. cbNewString = cbOriginalString * sizeof(WCHAR);
  179. SRtlAllocateString( pDest, cbNewString + sizeof( WCHAR ) );
  180. if (pDest->Buffer)
  181. {
  182. if (MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
  183. pszString, cbOriginalString + 1,
  184. pDest->Buffer, cbOriginalString + 1))
  185. {
  186. pDest->Length = cbNewString;
  187. }
  188. else
  189. {
  190. LocalFree(pDest->Buffer);
  191. pDest->Buffer = NULL;
  192. }
  193. }
  194. }
  195. //+-------------------------------------------------------------------------
  196. //
  197. // Function: SRtlAllocateString
  198. //
  199. // Synopsis: Allocates space for a Unicode string.
  200. //
  201. // Effects:
  202. //
  203. // Arguments:
  204. //
  205. // Requires:
  206. //
  207. // Returns:
  208. //
  209. // Notes: This is useful to later append to the string.
  210. //
  211. //--------------------------------------------------------------------------
  212. void
  213. SRtlAllocateString(PSECURITY_STRING pString, USHORT cbSize )
  214. {
  215. pString->Buffer = (PWSTR) LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, cbSize);
  216. if (pString->Buffer)
  217. {
  218. pString->MaximumLength = cbSize;
  219. }
  220. else
  221. {
  222. pString->MaximumLength = 0;
  223. }
  224. pString->Length = 0;
  225. }
  226. //+-----------------------------------------------------------------
  227. //
  228. // Name: SRtlFreeString
  229. //
  230. // Synopsis: Frees a string.
  231. //
  232. // Effects: .
  233. //
  234. // Arguments: [pString] -- String to be freed.
  235. //
  236. // Requires: .
  237. //
  238. // Returns: .
  239. //
  240. // Notes: .
  241. //
  242. //------------------------------------------------------------------
  243. void
  244. SRtlFreeString( PSECURITY_STRING pString)
  245. {
  246. LocalFree(pString->Buffer);
  247. #if DBG
  248. pString->Buffer = NULL;
  249. #endif
  250. }
  251. void
  252. SRtlInitStringOfSize(
  253. PSECURITY_STRING DestinationString,
  254. USHORT Size,
  255. PWSTR str
  256. )
  257. {
  258. DestinationString->MaximumLength = DestinationString->Length = Size;
  259. DestinationString->Buffer = str;
  260. }
  261. void
  262. SRtlInitBufferOfSize(
  263. PSTRING DestinationString,
  264. USHORT Size,
  265. PUCHAR buffer
  266. )
  267. {
  268. DestinationString->MaximumLength = DestinationString->Length = Size;
  269. DestinationString->Buffer = (PCHAR) buffer;
  270. }
  271. LONG
  272. SRtlCompareString(
  273. PSECURITY_STRING String1,
  274. PSECURITY_STRING String2,
  275. BOOLEAN CaseInSensitive
  276. )
  277. {
  278. return( RtlCompareUnicodeString((PUNICODE_STRING) String1,
  279. (PUNICODE_STRING) String2,
  280. CaseInSensitive));
  281. }
  282. void
  283. SRtlCopyString(
  284. PSECURITY_STRING DestinationString,
  285. PSECURITY_STRING SourceString
  286. )
  287. {
  288. RtlCopyUnicodeString( (PUNICODE_STRING) DestinationString,
  289. (PUNICODE_STRING) SourceString);
  290. }
  291. void
  292. SRtlCopyBuffer(
  293. PSTRING DestinationString,
  294. PSTRING SourceString
  295. )
  296. {
  297. RtlCopyString( (PSTRING)DestinationString,
  298. (PSTRING)SourceString);
  299. }
  300. //+-------------------------------------------------------------------------
  301. //
  302. // Function: SRtlCatString
  303. //
  304. // Synopsis: Concatenates pSrc into pDest->Buffer. It is assumed that
  305. // pDest->Buffer is pointing to enough memory.
  306. //
  307. //
  308. // Effects: The string will be null terminated.
  309. //
  310. //
  311. // Arguments:
  312. //
  313. // Requires: pDest->Buffer must point to enough memory.
  314. //
  315. // Returns:
  316. //
  317. // Notes:
  318. //
  319. //--------------------------------------------------------------------------
  320. void
  321. SRtlCatString( PSECURITY_STRING pDest,
  322. LPWSTR pSrc)
  323. {
  324. wcscat(pDest->Buffer, pSrc);
  325. pDest->Length = wcslen( pDest->Buffer ) * sizeof( WCHAR );
  326. pDest->Buffer[pDest->Length / sizeof( WCHAR )] = UNICODE_NULL;
  327. }
  328. #if 0
  329. //+-----------------------------------------------------------------
  330. //
  331. // Name: SRtlStrContains
  332. //
  333. // Synopsis: Looks for a needle in a haystack.
  334. //
  335. // Effects: .
  336. //
  337. // Arguments: [pNeedle] --
  338. // [pHaystack] --
  339. // [fCaseInSensitiv --
  340. //
  341. // Requires: .
  342. //
  343. // Returns: .
  344. //
  345. // Notes: This is not a clever algorithm.
  346. //
  347. //------------------------------------------------------------------
  348. BOOLEAN
  349. SRtlStrContains(
  350. const PSECURITY_STRING pNeedle,
  351. const PSECURITY_STRING pHaystack,
  352. BOOLEAN fCaseInSensitive)
  353. {
  354. WCHAR *pNeed = pNeedle->Buffer;
  355. WCHAR *pHay = pHaystack->Buffer;
  356. WCHAR *pHayEnd = pHay +
  357. (pHaystack->Length - pNeedle->Length)/sizeof(WCHAR);
  358. // Pre-compute some things that are used in the loop.
  359. int (__cdecl * CmpFcn)(const WCHAR*,const WCHAR*,unsigned int);
  360. CmpFcn = (fCaseInSensitive) ? wcsnicmp : wcsncmp;
  361. int cchNeed = pNeedle->Length/sizeof(WCHAR);
  362. while ( pHay <= pHayEnd )
  363. {
  364. if ( (*pNeed == *pHay) &&
  365. (CmpFcn( pNeed, pHay, cchNeed ) == 0) )
  366. {
  367. // Found a match!
  368. return(TRUE);
  369. }
  370. pHay++;
  371. }
  372. return(FALSE);
  373. }
  374. #endif