-
Notifications
You must be signed in to change notification settings - Fork 11
/
CBuf.cpp
217 lines (172 loc) · 4.22 KB
/
CBuf.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
/*
* CBuf.cpp
* stackimport
*
* Created by Mr. Z. on 03/31/10.
* Copyright 2010 Mr Z. All rights reserved.
*
*/
#include <cstring>
#include "CBuf.h"
#include "byteutils.h"
#include "assert.h"
CBuf::CBuf( size_t inSize )
: mShared(NULL)
{
alloc_buffer( inSize );
}
CBuf::CBuf( const CBuf& inTemplate, size_t startOffs, size_t amount )
: mShared(NULL)
{
if( amount == SIZE_MAX )
amount = inTemplate.size() -startOffs;
if( startOffs == 0 && amount == inTemplate.size() )
{
mShared = inTemplate.mShared;
mShared->mRefCount++;
}
else
{
alloc_buffer( amount );
::memcpy( mShared->mBuffer, inTemplate.buf(startOffs, amount), amount );
}
}
CBuf::~CBuf()
{
release_buffer();
}
void CBuf::alloc_buffer( size_t amount )
{
mShared = (shared_buffer*) calloc( 1, sizeof(shared_buffer) );
if( amount > 0 )
mShared->mBuffer = new char[amount];
mShared->mSize = amount;
mShared->mRefCount = 1;
}
void CBuf::release_buffer()
{
if( mShared->mRefCount > 1 )
mShared->mRefCount--;
else
{
if( mShared->mBuffer )
delete [] mShared->mBuffer;
mShared->mBuffer = NULL;
mShared->mSize = 0;
free( mShared );
}
mShared = NULL;
}
void CBuf::make_buffer_exclusive()
{
if( mShared->mRefCount == 1 )
return; // Already are exclusive owner.
shared_buffer* oldBuffer = mShared;
alloc_buffer( oldBuffer->mSize );
::memmove( mShared->mBuffer, oldBuffer->mBuffer, oldBuffer->mSize );
oldBuffer->mRefCount --;
}
void CBuf::resize( size_t inSize )
{
if( mShared->mRefCount == 1 )
{
if( mShared->mBuffer )
delete [] mShared->mBuffer;
mShared->mBuffer = NULL;
if( inSize > 0 )
mShared->mBuffer = new char[inSize];
mShared->mSize = inSize;
}
else
{
shared_buffer* oldBuffer = mShared;
alloc_buffer( inSize );
oldBuffer->mRefCount--;
}
}
void CBuf::memcpy( size_t toOffs, const char* fromPtr, size_t fromOffs, size_t amount )
{
make_buffer_exclusive();
char* thePtr = mShared->mBuffer;
if( (toOffs +amount) > mShared->mSize )
return;
::memcpy( thePtr + toOffs, fromPtr +fromOffs, amount );
}
void CBuf::memcpy( size_t toOffs, const CBuf& fromPtr, size_t fromOffs, size_t amount )
{
if( amount == SIZE_MAX )
amount = fromPtr.size() -fromOffs;
memcpy( toOffs, fromPtr.buf(fromOffs,amount), 0, amount );
}
const char CBuf::operator [] ( int idx ) const
{
if( idx >= mShared->mSize )
return 0;
return mShared->mBuffer[idx];
}
char& CBuf::operator [] ( int idx )
{
static char dummy[2048] = {0};
if( idx >= mShared->mSize )
return dummy[0];
make_buffer_exclusive();
return mShared->mBuffer[idx];
}
char* CBuf::buf( size_t offs, size_t amount )
{
if( amount == SIZE_MAX )
amount = mShared->mSize -offs;
assert( mShared->mBuffer != NULL );
static char dummy[2048] = {0};
if( (amount +offs) > mShared->mSize )
return dummy;
make_buffer_exclusive();
return mShared->mBuffer + offs;
}
const char* CBuf::buf( size_t offs, size_t amount ) const
{
static char dummy[2048] = {0};
if( amount == SIZE_MAX )
amount = mShared->mSize -offs;
assert( mShared->mBuffer != NULL );
if( (amount +offs) > mShared->mSize )
return dummy;
return mShared->mBuffer + offs;
}
void CBuf::xornstr( size_t dstOffs, char * src, size_t srcOffs, size_t amount )
{
assert( mShared->mBuffer != NULL);
assert( (amount +dstOffs) <= mShared->mSize );
make_buffer_exclusive();
::xornstr( mShared->mBuffer +dstOffs, src +srcOffs, amount );
}
void CBuf::xornstr( size_t dstOffs, const CBuf& src, size_t srcOffs, size_t amount )
{
assert( mShared->mBuffer != NULL );
assert( (amount +dstOffs) <= mShared->mSize );
make_buffer_exclusive();
::xornstr( mShared->mBuffer +dstOffs, src.buf(srcOffs,amount), amount );
}
void CBuf::shiftnstr( size_t dstOffs, int amount, int shiftAmount )
{
assert( mShared->mBuffer != NULL );
assert( (dstOffs +amount) <= mShared->mSize );
make_buffer_exclusive();
::shiftnstr( mShared->mBuffer +dstOffs, amount, shiftAmount );
}
void CBuf::tofile( const std::string& fpath )
{
FILE* theFile = fopen( fpath.c_str(), "w" );
fwrite( mShared->mBuffer, 1, mShared->mSize, theFile );
fclose( theFile );
}
CBuf& CBuf::operator = ( const CBuf& inTemplate )
{
if( mShared != inTemplate.mShared )
{
release_buffer();
mShared = inTemplate.mShared;
mShared->mRefCount++;
}
return *this;
}