00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00047
00048 #if !defined COBITBUF_H
00049 #define COBITBUF_H
00050
00051 #include <stdio.h>
00052 #include "datatype.h"
00053
00054 const long MIN_SIZE_LIMIT=sizeof(DWORD)+1;
00055
00056
00057 enum DIRECTION {MDC_FORWARD, MDC_BACKWARD};
00058 typedef struct bufpos_tag
00059 {
00060 long lCurBytePos;
00061 BYTE nCurBitOffset;
00062 long lStartPos;
00063 long lEndPos;
00064 long lTotalItemNum;
00065 } BUFPOS;
00066
00067 class COBitBuf
00068 {
00069
00070 public:
00071 COBitBuf();
00072 ~COBitBuf();
00073
00074
00075 protected:
00076 BYTE *pnBuf;
00077 DWORD dwBufSize;
00078 long lActualBufSize;
00079 BUFPOS BufPos;
00080 long lFwdLoadBlkSize, lBackLoadBlkSize;
00081 int iStatus;
00082
00083
00084 public:
00085 int InitBuffer(DWORD dwBufferSize);
00086 void ResetBuffer(void);
00087 DWORD GetBufferSize(void) { return dwBufSize; };
00088 BYTE GetCurBitOffset() { return BufPos.nCurBitOffset; };
00089 bool ByteAligned() { return (BufPos.nCurBitOffset==0)? true : false; };
00090 int SetLoadSize(long lNewLoadSize, DIRECTION eDirection);
00091 int SetLoadSize(long lFwdLoadSize, long lBackLoadSize);
00092 long GetLoadSize(DIRECTION eDirection);
00093 int FwdJumpBytes(DWORD dwOffset);
00094 int BackJumpBytes(DWORD dwOffset);
00095 BYTE ShowByte(int *piException);
00096 BYTE ShowByte(void);
00097 BYTE GetByte(int *piException);
00098 BYTE GetByte(void);
00099 WORD ShowWord(int *piException);
00100 WORD ShowWord(void);
00101 WORD GetWord(int *piException);
00102 WORD GetWord(void);
00103 DWORD ShowDWord(int *piException);
00104 DWORD ShowDWord(void);
00105 DWORD GetDWord(int *piException);
00106 DWORD GetDWord(void);
00107 inline int FwdJumpBits(DWORD dwOffset);
00108 inline int BackJumpBits(DWORD dwOffset);
00109 DWORD ShowBits(BYTE nBitNum, int *piException);
00110 DWORD ShowBits(BYTE nBitNum);
00111 inline DWORD GetBits(BYTE nBitNum, int *piException);
00112 inline DWORD GetBits(BYTE nBitNum);
00113 int GetStatus(void) {return iStatus;};
00114 void ClearStatus(void) {iStatus=MDC_SUCCESS;};
00115
00116 inline BYTE ShowByteBits(BYTE nBitNum, int *piException);
00117 inline BYTE ShowByteBits(BYTE nBitNum);
00118 inline BYTE GetByteBits(BYTE nBitNum);
00119
00120
00121 protected:
00122 void SetBufPos(const BUFPOS &dNewBufPos) {BufPos=dNewBufPos;};
00123
00124
00125 protected:
00126
00127 virtual long FwdLoadData(long lLoadStartPos, DWORD dwItemNum)=0;
00128
00129 virtual long BackLoadData(long lLoadEndPos, DWORD dwItemNum)=0;
00130
00131 virtual long FlushBuf(long lOffset, BUFPOS *pBufPos)=0;
00132 };
00133
00134 int COBitBuf::FwdJumpBits(DWORD dwOffset)
00135 {
00136 DWORD dwBitOffset;
00137 int iStatus;
00138
00139 dwBitOffset=dwOffset+BufPos.nCurBitOffset;
00140 if (dwBitOffset>=8)
00141 {
00142 iStatus=FwdJumpBytes(dwBitOffset>>3);
00143 if (iStatus!=MDC_SUCCESS)
00144 return iStatus;
00145 BufPos.nCurBitOffset=(BYTE)dwBitOffset&7;
00146 }
00147 else
00148 BufPos.nCurBitOffset=(BYTE)dwBitOffset;
00149 return MDC_SUCCESS;
00150 }
00151
00152 int COBitBuf::BackJumpBits(DWORD dwOffset)
00153 {
00154 long lBitOffset;
00155 int iStatus;
00156
00157 lBitOffset=BufPos.nCurBitOffset-dwOffset;
00158 if (lBitOffset>=0)
00159 {
00160 BufPos.nCurBitOffset=(BYTE)lBitOffset;
00161 }
00162 else
00163 {
00164 iStatus=BackJumpBytes(-(lBitOffset+1)/8+1);
00165 if (iStatus!=MDC_SUCCESS)
00166 return iStatus;
00167 BufPos.nCurBitOffset=7+(lBitOffset+1)%8;
00168 }
00169 return MDC_SUCCESS;
00170 }
00171
00172 DWORD COBitBuf::GetBits(BYTE nBitNum, int *piException)
00173 {
00174 DWORD dwResult;
00175 BYTE nBitEnd;
00176
00177 dwResult=ShowBits(nBitNum,piException);
00178 if (*piException!=MDC_SUCCESS)
00179 return 0;
00180
00181 nBitEnd=nBitNum+BufPos.nCurBitOffset;
00182 BufPos.lCurBytePos+=(nBitEnd>>3);
00183 if (BufPos.lCurBytePos>=(long)dwBufSize)
00184 BufPos.lCurBytePos-=dwBufSize;
00185 BufPos.nCurBitOffset=nBitEnd&7;
00186 return dwResult;
00187 }
00188
00189 DWORD COBitBuf::GetBits(BYTE nBitNum)
00190 {
00191 DWORD dwResult;
00192 BYTE nBitEnd;
00193 int iException;
00194
00195 dwResult=ShowBits(nBitNum,&iException);
00196 if (iException!=MDC_SUCCESS)
00197 return 0;
00198
00199 nBitEnd=nBitNum+BufPos.nCurBitOffset;
00200 BufPos.lCurBytePos+=(nBitEnd>>3);
00201 if (BufPos.lCurBytePos>=(long)dwBufSize)
00202 BufPos.lCurBytePos-=dwBufSize;
00203 BufPos.nCurBitOffset=nBitEnd&7;
00204 return dwResult;
00205 }
00206
00207 BYTE COBitBuf::ShowByteBits(BYTE nBitNum, int *piException)
00208 {
00209 #ifdef DEBUG
00210 if (nBitNum>8)
00211 {
00212 if (iStatus>0)
00213 iStatus=UNSUCCESS;
00214 *piException=UNSUCCESS;
00215 return 0;
00216 }
00217 #endif
00218 if (nBitNum+BufPos.nCurBitOffset<=8)
00219 return ( (BYTE)(ShowByte(piException)<<BufPos.nCurBitOffset) )>>( 8-nBitNum );
00220 else
00221 return (BYTE)(( (WORD)(ShowWord(piException)<<BufPos.nCurBitOffset) )>>( 16-nBitNum ));
00222 }
00223
00224 BYTE COBitBuf::ShowByteBits(BYTE nBitNum)
00225 {
00226 #ifdef DEBUG
00227 if (nBitNum>8)
00228 {
00229 if (iStatus>0)
00230 iStatus=UNSUCCESS;
00231 return 0;
00232 }
00233 #endif
00234 if (nBitNum+BufPos.nCurBitOffset<=8)
00235 return ( (BYTE)(ShowByte()<<BufPos.nCurBitOffset) )>>( 8-nBitNum );
00236 else
00237 return (BYTE)(( (WORD)(ShowWord()<<BufPos.nCurBitOffset) )>>( 16-nBitNum ));
00238 }
00239
00240 BYTE COBitBuf::GetByteBits(BYTE nBitNum)
00241 {
00242 BYTE nResult;
00243 BYTE nBitEnd;
00244 int iException;
00245
00246 nResult=ShowByteBits(nBitNum,&iException);
00247 if (iException!=MDC_SUCCESS)
00248 return 0;
00249
00250 nBitEnd=nBitNum+BufPos.nCurBitOffset;
00251 BufPos.lCurBytePos+=(nBitEnd>>3);
00252 if (BufPos.lCurBytePos>=(long)dwBufSize)
00253 BufPos.lCurBytePos-=dwBufSize;
00254 BufPos.nCurBitOffset=nBitEnd&7;
00255 return nResult;
00256 }
00257
00258 #endif // COBITBUF_H