Clone of UAS2 @ https://github.com/drudgedance/uas2

Message.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * This file is part of UAS2.
  3. *
  4. * UAS2 is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * UAS2 is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with UASv1; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. /**
  18. * @file Message.cpp
  19. * Encapsulates cMessage functionality.
  20. */
  21. #include "Message.h"
  22. #pragma intrinsic( memcpy, memset, strlen )
  23. cMessage::cBlock::cBlock ()
  24. : m_pStart ( g_dispenser.get () ),
  25. m_pUsed ( m_pStart )
  26. {
  27. }
  28. cMessage::cBlock::cBlock ( const cBlock &from )
  29. : m_pStart ( from.m_pStart ),
  30. m_pUsed ( from.m_pUsed )
  31. {
  32. from.m_pStart = NULL;
  33. from.m_pUsed = NULL;
  34. }
  35. cMessage::cBlock::~cBlock ()
  36. {
  37. if ( m_pStart != NULL )
  38. g_dispenser.release ( m_pStart );
  39. }
  40. BOOL cMessage::cBlock::pasteData ( BYTE * &pData, int &nBytes )
  41. {
  42. int nAvailable = BUFFER_BLOCK_SIZE - ( m_pUsed - m_pStart );
  43. if ( nAvailable < nBytes )
  44. {
  45. // Paste what we can
  46. CopyMemory( m_pUsed, pData, nAvailable );
  47. nBytes -= nAvailable;
  48. m_pUsed += nAvailable;
  49. return FALSE;
  50. }
  51. CopyMemory( m_pUsed, pData, nBytes );
  52. m_pUsed += nBytes;
  53. return TRUE;
  54. }
  55. int cMessage::cBlock::getAlign ( int nBoundary ) const
  56. {
  57. int nAlign = ( m_pUsed - m_pStart ) % nBoundary;
  58. return ( nAlign == 0 ) ? 0 : ( nBoundary - nAlign );
  59. }
  60. BOOL cMessage::cBlock::pasteAlign ( int &nBytes )
  61. {
  62. int nAvailable = BUFFER_BLOCK_SIZE - ( m_pUsed - m_pStart );
  63. if ( nAvailable < nBytes )
  64. {
  65. ZeroMemory( m_pUsed, nAvailable );
  66. nBytes -= nAvailable;
  67. m_pUsed += nAvailable;
  68. return FALSE;
  69. }
  70. ZeroMemory( m_pUsed, nBytes );
  71. m_pUsed += nBytes;
  72. return TRUE;
  73. }
  74. BOOL cMessage::cBlock::getData ( size_t &nOffset, size_t &nLength, BYTE * &pBuffer ) const
  75. {
  76. int nAvailable = BUFFER_BLOCK_SIZE - nOffset;
  77. if ( nAvailable < nLength )
  78. {
  79. CopyMemory( pBuffer, m_pStart + nOffset, nAvailable );
  80. nOffset = 0;
  81. nLength -= nAvailable;
  82. pBuffer += nAvailable;
  83. return FALSE;
  84. }
  85. CopyMemory( pBuffer, m_pStart + nOffset, nLength );
  86. return TRUE;
  87. }
  88. cMessage::cMessage()
  89. {
  90. // Immediately allocate one block
  91. m_blocks.push_back ( cBlock() );
  92. }
  93. void cMessage::pasteData ( BYTE *pbData, int nBytes )
  94. {
  95. while ( !m_blocks.back().pasteData ( pbData, nBytes ) )
  96. m_blocks.push_back ( cBlock() );
  97. }
  98. void cMessage::pasteAlign ( int nBoundary )
  99. {
  100. int nBytes = m_blocks.back().getAlign ( nBoundary );
  101. if ( nBytes != 0 )
  102. {
  103. while ( !m_blocks.back().pasteAlign ( nBytes ) )
  104. m_blocks.push_back ( cBlock() );
  105. }
  106. }
  107. void cMessage::pasteString ( const char *str )
  108. {
  109. if( str == NULL )
  110. {
  111. // Paste an empty string, 1 char (null) and then dword align
  112. paste ( 1L );
  113. return;
  114. }
  115. int nLength = lstrlen( str ) + 1;
  116. paste ( static_cast< short > ( nLength ) );
  117. pasteData ( reinterpret_cast< BYTE * > ( const_cast< char * > ( str ) ), nLength );
  118. pasteAlign ( sizeof ( long ) );
  119. }
  120. size_t cMessage::getSize () const
  121. {
  122. return ( m_blocks.size () - 1 ) * BUFFER_BLOCK_SIZE + m_blocks.back().getUsed ();
  123. }
  124. void cMessage::getData ( size_t nOffset, size_t nLength, BYTE *pBuffer ) const
  125. {
  126. nOffset %= BUFFER_BLOCK_SIZE;
  127. for ( cBlockList::const_iterator i = m_blocks.begin() + ( nOffset / BUFFER_BLOCK_SIZE );
  128. !i->getData ( nOffset, nLength, pBuffer); ++ i );
  129. #if _DEBUG
  130. //char szUpdate[50];
  131. // sprintf(szUpdate,"nOffset: %d",nOffset);
  132. // UpdateConsole((char *)szUpdate);
  133. #endif
  134. }
  135. cMessage::cBlockDispenser::cBlockDispenser ()
  136. : m_hHeap( ::HeapCreate ( 0, 0, 0 ) )
  137. {
  138. }
  139. cMessage::cBlockDispenser::~cBlockDispenser ()
  140. {
  141. ::HeapDestroy( m_hHeap );
  142. }
  143. BYTE *cMessage::cBlockDispenser::get ()
  144. {
  145. return reinterpret_cast< BYTE * > ( ::HeapAlloc ( m_hHeap, 0, BUFFER_BLOCK_SIZE ) );
  146. }
  147. void cMessage::cBlockDispenser::release ( BYTE *pBlock )
  148. {
  149. ::HeapFree ( m_hHeap, 0, pBlock );
  150. }
  151. cMessage::cBlockDispenser cMessage::g_dispenser;