-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataInput.cc
51 lines (46 loc) · 1 KB
/
DataInput.cc
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
#include <sstream>
#include <stdexcept>
#include "DataInput.hh"
DataInput::DataInput(const unsigned char* data, int length) :
_length(length), _i(0), _data(data)
{
}
short
DataInput::readShort() const
{
static const int n = 2;
if (_i + n > _length) {
std::stringstream ss;
ss << "DataInput::readShort overflow : " << _i
<< " length: " << _length << std::endl;
throw std::runtime_error(ss.str());
}
union {
short s;
char c[n];
} conv;
for (int j = 0; j < n; j++) {
conv.c[j] = _data[_i + j];
}
_i += n;
return conv.s;
}
std::string
DataInput::readEightBytes() const
{
std::string result;
static const int n = 8;
if (_i + n > _length) {
std::stringstream ss;
ss << "DataInput::readEightBytes overflow : " << _i
<< " length: " << _length << std::endl;
throw std::runtime_error(ss.str());
}
for (int j = 0; j < n; j++) {
char c = _data[_i + j];
if (c == 0) break;
result += c;
}
_i += n;
return result;
}