forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplebitstring.h
159 lines (123 loc) · 3.55 KB
/
simplebitstring.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
//*****************************************************************************
//
// Contents:
//
// CSimpleBitString
//
// Authors:
//
// Target restrictions:
//
// Tool restrictions:
//
// Things to do:
//
//
//
//*****************************************************************************
#ifndef INCLUDED_COMMON_SIMPLEBITSTRING_H
#define INCLUDED_COMMON_SIMPLEBITSTRING_H
#if defined(_MSC_VER) && (_MSC_VER > 1000)
#pragma once
#endif
//*****************************************************************************
//
// Include files required by this header.
//
// Note: Do NOT place any 'using' directives or declarations in header files -
// put them at the top of the source files that require them.
// Use fully-qualified names in header files.
//
//*****************************************************************************
// All precompiled headers (Stable.h) include this first to give use a common
// place for including order-dependent library headers (things that need to go first).
class CSimpleBitString
{
public:
explicit CSimpleBitString( uint32 ReserveNumBits = 0 )
:
m_uNumBits( 0 ),
m_vecU8()
{
m_vecU8.EnsureCapacity( (ReserveNumBits / 8) + 1 );
m_vecU8[ m_vecU8.AddToTail() ] = 0x00; // always need 1 byte
}
void clear()
{
m_uNumBits = 0;
m_vecU8.RemoveAll();
m_vecU8[ m_vecU8.AddToTail() ] = 0x00; // always need 1 byte
}
void AppendBits( uint64 data, uint32 NumSignificantLowBitsOfData );
void AppendBits( const uint8 * pData, uint32 NumBitsOfData );
void ReversiblyObfusticateBitsFromStart( uint32 NumBits, const uint8 * pObfusticationData, size_t uSizeOfObfusticationData );
uint8 GetByteChecksumFromStart( uint32 NumBits ) const;
uint GetCurrNumBits() const
{
return m_uNumBits;
}
const uint8 * data() const
{
return & m_vecU8[0];
}
size_t size() const
{
return m_vecU8.Count();
}
private:
uint32 m_uNumBits;
CUtlVector<uint8> m_vecU8;
public:
// Iterator class for retrieving bits
class iterator
{
public:
explicit iterator( const CSimpleBitString & bs )
:
m_rSimpleBitString( bs ),
m_uNextBitIdx( 0 )
{
}
void ResetToStart()
{
m_uNextBitIdx = 0;
}
uint32 GetNumConsumedBits() const
{
return m_uNextBitIdx;
}
uint32 GetNumRemainingBits() const
{
return m_rSimpleBitString.m_uNumBits - m_uNextBitIdx;
}
uint32 GetNextBits( uint32 NumBitsToGet );
uint64 GetNextBits64( uint32 NumBitsToGet );
void SkipNextBits( uint32 NumBitsToSkip )
{
if ( m_uNextBitIdx + NumBitsToSkip > m_rSimpleBitString.m_uNumBits )
{
AssertMsg( false, "Not enough bits in CSimpleBitString" );
NumBitsToSkip = 0;
}
m_uNextBitIdx += NumBitsToSkip;
}
bool operator ==( const iterator & other ) const
{
return (& m_rSimpleBitString == & other.m_rSimpleBitString)
&& m_uNextBitIdx == other.m_uNextBitIdx;
}
void DoAssertClassInvariant() const;
private:
const CSimpleBitString & m_rSimpleBitString; //lint !e1725 reference
uint32 m_uNextBitIdx;
};
friend class iterator;
};
#endif