-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.hpp
61 lines (50 loc) · 976 Bytes
/
util.hpp
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
#ifndef _TSD_UTIL_HPP_
#define _TSD_UTIL_HPP_
#include <cstdint>
#include <ostream>
#include <iomanip>
namespace tsd
{
inline
uint8_t get8(const uint8_t* p) {
return *p;
}
inline
uint16_t get16(const uint8_t* p) {
return (get8(p) << 8) | get8(p+1);
}
inline
uint32_t get32(const uint8_t* p) {
return (get16(p) << 16) | get16(p+2);
}
inline
uint8_t get8(const char* p) {
return *reinterpret_cast<const uint8_t*>(p);
}
inline
uint16_t get16(const char* p) {
return get16(reinterpret_cast<const uint8_t*>(p));
}
inline
uint32_t get32(const char* p) {
return get32(reinterpret_cast<const uint8_t*>(p));
}
inline
void hexdump(const char* data, size_t size, ostream& ost) {
size_t i;
for(i = 0; i < size; ++i){
ost
<< std::setw(2)
<< std::setfill('0')
<< std::hex
<< std::uppercase
<< static_cast<int>(get8(data+i))
<< ' ';
if(i % 16 == 15)
ost << endl;
}
if(i % 16 != 0)
ost << endl;
}
}
#endif