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.

286 lines
6.9 KiB

  1. /*++
  2. dotstuff.h
  3. This header files defines some of the Dot Stuffing helper objects that
  4. we have built for the File Handle Cache.
  5. --*/
  6. #include "refptr2.h"
  7. class IDotManipBase : public CRefCount2 {
  8. protected :
  9. //
  10. // Return the number of bytes in the buffer that should pass
  11. // through the IO operation !
  12. //
  13. virtual
  14. BOOL
  15. InternalProcessBuffer(
  16. BYTE* lpb, // The users original buffer
  17. DWORD cbIn, // Number of bytes to look at in orignal buffer
  18. DWORD cbAvailable, // Number of bytes available in the original buffer
  19. DWORD &cbRemains, // Number of bytes we left in the original Buffer - can be zero
  20. BYTE* &lpbOut, // An output buffer that holds a portion of the string !
  21. DWORD &cbOut, // The amount of stuff in our output buffer
  22. int &cBias // Whether we should offset associated IO's to overwrite
  23. // previous results !
  24. ) = 0 ;
  25. //
  26. // This is the number of times the string as appeared in the message -
  27. // NOTE : This can be initialized to -1 if we are examining the message
  28. // to determine that it needs to be dot stuffed. This is because
  29. // the terminating CRLF.CRLF will set occurrences to 0, indicating that
  30. // there is no issue.
  31. //
  32. long m_cOccurrences ;
  33. //
  34. // Private constructor allows us to initialize m_cOccurrences !
  35. //
  36. IDotManipBase( long l ) :
  37. m_cOccurrences( l ) {
  38. }
  39. public :
  40. /*++
  41. This class defines a pure virtual API used by our Dot Manipulation
  42. classes to define how they interact with other code.
  43. --*/
  44. //
  45. // Destruction is always virtual !
  46. //
  47. virtual ~IDotManipBase() {}
  48. //
  49. // Publicly exposed API !
  50. //
  51. inline BOOL
  52. ProcessBuffer( BYTE* lpb,
  53. DWORD cbIn,
  54. DWORD cbAvailable,
  55. DWORD &cbRemains,
  56. BYTE* &lpbOut,
  57. DWORD &cbOut,
  58. int &cBias,
  59. BOOL fFinalBuffer = FALSE,
  60. BOOL fTerminatorPresent = FALSE
  61. ) {
  62. //
  63. // Validate the callers arguments as much as possible !
  64. //
  65. _ASSERT( lpb != 0 ) ;
  66. _ASSERT( cbIn != 0 ) ;
  67. _ASSERT( cbAvailable >= cbIn ) ;
  68. //
  69. // Ensure that these are correctly setup !
  70. //
  71. cbRemains = 0 ;
  72. lpbOut = 0 ;
  73. cbOut = 0 ;
  74. cBias = 0 ;
  75. BOOL fReturn =
  76. InternalProcessBuffer(
  77. lpb,
  78. cbIn,
  79. cbAvailable,
  80. cbRemains,
  81. lpbOut,
  82. cbOut,
  83. cBias
  84. ) ;
  85. //
  86. // Do some checking on the results of the call !
  87. //
  88. _ASSERT( cBias <= 0 ) ;
  89. _ASSERT( (lpbOut == 0 && cbOut == 0) || (lpbOut != 0 && cbOut != 0) ) ;
  90. _ASSERT( cbRemains <= cbAvailable ) ;
  91. return fReturn ;
  92. }
  93. //
  94. // Return the number of times we saw a pattern matching the
  95. // sequence specified when we were constructed !
  96. //
  97. long
  98. NumberOfOccurrences() {
  99. return m_cOccurrences ;
  100. }
  101. } ;
  102. extern BYTE szDot[] ;
  103. extern BYTE szDotStuffed[] ;
  104. extern BYTE szShrink[] ;
  105. extern BYTE szGrow[] ;
  106. class CDotScanner : public IDotManipBase {
  107. /*++
  108. This class detects messages which have Dot Stuffing issues.
  109. We can operate in one of two modes - determine if the message flowing by
  110. is dot stuffed, or determine if the message flowing by would need
  111. to be dot stuffed.
  112. --*/
  113. private :
  114. enum {
  115. SIGNATURE = 'futs', // should appear as 'stuf' in the debugger !
  116. DEAD_SIGNATURE = 'futx'
  117. } ;
  118. //
  119. // Our signature !
  120. //
  121. DWORD m_dwSignature ;
  122. //
  123. // This is the string we are interested in detecting :
  124. //
  125. BYTE *m_pchMatch ;
  126. //
  127. // This is our current match state !
  128. //
  129. BYTE *m_pchState ;
  130. //
  131. // Don't allow copies and stuff so make these private !
  132. //
  133. CDotScanner( CDotScanner& ) ;
  134. CDotScanner& operator=( CDotScanner& ) ;
  135. public :
  136. CDotScanner( BOOL fWillGetTerminator = TRUE ) :
  137. IDotManipBase( (fWillGetTerminator ? -1 : 0) ),
  138. m_dwSignature( SIGNATURE ),
  139. m_pchMatch( szDotStuffed ),
  140. m_pchState( szDotStuffed ) {
  141. m_cRefs = 0 ; // Hack - our base class CRefCount2 doesn't do what we want !
  142. }
  143. ~CDotScanner( ) {
  144. m_dwSignature = DEAD_SIGNATURE ;
  145. }
  146. //
  147. // See if the user specified pattern occurs in the buffer !
  148. //
  149. BOOL
  150. InternalProcessBuffer(
  151. BYTE* lpb, // The users original buffer
  152. DWORD cbIn, // Number of bytes to look at in orignal buffer
  153. DWORD cbAvailable, // Number of bytes available in the original buffer
  154. DWORD &cbRemains, // Number of bytes we left in the original Buffer - can be zero
  155. BYTE* &lpbOut, // An output buffer that holds a portion of the string !
  156. DWORD &cbOut, // The amount of stuff in our output buffer
  157. int &cBias // Whether we should offset associated IO's to overwrite
  158. // previous results !
  159. ) ;
  160. } ;
  161. class CDotModifier : public IDotManipBase {
  162. /*++
  163. This class is used to detect messages that need to be dot-stuffed
  164. or de-dot stuffed. We can either remove or insert dot-stuffing as
  165. required on the fly, depending on how we are constructed !
  166. --*/
  167. private :
  168. enum {
  169. SIGNATURE = 'mtod', // should appear as 'stuf' in the debugger !
  170. DEAD_SIGNATURE = 'mtox'
  171. } ;
  172. //
  173. // Our signature !
  174. //
  175. DWORD m_dwSignature ;
  176. //
  177. // This is the string we are interested in detecting :
  178. //
  179. BYTE *m_pchMatch ;
  180. //
  181. // This is our current match state !
  182. //
  183. BYTE *m_pchState ;
  184. //
  185. // This is the string we want to have replace the string we're detecting
  186. //
  187. BYTE *m_pchReplace ;
  188. //
  189. // Number of characters in the matching and replacement string.
  190. //
  191. int m_cchMatch ;
  192. int m_cDiff ;
  193. //
  194. // This is the total offset we've accumulated in the stream !
  195. //
  196. long m_cOffsetBytes ;
  197. //
  198. // Don't allow copies and stuff so make these private !
  199. //
  200. CDotModifier( CDotModifier& ) ;
  201. CDotModifier& operator=( CDotModifier& ) ;
  202. public :
  203. //
  204. // Initialize our state - cannot fail !
  205. //
  206. CDotModifier(
  207. BYTE* szMatch = szDotStuffed,
  208. BYTE* szReplace = szShrink
  209. ) :
  210. IDotManipBase( 0 ),
  211. m_dwSignature( SIGNATURE ),
  212. m_pchMatch( szMatch ),
  213. m_pchState( szMatch ),
  214. m_pchReplace( szReplace ),
  215. m_cchMatch( strlen( (const char*)szMatch) ),
  216. m_cDiff( strlen( (const char*)szReplace) - strlen( (const char*)szMatch ) ),
  217. m_cOffsetBytes( 0 ) {
  218. m_cRefs = 0 ; // HACK - our base class CRefCount2 doesn't do what we want !
  219. }
  220. //
  221. // Just mark our Signature DWORD - handy for debugging !
  222. //
  223. ~CDotModifier() {
  224. m_dwSignature = DEAD_SIGNATURE ;
  225. }
  226. //
  227. // Modify dot sequences as they go by !
  228. //
  229. BOOL
  230. InternalProcessBuffer(
  231. BYTE* lpb, // The users original buffer
  232. DWORD cbIn, // Number of bytes to look at in orignal buffer
  233. DWORD cbAvailable, // Number of bytes available in the original buffer
  234. DWORD &cbRemains, // Number of bytes we left in the original Buffer - can be zero
  235. BYTE* &lpbOut, // An output buffer that holds a portion of the string !
  236. DWORD &cbOut, // The amount of stuff in our output buffer
  237. int &cBias // Whether we should offset associated IO's to overwrite
  238. // previous results !
  239. ) ;
  240. } ;