QGearsLZSDataStream.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-18 Tobias Peters <tobias.peters@kreativeffekt.at>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. -----------------------------------------------------------------------------
  21. */
  22. #include "data/QGearsLZSDataStream.h"
  23. #include <OgreException.h>
  24. namespace QGears
  25. {
  26. //---------------------------------------------------------------------
  27. LZSDataStream::LZSDataStream( const Ogre::DataStreamPtr &compressed_stream ) :
  28. Ogre::DataStream( compressed_stream->getAccessMode() )
  29. ,m_compressed_stream( compressed_stream )
  30. ,m_available_compressed( 0 )
  31. ,m_pos( 0 )
  32. {
  33. init();
  34. }
  35. //---------------------------------------------------------------------
  36. LZSDataStream::LZSDataStream( const String &name, const Ogre::DataStreamPtr &compressed_stream ) :
  37. Ogre::DataStream( name, compressed_stream->getAccessMode() )
  38. ,m_compressed_stream( compressed_stream )
  39. ,m_available_compressed( 0 )
  40. ,m_pos( 0 )
  41. {
  42. init();
  43. }
  44. //---------------------------------------------------------------------
  45. LZSDataStream::~LZSDataStream()
  46. {
  47. close();
  48. }
  49. //---------------------------------------------------------------------
  50. void
  51. LZSDataStream::init()
  52. {
  53. if ( m_compressed_stream->read( &m_available_compressed, 4 ) < 4 )
  54. {
  55. OGRE_EXCEPT( Ogre::Exception::ERR_INVALIDPARAMS,
  56. "Can't read LZS Header from stream",
  57. "LZSDataStream::init");
  58. }
  59. #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
  60. flipEndian( m_available_compressed );
  61. #endif
  62. mSize = m_available_compressed; // so the base size() works
  63. }
  64. //---------------------------------------------------------------------
  65. void
  66. LZSDataStream::flipEndian( uint32 &inout_data )
  67. {
  68. inout_data = ( ( inout_data & 0x000000FF ) << 24 )
  69. |( ( inout_data & 0x0000FF00 ) << 8 )
  70. |( ( inout_data & 0x00FF0000 ) >> 8 )
  71. |( ( inout_data & 0xFF000000 ) >> 24 );
  72. }
  73. //---------------------------------------------------------------------
  74. size_t
  75. LZSDataStream::read( void *buf, size_t count )
  76. {
  77. assert( buf && "NullPointer" );
  78. size_t read_total( 0 );
  79. while( count && !eof() )
  80. {
  81. if( !m_buffer.avail() )
  82. {
  83. decompressChunk();
  84. }
  85. size_t read( m_buffer.read( static_cast<uint8*>( buf ) + read_total, count ) );
  86. count -= read;
  87. read_total += read;
  88. }
  89. m_pos += read_total;
  90. return read_total;
  91. }
  92. //---------------------------------------------------------------------
  93. void
  94. LZSDataStream::decompressChunk()
  95. {
  96. if( m_compressed_stream->eof() )
  97. {
  98. OGRE_EXCEPT( Ogre::Exception::ERR_INVALIDPARAMS,
  99. "not enough data in Stream to read control byte",
  100. "LZSDataStream::decompressChunk");
  101. }
  102. uint8 data, control_byte, ref[2];
  103. uint16 ref_offset;
  104. m_available_compressed -= m_compressed_stream->read( &control_byte, sizeof( control_byte ) );
  105. for( uint8 control_bit( 8 )
  106. ;control_bit-- && !m_compressed_stream->eof() && m_available_compressed
  107. ;control_byte >>= 1 )
  108. {
  109. if( control_byte & 1 )
  110. {
  111. m_available_compressed -= m_compressed_stream->read( &data, sizeof( data ) );
  112. m_buffer.write( &data );
  113. }
  114. else if( m_available_compressed < 2 )
  115. {
  116. OGRE_EXCEPT( Ogre::Exception::ERR_INVALIDPARAMS,
  117. "not enough data in Stream to read reference bytes",
  118. "LZSDataStream::decompressChunk");
  119. }
  120. else
  121. {
  122. m_available_compressed -= m_compressed_stream->read( &ref, sizeof( ref ) );
  123. ref_offset = 18 + ( ((ref[1] & 0xF0) << 4) | ref[0] );
  124. for( uint8 ref_length( (ref[1] & 0xF ) + 3 ); ref_length--; )
  125. {
  126. data = m_buffer.get( ref_offset++ );
  127. m_buffer.write( &data );
  128. }
  129. }
  130. }
  131. }
  132. //---------------------------------------------------------------------
  133. void
  134. LZSDataStream::skip( long count )
  135. {
  136. OGRE_EXCEPT( Ogre::Exception::ERR_NOT_IMPLEMENTED,
  137. "not yet implemented",
  138. "LZSDataStream::skip");
  139. }
  140. //---------------------------------------------------------------------
  141. void
  142. LZSDataStream::seek( size_t pos )
  143. {
  144. OGRE_EXCEPT( Ogre::Exception::ERR_NOT_IMPLEMENTED,
  145. "not yet implemented",
  146. "LZSDataStream::seek");
  147. }
  148. //---------------------------------------------------------------------
  149. bool
  150. LZSDataStream::eof( void ) const
  151. {
  152. return !m_available_compressed
  153. && !m_buffer.avail();
  154. }
  155. //---------------------------------------------------------------------
  156. size_t
  157. LZSDataStream::tell( void ) const
  158. {
  159. return m_pos;
  160. }
  161. //---------------------------------------------------------------------
  162. void
  163. LZSDataStream::close( void )
  164. {
  165. m_compressed_stream.setNull();
  166. }
  167. //---------------------------------------------------------------------
  168. }