forked from derandark/PhatAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryWriter.cpp
93 lines (74 loc) · 1.49 KB
/
BinaryWriter.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
#include "StdAfx.h"
#include "BinaryWriter.h"
// Expandable buffer with AC style data IO
BinaryWriter::BinaryWriter()
{
m_pbData = new BYTE[0x20];
m_dwDataSize = 0x20;
m_pbDataPos = m_pbData;
m_dwSize = 0;
}
BinaryWriter::~BinaryWriter()
{
if (m_pbData)
{
delete[] m_pbData;
m_pbData = NULL;
}
}
void BinaryWriter::ExpandBuffer(size_t len)
{
if (m_dwDataSize < len)
{
DWORD dwExpandedSize;
if (len <= (m_dwDataSize + 0x80))
dwExpandedSize = m_dwDataSize + 0x80;
else
dwExpandedSize = (DWORD)len;
BYTE *pbExpandedBuffer = new BYTE[dwExpandedSize];
memcpy(pbExpandedBuffer, m_pbData, m_dwSize);
delete[] m_pbData;
m_pbData = pbExpandedBuffer;
m_dwDataSize = dwExpandedSize;
m_pbDataPos = m_pbData + m_dwSize;
}
}
void BinaryWriter::AppendString(const char *szString)
{
if (szString)
{
WORD len = (WORD)strlen(szString);
AppendData(&len, sizeof(WORD));
AppendData(szString, len);
}
else
AppendData((WORD)0);
Align();
}
void BinaryWriter::AppendData(const void *pData, size_t len)
{
ExpandBuffer(m_dwSize + len);
memcpy(m_pbDataPos, pData, len);
m_pbDataPos += len;
m_dwSize += (DWORD)len;
}
void BinaryWriter::Align(void)
{
DWORD offset = DWORD(m_pbDataPos - m_pbData);
if ((offset % 4) != 0)
{
DWORD len = 4 - (offset % 4);
ExpandBuffer(m_dwSize + len);
memset(m_pbDataPos, 0, len);
m_pbDataPos += len;
m_dwSize += len;
}
}
BYTE* BinaryWriter::GetData(void)
{
return m_pbData;
}
DWORD BinaryWriter::GetSize(void)
{
return m_dwSize;
}