bitpack.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_BITPACK_H
00033 #define LL_BITPACK_H
00034 
00035 #include "llerror.h"
00036 
00037 const U32 MAX_DATA_BITS = 8;
00038 
00039 
00040 class LLBitPack
00041 {
00042 public:
00043         LLBitPack(U8 *buffer, U32 max_size) : mBuffer(buffer), mBufferSize(0), mLoad(0), mLoadSize(0), mTotalBits(0), mMaxSize(max_size)
00044         {
00045         }
00046 
00047         ~LLBitPack()
00048         {
00049         }
00050 
00051         void resetBitPacking()
00052         {
00053                 mLoad = 0;
00054                 mLoadSize = 0;
00055                 mTotalBits = 0;
00056                 mBufferSize = 0;
00057         }
00058 
00059         U32 bitPack(U8 *total_data, U32 total_dsize)
00060         {
00061                 U32 dsize;
00062                 U8 data;
00063 
00064                 while (total_dsize > 0)
00065                 {
00066                         if (total_dsize > MAX_DATA_BITS)
00067                         {
00068                                 dsize = MAX_DATA_BITS;
00069                                 total_dsize -= MAX_DATA_BITS;
00070                         }
00071                         else
00072                         {
00073                                 dsize = total_dsize;
00074                                 total_dsize = 0;
00075                         }
00076 
00077                         data = *total_data++;
00078 
00079                         data <<= (MAX_DATA_BITS - dsize);
00080                         while (dsize > 0) 
00081                         {
00082                                 if (mLoadSize == MAX_DATA_BITS) 
00083                                 {
00084                                         *(mBuffer + mBufferSize++) = mLoad;
00085                                         if (mBufferSize > mMaxSize)
00086                                         {
00087                                                 llerror("mBufferSize exceeding mMaxSize!", 0);
00088                                         }
00089                                         mLoadSize = 0;
00090                                         mLoad = 0x00;
00091                                 }
00092                                 mLoad <<= 1;                    
00093                                 mLoad |= (data >> (MAX_DATA_BITS - 1));
00094                                 data <<= 1;
00095                                 mLoadSize++;
00096                                 mTotalBits++;
00097                                 dsize--;
00098                         }
00099                 }
00100                 return mBufferSize;
00101         }
00102 
00103         U32 bitCopy(U8 *total_data, U32 total_dsize)
00104         {
00105                 U32 dsize;
00106                 U8  data;
00107 
00108                 while (total_dsize > 0)
00109                 {
00110                         if (total_dsize > MAX_DATA_BITS)
00111                         {
00112                                 dsize = MAX_DATA_BITS;
00113                                 total_dsize -= MAX_DATA_BITS;
00114                         }
00115                         else
00116                         {
00117                                 dsize = total_dsize;
00118                                 total_dsize = 0;
00119                         }
00120 
00121                         data = *total_data++;
00122 
00123                         while (dsize > 0) 
00124                         {
00125                                 if (mLoadSize == MAX_DATA_BITS) 
00126                                 {
00127                                         *(mBuffer + mBufferSize++) = mLoad;
00128                                         if (mBufferSize > mMaxSize)
00129                                         {
00130                                                 llerror("mBufferSize exceeding mMaxSize!", 0);
00131                                         }
00132                                         mLoadSize = 0;
00133                                         mLoad = 0x00;
00134                                 }
00135                                 mLoad <<= 1;                    
00136                                 mLoad |= (data >> (MAX_DATA_BITS - 1));
00137                                 data <<= 1;
00138                                 mLoadSize++;
00139                                 mTotalBits++;
00140                                 dsize--;
00141                         }
00142                 }
00143                 return mBufferSize;
00144         }
00145 
00146         U32 bitUnpack(U8 *total_retval, U32 total_dsize)
00147         {
00148                 U32 dsize;
00149                 U8      *retval;
00150 
00151                 while (total_dsize > 0)
00152                 {
00153                         if (total_dsize > MAX_DATA_BITS)
00154                         {
00155                                 dsize = MAX_DATA_BITS;
00156                                 total_dsize -= MAX_DATA_BITS;
00157                         }
00158                         else
00159                         {
00160                                 dsize = total_dsize;
00161                                 total_dsize = 0;
00162                         }
00163 
00164                         retval = total_retval++;
00165                         *retval = 0x00;
00166                         while (dsize > 0) 
00167                         {
00168                                 if (mLoadSize == 0) 
00169                                 {
00170 #ifdef _DEBUG
00171                                         if (mBufferSize > mMaxSize)
00172                                         {
00173                                                 llerrs << "mBufferSize exceeding mMaxSize" << llendl;
00174                                                 llerrs << mBufferSize << " > " << mMaxSize << llendl;
00175                                         }
00176 #endif
00177                                         mLoad = *(mBuffer + mBufferSize++);
00178                                         mLoadSize = MAX_DATA_BITS;
00179                                 }
00180                                 *retval <<= 1;
00181                                 *retval |= (mLoad >> (MAX_DATA_BITS - 1));
00182                                 mLoadSize--;
00183                                 mLoad <<= 1;
00184                                 dsize--;
00185                         }
00186                 }
00187                 return mBufferSize;
00188         }
00189 
00190         U32 flushBitPack()
00191         {
00192                 if (mLoadSize) 
00193                 {
00194                         mLoad <<= (MAX_DATA_BITS - mLoadSize);
00195                         *(mBuffer + mBufferSize++) = mLoad;
00196                         if (mBufferSize > mMaxSize)
00197                         {
00198                                 llerror("mBufferSize exceeding mMaxSize!", 0);
00199                         }
00200                         mLoadSize = 0;
00201                 }
00202                 return mBufferSize;
00203         }
00204 
00205         U8              *mBuffer;
00206         U32             mBufferSize;
00207         U8              mLoad;
00208         U32             mLoadSize;
00209         U32             mTotalBits;
00210         U32             mMaxSize;
00211 };
00212 
00213 #endif

Generated on Thu Jul 1 06:08:15 2010 for Second Life Viewer by  doxygen 1.4.7