plptools
Loading...
Searching...
No Matches
bufferstore.cc
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 1999 Philip Proudman <philip.proudman@btinternet.com>
5 * Copyright (C) 2000, 2001 Fritz Elfert <felfert@to.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19 *
20 */
21
22#include "config.h"
23
24#include "bufferstore.h"
25
26#include <cstring>
27#include <cstdlib>
28
29// Should be iostream.h, but won't build on Sun WorkShop C++ 5.0
30#include <iomanip>
31#include <string>
32
33#include <stdlib.h>
34#include <ctype.h>
35#include <assert.h>
36
37using namespace std;
38
40 : len(0)
41 , lenAllocd(0)
42 , start(0)
43 , buff(0)
44{
45}
46
48 : start(0)
49{
50 lenAllocd = (a.getLen() > MIN_LEN) ? a.getLen() : MIN_LEN;
51 buff = (unsigned char *)malloc(lenAllocd);
52 assert(buff);
53 len = a.getLen();
54 memcpy(buff, a.getString(0), len);
55}
56
57bufferStore::bufferStore(const unsigned char *_buff, long _len)
58 : start(0)
59{
60 lenAllocd = (_len > MIN_LEN) ? _len : MIN_LEN;
61 buff = (unsigned char *)malloc(lenAllocd);
62 assert(buff);
63 len = _len;
64 memcpy(buff, _buff, len);
65}
66
68 if (this != &a) {
69 checkAllocd(a.getLen());
70 len = a.getLen();
71 memcpy(buff, a.getString(0), len);
72 start = 0;
73 }
74 return *this;
75}
76
78 start = 0;
79 len = 0;
80}
81
82void bufferStore::init(const unsigned char *_buff, long _len) {
83 checkAllocd(_len);
84 start = 0;
85 len = _len;
86 memcpy(buff, _buff, len);
87}
88
90 if (buff)
91 ::free(buff);
92}
93
94unsigned long bufferStore::getLen() const {
95 return (start > len) ? 0 : len - start;
96}
97
98unsigned char bufferStore::getByte(long pos) const {
99 return buff[pos+start];
100}
101
102uint16_t bufferStore::getWord(long pos) const {
103 return buff[pos+start] + (buff[pos+start+1] << 8);
104}
105
106uint32_t bufferStore::getDWord(long pos) const {
107 return buff[pos+start] +
108 (buff[pos+start+1] << 8) +
109 (buff[pos+start+2] << 16) +
110 (buff[pos+start+3] << 24);
111}
112
113int32_t bufferStore::getSDWord(long pos) const {
114 return buff[pos+start] +
115 (buff[pos+start+1] << 8) +
116 (buff[pos+start+2] << 16) +
117 (buff[pos+start+3] << 24);
118}
119
120const char * bufferStore::getString(long pos) const {
121 return (const char *)buff + pos + start;
122}
123
125 // save stream flags
126 ostream::fmtflags old = s.flags();
127
128 for (int i = m.start; i < m.len; i++)
129 s << hex << setw(2) << setfill('0') << (int)m.buff[i] << " ";
130
131 // restore stream flags
132 s.flags(old);
133 s << "(";
134
135 for (int i = m.start; i < m.len; i++) {
136 unsigned char c = m.buff[i];
137 s << (unsigned char)(isprint(c) ? c : '.');
138 }
139
140 return s << ")";
141}
142
144 start += n;
145 if (start > len) start = len;
146}
147
148void bufferStore::checkAllocd(long newLen) {
149 if (newLen >= lenAllocd) {
150 do {
152 } while (newLen >= lenAllocd);
153 assert(lenAllocd);
154 buff = (unsigned char *)realloc(buff, lenAllocd);
155 assert(buff);
156 }
157}
158
159void bufferStore::addByte(unsigned char cc) {
160 checkAllocd(len + 1);
161 buff[len++] = cc;
162}
163
164void bufferStore::addString(const char *s) {
165 int l = strlen(s);
166 checkAllocd(len + l);
167 memcpy(&buff[len], s, l);
168 len += l;
169}
170
171void bufferStore::addStringT(const char *s) {
172 addString(s);
173 addByte(0);
174}
175
176void bufferStore::addBytes(const unsigned char *s, int l) {
177 checkAllocd(len + l);
178 memcpy(&buff[len], s, l);
179 len += l;
180}
181
182void bufferStore::addBuff(const bufferStore &s, long maxLen) {
183 long l = s.getLen();
184 checkAllocd(len + l);
185 if ((maxLen >= 0) && (maxLen < l))
186 l = maxLen;
187 if (l > 0) {
188 memcpy(&buff[len], s.getString(0), l);
189 len += l;
190 }
191}
192
194 checkAllocd(len + 2);
195 buff[len++] = a & 0xff;
196 buff[len++] = (a>>8) & 0xff;
197}
198
200 checkAllocd(len + 4);
201 buff[len++] = a & 0xff;
202 buff[len++] = (a>>8) & 0xff;
203 buff[len++] = (a>>16) & 0xff;
204 buff[len++] = (a>>24) & 0xff;
205}
206
207void bufferStore::truncate(long newLen) {
208 if (newLen < len)
209 len = newLen;
210}
211
212void bufferStore::prependByte(unsigned char cc) {
213 checkAllocd(len + 1);
214 memmove(&buff[1], buff, len++);
215 buff[0] = cc;
216}
217
219 checkAllocd(len + 2);
220 memmove(&buff[2], buff, len);
221 len += 2;
222 buff[0] = a & 0xff;
223 buff[1] = (a>>8) & 0xff;
224}
ostream & operator<<(std::ostream &s, const bufferStore &m)
Definition: bufferstore.cc:124
A generic container for an array of bytes.
Definition: bufferstore.h:37
void discardFirstBytes(int len=0)
Removes bytes from the start of the buffer.
Definition: bufferstore.cc:143
void init()
Initializes the bufferStore.
Definition: bufferstore.cc:77
uint16_t getWord(long pos=0) const
Retrieves the word at index pos.
Definition: bufferstore.cc:102
void addBuff(const bufferStore &b, long maxLen=-1)
Appends data to the content of this instance.
Definition: bufferstore.cc:182
void checkAllocd(long newLen)
Definition: bufferstore.cc:148
bufferStore()
Constructs a new bufferStore.
Definition: bufferstore.cc:39
void addByte(unsigned char c)
Appends a byte to the content of this instance.
Definition: bufferstore.cc:159
void addBytes(const unsigned char *buf, int len)
Appends data to the content of this instance.
Definition: bufferstore.cc:176
const char * getString(long pos=0) const
Retrieves the characters at index pos.
Definition: bufferstore.cc:120
void prependByte(unsigned char c)
Prepends a byte to the content of this instance.
Definition: bufferstore.cc:212
unsigned char * buff
Definition: bufferstore.h:253
void prependWord(int w)
Prepends a word to the content of this instance.
Definition: bufferstore.cc:218
long lenAllocd
Definition: bufferstore.h:251
bufferStore & operator=(const bufferStore &)
Copies a bufferStore.
Definition: bufferstore.cc:67
void addDWord(long dw)
Appends a dword to the content of this instance.
Definition: bufferstore.cc:199
void addStringT(const char *s)
Appends a string to the content of this instance.
Definition: bufferstore.cc:171
void truncate(long newLen)
Truncates the buffer.
Definition: bufferstore.cc:207
void addWord(int w)
Appends a word to the content of this instance.
Definition: bufferstore.cc:193
unsigned long getLen() const
Retrieves the length of a bufferStore.
Definition: bufferstore.cc:94
~bufferStore()
Destroys a bufferStore instance.
Definition: bufferstore.cc:89
void addString(const char *s)
Appends a string to the content of this instance.
Definition: bufferstore.cc:164
uint32_t getDWord(long pos=0) const
Retrieves the dword at index pos.
Definition: bufferstore.cc:106
unsigned char getByte(long pos=0) const
Retrieves the byte at index pos.
Definition: bufferstore.cc:98
int32_t getSDWord(long pos=0) const
Retrieves the signed dword at index pos.
Definition: bufferstore.cc:113
Definition: doctest.h:522
static rfsv * a
Definition: main.cc:53