aboutsummaryrefslogtreecommitdiffstats
path: root/lib/bufferstore.cc
blob: 56cd8b1ce46f86e220b18566ac856e01ca88dfa9 (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
/*-*-c++-*-
 * $Id$
 *
 * This file is part of plptools.
 *
 *  Copyright (C) 1999  Philip Proudman <philip.proudman@btinternet.com>
 *  Copyright (C) 2000, 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
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "bufferstore.h"

#include <cstring>
#include <cstdlib>

// Should be iostream.h, but won't build on Sun WorkShop C++ 5.0
#include <iomanip>
#include <string>

#include <stdlib.h>
#include <ctype.h>
#include <assert.h>

using namespace std;

bufferStore::bufferStore()
    : len(0)
    , lenAllocd(0)
    , start(0)
    , buff(0)
{
}

bufferStore::bufferStore(const bufferStore &a)
    : start(0)
{
    lenAllocd = (a.getLen() > MIN_LEN) ? a.getLen() : MIN_LEN;
    buff = (unsigned char *)malloc(lenAllocd);
    assert(buff);
    len = a.getLen();
    memcpy(buff, a.getString(0), len);
}

bufferStore::bufferStore(const unsigned char *_buff, long _len)
    : start(0)
{
    lenAllocd = (_len > MIN_LEN) ? _len : MIN_LEN;
    buff = (unsigned char *)malloc(lenAllocd);
    assert(buff);
    len = _len;
    memcpy(buff, _buff, len);
}

bufferStore &bufferStore::operator =(const bufferStore &a) {
    if (this != &a) {
        checkAllocd(a.getLen());
        len = a.getLen();
        memcpy(buff, a.getString(0), len);
        start = 0;
    }
    return *this;
}

void bufferStore::init() {
    start = 0;
    len = 0;
}

void bufferStore::init(const unsigned char *_buff, long _len) {
    checkAllocd(_len);
    start = 0;
    len = _len;
    memcpy(buff, _buff, len);
}

bufferStore::~bufferStore() {
    if (buff)
	::free(buff);
}

unsigned long bufferStore::getLen() const {
    return (start > len) ? 0 : len - start;
}

unsigned char bufferStore::getByte(long pos) const {
    return buff[pos+start];
}

u_int16_t bufferStore::getWord(long pos) const {
    return buff[pos+start] + (buff[pos+start+1] << 8);
}

u_int32_t bufferStore::getDWord(long pos) const {
    return buff[pos+start] +
	(buff[pos+start+1] << 8) +
	(buff[pos+start+2] << 16) +
	(buff[pos+start+3] << 24);
}

const char * bufferStore::getString(long pos) const {
    return (const char *)buff + pos + start;
}

ostream &operator<<(ostream &s, const bufferStore &m) {
    // save stream flags
    ostream::fmtflags old = s.flags();

    for (int i = m.start; i < m.len; i++)
	s << hex << setw(2) << setfill('0') << (int)m.buff[i] << " ";

    // restore stream flags
    s.flags(old);
    s << "(";

    for (int i = m.start; i < m.len; i++) {
	unsigned char c = m.buff[i];
	s << (unsigned char)(isprint(c) ? c : '.');
    }

    return s << ")";
}

void bufferStore::discardFirstBytes(int n) {
    start += n;
    if (start > len) start = len;
}

void bufferStore::checkAllocd(long newLen) {
    if (newLen >= lenAllocd) {
	do {
	    lenAllocd = (lenAllocd < MIN_LEN) ? MIN_LEN : (lenAllocd * 2);
	} while (newLen >= lenAllocd);
	assert(lenAllocd);
	buff = (unsigned char *)realloc(buff, lenAllocd);
	assert(buff);
    }
}

void bufferStore::addByte(unsigned char cc) {
    checkAllocd(len + 1);
    buff[len++] = cc;
}

void bufferStore::addString(const char *s) {
    int l = strlen(s);
    checkAllocd(len + l);
    memcpy(&buff[len], s, l);
    len += l;
}

void bufferStore::addStringT(const char *s) {
    addString(s);
    addByte(0);
}

void bufferStore::addBytes(const unsigned char *s, int l) {
    checkAllocd(len + l);
    memcpy(&buff[len], s, l);
    len += l;
}

void bufferStore::addBuff(const bufferStore &s, long maxLen) {
    long l = s.getLen();
    checkAllocd(len + l);
    if ((maxLen >= 0) && (maxLen < l))
	l = maxLen;
    if (l > 0) {
	memcpy(&buff[len], s.getString(0), l);
	len += l;
    }
}

void bufferStore::addWord(int a) {
    checkAllocd(len + 2);
    buff[len++] = a & 0xff;
    buff[len++] = (a>>8) & 0xff;
}

void bufferStore::addDWord(long a) {
    checkAllocd(len + 4);
    buff[len++] = a & 0xff;
    buff[len++] = (a>>8) & 0xff;
    buff[len++] = (a>>16) & 0xff;
    buff[len++] = (a>>24) & 0xff;
}

void bufferStore::truncate(long newLen) {
    if (newLen < len)
	len = newLen;
}

void bufferStore::prependByte(unsigned char cc) {
    checkAllocd(len + 1);
    memmove(&buff[1], buff, len++);
    buff[0] = cc;
}

void bufferStore::prependWord(int a) {
    checkAllocd(len + 2);
    memmove(&buff[2], buff, len);
    len += 2;
    buff[0] = a & 0xff;
    buff[1] = (a>>8) & 0xff;
}

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