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.

220 lines
6.1 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1998-2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: PackBuff.cpp
  6. * Content: Packed Buffers
  7. *
  8. *
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 11/01/00 mjn Created
  13. * 06/15/2000 rmt Added func to add string to packbuffer
  14. * 02/06/2001 rodtoll WINBUG #293871: DPLOBBY8: [IA64] Lobby launching a 64-bit
  15. * app from 64-bit lobby launcher crashes with unaligned memory error.
  16. ***************************************************************************/
  17. #include "dncmni.h"
  18. #include "PackBuff.h"
  19. //**********************************************************************
  20. // Constant definitions
  21. //**********************************************************************
  22. //**********************************************************************
  23. // Macro definitions
  24. //**********************************************************************
  25. //**********************************************************************
  26. // Structure definitions
  27. //**********************************************************************
  28. //**********************************************************************
  29. // Variable definitions
  30. //**********************************************************************
  31. //**********************************************************************
  32. // Function prototypes
  33. //**********************************************************************
  34. //**********************************************************************
  35. // Function definitions
  36. //**********************************************************************
  37. //**********************************************************************
  38. // ------------------------------
  39. // CPackedBuffer::Initialize
  40. //
  41. // Entry: void *const pvBuffer - Buffer to fill up (may be NULL)
  42. // const DWORD dwBufferSize - Size of buffer (may be 0)
  43. //
  44. // Exit: Nothing
  45. // ------------------------------
  46. #undef DPF_MODNAME
  47. #define DPF_MODNAME "CPackedBuffer::Initialize"
  48. void CPackedBuffer::Initialize(void *const pvBuffer,
  49. const DWORD dwBufferSize, const BOOL fAlignRequired)
  50. {
  51. if (pvBuffer == NULL || dwBufferSize == 0)
  52. {
  53. m_pStart = NULL;
  54. m_pHead = NULL;
  55. m_pTail = NULL;
  56. m_dwRemaining = 0;
  57. m_bBufferTooSmall = TRUE;
  58. }
  59. else
  60. {
  61. m_pStart = reinterpret_cast<BYTE*>(pvBuffer);
  62. m_pHead = m_pStart;
  63. m_pTail = m_pStart + dwBufferSize;
  64. m_dwRemaining = dwBufferSize;
  65. if( fAlignRequired )
  66. {
  67. DWORD dwExtra = m_dwRemaining % sizeof( void * );
  68. m_dwRemaining -= dwExtra;
  69. m_pTail -= dwExtra;
  70. }
  71. m_bBufferTooSmall = FALSE;
  72. }
  73. m_dwRequired = 0;
  74. }
  75. //**********************************************************************
  76. // ------------------------------
  77. // CPackedBuffer::AddToFront
  78. //
  79. // Entry: void *const pvBuffer - Buffer to add (may be NULL)
  80. // const DWORD dwBufferSize - Size of buffer (may be 0)
  81. //
  82. // Exit: Error Code: DPN_OK if able to add
  83. // DPNERR_BUFFERTOOSMALL if buffer is full
  84. // ------------------------------
  85. #undef DPF_MODNAME
  86. #define DPF_MODNAME "CPackedBuffer::AddToFront"
  87. HRESULT CPackedBuffer::AddToFront(const void *const pvBuffer,
  88. const DWORD dwBufferSize,
  89. const BOOL fAlignedRequired )
  90. {
  91. DWORD dwBytesToAdd = dwBufferSize;
  92. if( fAlignedRequired )
  93. {
  94. DWORD dwNumBytesFromAligned = dwBufferSize % sizeof( void *);
  95. if( dwNumBytesFromAligned )
  96. {
  97. dwBytesToAdd += sizeof( void * ) - dwNumBytesFromAligned;
  98. }
  99. }
  100. DPFX( DPFPREP, 9, "Adding to front: %d bytes --> %d bytes aligned, pointer %p new pointer %p", dwBufferSize, dwBytesToAdd, m_pHead, m_pHead + dwBytesToAdd );
  101. m_dwRequired += dwBytesToAdd;
  102. if (!m_bBufferTooSmall)
  103. {
  104. if (m_dwRemaining >= dwBytesToAdd)
  105. {
  106. if (pvBuffer)
  107. {
  108. memcpy(m_pHead,pvBuffer,dwBufferSize);
  109. }
  110. m_pHead += dwBytesToAdd;
  111. m_dwRemaining -= dwBytesToAdd;
  112. }
  113. else
  114. {
  115. m_bBufferTooSmall = TRUE;
  116. }
  117. }
  118. if (m_bBufferTooSmall)
  119. return(DPNERR_BUFFERTOOSMALL);
  120. return(DPN_OK);
  121. }
  122. //**********************************************************************
  123. // ------------------------------
  124. // CPackedBuffer::AddWCHARStringToBack
  125. //
  126. // Entry: const wchar_t * const pwszString - String to add (may be NULL)
  127. //
  128. // Exit: Error Code: DPN_OK if able to add
  129. // DPNERR_BUFFERTOOSMALL if buffer is full
  130. // ------------------------------
  131. #undef DPF_MODNAME
  132. #define DPF_MODNAME "CPackedBuffer::AddWCHARStringToBack"
  133. HRESULT CPackedBuffer::AddWCHARStringToBack( const wchar_t * const pwszString, const BOOL fAlignedRequired )
  134. {
  135. return AddToBack( pwszString, sizeof( wchar_t ) * (wcslen( pwszString )+1), fAlignedRequired );
  136. }
  137. //**********************************************************************
  138. // ------------------------------
  139. // CPackedBuffer::AddToBack
  140. //
  141. // Entry: void *const pvBuffer - Buffer to add (may be NULL)
  142. // const DWORD dwBufferSize - Size of buffer (may be 0)
  143. //
  144. // Exit: Error Code: DPN_OK if able to add
  145. // DPNERR_BUFFERTOOSMALL if buffer is full
  146. // ------------------------------
  147. #undef DPF_MODNAME
  148. #define DPF_MODNAME "CPackedBuffer::AddToBack"
  149. HRESULT CPackedBuffer::AddToBack(const void *const pvBuffer,
  150. const DWORD dwBufferSize,
  151. const BOOL fAlignedRequired )
  152. {
  153. DWORD dwBytesToAdd = dwBufferSize;
  154. if( fAlignedRequired )
  155. {
  156. DWORD dwNumBytesFromAligned = dwBufferSize % sizeof( void * );
  157. if( dwNumBytesFromAligned )
  158. {
  159. dwBytesToAdd += sizeof( void * ) - dwNumBytesFromAligned;
  160. }
  161. }
  162. DPFX( DPFPREP, 9, "Adding to back: %d bytes --> %d bytes aligned, pointer %p new pointer %p", dwBufferSize, dwBytesToAdd, m_pTail, m_pTail -dwBytesToAdd );
  163. m_dwRequired += dwBytesToAdd;
  164. if (!m_bBufferTooSmall)
  165. {
  166. if (m_dwRemaining >= dwBytesToAdd)
  167. {
  168. m_pTail -= dwBytesToAdd;
  169. m_dwRemaining -= dwBytesToAdd;
  170. if (pvBuffer)
  171. {
  172. memcpy(m_pTail,pvBuffer,dwBufferSize);
  173. }
  174. }
  175. else
  176. {
  177. m_bBufferTooSmall = TRUE;
  178. }
  179. }
  180. if (m_bBufferTooSmall)
  181. return(DPNERR_BUFFERTOOSMALL);
  182. return(DPN_OK);
  183. }