summaryrefslogtreecommitdiffstats
path: root/base/symkey/src/com/netscape/symkey/Buffer.cpp
blob: 5c687c5f5459cfacd07ca49d2096a9dcc686d542 (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
// --- 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 ---

#include <memory.h>
#include <assert.h>
#include <stdio.h>
#include <cstdarg>
#include <string>

#include "Buffer.h"

Buffer::Buffer(const BYTE *buf_, unsigned int len_) : len(len_), res(len_)
{
    buf = new BYTE[len];
    memcpy(buf, buf_, len);
}

Buffer::Buffer(const Buffer& cpy)
{
    buf = 0;
    *this = cpy;
}

Buffer::Buffer(unsigned int len_) : len(len_), res(len_)
{
    buf = new BYTE[res];
    memset(buf, 0, len_);
}

Buffer::Buffer(unsigned int len_, BYTE b) : len(len_), res(len_)
{
    buf = new BYTE[res];
    memset(buf, b, len);
}

Buffer::~Buffer()
{
    delete [] buf;
}

bool
Buffer::operator==(const Buffer& cmp) const
{
    if( len != cmp.len ) return false;
    for( unsigned int i=0; i < len; ++i ) {
        if( buf[i] != cmp.buf[i] ) {
            return false;
        }
    }
    return true;
}

Buffer&
Buffer::operator=(const Buffer& cpy)
{
    if( this == &cpy ) return *this;
    len = cpy.len;
    delete [] buf;
    buf = new BYTE[len];
    memcpy(buf, cpy.buf, len);
    res = len;

    return *this;
}

void
Buffer::zeroize()
{
    if( len > 0 ) {
        memset( buf, 0, len );
    }
}

Buffer
Buffer::operator+(const Buffer& addend) const
{
    Buffer result(len + addend.len);
    memcpy(result.buf, buf, len);
    memcpy(result.buf+len, addend.buf, addend.len);
    return result;
}

Buffer&
Buffer::operator+=(const Buffer& addend)
{
    unsigned int oldLen = len;
    resize(len + addend.len);
    memcpy(buf+oldLen, addend.buf, addend.len);
    return *this;
}

Buffer&
Buffer::operator+=(BYTE b)
{
    resize(len+1);
    buf[len-1] = b;
    return *this;
}

void
Buffer::reserve(unsigned int n)
{
    if( n > res ) {
        BYTE *newBuf = new BYTE[n];
        memcpy(newBuf, buf, len);
        delete [] buf;
        buf = newBuf;
        res = n;
    }
}

void
Buffer::resize(unsigned int newLen)
{
    if( newLen == len ) {
        return;
    } else if( newLen < len ) {
        len = newLen;
    } else if( newLen <= res ) {
        assert( newLen > len );
        memset(buf+len, 0, newLen-len);
        len = newLen;
    } else {
        assert( newLen > len && newLen > res );
        BYTE *newBuf = new BYTE[newLen];
        memcpy(newBuf, buf, len);
        memset(newBuf+len, 0, newLen-len);
        delete [] buf;
        buf = newBuf;
        len = newLen;
        res = newLen;
    }
}

Buffer
Buffer::substr(unsigned int i, unsigned int n) const
{
    assert( i < len  && (i+n) <= len );
    return Buffer( buf+i, n );
}

void
Buffer::replace(unsigned int i, const BYTE* cpy, unsigned int n)
{
    if (len > i+n) {
    resize( len);
    }else {
    resize( i+n );
    }
    memcpy(buf+i, cpy, n);
}

void
Buffer::dump() const
{
    unsigned int i;

    for( i=0; i < len; ++i ) {
        printf("%02x ", buf[i]);
        if( i % 16 == 15 )  printf("\n");
    }
    printf("\n");
}

static const char hextbl[] = {
    '0', '1', '2', '3', '4', '5', '6', '7',
    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};