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.

302 lines
9.1 KiB

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. frmtreg.cxx
  5. Abstract:
  6. Registry for format string reuse.
  7. Notes:
  8. This file defines reuse registry for format string fragments which may
  9. be reused later.
  10. History:
  11. Mar-14-1993 GregJen Created.
  12. ----------------------------------------------------------------------------*/
  13. /****************************************************************************
  14. * include files
  15. ***************************************************************************/
  16. #include "becls.hxx"
  17. #pragma hdrstop
  18. /***********************************************************************
  19. * global data
  20. **********************************************************************/
  21. FRMTREG_DICT::FRMTREG_DICT( FORMAT_STRING * pOurs)
  22. : Dictionary()
  23. {
  24. pOurFormatString = pOurs;
  25. }
  26. SSIZE_T
  27. FRMTREG_DICT::Compare( pUserType pL, pUserType pR )
  28. {
  29. FRMTREG_ENTRY * pLeft = (FRMTREG_ENTRY *) pL;
  30. FRMTREG_ENTRY * pRight = (FRMTREG_ENTRY *) pR;
  31. // first, sort by string length
  32. int Result = ( pLeft->EndOffset - pLeft->StartOffset ) -
  33. ( pRight->EndOffset - pRight->StartOffset );
  34. if ( Result )
  35. return Result;
  36. long LeftOffset = pLeft->StartOffset;
  37. long RightOffset = pRight->StartOffset;
  38. unsigned char * pBuffer = pOurFormatString->pBuffer;
  39. unsigned char * pBufferType = pOurFormatString->pBufferType;
  40. // the same format string is, of course identical
  41. if ( LeftOffset == RightOffset )
  42. return 0;
  43. // There is a tricky situation when the strings are apart by more than 32k.
  44. // In the proc format string there is no problem with that, we can optize.
  45. // With type format string, we optimize as this is our best bet for the
  46. // final accessability of fragments.
  47. // Sort by values of format characters
  48. while ( ( Result == 0 ) && ( LeftOffset < pLeft->EndOffset) )
  49. {
  50. if ( pBufferType[ LeftOffset ] != pBufferType[ RightOffset ] )
  51. {
  52. // Types don't match, force the result to be unequal.
  53. Result = pBufferType[ LeftOffset ] - pBufferType[ RightOffset ];
  54. continue;
  55. }
  56. switch ( pBufferType[ LeftOffset ] )
  57. {
  58. case FS_SHORT_TYPE_OFFSET:
  59. // This is a comparison for the absolute type offset.
  60. {
  61. TypeOffsetDictElem *pLeftTO, *pRightTO;
  62. pLeftTO = pOurFormatString->TypeOffsetDict.
  63. LookupOffset( LeftOffset );
  64. pRightTO = pOurFormatString->TypeOffsetDict.
  65. LookupOffset( RightOffset );
  66. Result = pLeftTO->TypeOffset - pRightTO->TypeOffset;
  67. LeftOffset++; RightOffset++;
  68. }
  69. break;
  70. case FS_SHORT_OFFSET:
  71. // This is a comparison for the relative type offset.
  72. //
  73. {
  74. TypeOffsetDictElem *pLeftTO, *pRightTO;
  75. pLeftTO = pOurFormatString->TypeOffsetDict.
  76. LookupOffset( LeftOffset );
  77. pRightTO = pOurFormatString->TypeOffsetDict.
  78. LookupOffset( RightOffset );
  79. if ( ( pLeftTO->TypeOffset == 0 ) &&
  80. ( pRightTO->TypeOffset == 0 ) )
  81. Result = 0;
  82. else
  83. // compare absolute offsets
  84. Result = ( LeftOffset + pLeftTO->TypeOffset ) -
  85. ( RightOffset + pRightTO->TypeOffset );
  86. LeftOffset++; RightOffset++;
  87. }
  88. break;
  89. case FS_SHORT_STACK_OFFSET:
  90. // Compare stack offset - multiplatform issue.
  91. {
  92. Result = *((short UNALIGNED *)(pBuffer + LeftOffset)) -
  93. *((short UNALIGNED *)(pBuffer + RightOffset));
  94. if ( Result == 0 )
  95. {
  96. BOOL f32bitServer = pCommand->Is32BitEnv();
  97. if ( f32bitServer )
  98. {
  99. OffsetDictElem *pLeftStackOffsets, *pRightStackOffsets;
  100. pLeftStackOffsets = pOurFormatString->OffsetDict.
  101. LookupOffset( LeftOffset );
  102. pRightStackOffsets = pOurFormatString->OffsetDict.
  103. LookupOffset( RightOffset );
  104. long Ilo = pLeftStackOffsets->X86Offset;
  105. long Iro = pRightStackOffsets->X86Offset;
  106. Result = Ilo - Iro;
  107. }
  108. }
  109. LeftOffset++; RightOffset++;
  110. }
  111. break;
  112. case FS_PAD_MACRO:
  113. case FS_SIZE_MACRO:
  114. case FS_UNKNOWN_STACK_SIZE:
  115. // Can't compare those, so force the result to be unequal.
  116. //
  117. Result = LeftOffset - RightOffset;
  118. break;
  119. case FS_LONG:
  120. // Compare longs
  121. //
  122. Result = *((long UNALIGNED *)(pBuffer + LeftOffset)) -
  123. *((long UNALIGNED *)(pBuffer + RightOffset));
  124. LeftOffset += 3; RightOffset += 3;
  125. break;
  126. case FS_SHORT:
  127. case FS_PARAM_FLAG_SHORT:
  128. case FS_MAGIC_UNION_SHORT:
  129. case FS_CORR_FLAG_SHORT:
  130. // Compare plain shorts.
  131. //
  132. Result = *((short UNALIGNED *)(pBuffer + LeftOffset)) -
  133. *((short UNALIGNED *)(pBuffer + RightOffset));
  134. LeftOffset++; RightOffset++;
  135. break;
  136. case FS_FORMAT_CHARACTER:
  137. case FS_POINTER_FORMAT_CHARACTER:
  138. case FS_SMALL:
  139. case FS_SMALL_STACK_SIZE:
  140. case FS_OLD_PROC_FLAG_BYTE:
  141. case FS_Oi2_PROC_FLAG_BYTE:
  142. case FS_EXT_PROC_FLAG_BYTE:
  143. case FS_CORR_TYPE_BYTE:
  144. case FS_CONTEXT_HANDLE_FLAG_BYTE:
  145. default:
  146. // Compare bytes, format chars, bytes decorated for comments,
  147. //
  148. Result = pBuffer[ LeftOffset ] - pBuffer[ RightOffset ];
  149. break;
  150. }
  151. LeftOffset++; RightOffset++;
  152. }
  153. return Result;
  154. }
  155. FRMTREG_ENTRY *
  156. FRMTREG_DICT::IsRegistered(
  157. FRMTREG_ENTRY * pInfo )
  158. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  159. Routine Description:
  160. Search for a type with the reuse registry.
  161. Arguments:
  162. pInfo - A pointer to the type being registered.
  163. Return Value:
  164. The node that gets registered.
  165. Notes:
  166. ----------------------------------------------------------------------------*/
  167. {
  168. Dict_Status Status = Dict_Find( pInfo );
  169. switch( Status )
  170. {
  171. case EMPTY_DICTIONARY:
  172. case ITEM_NOT_FOUND:
  173. return (FRMTREG_ENTRY *)0;
  174. default:
  175. return (FRMTREG_ENTRY *)Dict_Curr_Item();
  176. }
  177. }
  178. FRMTREG_ENTRY *
  179. FRMTREG_DICT::Register(
  180. FRMTREG_ENTRY * pInfo )
  181. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  182. Routine Description:
  183. Register a type with the dictionary.
  184. Arguments:
  185. pType - A pointer to the type node.
  186. Return Value:
  187. The final inserted type.
  188. Notes:
  189. ----------------------------------------------------------------------------*/
  190. {
  191. Dict_Insert( (pUserType) pInfo );
  192. return pInfo;
  193. }
  194. BOOL
  195. FRMTREG_DICT::GetReUseEntry(
  196. FRMTREG_ENTRY * & pOut,
  197. FRMTREG_ENTRY * pIn )
  198. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  199. Routine Description:
  200. Register a type with the dictionary.
  201. Arguments:
  202. pRI - A pointer to the returned FRMTREG_ENTRY block
  203. pNode - A pointer to the type node.
  204. Return Value:
  205. True if the entry was already in the table,
  206. False if the entry is new.
  207. Notes:
  208. ----------------------------------------------------------------------------*/
  209. {
  210. FRMTREG_ENTRY * pRealEntry;
  211. if ( ( pRealEntry = IsRegistered( pIn ) ) == 0 )
  212. {
  213. pRealEntry = new FRMTREG_ENTRY( pIn->StartOffset, pIn->EndOffset );
  214. Register( pRealEntry );
  215. pOut = pRealEntry;
  216. return FALSE;
  217. }
  218. pOut = pRealEntry;
  219. pOut->UseCount++;
  220. return TRUE;
  221. }