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.

419 lines
11 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. MEMBER_VARIABLE_INFO _MemberInfo;
  4. #define STATE_FILENAME "isnext.state"
  5. STRUCTURE_TABLE StructureTable[] =
  6. {
  7. IPX_MAJOR_STRUCTURES,
  8. SPX_MAJOR_STRUCTURES,
  9. { NULL }
  10. };
  11. BOOL NextListEntry( ULONG Current, PULONG Next );
  12. BOOL PrevListEntry( ULONG Current, PULONG Prev );
  13. VOID NextElement( PMEMBER_VARIABLE_INFO pMemberInfo );
  14. VOID PrevElement( PMEMBER_VARIABLE_INFO pMemberInfo );
  15. VOID DumpListItem( PMEMBER_VARIABLE_INFO pMemberInfo );
  16. BOOL
  17. LocateMemberVariable
  18. (
  19. PCHAR pchStructName,
  20. PCHAR pchMemberName,
  21. PVOID pvStructure,
  22. PMEMBER_VARIABLE_INFO pMemberInfo
  23. )
  24. {
  25. BOOL bMatch;
  26. int index;
  27. PMEMBER_TABLE pMemberTable;
  28. CHAR pchCurrent[ MAX_LIST_VARIABLE_NAME_LENGTH + 1 ];
  29. CHAR _pchStructName[ MAX_LIST_VARIABLE_NAME_LENGTH + 1 ];
  30. CHAR _pchMemberName[ MAX_LIST_VARIABLE_NAME_LENGTH + 1 ];
  31. dprintf( "LocateMemberVariable( \"%s\", \"%s\", 0x%08X )\n", pchStructName, pchMemberName, pMemberInfo );
  32. strcpy( _pchStructName, pchStructName );
  33. strcpy( _pchMemberName, pchMemberName );
  34. _strupr( _pchStructName );
  35. _strupr( _pchMemberName );
  36. pMemberInfo->StructureIndex = 0;
  37. pMemberInfo->MemberIndex = 0;
  38. bMatch = FALSE;
  39. for ( index = 0; StructureTable[ index ].pchStructName != NULL; index ++ )
  40. {
  41. strcpy( pchCurrent, StructureTable[ index ].pchStructName );
  42. _strupr( pchCurrent );
  43. if ( strstr( pchCurrent, _pchStructName ))
  44. {
  45. if ( bMatch )
  46. {
  47. dprintf( "The specified structure name is ambiguous.\n" );
  48. return( FALSE );
  49. }
  50. pMemberInfo->StructureIndex = index;
  51. bMatch = TRUE;
  52. }
  53. }
  54. if ( !bMatch )
  55. {
  56. dprintf( "No matching structure name was found.\n" );
  57. return( FALSE );
  58. }
  59. pMemberTable = StructureTable[ pMemberInfo->StructureIndex ].pMemberTable;
  60. bMatch = FALSE;
  61. for ( index = 0; pMemberTable[ index ].pchMemberName != NULL; index ++ )
  62. {
  63. strcpy( pchCurrent, pMemberTable[ index ].pchMemberName );
  64. _strupr( pchCurrent );
  65. if ( strstr( pchCurrent, _pchMemberName ))
  66. {
  67. if ( bMatch )
  68. {
  69. dprintf( "The variable specified is ambiguous.\n" );
  70. return( FALSE );
  71. }
  72. pMemberInfo->MemberIndex = index;
  73. bMatch = TRUE;
  74. }
  75. }
  76. if ( !bMatch )
  77. {
  78. dprintf( "No matching member name was found in the %s structure.\n", pchStructName );
  79. return( FALSE );
  80. }
  81. pMemberInfo->prHeadContainingObject = ( ULONG )pvStructure;
  82. pMemberInfo->prHeadLinkage = (( ULONG )pvStructure ) + pMemberTable[ pMemberInfo->MemberIndex ].cbOffsetToHead;
  83. pMemberInfo->prCurrentLinkage = pMemberInfo->prHeadLinkage;
  84. pMemberInfo->cCurrentElement = 0;
  85. return( TRUE );
  86. }
  87. BOOL WriteMemberInfo( PMEMBER_VARIABLE_INFO pMemberInfo )
  88. {
  89. HANDLE hStateFile;
  90. DWORD dwWritten;
  91. hStateFile = CreateFile( STATE_FILENAME,
  92. GENERIC_WRITE,
  93. 0,
  94. NULL,
  95. CREATE_ALWAYS,
  96. FILE_ATTRIBUTE_NORMAL,
  97. NULL );
  98. if ( hStateFile == INVALID_HANDLE_VALUE )
  99. {
  100. dprintf( "Can't create state file\n" );
  101. return( FALSE );
  102. }
  103. if ( !WriteFile( hStateFile,
  104. pMemberInfo,
  105. sizeof( MEMBER_VARIABLE_INFO ),
  106. &dwWritten,
  107. NULL ) || ( dwWritten != sizeof( MEMBER_VARIABLE_INFO )))
  108. {
  109. dprintf( "Can't write to state file\n" );
  110. CloseHandle( hStateFile );
  111. return( FALSE );
  112. }
  113. CloseHandle( hStateFile );
  114. return( TRUE );
  115. }
  116. BOOL ReadMemberInfo( PMEMBER_VARIABLE_INFO pMemberInfo )
  117. {
  118. HANDLE hStateFile;
  119. DWORD dwRead;
  120. hStateFile = CreateFile( STATE_FILENAME,
  121. GENERIC_READ,
  122. 0,
  123. NULL,
  124. OPEN_EXISTING,
  125. FILE_ATTRIBUTE_NORMAL,
  126. NULL );
  127. if ( hStateFile == INVALID_HANDLE_VALUE )
  128. {
  129. dprintf( "Can't open state file\n" );
  130. return( FALSE );
  131. }
  132. if ( !ReadFile( hStateFile,
  133. pMemberInfo,
  134. sizeof( MEMBER_VARIABLE_INFO ),
  135. &dwRead,
  136. NULL ) || ( dwRead != sizeof( MEMBER_VARIABLE_INFO )))
  137. {
  138. dprintf( "Can't read from state file\n" );
  139. CloseHandle( hStateFile );
  140. return( FALSE );
  141. }
  142. CloseHandle( hStateFile );
  143. return( TRUE );
  144. }
  145. DECLARE_API( next )
  146. {
  147. MEMBER_VARIABLE_INFO MemberInfo;
  148. if ( !ReadMemberInfo( &MemberInfo ) )
  149. {
  150. return;
  151. }
  152. NextElement( &MemberInfo );
  153. DumpListItem( &MemberInfo );
  154. WriteMemberInfo( &MemberInfo );
  155. }
  156. DECLARE_API( prev )
  157. {
  158. MEMBER_VARIABLE_INFO MemberInfo;
  159. if ( !ReadMemberInfo( &MemberInfo ) )
  160. {
  161. return;
  162. }
  163. PrevElement( &MemberInfo );
  164. DumpListItem( &MemberInfo );
  165. WriteMemberInfo( &MemberInfo );
  166. }
  167. VOID DumpListItem( PMEMBER_VARIABLE_INFO pMemberInfo )
  168. {
  169. PBYTE pbObject;
  170. PMEMBER_TABLE pMemberTable;
  171. dprintf( "Focus is on: %s.%s, element # %d\n",
  172. StructureTable[ pMemberInfo->StructureIndex ].pchStructName,
  173. StructureTable[ pMemberInfo->StructureIndex ].pMemberTable[ pMemberInfo->MemberIndex ].pchMemberName,
  174. pMemberInfo->cCurrentElement );
  175. pMemberTable = &StructureTable[ pMemberInfo->StructureIndex ].pMemberTable[ pMemberInfo->MemberIndex ];
  176. if ( pMemberInfo->prCurrentLinkage == pMemberInfo->prHeadLinkage )
  177. {
  178. //
  179. // Rather than dumping the head list item, dump all the items on the list,
  180. // in summary form.
  181. //
  182. do
  183. {
  184. NextElement( pMemberInfo );
  185. if ( pMemberInfo->prCurrentLinkage != pMemberInfo->prHeadLinkage )
  186. {
  187. pbObject = (( PBYTE )pMemberInfo->prCurrentLinkage )
  188. - pMemberTable->cbOffsetToLink;
  189. pMemberTable->DumpStructure( ( ULONG )pbObject, VERBOSITY_ONE_LINER );
  190. dprintf( "\n" );
  191. }
  192. } while ( pMemberInfo->prCurrentLinkage != pMemberInfo->prHeadLinkage );
  193. }
  194. else
  195. {
  196. pbObject = (( PBYTE )pMemberInfo->prCurrentLinkage )
  197. - pMemberTable->cbOffsetToLink;
  198. pMemberTable->DumpStructure( ( ULONG )pbObject, VERBOSITY_NORMAL );
  199. }
  200. }
  201. VOID NextElement( PMEMBER_VARIABLE_INFO pMemberInfo )
  202. {
  203. ULONG NextLinkage;
  204. PMEMBER_TABLE pMember;
  205. pMember = &StructureTable[ pMemberInfo->StructureIndex ].pMemberTable[ pMemberInfo->MemberIndex ];
  206. if ( !pMember->Next( pMemberInfo->prCurrentLinkage, &NextLinkage ))
  207. {
  208. dprintf( "Command failed.\n" );
  209. return;
  210. }
  211. pMemberInfo->prCurrentLinkage = NextLinkage;
  212. pMemberInfo->cCurrentElement++;
  213. if ( pMemberInfo->prCurrentLinkage == pMemberInfo->prHeadLinkage )
  214. {
  215. pMemberInfo->cCurrentElement = 0;
  216. }
  217. }
  218. BOOL NextListEntry( ULONG Current, PULONG Next )
  219. {
  220. ULONG result;
  221. ULONG prNextEntry;
  222. LIST_ENTRY Entry;
  223. LIST_ENTRY NextEntry;
  224. if ( !ReadMemory( Current,
  225. &Entry,
  226. sizeof( Entry ),
  227. &result ))
  228. {
  229. dprintf( "Couldn't read current list entry at 0x%08X.\n", Current );
  230. return( FALSE );
  231. }
  232. prNextEntry = ( ULONG )Entry.Flink;
  233. if ( !ReadMemory( prNextEntry,
  234. &NextEntry,
  235. sizeof( NextEntry ),
  236. &result ))
  237. {
  238. dprintf( "Couldn't read next list entry at 0x%08X.\n", prNextEntry );
  239. return( FALSE );
  240. }
  241. if ( ( ULONG )NextEntry.Blink != Current )
  242. {
  243. dprintf( "Next entry's Blink doesn't match current entry's address.\n" );
  244. dprintf( "The list might be corrupt, or you may be using traversal state saved before the list changed.\n" );
  245. return( FALSE );
  246. }
  247. *Next = prNextEntry;
  248. return( TRUE );
  249. }
  250. VOID PrevElement( PMEMBER_VARIABLE_INFO pMemberInfo )
  251. {
  252. ULONG PrevLinkage;
  253. PMEMBER_TABLE pMember;
  254. pMember = &StructureTable[ pMemberInfo->StructureIndex ].pMemberTable[ pMemberInfo->MemberIndex ];
  255. if ( !pMember->Prev( pMemberInfo->prCurrentLinkage, &PrevLinkage ))
  256. {
  257. dprintf( "Command failed.\n" );
  258. return;
  259. }
  260. pMemberInfo->prCurrentLinkage = PrevLinkage;
  261. pMemberInfo->cCurrentElement++;
  262. if ( pMemberInfo->prCurrentLinkage == pMemberInfo->prHeadLinkage )
  263. {
  264. pMemberInfo->cCurrentElement = 0;
  265. }
  266. }
  267. BOOL PrevListEntry( ULONG Current, PULONG Prev )
  268. {
  269. ULONG result;
  270. ULONG prPrevEntry;
  271. LIST_ENTRY Entry;
  272. LIST_ENTRY PrevEntry;
  273. if ( !ReadMemory( Current,
  274. &Entry,
  275. sizeof( Entry ),
  276. &result ))
  277. {
  278. dprintf( "Couldn't read current list entry at 0x%08X.\n", Current );
  279. return( FALSE );
  280. }
  281. prPrevEntry = ( ULONG )Entry.Blink;
  282. if ( !ReadMemory( prPrevEntry,
  283. &PrevEntry,
  284. sizeof( PrevEntry ),
  285. &result ))
  286. {
  287. dprintf( "Couldn't read previous list entry at 0x%08X.\n", prPrevEntry );
  288. return( FALSE );
  289. }
  290. if ( ( ULONG )PrevEntry.Blink != Current )
  291. {
  292. dprintf( "Previous entry's Blink doesn't match current entry's address.\n" );
  293. dprintf( "The list might be corrupt, or you may be using traversal state saved before the list changed.\n" );
  294. return( FALSE );
  295. }
  296. *Prev = prPrevEntry;
  297. return( TRUE );
  298. }
  299. BOOL ReadArgsForTraverse( const char *args, char *VarName )
  300. {
  301. PCHAR pchListVar;
  302. int index;
  303. BOOL bRetval = FALSE;
  304. pchListVar = strstr( args, "-l" );
  305. if ( pchListVar )
  306. {
  307. pchListVar += 2;
  308. while ( *pchListVar == ' ' )
  309. {
  310. pchListVar ++;
  311. }
  312. if ( *pchListVar == '\0' )
  313. {
  314. dprintf( "NOT IMPLEMENTED: usage on -l\n" );
  315. // ipxdev_usage();
  316. return( bRetval );
  317. }
  318. for ( index = 0; index < MAX_LIST_VARIABLE_NAME_LENGTH; index ++ )
  319. {
  320. VarName[ index ] = *pchListVar;
  321. if ( *pchListVar == ' ' || *pchListVar == '\0' )
  322. {
  323. VarName[ index ] = '\0';
  324. break;
  325. }
  326. VarName[ index + 1 ] = '\0';
  327. pchListVar ++;
  328. }
  329. bRetval = TRUE;
  330. }
  331. return( bRetval );
  332. }