r5sdk/r5dev/tier1/bitbuf.h

89 lines
2.0 KiB
C
Raw Permalink Normal View History

#ifndef BITBUF_H
#define BITBUF_H
typedef enum
{
BITBUFERROR_VALUE_OUT_OF_RANGE = 0, // Tried to write a value with too few bits.
BITBUFERROR_BUFFER_OVERRUN, // Was about to overrun a buffer.
BITBUFERROR_NUM_ERRORS
} BitBufErrorType;
#define LittleDWord(val) (val)
//-----------------------------------------------------------------------------
// Used for serialization
//-----------------------------------------------------------------------------
class CBitBuffer
{
public:
CBitBuffer(void);
void SetDebugName(const char* pName);
2022-05-10 13:24:22 +02:00
const char* GetDebugName() const;
bool IsOverflowed() const;
void SetOverflowFlag();
////////////////////////////////////
const char* m_pDebugName;
uint8_t m_bOverflow;
size_t m_nDataBits;
size_t m_nDataBytes;
};
class CBitRead : public CBitBuffer
{
public:
void GrabNextDWord(bool bOverFlowImmediately = false);
void FetchNext();
int ReadSBitLong(int numbits);
uint32 ReadUBitLong(int numbits);
int ReadByte();
int ReadChar();
bool ReadString(char* pStr, int bufLen, bool bLine = false, int* pOutNumChars = nullptr);
void StartReading(const void* pData, size_t nBytes, size_t iStartBit = 0, size_t nBits = -1);
bool Seek(size_t nPosition);
////////////////////////////////////
uint32_t m_nInBufWord;
uint32_t m_nBitsAvail;
const uint32* m_pDataIn;
const uint32* m_pBufferEnd;
const uint32* m_pData;
};
class bf_read : public CBitRead
{
public:
};
struct bf_write
{
public:
// How many bytes are filled in?
int GetNumBytesWritten() const;
int GetNumBitsWritten() const;
int GetMaxNumBits() const;
int GetNumBitsLeft() const;
int GetNumBytesLeft() const;
2022-06-08 14:35:44 +02:00
unsigned char* GetData() const;
const char* GetDebugName() const;
// Has the buffer overflowed?
bool CheckForOverflow(int nBits);
bool IsOverflowed() const;
void SetOverflowFlag();
private:
2022-05-14 18:22:46 +02:00
uint8_t* m_pData;
int m_nDataBytes;
int m_nDataBits;
int m_iCurBit;
bool m_bOverflow;
bool m_bAssertOnOverflow;
const char* m_pDebugName;
};
#endif // BITBUF_H