Source code of Windows XP (NT5)
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.

293 lines
7.3 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::AddStringToBack
  140. //
  141. // Entry: Pointer to source string
  142. //
  143. // Exit: Error Code: DPN_OK if able to add
  144. // DPNERR_BUFFERTOOSMALL if buffer is full
  145. // ------------------------------
  146. #undef DPF_MODNAME
  147. #define DPF_MODNAME "CPackedBuffer::AddStringToBack"
  148. HRESULT CPackedBuffer::AddStringToBack( const char *const pszString )
  149. {
  150. HRESULT hr;
  151. DWORD dwStringSize;
  152. DWORD dwBufferSize;
  153. DNASSERT( pszString != NULL );
  154. //
  155. // initialize
  156. //
  157. hr = DPN_OK;
  158. dwStringSize = 0;
  159. hr = STR_AnsiToWide( pszString,
  160. -1,
  161. NULL,
  162. &dwStringSize );
  163. if ( hr != DPNERR_BUFFERTOOSMALL )
  164. {
  165. DNASSERT( hr != DPN_OK );
  166. goto Failure;
  167. }
  168. dwBufferSize = dwStringSize * sizeof( WCHAR );
  169. m_dwRequired += dwBufferSize;
  170. if ( !m_bBufferTooSmall )
  171. {
  172. if (m_dwRemaining >= dwBufferSize)
  173. {
  174. m_pTail -= dwBufferSize;
  175. m_dwRemaining -= dwBufferSize;
  176. hr = STR_AnsiToWide( pszString,
  177. -1,
  178. reinterpret_cast<WCHAR*>( m_pTail ),
  179. &dwStringSize );
  180. if ( hr != DPN_OK )
  181. {
  182. goto Failure;
  183. }
  184. DNASSERT( ( dwStringSize * sizeof( WCHAR ) ) == dwBufferSize );
  185. }
  186. else
  187. {
  188. m_bBufferTooSmall = TRUE;
  189. }
  190. }
  191. if ( m_bBufferTooSmall )
  192. {
  193. hr = DPNERR_BUFFERTOOSMALL;
  194. }
  195. Exit:
  196. return hr;
  197. Failure:
  198. goto Exit;
  199. }
  200. //**********************************************************************
  201. // ------------------------------
  202. // CPackedBuffer::AddToBack
  203. //
  204. // Entry: void *const pvBuffer - Buffer to add (may be NULL)
  205. // const DWORD dwBufferSize - Size of buffer (may be 0)
  206. //
  207. // Exit: Error Code: DPN_OK if able to add
  208. // DPNERR_BUFFERTOOSMALL if buffer is full
  209. // ------------------------------
  210. #undef DPF_MODNAME
  211. #define DPF_MODNAME "CPackedBuffer::AddToBack"
  212. HRESULT CPackedBuffer::AddToBack(const void *const pvBuffer,
  213. const DWORD dwBufferSize,
  214. const BOOL fAlignedRequired )
  215. {
  216. DWORD dwBytesToAdd = dwBufferSize;
  217. if( fAlignedRequired )
  218. {
  219. DWORD dwNumBytesFromAligned = dwBufferSize % sizeof( void * );
  220. if( dwNumBytesFromAligned )
  221. {
  222. dwBytesToAdd += sizeof( void * ) - dwNumBytesFromAligned;
  223. }
  224. }
  225. DPFX( DPFPREP, 9, "Adding to back: %d bytes --> %d bytes aligned, pointer %p new pointer %p", dwBufferSize, dwBytesToAdd, m_pTail, m_pTail -dwBytesToAdd );
  226. m_dwRequired += dwBytesToAdd;
  227. if (!m_bBufferTooSmall)
  228. {
  229. if (m_dwRemaining >= dwBytesToAdd)
  230. {
  231. m_pTail -= dwBytesToAdd;
  232. m_dwRemaining -= dwBytesToAdd;
  233. if (pvBuffer)
  234. {
  235. memcpy(m_pTail,pvBuffer,dwBufferSize);
  236. }
  237. }
  238. else
  239. {
  240. m_bBufferTooSmall = TRUE;
  241. }
  242. }
  243. if (m_bBufferTooSmall)
  244. return(DPNERR_BUFFERTOOSMALL);
  245. return(DPN_OK);
  246. }