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.

339 lines
7.2 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. tprefix.c
  5. Abstract:
  6. Test program for the Prefix table package
  7. Author:
  8. Gary Kimura [GaryKi] 03-Aug-1989
  9. Revision History:
  10. --*/
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "nt.h"
  14. #include "ntrtl.h"
  15. //
  16. // Routines and types for generating random prefixes
  17. //
  18. ULONG RtlRandom ( IN OUT PULONG Seed );
  19. ULONG Seed;
  20. PSZ AnotherPrefix(IN ULONG MaxNameLength);
  21. ULONG AlphabetLength;
  22. //PSZ Alphabet = "AlphaBravoCharlieDeltaEchoFoxtrotGolfHotelIndiaJuliettKiloLimaMikeNovemberOscarPapaQuebecRomeoSierraTangoUniformVictorWhiskeyXrayYankeeZulu";
  23. PSZ Alphabet = "\
  24. Aa\
  25. BBbb\
  26. CCCccc\
  27. DDDDdddd\
  28. EEEEEeeeee\
  29. FFFFFFffffff\
  30. GGGGGGGggggggg\
  31. HHHHHHHHhhhhhhhh\
  32. IIIIIIIIIiiiiiiiii\
  33. JJJJJJJJJJjjjjjjjjjj\
  34. KKKKKKKKKKKkkkkkkkkkkk\
  35. LLLLLLLLLLLLllllllllllll\
  36. MMMMMMMMMMMMMmmmmmmmmmmmmm\
  37. NNNNNNNNNNNNNNnnnnnnnnnnnnnn\
  38. OOOOOOOOOOOOOOOooooooooooooooo\
  39. PPPPPPPPPPPPPPPPpppppppppppppppp\
  40. QQQQQQQQQQQQQQQQQqqqqqqqqqqqqqqqqq\
  41. RRRRRRRRRRRRRRRRRRrrrrrrrrrrrrrrrrrr\
  42. SSSSSSSSSSSSSSSSSSSsssssssssssssssssss\
  43. TTTTTTTTTTTTTTTTTTTTtttttttttttttttttttt\
  44. UUUUUUUUUUUUUUUUUUUUUuuuuuuuuuuuuuuuuuuuuu\
  45. VVVVVVVVVVVVVVVVVVVVVVvvvvvvvvvvvvvvvvvvvvvv\
  46. WWWWWWWWWWWWWWWWWWWWWWWwwwwwwwwwwwwwwwwwwwwwww\
  47. XXXXXXXXXXXXXXXXXXXXXXXXxxxxxxxxxxxxxxxxxxxxxxxx\
  48. YYYYYYYYYYYYYYYYYYYYYYYYYyyyyyyyyyyyyyyyyyyyyyyyyy\
  49. ZZZZZZZZZZZZZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzzzzzzzzzz";
  50. #define BUFFER_LENGTH 8192
  51. CHAR Buffer[BUFFER_LENGTH];
  52. ULONG NextBufferChar = 0;
  53. //
  54. // record structure and variables for the prefix table and it
  55. // elements
  56. //
  57. typedef struct _PREFIX_NODE {
  58. PREFIX_TABLE_ENTRY PfxEntry;
  59. STRING String;
  60. } PREFIX_NODE;
  61. typedef PREFIX_NODE *PPREFIX_NODE;
  62. #define PREFIXES 512
  63. PREFIX_NODE Prefixes[PREFIXES];
  64. PREFIX_TABLE PrefixTable;
  65. int
  66. main(
  67. int argc,
  68. char *argv[]
  69. )
  70. {
  71. ULONG i;
  72. PSZ Psz;
  73. PPREFIX_TABLE_ENTRY PfxEntry;
  74. PPREFIX_NODE PfxNode;
  75. STRING String;
  76. //
  77. // We're starting the test
  78. //
  79. DbgPrint("Start Prefix Test\n");
  80. //
  81. // Calculate the alphabet size for use by AnotherPrefix
  82. //
  83. AlphabetLength = strlen(Alphabet);
  84. //
  85. // Initialize the prefix table
  86. //
  87. PfxInitialize(&PrefixTable);
  88. //
  89. // Insert the root prefix
  90. //
  91. RtlInitString( &Prefixes[i].String, "\\" );
  92. if (PfxInsertPrefix( &PrefixTable,
  93. &Prefixes[0].String,
  94. &Prefixes[0].PfxEntry )) {
  95. DbgPrint("Insert root prefix\n");
  96. } else {
  97. DbgPrint("error inserting root prefix\n");
  98. }
  99. //
  100. // Insert prefixes
  101. //
  102. Seed = 0;
  103. for (i = 1, Psz = AnotherPrefix(3);
  104. (i < PREFIXES) && (Psz != NULL);
  105. i += 1, Psz = AnotherPrefix(3)) {
  106. DbgPrint("[0x%x] = ", i);
  107. DbgPrint("\"%s\"", Psz);
  108. RtlInitString(&Prefixes[i].String, Psz);
  109. if (PfxInsertPrefix( &PrefixTable,
  110. &Prefixes[i].String,
  111. &Prefixes[i].PfxEntry )) {
  112. DbgPrint(" inserted in table\n");
  113. } else {
  114. DbgPrint(" already in table\n");
  115. }
  116. }
  117. //
  118. // Enumerate the prefix table
  119. //
  120. DbgPrint("Enumerate Prefix Table the first time\n");
  121. for (PfxEntry = PfxNextPrefix(&PrefixTable, TRUE);
  122. PfxEntry != NULL;
  123. PfxEntry = PfxNextPrefix(&PrefixTable, FALSE)) {
  124. PfxNode = CONTAINING_RECORD(PfxEntry, PREFIX_NODE, PfxEntry);
  125. DbgPrint("%s\n", PfxNode->String.Buffer);
  126. }
  127. DbgPrint("Start Prefix search 0x%x\n", NextBufferChar);
  128. //
  129. // Search for prefixes
  130. //
  131. for (Psz = AnotherPrefix(4); Psz != NULL; Psz = AnotherPrefix(4)) {
  132. DbgPrint("0x%x ", NextBufferChar);
  133. RtlInitString(&String, Psz);
  134. PfxEntry = PfxFindPrefix( &PrefixTable, &String, FALSE );
  135. if (PfxEntry == NULL) {
  136. PfxEntry = PfxFindPrefix( &PrefixTable, &String, TRUE );
  137. if (PfxEntry == NULL) {
  138. DbgPrint("Not found \"%s\"\n", Psz);
  139. NOTHING;
  140. } else {
  141. PfxNode = CONTAINING_RECORD(PfxEntry, PREFIX_NODE, PfxEntry);
  142. DbgPrint("Case blind \"%s\" is \"%s\"\n", Psz, PfxNode->String.Buffer);
  143. PfxRemovePrefix( &PrefixTable, PfxEntry );
  144. }
  145. } else {
  146. PfxNode = CONTAINING_RECORD(PfxEntry, PREFIX_NODE, PfxEntry);
  147. DbgPrint( "Case sensitive \"%s\" is \"%s\"\n", Psz, PfxNode->String.Buffer);
  148. if (PfxNode != &Prefixes[0]) {
  149. PfxRemovePrefix( &PrefixTable, PfxEntry );
  150. }
  151. }
  152. }
  153. //
  154. // Enumerate the prefix table
  155. //
  156. DbgPrint("Enumerate Prefix Table a second time\n");
  157. for (PfxEntry = PfxNextPrefix(&PrefixTable, TRUE);
  158. PfxEntry != NULL;
  159. PfxEntry = PfxNextPrefix(&PrefixTable, FALSE)) {
  160. PfxNode = CONTAINING_RECORD(PfxEntry, PREFIX_NODE, PfxEntry);
  161. DbgPrint("%s\n", PfxNode->String.Buffer);
  162. }
  163. //
  164. // Now enumerate and zero out the table
  165. //
  166. for (PfxEntry = PfxNextPrefix(&PrefixTable, TRUE);
  167. PfxEntry != NULL;
  168. PfxEntry = PfxNextPrefix(&PrefixTable, FALSE)) {
  169. PfxNode = CONTAINING_RECORD(PfxEntry, PREFIX_NODE, PfxEntry);
  170. DbgPrint("Delete %s\n", PfxNode->String.Buffer);
  171. PfxRemovePrefix( &PrefixTable, PfxEntry );
  172. }
  173. //
  174. // Enumerate again but this time the table should be empty
  175. //
  176. for (PfxEntry = PfxNextPrefix(&PrefixTable, TRUE);
  177. PfxEntry != NULL;
  178. PfxEntry = PfxNextPrefix(&PrefixTable, FALSE)) {
  179. PfxNode = CONTAINING_RECORD(PfxEntry, PREFIX_NODE, PfxEntry);
  180. DbgPrint("This Node should be gone \"%s\"\n", PfxNode->String.Buffer);
  181. }
  182. DbgPrint("End PrefixTest()\n");
  183. return TRUE;
  184. }
  185. PSZ
  186. AnotherPrefix(IN ULONG MaxNameLength)
  187. {
  188. ULONG AlphabetPosition;
  189. ULONG NameLength;
  190. ULONG IndividualNameLength;
  191. ULONG StartBufferPosition;
  192. ULONG i;
  193. ULONG j;
  194. //
  195. // Check if there is enough room for another name
  196. //
  197. if (NextBufferChar > (BUFFER_LENGTH - (MaxNameLength * 4))) {
  198. return NULL;
  199. }
  200. //
  201. // Where in the alphabet soup we start
  202. //
  203. AlphabetPosition = RtlRandom(&Seed) % AlphabetLength;
  204. //
  205. // How many names we want in our prefix
  206. //
  207. NameLength = (RtlRandom(&Seed) % MaxNameLength) + 1;
  208. //
  209. // Compute each name
  210. //
  211. StartBufferPosition = NextBufferChar;
  212. for (i = 0; i < NameLength; i += 1) {
  213. Buffer[NextBufferChar++] = '\\';
  214. IndividualNameLength = (RtlRandom(&Seed) % 3) + 1;
  215. for (j = 0; j < IndividualNameLength; j += 1) {
  216. Buffer[NextBufferChar++] = Alphabet[AlphabetPosition];
  217. AlphabetPosition = (AlphabetPosition + 1) % AlphabetLength;
  218. }
  219. }
  220. Buffer[NextBufferChar++] = '\0';
  221. return &Buffer[StartBufferPosition];
  222. }