aboutsummaryrefslogtreecommitdiffstats
path: root/lib/bufferstore.h
blob: 0e951b8b3dc8d4d2bacfef4122af822e97619f2b (plain)
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
/*-*-c++-*-
 * $Id$
 *
 * This file is part of plptools.
 *
 *  Copyright (C) 1999  Philip Proudman <philip.proudman@btinternet.com>
 *  Copyright (C) 1999-2001 Fritz Elfert <felfert@to.com>
 *
 *  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 details.
 *
 *  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 _BUFFERSTORE_H_
#define _BUFFERSTORE_H_

#include <sys/types.h>
#include <stream.h>

/**
 * A generic container for an array of bytes.
 *
 * bufferStore provides an array of bytes which
 * can be accessed using various types.
 */
class bufferStore {
public:
    /**
    * Constructs a new bufferStore.
    */
    bufferStore();

    /**
    * Constructs a new bufferStore and
    * initializes its content.
    *
    * @param buf Pointer to data for initialization.
    * @param len Length of data for initialization.
    */
    bufferStore(const unsigned char *, long);

    /**
    * Destroys a bufferStore instance.
    */
    ~bufferStore();

    /**
    * Constructs a new bufferStore and
    * initializes its content.
    *
    * @param b A bufferStore, whose content is
    * used for initialization.
    */
    bufferStore(const bufferStore &);

    /**
    * Copies a bufferStore.
    */
    bufferStore &operator =(const bufferStore &);

    /**
    * Retrieves the length of a bufferStore.
    *
    * @returns The current length of the contents
    * 	in bytes.
    */
    unsigned long getLen() const;

    /**
    * Retrieves the byte at index <em>pos</em>.
    *
    * @param pos The index of the byte to retrieve.
    *
    * @returns The value of the byte at index <em>pos</em>
    */
    unsigned char getByte(long pos = 0) const;

    /**
    * Retrieves the word at index <em>pos</em>.
    *
    * @param pos The index of the word to retrieve.
    *
    * @returns The value of the word at index <em>pos</em>
    */
    u_int16_t getWord(long pos = 0) const;

    /**
    * Retrieves the dword at index <em>pos</em>.
    *
    * @param pos The index of the dword to retrieve.
    *
    * @returns The value of the dword at index <em>pos</em>
    */
    u_int32_t getDWord(long pos = 0) const;

    /**
    * Retrieves the characters at index <em>pos</em>.
    *
    * @param pos The index of the characters to retrieve.
    *
    * @returns A pointer to characters at index <em>pos</em>
    */
    const char * getString(long pos = 0) const;

    /**
    * Removes bytes from the start of the buffer.
    *
    * @param len Number of bytes to remove.
    */
    void discardFirstBytes(int len = 0);

    /**
    * Prints a dump of the content.
    *
    * Mainly used for debugging purposes.
    *
    * @param s The stream to write to.
    * @param b The bufferStore do be dumped.
    *
    * @returns The stream.
    */
    friend class std::ostream &operator<<(std::ostream &, const bufferStore &);

    /**
    * Tests if the bufferStore is empty.
    *
    * @returns true, if the bufferStore is empty.
    * 	false, if it contains data.
    */
    bool empty() const;

    /**
    * Initializes the bufferStore.
    *
    * All data is removed, the length is
    * reset to 0.
    */
    void init();

    /**
    * Initializes the bufferStore with
    * a given data.
    *
    * @param buf Pointer to data to initialize from.
    * @param len Length of data.
    */
    void init(const unsigned char * buf, long len);

    /**
    * Appends a byte to the content of this instance.
    *
    * @param c The byte to append.
    */
    void addByte(unsigned char c);

    /**
    * Appends a word to the content of this instance.
    *
    * @param w The word to append.
    */
    void addWord(int);

    /**
    * Appends a dword to the content of this instance.
    *
    * @param dw The dword to append.
    */
    void addDWord(long dw);

    /**
    * Appends a string to the content of this instance.
    *
    * The trailing zero byte is <em>not</em> copied
    * to the content.
    *
    * @param s The string to append.
    */
    void addString(const char *s);

    /**
    * Appends a string to the content of this instance.
    *
    * The trailing zero byte <em>is</em> copied
    * to the content.
    *
    * @param s The string to append.
    */
    void addStringT(const char *s);

    /**
    * Appends data to the content of this instance.
    *
    * @param buf The data to append.
    * @param len Length of data.
    */
    void addBytes(const unsigned char *buf, int len);

    /**
    * Appends data to the content of this instance.
    *
    * @param b The bufferStore whose content to append.
    * @param maxLen Length of content to append. If
    *               @p maxLen is less than 0 or greater than
    *               the current length of @p b , then the
    *               whole content of @p b is appended.
    */
    void addBuff(const bufferStore &b, long maxLen = -1);

    /**
    * Truncates the buffer.
    * If the buffer is smaller, does nothing.
    *
    * @param newLen The new length of the buffer.
    */
    void truncate(long newLen);

    /**
    * Prepends a byte to the content of this instance.
    *
    * @param c The byte to append.
    */
    void prependByte(unsigned char c);

    /**
    * Prepends a word to the content of this instance.
    *
    * @param w The word to append.
    */
    void prependWord(int);

private:
    void checkAllocd(long newLen);

    long len;
    long lenAllocd;
    long start;
    unsigned char * buff;

    enum c { MIN_LEN = 300 };
};

inline bool bufferStore::empty() const {
    return (len - start) == 0;
}

#endif

/*
 * Local variables:
 * c-basic-offset: 4
 * End:
 */