summaryrefslogtreecommitdiffstats
path: root/pki/base/symkey/src/com/netscape/symkey/Buffer.h
blob: 2e0256d87a6e3040d961f8781693043fe1b585b0 (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
// --- BEGIN COPYRIGHT BLOCK ---
// 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; version 2 of the License.
//
// 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.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

#ifndef BUFFER_H
#define BUFFER_H

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

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

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

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

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

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

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

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

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

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

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

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

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

    /**
     * Append operators.
     */
    Buffer& operator+=(const Buffer&addend);
    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]'.
     */
    operator BYTE*() { return buf; }
    operator const BYTE*() const { return buf; }

    /**
     * The length of buffer. The actual amount of space allocated may be
     * higher--see capacity().
     */
    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.
     */
    unsigned int capacity() const { return res; }

    /**
     * Sets all bytes in the buffer to 0.
     */
    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.
     */
    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().
     */
    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).
     */
    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.
     */
    void replace(unsigned int i, const BYTE* cpy, unsigned int n);

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

    /**
     * Dumps this Buffer to the given file as formatted hex: 16 bytes per
     * line, separated by spaces.
     */
    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.
     */
    char *string();

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

};

#endif