-
Notifications
You must be signed in to change notification settings - Fork 3
/
GenericEndianBuffer.cpp
189 lines (165 loc) · 4.58 KB
/
GenericEndianBuffer.cpp
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
/*
* This file is part of ShapeFusion (Copyright 2000 Tito Dal Canton)
*
* ShapeFusion 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.
*
* ShapeFusion 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 ShapeFusion; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <iostream>
#include <cstring>
#include <vector>
#include "GenericEndianBuffer.h"
GenericEndianBuffer::GenericEndianBuffer(unsigned int _size):
mSize(0), mSelfAllocated(true)
{
mData = new unsigned char[_size];
if (mData != NULL) {
mPosition = mData;
mSize = _size;
}
}
GenericEndianBuffer::GenericEndianBuffer(unsigned char *_data, unsigned int _size):
mData(_data), mPosition(_data), mSize(_size), mSelfAllocated(false)
{
}
GenericEndianBuffer::~GenericEndianBuffer(void)
{
if (mSelfAllocated && mData != NULL) {
delete[] mData;
mData = NULL;
}
}
char GenericEndianBuffer::ReadChar(void)
{
if ((unsigned int)(mPosition - mData) < mSize) {
unsigned char v = *mPosition;
mPosition++;
return (char)v;
} else {
std::cerr << "GenericEndianBuffer: attempted read beyond buffer limits\n";
return 0;
}
}
unsigned char GenericEndianBuffer::ReadUChar(void)
{
if ((unsigned int)(mPosition - mData) < mSize) {
unsigned char v = *mPosition;
mPosition++;
return v;
} else {
std::cerr << "GenericEndianBuffer: attempted read beyond buffer limits\n";
return 0;
}
}
void GenericEndianBuffer::ReadBlock(unsigned long _size, unsigned char *dest)
{
if ((unsigned int)(mPosition - mData + _size - 1) < mSize) {
memcpy(dest, mPosition, _size);
mPosition += _size;
} else {
std::cerr << "GenericEndianBuffer: attempted read beyond buffer limits\n";
}
}
void GenericEndianBuffer::WriteChar(char v)
{
if ((unsigned int)(mPosition - mData) < mSize)
*mPosition++ = v;
else
std::cerr << "GenericEndianBuffer: attempted write beyond buffer limits\n";
}
void GenericEndianBuffer::WriteUChar(unsigned char v)
{
if ((unsigned int)(mPosition - mData) < mSize)
*mPosition++ = v;
else
std::cerr << "GenericEndianBuffer: attempted write beyond buffer limits\n";
}
void GenericEndianBuffer::Write4CharCode(char c1, char c2, char c3, char c4)
{
if ((unsigned int)(mPosition - mData + 4 - 1) < mSize) {
*mPosition++ = c1;
*mPosition++ = c2;
*mPosition++ = c3;
*mPosition++ = c4;
} else {
std::cerr << "GenericEndianBuffer: attempted write beyond buffer limits\n";
}
}
void GenericEndianBuffer::WriteBlock(unsigned long _size, const void *src)
{
if ((unsigned int)(mPosition - mData + _size - 1) < mSize) {
memcpy(mPosition, src, _size);
mPosition += _size;
} else {
std::cerr << "GenericEndianBuffer: attempted write beyond buffer limits\n";
}
}
void GenericEndianBuffer::WriteZeroes(unsigned int n)
{
if ((unsigned int)(mPosition - mData + n - 1) < mSize) {
memset(mPosition, 0, n);
mPosition += n;
} else {
std::cerr << "GenericEndianBuffer: attempted write beyond buffer limits\n";
}
}
unsigned char *GenericEndianBuffer::Data(void) const
{
return mData;
}
unsigned int GenericEndianBuffer::Size(void) const
{
return mSize;
}
void GenericEndianBuffer::Position(unsigned int pos)
{
if (pos < mSize)
mPosition = mData + pos;
else
std::cerr << "GenericEndianBuffer: attempted to position beyond buffer limits (" << pos << "/" << (mSize-1) << ")\n";
}
unsigned int GenericEndianBuffer::Position(void) const
{
return mPosition - mData;
}
static const unsigned long CRC32_POLYNOMIAL = 0xEDB88320L;
static std::vector<unsigned long> crc_table;
static void BuildCRCTable()
{
if (crc_table.empty()) {
crc_table.resize(256);
for (unsigned int i = 0; i < crc_table.size(); ++i) {
unsigned long crc = i;
for (int j = 0; j < 8; ++j) {
if (crc & 1) {
crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
} else {
crc >>= 1;
}
crc_table[i] = crc;
}
}
}
}
unsigned long GenericEndianBuffer::CalculateCRC() const
{
BuildCRCTable();
unsigned long crc = 0xFFFFFFFFL;
unsigned char* p = mData;
for (unsigned int i = 0; i < mSize; ++i) {
unsigned long a = (crc >> 8) & 0x00FFFFFFL;
unsigned long b = crc_table[((int) crc ^ *p++) & 0xff];
crc = a^b;
}
return crc ^ 0xFFFFFFFFL;
}