-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLBPBuffer.cpp
289 lines (226 loc) · 4.66 KB
/
LBPBuffer.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "stdafx.h"
#include "LBPBuffer.h"
#include "LBPString.h"
#include "LBPBase64.h"
#include "LBP_CRC32.h"
static const WCHAR* wszBase64Prefix = L"base64:";
const WCHAR* CLBPBuffer::Base64Prefix()
{
return wszBase64Prefix;
}
CLBPInternalBuffer::CLBPInternalBuffer()
{
m_pBytes = NULL;
m_NumBytes = 0;
m_iRefCount = 1;
}
CLBPInternalBuffer::CLBPInternalBuffer(const void* pBuffer, size_t size)
{
m_pBytes = NULL;
m_NumBytes = 0;
m_iRefCount = 1;
Set(pBuffer, size);
}
CLBPInternalBuffer::CLBPInternalBuffer(size_t size)
{
m_pBytes = NULL;
m_NumBytes = 0;
m_iRefCount = 1;
Set(size);
}
CLBPInternalBuffer::CLBPInternalBuffer(const CLBPString& sBase64)
{
m_pBytes = NULL;
m_NumBytes = 0;
m_iRefCount = 1;
*this = sBase64;
}
CLBPInternalBuffer::~CLBPInternalBuffer()
{
delete[] m_pBytes;
}
int CLBPInternalBuffer::Release()
{
const int iRefCount = ::InterlockedDecrement(&m_iRefCount);
if (0 == iRefCount)
delete this;
return iRefCount;
}
void CLBPInternalBuffer::Set(const void* pBuffer, size_t size)
{
Set(size);
if (NULL != m_pBytes)
{
::CopyMemory(m_pBytes, pBuffer, size);
}
}
void CLBPInternalBuffer::Set(size_t size)
{
m_bBase64Dirty = true;
m_bCRCDirty = true;
if (m_NumBytes != size)
{
delete [] m_pBytes;
m_NumBytes = size;
m_pBytes = (size > 0) ? new BYTE[size] : NULL;
}
}
int CLBPInternalBuffer::Compare(const CLBPInternalBuffer& ib) const
{
if (m_NumBytes != ib.m_NumBytes)
return (int)(m_NumBytes - ib.m_NumBytes);
if (NULL == m_pBytes)
{
return (NULL == ib.m_pBytes) ? 0 : -1;
}
if (NULL == ib.m_pBytes)
return +1;
return memcmp(m_pBytes, ib.m_pBytes, m_NumBytes);
}
const CLBPInternalBuffer& CLBPInternalBuffer::operator = (const CLBPInternalBuffer& ib)
{
Set(ib.m_pBytes, ib.m_NumBytes);
return *this;
}
const CLBPInternalBuffer& CLBPInternalBuffer::operator = (const CLBPString& s)
{
if (s.StartsWithNoCase(CLBPBuffer::Base64Prefix()) && s != CLBPBuffer::Base64Prefix())
{
const int iPrefixLen = CLBPString(CLBPBuffer::Base64Prefix()).GetLength();
CLBPBase64 b;
const int iSize = s.GetLength() - iPrefixLen; // Base64 is about 33% bigger than the resulting data.
Set(iSize);
m_NumBytes = b.Decode(m_pBytes, s.Mid(iPrefixLen));
}
else
{
Clear();
}
return *this;
}
const CLBPString& CLBPInternalBuffer::Base64() const
{
if (m_bBase64Dirty)
{
Base64(m_sBase64);
}
return m_sBase64;
}
void CLBPInternalBuffer::Base64(CLBPString& s) const
{
if (m_bBase64Dirty)
{
m_bBase64Dirty = false;
s = CLBPBuffer::Base64Prefix();
if (NULL != m_pBytes)
{
CLBPBase64 b;
b.Encode(m_pBytes, m_NumBytes, s, true);
}
m_sBase64 = s;
}
else
{
s = m_sBase64;
}
}
DWORD CLBPInternalBuffer::CRC(void) const
{
if (m_bCRCDirty)
{
m_dwCRC = CLBP_CRC32::Calculate(m_pBytes, m_NumBytes);
m_bCRCDirty = false;
}
return m_dwCRC;
}
CLBPBuffer::CLBPBuffer()
{
m_pBuffer = new CLBPInternalBuffer();
}
CLBPBuffer::CLBPBuffer(const void* pBuffer, size_t size)
{
m_pBuffer = new CLBPInternalBuffer(pBuffer, size);
}
CLBPBuffer::CLBPBuffer(size_t size)
{
m_pBuffer = new CLBPInternalBuffer(size);
}
CLBPBuffer::CLBPBuffer(const CLBPBuffer& src)
{
src.m_pBuffer->AddRef();
m_pBuffer = src.m_pBuffer;
}
CLBPBuffer::CLBPBuffer(const CLBPString& sBase64)
{
m_pBuffer = new CLBPInternalBuffer(sBase64);
}
const CLBPBuffer& CLBPBuffer::operator=(const CLBPBuffer& src)
{
if (this == &src)
return *this;
src.m_pBuffer->AddRef();
ASSERT(m_pBuffer);
m_pBuffer->Release();
m_pBuffer = src.m_pBuffer;
return *this;
}
const CLBPBuffer& CLBPBuffer::operator = (const CLBPString& sBase64)
{
ASSERT(m_pBuffer);
m_pBuffer->Release();
m_pBuffer = new CLBPInternalBuffer(sBase64);
return *this;
}
CLBPBuffer::~CLBPBuffer()
{
ASSERT(m_pBuffer);
m_pBuffer->Release();
}
int CLBPBuffer::Compare(const CLBPBuffer& b) const
{
ASSERT(m_pBuffer);
ASSERT(b.m_pBuffer);
return m_pBuffer->Compare(*b.m_pBuffer);
}
void CLBPBuffer::Set(const void* pBuffer, size_t size)
{
ASSERT(m_pBuffer);
if (m_pBuffer->m_iRefCount > 1)
{
//We are not the only owners - need to clone up
*this = CLBPBuffer(pBuffer, size);
}
else
{
m_pBuffer->Set(pBuffer, size);
}
}
void CLBPBuffer::Set(size_t size)
{
Set(NULL, size);
}
BYTE* CLBPBuffer::GetBuffer(size_t size)
{
*this = CLBPBuffer(size);
return m_pBuffer->m_pBytes;
}
bool CLBPBuffer::operator == (const CLBPBuffer& b) const
{
return Compare(b) == 0;
}
bool CLBPBuffer::operator > (const CLBPBuffer& b) const
{
return Compare(b) > 0;
}
bool CLBPBuffer::operator < (const CLBPBuffer& b) const
{
return Compare(b) < 0;
}
const CLBPString& CLBPBuffer::Base64() const
{
return m_pBuffer->Base64();
}
void CLBPBuffer::Base64(CLBPString& s) const
{
m_pBuffer->Base64(s);
}