SWF_Bitstream.h (5495B)
1 /* 2 =========================================================================== 3 4 Doom 3 BFG Edition GPL Source Code 5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 6 7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). 8 9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>. 21 22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below. 23 24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. 25 26 =========================================================================== 27 */ 28 #ifndef __SWF_BITSTREAM_H__ 29 #define __SWF_BITSTREAM_H__ 30 31 class idSWFBitStream { 32 public: 33 idSWFBitStream(); 34 idSWFBitStream( const byte * data, uint32 len, bool copy ) { free = false; Load( data, len, copy ); } 35 ~idSWFBitStream() { Free(); } 36 37 idSWFBitStream & operator=( idSWFBitStream & other ); 38 39 void Load( const byte * data, uint32 len, bool copy ); 40 void Free(); 41 const byte * Ptr() { return startp; } 42 43 uint32 Length() const { return (uint32)( endp - startp ); } 44 uint32 Tell() const { return (uint32)( readp - startp ); } 45 void Seek( int32 offset ) { readp += offset; } 46 void Rewind() { readp = startp; } 47 48 void ResetBits(); 49 50 int ReadS( unsigned int numBits ); 51 unsigned int ReadU( unsigned int numBits ); 52 bool ReadBool(); 53 54 const byte * ReadData( int size ); 55 56 template< typename T > 57 void ReadLittle( T & val ); 58 59 uint8 ReadU8(); 60 uint16 ReadU16(); 61 uint32 ReadU32(); 62 int16 ReadS16(); 63 int32 ReadS32(); 64 uint32 ReadEncodedU32(); 65 float ReadFixed8(); 66 float ReadFixed16(); 67 float ReadFloat(); 68 double ReadDouble(); 69 const char * ReadString(); 70 71 void ReadRect( swfRect_t & rect ); 72 void ReadMatrix( swfMatrix_t & matrix ); 73 void ReadColorXFormRGBA( swfColorXform_t & cxf ); 74 void ReadColorRGB( swfColorRGB_t & color ); 75 void ReadColorRGBA( swfColorRGBA_t & color ); 76 void ReadGradient( swfGradient_t & grad, bool rgba ); 77 void ReadMorphGradient( swfGradient_t & grad ); 78 79 private: 80 bool free; 81 82 const byte * startp; 83 const byte * endp; 84 const byte * readp; 85 86 uint64 currentBit; 87 uint64 currentByte; 88 89 int ReadInternalS( uint64 & regCurrentBit, uint64 & regCurrentByte, unsigned int numBits ); 90 unsigned int ReadInternalU( uint64 & regCurrentBit, uint64 & regCurrentByte, unsigned int numBits ); 91 }; 92 93 /* 94 ======================== 95 idSWFBitStream::ResetBits 96 ======================== 97 */ 98 ID_INLINE void idSWFBitStream::ResetBits() { 99 currentBit = 0; 100 currentByte = 0; 101 } 102 103 /* 104 ======================== 105 idSWFBitStream::ReadLittle 106 ======================== 107 */ 108 template< typename T > 109 void idSWFBitStream::ReadLittle( T & val ) { 110 val = *(T *)ReadData( sizeof( val ) ); 111 idSwap::Little( val ); 112 } 113 114 /* 115 ======================== 116 Wrappers for the most basic types 117 ======================== 118 */ 119 ID_INLINE bool idSWFBitStream::ReadBool() { return ( ReadU( 1 ) != 0 ); } 120 ID_INLINE uint8 idSWFBitStream::ReadU8() { ResetBits(); return *readp++; } 121 ID_INLINE uint16 idSWFBitStream::ReadU16() { ResetBits(); readp += 2; return ( readp[-2] | ( readp[-1] << 8 ) ); } 122 ID_INLINE uint32 idSWFBitStream::ReadU32() { ResetBits(); readp += 4; return ( readp[-4] | ( readp[-3] << 8 ) | ( readp[-2] << 16 ) | ( readp[-1] << 24 ) ); } 123 ID_INLINE int16 idSWFBitStream::ReadS16() { ResetBits(); readp += 2; return ( readp[-2] | ( readp[-1] << 8 ) ); } 124 ID_INLINE int32 idSWFBitStream::ReadS32() { ResetBits(); readp += 4; return ( readp[-4] | ( readp[-3] << 8 ) | ( readp[-2] << 16 ) | ( readp[-1] << 24 ) ); } 125 ID_INLINE float idSWFBitStream::ReadFixed8() { ResetBits(); readp += 2; return SWFFIXED8( ( readp[-2] | ( readp[-1] << 8 ) ) ); } 126 ID_INLINE float idSWFBitStream::ReadFixed16() { ResetBits(); readp += 4; return SWFFIXED16( ( readp[-4] | ( readp[-3] << 8 ) | ( readp[-2] << 16 ) | ( readp[-1] << 24 ) ) ); } 127 ID_INLINE float idSWFBitStream::ReadFloat() { ResetBits(); readp += 4; uint32 i = ( readp[-4] | ( readp[-3] << 8 ) | ( readp[-2] << 16 ) | ( readp[-1] << 24 ) ); return (float &)i; } 128 129 ID_INLINE double idSWFBitStream::ReadDouble() { 130 const byte * swfIsRetarded = ReadData( 8 ); 131 byte buffer[8]; 132 buffer[0] = swfIsRetarded[4]; 133 buffer[1] = swfIsRetarded[5]; 134 buffer[2] = swfIsRetarded[6]; 135 buffer[3] = swfIsRetarded[7]; 136 buffer[4] = swfIsRetarded[0]; 137 buffer[5] = swfIsRetarded[1]; 138 buffer[6] = swfIsRetarded[2]; 139 buffer[7] = swfIsRetarded[3]; 140 double d = *(double *)buffer; 141 idSwap::Little( d ); 142 return d; 143 } 144 145 #endif // !__SWF_BITSTREAM_H__