-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcByteStream.cpp
175 lines (151 loc) · 3.24 KB
/
cByteStream.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "StdAfx.h"
#include ".\cbytestream.h"
cByteStream::cByteStream(void)
{
m_pbDataStart = NULL;
m_pbDataPtr = NULL;
m_dwLength = NULL;
}
cByteStream::~cByteStream(void)
{
}
cByteStream::cByteStream(BYTE *Data, DWORD Length)
{
SetStream(Data, Length);
}
void cByteStream::SetStream(BYTE *Data, DWORD Length)
{
m_pbDataPtr = m_pbDataStart = Data;
m_dwLength = Length;
}
DWORD cByteStream::GetOffset()
{
return (DWORD) (m_pbDataPtr - m_pbDataStart);
}
bool cByteStream::AtEOF()
{
return (GetOffset() >= m_dwLength);
}
void cByteStream::ReadBegin( void )
{
m_pbDataPtr = m_pbDataStart;
}
void cByteStream::ReadAlign( void )
{
int iOffset = (int) (m_pbDataPtr - m_pbDataStart);
if ( (iOffset & 3) != 0)
m_pbDataPtr += (4 - (iOffset & 3));
// if ( (iOffset % 4) != 0)
// m_pbDataPtr += (4 - (iOffset % 4));
}
void cByteStream::ReadSkip(int iAmount)
{
m_pbDataPtr += iAmount;
}
//Ew, =)
BYTE cByteStream::ReadByte() {
BYTE Result = *((BYTE *)m_pbDataPtr);
m_pbDataPtr += sizeof(BYTE);
return Result;
}
WORD cByteStream::ReadWORD() {
WORD Result = *((WORD *)m_pbDataPtr);
m_pbDataPtr += sizeof(WORD);
return Result;
}
DWORD cByteStream::ReadDWORD() {
DWORD Result = *((DWORD *)m_pbDataPtr);
m_pbDataPtr += sizeof(DWORD);
return Result;
}
QWORD cByteStream::ReadQWORD() {
QWORD Result = *((QWORD *)m_pbDataPtr);
m_pbDataPtr += sizeof(QWORD);
return Result;
}
WORD cByteStream::ReadPackedWORD() {
WORD Result = *((BYTE *)m_pbDataPtr);
m_pbDataPtr += sizeof(BYTE);
if (Result & 0x80)
{
Result <<= 8;
Result &= 0x7FFF;
Result |= *((BYTE *)m_pbDataPtr);
m_pbDataPtr += sizeof(BYTE);
}
return Result;
}
DWORD cByteStream::ReadPackedDWORD() {
DWORD Result = *((WORD *)m_pbDataPtr);
m_pbDataPtr += sizeof(WORD);
if (Result & 0x8000)
{
Result <<= 16;
Result &= 0x7FFFFFFF;
Result |= *((WORD *)m_pbDataPtr);
m_pbDataPtr += sizeof(WORD);
}
return Result;
}
float cByteStream::ReadFloat() {
float Result = *((float *)m_pbDataPtr);
m_pbDataPtr += sizeof(float);
return Result;
}
double cByteStream::ReadDouble() {
double Result = *((double *)m_pbDataPtr);
m_pbDataPtr += sizeof(double);
return Result;
}
char* cByteStream::ReadString() {
WORD wStrLen = ReadWORD();
char* Result = new char[wStrLen+1];
Result[wStrLen] = 0;
memcpy(Result, m_pbDataPtr, wStrLen);
m_pbDataPtr += wStrLen;
ReadAlign();
return Result;
}
wchar_t* cByteStream::ReadWString() {
WORD wStrLen = ReadWORD();
wchar_t* Result = new wchar_t[wStrLen+1];
Result[wStrLen] = 0;
memcpy(Result, m_pbDataPtr, 2*wStrLen);
m_pbDataPtr += wStrLen*2;
ReadAlign();
return Result;
}
char * cByteStream::ReadEncodedString()
{
WORD wLength = ReadWORD();
WORD wCount = 0;
char *szTemp = new char[ wLength + 4 ];
memset( szTemp, 0, wLength + 4 );
long lTell = (long) (m_pbDataPtr - m_pbDataStart);
if( lTell % 4 )
{
memcpy(szTemp, ReadGroup(2), 2);
wCount += 2;
}
for(; wCount < wLength; wCount += 4)
memcpy(szTemp + wCount, ReadGroup(4), 4);
//if( bNibbled )
if (true)
{
char *temp;
for( temp = szTemp; *temp; ++temp )
{
WORD c = *(BYTE *) temp;
WORD c2 = c >> 4;
c <<= 4;
*temp = (c | c2);
}
}
return szTemp;
}
BYTE * cByteStream::ReadGroup(int iAmount)
{
BYTE * Result = m_pbDataPtr;
m_pbDataPtr += iAmount;
return Result;
}