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.

484 lines
9.1 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. routing\monitor2\shell\alias.c
  5. Abstract:
  6. Alias table manipulation functions to add/delete/read aliases.
  7. The aliases are stored using a hash table (chaining).
  8. Revision History:
  9. Anand Mahalingam 7/6/98 Created
  10. --*/
  11. #include "precomp.h"
  12. //
  13. // The Alias Table
  14. //
  15. PLIST_ENTRY AliasTable[ALIAS_TABLE_SIZE];
  16. //
  17. // Functions to manipulate the Alias Table
  18. //
  19. DWORD
  20. ATHashAlias(
  21. IN LPCWSTR pwszAliasName,
  22. OUT PWORD pwHashValue
  23. )
  24. /*++
  25. Routine Description:
  26. Computes the hash value for the given string.
  27. Arguments:
  28. pwszAliasName - Alias Name
  29. pwHashValue - Hash value for Alias
  30. Return Value:
  31. NO_ERROR
  32. --*/
  33. {
  34. LPCWSTR p;
  35. WORD h = 0,g;
  36. for (p = pwszAliasName; *p != L'\0'; p++)
  37. {
  38. h = (h<<4) + (*p);
  39. if(g = h&0xf0000000)
  40. {
  41. h = h ^ (g >> 24);
  42. h = h ^ g;
  43. }
  44. }
  45. *pwHashValue = h % ALIAS_TABLE_SIZE;
  46. return NO_ERROR;
  47. }
  48. DWORD
  49. ATInitTable(
  50. VOID
  51. )
  52. /*++
  53. Routine Description:
  54. Initializes the Alias Table to NULL.
  55. Arguments:
  56. Return Value:
  57. --*/
  58. {
  59. DWORD i;
  60. PLIST_ENTRY ple;
  61. for (i = 0; i < ALIAS_TABLE_SIZE; i++)
  62. {
  63. ple = HeapAlloc(GetProcessHeap(),
  64. 0,
  65. sizeof(LIST_ENTRY));
  66. if (ple is NULL)
  67. {
  68. PrintMessageFromModule(g_hModule, MSG_NOT_ENOUGH_MEMORY);
  69. break;
  70. }
  71. InitializeListHead(ple);
  72. AliasTable[i] = ple;
  73. }
  74. if (i isnot ALIAS_TABLE_SIZE)
  75. {
  76. //
  77. // malloc error
  78. //
  79. for (; i < ALIAS_TABLE_SIZE; AliasTable[i++] = NULL);
  80. FreeAliasTable();
  81. return ERROR_NOT_ENOUGH_MEMORY;
  82. }
  83. return NO_ERROR;
  84. }
  85. VOID
  86. ATCleanupTable(
  87. VOID
  88. )
  89. /*++
  90. Routine Description:
  91. Frees memory allocated for the alias table
  92. Arguments:
  93. Return Value:
  94. --*/
  95. {
  96. DWORD i;
  97. for (i = 0; i < ALIAS_TABLE_SIZE; i++)
  98. {
  99. HeapFree(GetProcessHeap(), 0, AliasTable[i]);
  100. }
  101. return;
  102. }
  103. DWORD
  104. ATAddAlias(
  105. IN LPCWSTR pwszAliasName,
  106. IN LPCWSTR pwszAliasString
  107. )
  108. /*++
  109. Routine Description:
  110. Adds a new entry in the Alias Table. If alias already
  111. exists, then set it to the new string.
  112. Arguments:
  113. pwszAliasName - Alias Name
  114. pwszAliasString - Equivalent string
  115. Return Value:
  116. NO_ERROR, ERROR_NOT_ENOUGH_MEMORY
  117. --*/
  118. {
  119. WORD wHashValue;
  120. PALIAS_TABLE_ENTRY pateATEntry;
  121. PLIST_ENTRY ple;
  122. //
  123. // Compute hash value for the alias
  124. //
  125. ATHashAlias(pwszAliasName,&wHashValue);
  126. //
  127. // Check if alias is already present
  128. //
  129. ple = AliasTable[wHashValue]->Flink;
  130. while (ple != AliasTable[wHashValue])
  131. {
  132. pateATEntry = CONTAINING_RECORD(ple,ALIAS_TABLE_ENTRY, le);
  133. if (wcscmp(pwszAliasName,pateATEntry->pszAlias) == 0)
  134. {
  135. //
  136. // Alias already exists. Free memory allocated for
  137. // previous string. Allocate memory for this string.
  138. //
  139. HeapFree(GetProcessHeap(), 0 , pateATEntry->pszString);
  140. pateATEntry->pszString = HeapAlloc(GetProcessHeap(),
  141. 0 ,
  142. (wcslen(pwszAliasString) + 1)
  143. * sizeof(WCHAR));
  144. if (pateATEntry->pszString is NULL)
  145. {
  146. PrintMessageFromModule(g_hModule, MSG_NOT_ENOUGH_MEMORY);
  147. return ERROR_NOT_ENOUGH_MEMORY;
  148. }
  149. wcscpy(pateATEntry->pszString,pwszAliasString);
  150. return NO_ERROR;
  151. }
  152. ple = ple->Flink;
  153. }
  154. //
  155. // Create new Entry and add to beginning of list
  156. //
  157. pateATEntry = HeapAlloc(GetProcessHeap(),
  158. 0,
  159. sizeof(ALIAS_TABLE_ENTRY));
  160. if (pateATEntry is NULL)
  161. {
  162. PrintMessageFromModule(g_hModule, MSG_NOT_ENOUGH_MEMORY);
  163. return ERROR_NOT_ENOUGH_MEMORY;
  164. }
  165. pateATEntry->pszAlias = HeapAlloc(GetProcessHeap(),
  166. 0 ,
  167. (wcslen(pwszAliasName) + 1)
  168. * sizeof(WCHAR));
  169. if (pateATEntry->pszAlias is NULL)
  170. {
  171. HeapFree(GetProcessHeap(), 0 , pateATEntry);
  172. PrintMessageFromModule(g_hModule, MSG_NOT_ENOUGH_MEMORY);
  173. return ERROR_NOT_ENOUGH_MEMORY;
  174. }
  175. pateATEntry->pszString = HeapAlloc(GetProcessHeap(),
  176. 0 ,
  177. (wcslen(pwszAliasString) + 1)
  178. * sizeof(WCHAR));
  179. if (pateATEntry->pszString is NULL)
  180. {
  181. HeapFree(GetProcessHeap(), 0 , pateATEntry->pszAlias);
  182. HeapFree(GetProcessHeap(), 0 , pateATEntry);
  183. PrintMessageFromModule(g_hModule, MSG_NOT_ENOUGH_MEMORY);
  184. return ERROR_NOT_ENOUGH_MEMORY;
  185. }
  186. wcscpy(pateATEntry->pszAlias, pwszAliasName);
  187. wcscpy(pateATEntry->pszString, pwszAliasString);
  188. InsertHeadList(AliasTable[wHashValue],&(pateATEntry->le));
  189. return NO_ERROR;
  190. }
  191. DWORD
  192. ATDeleteAlias(
  193. IN LPCWSTR pwszAliasName
  194. )
  195. /*++
  196. Routine Description:
  197. Deletes an alias in the Alias Table.
  198. Arguments:
  199. pwszAliasName - Alias Name
  200. Return Value:
  201. NO_ERROR, ERROR_ALIAS_NOT_FOUND
  202. --*/
  203. {
  204. WORD wHashValue;
  205. PALIAS_TABLE_ENTRY pateATEntry;
  206. PLIST_ENTRY ple;
  207. //
  208. // Compute hash value of alias
  209. //
  210. ATHashAlias(pwszAliasName,&wHashValue);
  211. //
  212. // Try to find the alias
  213. //
  214. ple = AliasTable[wHashValue];
  215. if (IsListEmpty(AliasTable[wHashValue]))
  216. {
  217. return ERROR_ALIAS_NOT_FOUND;
  218. }
  219. ple = AliasTable[wHashValue]->Flink;
  220. while (ple != AliasTable[wHashValue])
  221. {
  222. pateATEntry = CONTAINING_RECORD(ple, ALIAS_TABLE_ENTRY, le);
  223. if (!wcscmp(pwszAliasName,pateATEntry->pszAlias))
  224. {
  225. //
  226. // Found it.
  227. //
  228. RemoveEntryList(ple);
  229. HeapFree(GetProcessHeap(), 0 , pateATEntry->pszAlias);
  230. HeapFree(GetProcessHeap(), 0 , pateATEntry->pszString);
  231. HeapFree(GetProcessHeap(),
  232. 0,
  233. pateATEntry);
  234. return NO_ERROR;
  235. }
  236. ple = ple->Flink;
  237. }
  238. return ERROR_ALIAS_NOT_FOUND;
  239. }
  240. DWORD
  241. ATLookupAliasTable(
  242. IN LPCWSTR pwszAliasName,
  243. OUT LPWSTR *ppwszAliasString
  244. )
  245. /*++
  246. Routine Description:
  247. Looks up an alias in the Alias Table.
  248. Arguments:
  249. pwszAliasName - Alias name
  250. ppwszAliasString - Equivalent string
  251. Return Value:
  252. NO_ERROR
  253. --*/
  254. {
  255. WORD wHashValue;
  256. PALIAS_TABLE_ENTRY pateATEntry;
  257. PLIST_ENTRY ple;
  258. //
  259. // Compute hash value for alias
  260. //
  261. ATHashAlias(pwszAliasName,&wHashValue);
  262. if (IsListEmpty(AliasTable[wHashValue]))
  263. {
  264. *ppwszAliasString = NULL;
  265. return NO_ERROR;
  266. }
  267. ple = AliasTable[wHashValue]->Flink;
  268. while (ple != AliasTable[wHashValue])
  269. {
  270. pateATEntry = CONTAINING_RECORD(ple, ALIAS_TABLE_ENTRY, le);
  271. if (wcscmp(pateATEntry->pszAlias,pwszAliasName) == 0)
  272. {
  273. //
  274. // Found Alias
  275. //
  276. *ppwszAliasString = pateATEntry->pszString;
  277. return NO_ERROR;
  278. }
  279. ple = ple->Flink;
  280. }
  281. //
  282. // Alias not found
  283. //
  284. *ppwszAliasString = NULL;
  285. return NO_ERROR;
  286. }
  287. DWORD
  288. PrintAliasTable(
  289. VOID
  290. )
  291. /*++
  292. Routine Description:
  293. Prints the aliases in the alias table
  294. Arguments:
  295. Return Value:
  296. NO_ERROR
  297. --*/
  298. {
  299. DWORD k;
  300. PALIAS_TABLE_ENTRY pateATEntry;
  301. PLIST_ENTRY ple;
  302. for ( k = 0; k < ALIAS_TABLE_SIZE ; k++)
  303. {
  304. ple = AliasTable[k]->Flink;
  305. while ( ple != AliasTable[k])
  306. {
  307. pateATEntry = CONTAINING_RECORD(ple, ALIAS_TABLE_ENTRY, le);
  308. PrintMessage(L"%1!s!\t%2!s!\n",
  309. pateATEntry->pszAlias,
  310. pateATEntry->pszString);
  311. ple = ple->Flink;
  312. }
  313. }
  314. return NO_ERROR;
  315. }
  316. DWORD
  317. FreeAliasTable(
  318. VOID
  319. )
  320. /*++
  321. Routine Description:
  322. Prints the aliases in the alias table
  323. Arguments:
  324. Return Value:
  325. NO_ERROR
  326. --*/
  327. {
  328. DWORD k;
  329. PALIAS_TABLE_ENTRY pateATEntry;
  330. PLIST_ENTRY pleHead;
  331. for ( k = 0; k < ALIAS_TABLE_SIZE ; k++)
  332. {
  333. pleHead = AliasTable[k];
  334. if (pleHead is NULL)
  335. continue;
  336. while ( !IsListEmpty(pleHead) )
  337. {
  338. pateATEntry = CONTAINING_RECORD(pleHead->Flink,
  339. ALIAS_TABLE_ENTRY, le);
  340. RemoveHeadList(pleHead);
  341. HeapFree(GetProcessHeap(), 0, pateATEntry);
  342. }
  343. HeapFree(GetProcessHeap(), 0, pleHead);
  344. }
  345. return NO_ERROR;
  346. }