forked from ZoneMinder/zoneminder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzm_buffer.h
208 lines (197 loc) · 5.61 KB
/
zm_buffer.h
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
/*
* ZoneMinder Flexible Memory Interface, $Date$, $Revision$
* Copyright (C) 2001-2008 Philip Coombes
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more demTails.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef ZM_BUFFER_H
#define ZM_BUFFER_H
#include "zm.h"
#include <string.h>
class Buffer
{
protected:
unsigned char *mStorage;
unsigned int mAllocation;
unsigned int mSize;
unsigned char *mHead;
unsigned char *mTail;
public:
Buffer() : mStorage( 0 ), mAllocation( 0 ), mSize( 0 ), mHead( 0 ), mTail( 0 )
{
}
Buffer( unsigned int pSize ) : mAllocation( pSize ), mSize( 0 )
{
mHead = mStorage = new unsigned char[mAllocation];
mTail = mHead;
}
Buffer( const unsigned char *pStorage, unsigned int pSize ) : mAllocation( pSize ), mSize( pSize )
{
mHead = mStorage = new unsigned char[mSize];
memcpy( mStorage, pStorage, mSize );
mTail = mHead + mSize;
}
Buffer( const Buffer &buffer ) : mAllocation( buffer.mSize ), mSize( buffer.mSize )
{
mHead = mStorage = new unsigned char[mSize];
memcpy( mStorage, buffer.mHead, mSize );
mTail = mHead + mSize;
}
~Buffer()
{
delete[] mStorage;
}
unsigned char *head() const { return( mHead ); }
unsigned char *tail() const { return( mTail ); }
unsigned int size() const { return( mSize ); }
bool empty() const { return( mSize == 0 ); }
unsigned int size( unsigned int pSize )
{
if ( mSize < pSize )
{
expand( pSize-mSize );
}
return( mSize );
}
//unsigned int Allocation() const { return( mAllocation ); }
void clear()
{
mSize = 0;
mHead = mTail = mStorage;
}
unsigned int assign( const unsigned char *pStorage, unsigned int pSize );
unsigned int assign( const Buffer &buffer )
{
return( assign( buffer.mHead, buffer.mSize ) );
}
// Trim from the front of the buffer
unsigned int consume( unsigned int count )
{
if ( count > mSize )
{
Warning( "Attempt to consume %d bytes of buffer, size is only %d bytes", count, mSize );
count = mSize;
}
mHead += count;
mSize -= count;
tidy( 0 );
return( count );
}
// Trim from the end of the buffer
unsigned int shrink( unsigned int count )
{
if ( count > mSize )
{
Warning( "Attempt to shrink buffer by %d bytes, size is only %d bytes", count, mSize );
count = mSize;
}
mSize -= count;
if ( mTail > (mHead + mSize) )
mTail = mHead + mSize;
tidy( 0 );
return( count );
}
// Add to the end of the buffer
unsigned int expand( unsigned int count );
// Return pointer to the first pSize bytes and advance the head
unsigned char *extract( unsigned int pSize )
{
if ( pSize > mSize )
{
Warning( "Attempt to extract %d bytes of buffer, size is only %d bytes", pSize, mSize );
pSize = mSize;
}
unsigned char *oldHead = mHead;
mHead += pSize;
mSize -= pSize;
tidy( 0 );
return( oldHead );
}
// Add bytes to the end of the buffer
unsigned int append( const unsigned char *pStorage, unsigned int pSize )
{
expand( pSize );
memcpy( mTail, pStorage, pSize );
mTail += pSize;
mSize += pSize;
return( mSize );
}
unsigned int append( const char *pStorage, unsigned int pSize )
{
return( append( (const unsigned char *)pStorage, pSize ) );
}
unsigned int append( const Buffer &buffer )
{
return( append( buffer.mHead, buffer.mSize ) );
}
void tidy( bool level=0 )
{
if ( mHead != mStorage )
{
if ( mSize == 0 )
mHead = mTail = mStorage;
else if ( level )
{
if ( (mHead-mStorage) > mSize )
{
memcpy( mStorage, mHead, mSize );
mHead = mStorage;
mTail = mHead + mSize;
}
}
}
}
Buffer &operator=( const Buffer &buffer )
{
assign( buffer );
return( *this );
}
Buffer &operator+=( const Buffer &buffer )
{
append( buffer );
return( *this );
}
Buffer &operator+=( unsigned int count )
{
expand( count );
return( *this );
}
Buffer &operator-=( unsigned int count )
{
consume( count );
return( *this );
}
operator unsigned char *() const
{
return( mHead );
}
operator char *() const
{
return( (char *)mHead );
}
unsigned char *operator+(int offset) const
{
return( (unsigned char *)(mHead+offset) );
}
unsigned char operator[](int index) const
{
return( *(mHead+index) );
}
operator int () const
{
return( (int)mSize );
}
};
#endif // ZM_BUFFER_H