Subversion Repositories spk

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 cycrow 1
#include "ByteReader.h"
2
 
3
//
4
// constructors
5
//
6
ByteReader::ByteReader(void)
7
{
8
	InitVars();
9
}
10
 
11
ByteReader::ByteReader( void* pBuff, DWORD dwSize )
12
{
13
	Assign( pBuff, dwSize );
14
}
15
 
16
//
17
// destructor
18
//
19
ByteReader::~ByteReader(void)
20
{
21
}
22
 
23
//
24
// Purpose:
25
//   Member variable initializer
26
//
27
void ByteReader::InitVars()
28
{
29
	pBuffer       = NULL;
30
	cbBuffer      = 0;
31
	dwBitsLeft    = dwBitsTotal = 0;
32
	byBits2Do     = 8;
33
	byCur         = 0;
34
	dwIndex       = 0;
35
 
36
	return;
37
}
38
 
39
//
40
// Purpose:
41
//   Class initialization handler
42
//
43
BOOL ByteReader::Assign( void *pBuff, DWORD dwSize )
44
{
45
	InitVars();
46
	if ( !pBuff || !dwSize)
47
		return FALSE; // ERR
48
 
49
	this->pBuffer  = pBuff;
50
	this->cbBuffer = dwSize;
51
 
52
	dwBitsLeft     = dwBitsTotal = dwSize * 8;
53
	byCur          = ((PBYTE)pBuff)[ dwIndex++ ];
54
 
55
	return TRUE; // OK
56
}
57
 
58
DWORD ByteReader::GetUnusedBitCount()
59
{
60
	return dwBitsLeft;
61
}
62
 
63
DWORD ByteReader::GetTotalBitCount()
64
{
65
	return dwBitsTotal;
66
}
67
 
68
//
69
// Purpose:
70
//   Returns the number of accessed bytes
71
//
72
DWORD ByteReader::GetReadByteCount()
73
{
74
	DWORD  dwcBitsRead  = (dwBitsTotal - dwBitsLeft);
75
	DWORD  c = (DWORD)(dwcBitsRead / 8);
76
 
77
	if ( dwcBitsRead % 8 ) // round up !
78
		++c;
79
 
80
	return c;
81
}
82
 
83
//
84
// Purpose:
85
//   The key member function for the buffer querying
86
//
87
BYTE ByteReader::ReadBit()
88
{
89
	BYTE bit = 0;
90
 
91
	if ( dwBitsLeft == 0 )
92
		return -1; // ERR
93
 
94
	bit = byCur & 1;
95
	byCur >>= 1;
96
	--dwBitsLeft;
97
	--byBits2Do;
98
 
99
	// did we already build a complete byte ? -> paste in buffer
100
	if ( !byBits2Do )
101
	{
102
		byCur = ((PBYTE)pBuffer)[ dwIndex++ ];
103
		byBits2Do = 8;
104
	}
105
 
106
	return bit; // OK
107
}
108
 
109
//
110
// Purpose:
111
//   Read a 1 byte variable from the buffer
112
//
113
BYTE ByteReader::ReadByte()
114
{
115
	BYTE byte = 0;
116
 
117
	for ( UINT i = 0; i < 8; i++ )
118
	{
119
		byte  |= (ReadBit() << 7);
120
		if ( i != 7) // don't shift to last byte !
121
            byte >>= 1;
122
	}
123
	return byte; // OK
124
}
125
 
126
//
127
// Purpose:
128
//   Read a 2 byte variable from the buffer
129
//
130
WORD ByteReader::ReadWord()
131
{
132
	WORD word = 0;
133
 
134
	for ( UINT i = 0; i < 16; i++ )
135
	{
136
		word  |= (ReadBit() << 15);
137
		if ( i != 15) // don't shift to last byte !
138
            word >>= 1;
139
	}
140
	return word; // OK
141
}
142
 
143
//
144
// Purpose:
145
//   Read a 4 byte variable from the buffer
146
//
147
DWORD ByteReader::ReadDword()
148
{
149
	DWORD dword = 0;
150
 
151
	for ( UINT i = 0; i < 32; i++ )
152
	{
153
		dword  |= (ReadBit() << 31);
154
		if ( i != 31) // don't shift to last byte !
155
            dword >>= 1;
156
	}
157
	return dword; // OK
158
}
159
 
160
//
161
// Purpose:
162
//   Read a user defined number of bits from the buffer
163
//
164
DWORD ByteReader::ReadBits( DWORD dwBitCount )
165
{
166
	DWORD dword = 0;
167
 
168
	if ( dwBitCount > 32 )
169
		return -1; // ERR
170
 
171
	for ( UINT i = 0; i < 32; i++ )
172
	{
173
		if ( i < dwBitCount )
174
			dword |= (ReadBit() << 31);
175
		if ( i != 31) // don't shift to last byte !
176
			dword >>= 1;
177
	}
178
 
179
	return dword; // OK
180
}