SEGS  0.4.2
Super Entity Game Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BitStream.h
Go to the documentation of this file.
1 /*
2  * SEGS - Super Entity Game Server
3  * http://www.segs.io/
4  * Copyright (c) 2006 - 2018 SEGS Team (see Authors.txt)
5  * This software is licensed! (See License.txt for details)
6  */
7 
8 /************************************************************************
9 Class: BitStream
10 Author: Darawk,nemerle
11 Description: The BitStream class allows it's user to manipulate data in
12  terms of individual bits, thus allowing said data to achieve
13  much greater levels of density. It abstracts the nuts and
14  bolts of this functionality away from it's user.
15 ************************************************************************/
16 
17 #pragma once
18 
19 #include "Buffer.h"
20 
21 #include <cstdint>
22 
23 #ifdef CUSTOM_CLIENT_CODE
24 #define PUTDEBUG(x) bs.StoreString(x);
25 #else
26 #define PUTDEBUG(x) ;
27 #endif
28 
29 class QString;
30 class BitStream : public GrowingBuffer
31 {
32 public:
33 
34 explicit BitStream(size_t size);
35  BitStream(uint8_t *from,size_t bitsize);
36  BitStream(const BitStream &bs);
37  BitStream &operator=(const BitStream &bs);
38  ~BitStream();
39 
40  void StoreBits(uint32_t nBits, uint32_t dataBits);
41  void uStoreBits(uint32_t nBits, uint32_t dataBits);
42  void StoreFloat(float val);
43  void StorePackedBits(uint32_t nBits, uint32_t dataBits);
45  {
46  if((src.GetReadPos()&7)==0) // source is aligned ?
48  else
49  {
50  size_t bits_to_store=src.GetReadableBits();
51  ByteAlign(false,true);
52  while(bits_to_store>32)
53  {
54  StoreBits(32,src.uGetBits(32));
55  bits_to_store-=32;
56  }
57  StoreBits(bits_to_store,src.uGetBits(bits_to_store));
58  }
59  }
60  void StoreBitArray(const uint8_t *array,size_t nBits);
61  void StoreString(const char *str);
62  void StoreString(const QString &str);
63  int32_t GetBits(uint32_t nBits);
64  int32_t uGetBits(uint32_t nBits);
65  int32_t GetPackedBits(uint32_t minbits);
66  void GetBitArray(uint8_t *array, uint32_t nBits);
67  void GetString(QString &str);
68  float GetFloat();
69  int64_t Get64Bits();
70  size_t GetWritableBits() const { return (GetAvailSize()<<3)-m_write_bit_off;}
71  size_t GetReadableBits() const { return (GetReadableDataSize()<<3)+(m_write_bit_off-m_read_bit_off);}
72  size_t GetAvailSize() const;
73  bool IsByteAligned() const { return m_byteAligned;}
74 
75  void SetReadPos(uint32_t pos) { m_read_off = pos >> 3; m_read_bit_off = uint8_t(pos & 0x7);}
76  size_t GetReadPos() const { return (m_read_off<<3) + m_read_bit_off;}
77  void SetWritePos(uint32_t pos) { m_write_off = pos >> 3; m_write_bit_off = uint8_t(pos & 0x7);}
78  size_t GetWritePos() const { return (m_write_off<<3) + m_write_bit_off;}
79 
80  void UseByteAlignedMode(bool toggle);
81  void ByteAlign(bool read_part=true,bool write_part=true);
82  void ResetReading() { m_read_bit_off=0; m_read_off=0; }
83  void ResetOffsets();
84 
85  void CompressAndStoreString(const char *str);
86  void GetAndDecompressString(QString &tgt);
87 
88 private:
89  bool m_byteAligned;
90  uint8_t m_read_bit_off;
91  uint8_t m_write_bit_off;
92 };
size_t m_read_off
Definition: Buffer.h:115
void GetBitArray(uint8_t *array, uint32_t nBits)
Definition: BitStream.cpp:316
void StoreBits(uint32_t nBits, uint32_t dataBits)
Definition: BitStream.cpp:114
void SetWritePos(uint32_t pos)
Definition: BitStream.h:77
void SetReadPos(uint32_t pos)
Definition: BitStream.h:75
int32_t uGetBits(uint32_t nBits)
Definition: BitStream.cpp:269
size_t GetReadableDataSize() const
Definition: Buffer.h:96
void GetAndDecompressString(QString &tgt)
Definition: BitStream.cpp:449
void CompressAndStoreString(const char *str)
Definition: BitStream.cpp:431
void ResetOffsets()
Definition: BitStream.cpp:403
void ResetReading()
Definition: BitStream.h:82
int32_t GetPackedBits(uint32_t minbits)
Definition: BitStream.cpp:285
void UseByteAlignedMode(bool toggle)
Definition: BitStream.cpp:409
~BitStream()
Definition: BitStream.cpp:96
Definition: BitStream.h:30
size_t GetAvailSize() const
Definition: BitStream.cpp:370
void appendBitStream(BitStream &src)
Definition: BitStream.h:44
void StorePackedBits(uint32_t nBits, uint32_t dataBits)
Definition: BitStream.cpp:161
int32_t GetBits(uint32_t nBits)
Definition: BitStream.cpp:257
Definition: Buffer.h:17
void StoreString(const char *str)
Definition: BitStream.cpp:204
size_t GetWritePos() const
Definition: BitStream.h:78
BitStream & operator=(const BitStream &bs)
Definition: BitStream.cpp:72
size_t GetReadPos() const
Definition: BitStream.h:76
bool IsByteAligned() const
Definition: BitStream.h:73
void GetString(QString &str)
Retrieves a null-terminated C-style string from the bit stream.
Definition: BitStream.cpp:326
size_t m_write_off
Definition: Buffer.h:114
int64_t Get64Bits()
Read upto 8 bytes from input stream and return them as 64bit integer.
Definition: BitStream.cpp:354
void StoreBitArray(const uint8_t *array, size_t nBits)
BitStream::StoreBitArray Stores an array of bits in the bit stream buffer. The main difference betwee...
Definition: BitStream.cpp:183
void ByteAlign(bool read_part=true, bool write_part=true)
Definition: BitStream.cpp:416
size_t GetReadableBits() const
Definition: BitStream.h:71
void uStoreBits(uint32_t nBits, uint32_t dataBits)
Definition: BitStream.cpp:138
size_t GetWritableBits() const
Definition: BitStream.h:70
uint8_t * read_ptr()
Definition: Buffer.h:101
BitStream(size_t size)
Definition: BitStream.cpp:39
void StoreFloat(float val)
Definition: BitStream.cpp:393
float GetFloat()
Definition: BitStream.cpp:380