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
47: start(0) {
48 lenAllocd = (a.getLen() > MIN_LEN) ? a.getLen() : MIN_LEN;
49 buff = (unsigned char *)malloc(lenAllocd);
50 assert(buff);
51 len = a.getLen();
52 memcpy(buff, a.getString(0), len);
53}
54
55BufferStore::BufferStore(const unsigned char *_buff, long _len)
56: start(0) {
57 lenAllocd = (_len > MIN_LEN) ? _len : MIN_LEN;
58 buff = (unsigned char *)malloc(lenAllocd);
59 assert(buff);
60 len = _len;
61 memcpy(buff, _buff, len);
62}
63
65 if (this != &a) {
66 checkAllocd(a.getLen());
67 len = a.getLen();
68 memcpy(buff, a.getString(0), len);
69 start = 0;
70 }
71 return *this;
72}
73
75 start = 0;
76 len = 0;
77}
78
79void BufferStore::init(const unsigned char *_buff, long _len) {
80 checkAllocd(_len);
81 start = 0;
82 len = _len;
83 memcpy(buff, _buff, len);
84}
85
87 if (buff) {
88 ::free(buff);
89 }
90}
91
92unsigned long BufferStore::getLen() const {
93 return (start > len) ? 0 : len - start;
94}
95
96unsigned char BufferStore::getByte(long pos) const {
97 return buff[pos+start];
98}
99
100uint16_t BufferStore::getWord(long pos) const {
101 return buff[pos+start] + (buff[pos+start+1] << 8);
102}
103
104uint32_t BufferStore::getDWord(long pos) const {
105 return buff[pos+start] +
106 (buff[pos+start+1] << 8) +
107 (buff[pos+start+2] << 16) +
108 (buff[pos+start+3] << 24);
109}
110
111int32_t BufferStore::getSDWord(long pos) const {
112 return buff[pos+start] +
113 (buff[pos+start+1] << 8) +
114 (buff[pos+start+2] << 16) +
115 (buff[pos+start+3] << 24);
116}
117
118const char * BufferStore::getString(long pos) const {
119 return (const char *)buff + pos + start;
120}
121
123 // save stream flags
124 ostream::fmtflags old = s.flags();
125
126 for (int i = m.start; i < m.len; i++)
127 s << hex << setw(2) << setfill('0') << (int)m.buff[i] << " ";
128
129 // restore stream flags
130 s.flags(old);
131 s << "(";
132
133 for (int i = m.start; i < m.len; i++) {
134 unsigned char c = m.buff[i];
135 s << (unsigned char)(isprint(c) ? c : '.');
136 }
137
138 return s << ")";
139}
140
142 start += n;
143 if (start > len) start = len;
144}
145
146void BufferStore::checkAllocd(long newLen) {
147 if (newLen >= lenAllocd) {
148 do {
150 } while (newLen >= lenAllocd);
151 assert(lenAllocd);
152 buff = (unsigned char *)realloc(buff, lenAllocd);
153 assert(buff);
154 }
155}
156
157void BufferStore::addByte(unsigned char cc) {
158 checkAllocd(len + 1);
159 buff[len++] = cc;
160}
161
162void BufferStore::addString(const char *s) {
163 int l = strlen(s);
164 checkAllocd(len + l);
165 memcpy(&buff[len], s, l);
166 len += l;
167}
168
169void BufferStore::addStringT(const char *s) {
170 addString(s);
171 addByte(0);
172}
173
174void BufferStore::addBytes(const unsigned char *s, int l) {
175 checkAllocd(len + l);
176 memcpy(&buff[len], s, l);
177 len += l;
178}
179
180void BufferStore::addBuff(const BufferStore &s, long maxLen) {
181 long l = s.getLen();
182 checkAllocd(len + l);
183 if ((maxLen >= 0) && (maxLen < l))
184 l = maxLen;
185 if (l > 0) {
186 memcpy(&buff[len], s.getString(0), l);
187 len += l;
188 }
189}
190
192 checkAllocd(len + 2);
193 buff[len++] = a & 0xff;
194 buff[len++] = (a>>8) & 0xff;
195}
196
198 checkAllocd(len + 4);
199 buff[len++] = a & 0xff;
200 buff[len++] = (a>>8) & 0xff;
201 buff[len++] = (a>>16) & 0xff;
202 buff[len++] = (a>>24) & 0xff;
203}
204
205void BufferStore::truncate(long newLen) {
206 if (newLen < len) {
207 len = newLen;
208 }
209}
210
211void BufferStore::prependByte(unsigned char cc) {
212 checkAllocd(len + 1);
213 memmove(&buff[1], buff, len++);
214 buff[0] = cc;
215}
216
218 checkAllocd(len + 2);
219 memmove(&buff[2], buff, len);
220 len += 2;
221 buff[0] = a & 0xff;
222 buff[1] = (a>>8) & 0xff;
223}
ostream & operator<<(std::ostream &s, const BufferStore &m)
Definition: bufferstore.cc:122
A generic container for an array of bytes.
Definition: bufferstore.h:36
unsigned char * buff
Definition: bufferstore.h:252
const char * getString(long pos=0) const
Retrieves the characters at index pos.
Definition: bufferstore.cc:118
void prependByte(unsigned char c)
Prepends a byte to the content of this instance.
Definition: bufferstore.cc:211
uint16_t getWord(long pos=0) const
Retrieves the word at index pos.
Definition: bufferstore.cc:100
~BufferStore()
Destroys a BufferStore instance.
Definition: bufferstore.cc:86
uint32_t getDWord(long pos=0) const
Retrieves the dword at index pos.
Definition: bufferstore.cc:104
void truncate(long newLen)
Truncates the buffer.
Definition: bufferstore.cc:205
void prependWord(int w)
Prepends a word to the content of this instance.
Definition: bufferstore.cc:217
void addByte(unsigned char c)
Appends a byte to the content of this instance.
Definition: bufferstore.cc:157
void addWord(int w)
Appends a word to the content of this instance.
Definition: bufferstore.cc:191
void addBuff(const BufferStore &b, long maxLen=-1)
Appends data to the content of this instance.
Definition: bufferstore.cc:180
void addStringT(const char *s)
Appends a string to the content of this instance.
Definition: bufferstore.cc:169
void addDWord(long dw)
Appends a dword to the content of this instance.
Definition: bufferstore.cc:197
long lenAllocd
Definition: bufferstore.h:250
unsigned long getLen() const
Retrieves the length of a BufferStore.
Definition: bufferstore.cc:92
BufferStore()
Constructs a new BufferStore.
Definition: bufferstore.cc:39
void checkAllocd(long newLen)
Definition: bufferstore.cc:146
unsigned char getByte(long pos=0) const
Retrieves the byte at index pos.
Definition: bufferstore.cc:96
void addBytes(const unsigned char *buf, int len)
Appends data to the content of this instance.
Definition: bufferstore.cc:174
BufferStore & operator=(const BufferStore &)
Copies a BufferStore.
Definition: bufferstore.cc:64
void addString(const char *s)
Appends a string to the content of this instance.
Definition: bufferstore.cc:162
int32_t getSDWord(long pos=0) const
Retrieves the signed dword at index pos.
Definition: bufferstore.cc:111
void discardFirstBytes(int len=0)
Removes bytes from the start of the buffer.
Definition: bufferstore.cc:141
void init()
Initializes the BufferStore.
Definition: bufferstore.cc:74
Definition: doctest.h:522
static RFSV * a
Definition: main.cc:55