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.

339 lines
7.5 KiB

  1. /****************************************************************************
  2. PROGRAM: UTIL.C
  3. PURPOSE: System utility routines
  4. ****************************************************************************/
  5. #include "PVIEWP.h"
  6. #include <string.h>
  7. /****************************************************************************
  8. FUNCTION: Alloc
  9. PURPOSE: Allocates memory to hold the specified number of bytes
  10. RETURNS : Pointer to allocated memory or NULL on failure
  11. ****************************************************************************/
  12. PVOID Alloc(
  13. ULONG Bytes)
  14. {
  15. HANDLE hMem;
  16. PVOID Buffer;
  17. hMem = LocalAlloc(LMEM_MOVEABLE, Bytes + sizeof(hMem));
  18. if (hMem == NULL) {
  19. return(NULL);
  20. }
  21. // Lock down the memory
  22. //
  23. Buffer = LocalLock(hMem);
  24. if (Buffer == NULL) {
  25. LocalFree(hMem);
  26. return(NULL);
  27. }
  28. //
  29. // Store the handle at the start of the memory block and return
  30. // a pointer to just beyond it.
  31. //
  32. *((PHANDLE)Buffer) = hMem;
  33. return (PVOID)(((PHANDLE)Buffer)+1);
  34. }
  35. /****************************************************************************
  36. FUNCTION: GetAllocSize
  37. PURPOSE: Returns the allocated size of the specified memory block.
  38. The block must have been previously allocated using Alloc
  39. RETURNS : Size of memory block in bytes or 0 on error
  40. ****************************************************************************/
  41. ULONG GetAllocSize(
  42. PVOID Buffer)
  43. {
  44. HANDLE hMem;
  45. hMem = *(((PHANDLE)Buffer) - 1);
  46. return (ULONG)(LocalSize(hMem) - sizeof(hMem));
  47. }
  48. /****************************************************************************
  49. FUNCTION: Free
  50. PURPOSE: Frees the memory previously allocated with Alloc
  51. RETURNS : TRUE on success, otherwise FALSE
  52. ****************************************************************************/
  53. BOOL Free(
  54. PVOID Buffer)
  55. {
  56. HANDLE hMem;
  57. hMem = *(((PHANDLE)Buffer) - 1);
  58. LocalUnlock(hMem);
  59. return(LocalFree(hMem) == NULL);
  60. }
  61. /****************************************************************************
  62. FUNCTION: LUID2String
  63. PURPOSE: Converts a LUID into a readable string.
  64. RETURNS : TRUE on success otherwise FALSE.
  65. ****************************************************************************/
  66. BOOL LUID2String(
  67. LUID Luid,
  68. LPSTR String,
  69. UINT MaxStringBytes)
  70. {
  71. if (Luid.HighPart == 0) {
  72. wsprintf(String, "0x%lx", Luid.LowPart);
  73. } else {
  74. wsprintf(String, "0x%lx%08lx", Luid.HighPart, Luid.LowPart);
  75. }
  76. return(TRUE);
  77. }
  78. /****************************************************************************
  79. FUNCTION: Time2String
  80. PURPOSE: Converts a time into a readable string.
  81. RETURNS : TRUE on success otherwise FALSE.
  82. ****************************************************************************/
  83. BOOL Time2String(
  84. TIME Time,
  85. LPSTR String,
  86. UINT MaxStringBytes)
  87. {
  88. TIME_FIELDS TimeFields;
  89. RtlTimeToTimeFields(&Time, &TimeFields);
  90. if (TimeFields.Year > 2900) {
  91. strcpy(String, "Never");
  92. } else {
  93. wsprintf(String, "%d/%d/%d %02d:%02d:%02d",
  94. TimeFields.Year, TimeFields.Month, TimeFields.Day,
  95. TimeFields.Hour, TimeFields.Minute, TimeFields.Second);
  96. }
  97. return(TRUE);
  98. }
  99. /****************************************************************************
  100. FUNCTION: TokenType2String
  101. PURPOSE: Converts a tokentype into a readable string.
  102. RETURNS : TRUE on success otherwise FALSE.
  103. ****************************************************************************/
  104. BOOL TokenType2String(
  105. TOKEN_TYPE TokenType,
  106. LPSTR String,
  107. UINT MaxStringBytes)
  108. {
  109. switch (TokenType) {
  110. case TokenPrimary:
  111. strcpy(String, "Primary");
  112. break;
  113. case TokenImpersonation:
  114. strcpy(String, "Impersonation");
  115. break;
  116. default:
  117. DbgPrint("SECEDIT: TokenType2String fed unrecognised token type : 0x%x\n", TokenType);
  118. return(FALSE);
  119. break;
  120. }
  121. return(TRUE);
  122. }
  123. /****************************************************************************
  124. FUNCTION: ImpersonationLevel2String
  125. PURPOSE: Converts an impersonation level into a readable string.
  126. RETURNS : TRUE on success otherwise FALSE.
  127. ****************************************************************************/
  128. BOOL ImpersonationLevel2String(
  129. SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
  130. LPSTR String,
  131. UINT MaxStringBytes)
  132. {
  133. switch (ImpersonationLevel) {
  134. case SecurityAnonymous:
  135. strcpy(String, "Anonymous");
  136. break;
  137. case SecurityIdentification:
  138. strcpy(String, "Identification");
  139. break;
  140. case SecurityImpersonation:
  141. strcpy(String, "Impersonation");
  142. break;
  143. case SecurityDelegation:
  144. strcpy(String, "Delegation");
  145. break;
  146. default:
  147. DbgPrint("SECEDIT: ImpersonationLevel2String fed unrecognised impersonation level : 0x%x\n", ImpersonationLevel);
  148. return(FALSE);
  149. break;
  150. }
  151. return(TRUE);
  152. }
  153. /****************************************************************************
  154. FUNCTION: Dynamic2String
  155. PURPOSE: Converts an dynamic quota level into a readable string.
  156. RETURNS : TRUE on success otherwise FALSE.
  157. ****************************************************************************/
  158. BOOL Dynamic2String(
  159. ULONG Dynamic,
  160. LPSTR String,
  161. UINT MaxStringBytes)
  162. {
  163. wsprintf(String, "%ld", Dynamic);
  164. return(TRUE);
  165. }
  166. /****************************************************************************
  167. FUNCTION: AddItem
  168. PURPOSE: Adds the item string and data to the specified control
  169. The control is assumed to be a list-box unless fCBox == TRUE
  170. in which case the control is assumed to be a ComboBox
  171. RETURNS: Index at which the item was added or < 0 on error
  172. ****************************************************************************/
  173. INT AddItem(
  174. HWND hDlg,
  175. INT ControlID,
  176. LPSTR String,
  177. LPARAM Data,
  178. BOOL fCBox)
  179. {
  180. HWND hwnd;
  181. LRESULT iItem;
  182. USHORT AddStringMsg = LB_ADDSTRING;
  183. USHORT SetDataMsg = LB_SETITEMDATA;
  184. if (fCBox) {
  185. AddStringMsg = CB_ADDSTRING;
  186. SetDataMsg = CB_SETITEMDATA;
  187. }
  188. hwnd = GetDlgItem(hDlg, ControlID);
  189. iItem = SendMessage(hwnd, AddStringMsg, 0, (LPARAM)String);
  190. if (iItem >= 0) {
  191. SendMessage(hwnd, SetDataMsg, iItem, Data);
  192. }
  193. return((INT)iItem);
  194. }
  195. /****************************************************************************
  196. FUNCTION: FindSid
  197. PURPOSE: Searches for the specified Sid in a control.
  198. RETURNS: Index of matching item or < 0 on error
  199. ****************************************************************************/
  200. INT FindSid(
  201. HWND hDlg,
  202. INT ControlID,
  203. PSID Sid,
  204. BOOL fCBox)
  205. {
  206. HWND hwnd;
  207. INT cItems;
  208. USHORT GetCountMsg = LB_GETCOUNT;
  209. USHORT GetDataMsg = LB_GETITEMDATA;
  210. if (fCBox) {
  211. GetCountMsg = CB_GETCOUNT;
  212. GetDataMsg = CB_GETITEMDATA;
  213. }
  214. hwnd = GetDlgItem(hDlg, ControlID);
  215. cItems = (INT)SendMessage(hwnd, GetCountMsg, 0, 0);
  216. if (cItems >= 0) {
  217. INT iItem;
  218. PSID ItemSid;
  219. for (iItem =0; iItem < cItems; iItem ++) {
  220. ItemSid = (PSID)SendMessage(hwnd, GetDataMsg, (WPARAM)iItem, 0);
  221. if (RtlEqualSid(ItemSid, Sid)) {
  222. return(iItem);
  223. }
  224. }
  225. }
  226. return(-1);
  227. }