summaryrefslogtreecommitdiffstats
path: root/base/tps/src/include/main/Buffer.h
blob: e3f08925e430923168b8393cde2547199b95fbb8 (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
// --- BEGIN COPYRIGHT BLOCK ---
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// version 2.1 of the License.
// 
// This library 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
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA  02110-1301  USA 
// 
// Copyright (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

#ifndef BUFFER_H
#define BUFFER_H

#ifdef HAVE_CONFIG_H
#ifndef AUTOTOOLS_CONFIG_H
#define AUTOTOOLS_CONFIG_H

/* Eliminate warnings when using Autotools */
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION

#include <config.h>
#endif /* AUTOTOOLS_CONFIG_H */
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include "main/Base.h"

#ifdef XP_WIN32
#define TPS_PUBLIC __declspec(dllexport)
#else /* !XP_WIN32 */
#define TPS_PUBLIC
#endif /* !XP_WIN32 */

/**
 * This class represents a byte array.
 */
class Buffer {

  private:
    BYTE *buf;
    unsigned int len;
    unsigned int res;

  public:
    /**
     * Creates an empty Buffer.
     */
    TPS_PUBLIC Buffer() : buf(0), len(0), res(0) { }

    /**
     * Creates a Buffer of length 'len', with each byte initialized to 'b'.
     */
    TPS_PUBLIC Buffer(unsigned int len, BYTE b);

    /**
     * Creates a Buffer of length 'len', initialized to zeroes.
     */
    TPS_PUBLIC explicit Buffer(unsigned int len);

    /**
     * Creates a Buffer of length 'len', initialized from 'buf'. 'buf' must
     * contain at least 'len' bytes.
     */
    TPS_PUBLIC Buffer(const BYTE* buf, unsigned int len);

    /**
     * Copy constructor.
     */
    TPS_PUBLIC Buffer(const Buffer& cpy);

    /**
     * Destructor.
     */
    TPS_PUBLIC ~Buffer();

    /**
     * Assignment operator.
     */
    TPS_PUBLIC Buffer& operator=(const Buffer& cpy);

    /**
     * Returns true if the two buffers are the same length and contain
     * the same byte at each offset.
     */
    TPS_PUBLIC bool operator==(const Buffer& cmp) const;

    /**
     * Returns ! operator==(cmp).
     */
    TPS_PUBLIC bool operator!=(const Buffer& cmp) const { return ! (*this == cmp); }

    /**
     * Concatenation operator.
     */
    TPS_PUBLIC Buffer operator+(const Buffer&addend) const;

    /**
     * Append operators.
     */
    TPS_PUBLIC Buffer& operator+=(const Buffer&addend);
    TPS_PUBLIC Buffer& operator+=(BYTE b);

    /**
     * Returns a pointer into the Buffer. This also enables the subscript
     * operator, so you can say, for example, 'buf[4] = b' or 'b = buf[4]'.
     */
    TPS_PUBLIC operator BYTE*() { return buf; }
    TPS_PUBLIC operator const BYTE*() const { return buf; }

    /**
     * The length of buffer. The actual amount of space allocated may be
     * higher--see capacity().
     */
    TPS_PUBLIC unsigned int size() const { return len; }

    /**
     * The amount of memory allocated for the buffer. This is the maximum
     * size the buffer can grow before it needs to allocate more memory.
     */
    TPS_PUBLIC unsigned int capacity() const { return res; }

    /**
     * Sets all bytes in the buffer to 0.
     */
    TPS_PUBLIC void zeroize();

    /**
     * Changes the length of the Buffer. If 'newLen' is shorter than the
     * current length, the Buffer is truncated. If 'newLen' is longer, the
     * new bytes are initialized to 0. If 'newLen' is the same as size(),
     * this is a no-op.
     */
    TPS_PUBLIC void resize(unsigned int newLen);

    /**
     * Ensures that capacity() is at least 'reserve'. Allocates more memory
     * if necessary. If 'reserve' is <= capacity(), this is a no-op.
     * Does not affect size().
     */
    TPS_PUBLIC void reserve(unsigned int reserve);

    /**
     * Returns a new Buffer that is a substring of this Buffer, starting
     * from offset 'start' and continuing for 'len' bytes. This Buffer
     * must have size() >= (start + len).
     */
    TPS_PUBLIC Buffer substr(unsigned int start, unsigned int len) const;

    /**
     * Replaces bytes i through i+n in this Buffer using the values in 'cpy'.
     * This Buffer is resized if necessary. The 'cpy' argument can be a
     * Buffer.
     */
    TPS_PUBLIC void replace(unsigned int i, const BYTE* cpy, unsigned int n);

    TPS_PUBLIC unsigned char* getBuf();
    TPS_PUBLIC unsigned int getLen();

    /**
     * returns a hex version of the buffer
     */
    TPS_PUBLIC char *toHex();

    /**
     * Dumps this Buffer to the given file as formatted hex: 16 bytes per
     * line, separated by spaces.
     */
    TPS_PUBLIC void dump(FILE* file) const;

    /**
     * returns a null-terminated string of the buf.
     * should be called only by callers that are certain that buf
     * is entirely representable by printable characters and wants
     * a string instead.
     */
    TPS_PUBLIC char *string();

    /**
     * dump()s this Buffer to stdout.
     */
    TPS_PUBLIC void dump() const;

};

#endif