-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
62 lines (50 loc) · 1.59 KB
/
utils.h
File metadata and controls
62 lines (50 loc) · 1.59 KB
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
#ifndef UTILS_H
#define UTILS_H
#include "includes.h"
namespace utils {
int16 endian16(int16 data){
int8 l, h;
h = (data & 0xff00) >> 8;
l = (data & 0x00ff);
return (int16)(l << 8) | h;
}
int16 checksum(int8* buffer, int16 size){
int32 acc = 0;
int16* i;
for(i = (int16 *)buffer; size > 1; size-=2, ++i)
acc+=(int32)(*i);
if(size == 1) acc += ((int32)*((int8 *)i)) << 8;
int16 sum = (acc & 0x0000ffff);
int16 carry = ((acc & 0xffff0000) >> 16);
return (~(sum + carry));
}
vector<int8> getStringToByteVector(string s){
return vector<int8>(s.begin(), s.end());
}
string convertIpToString(int32 ip){
int8 o1 = (ip >> 24) & 0xff;
int8 o2 = (ip >> 16) & 0xff;
int8 o3 = (ip >> 8) & 0xff;
int8 o4 = ip & 0xff;
ostringstream oss;
oss<<(int32)o1<<'.'<<(int32)o2<<'.'<<(int32)o3<<'.'<<(int32)o4;
return oss.str();
}
void copy_data(int8* src_data, int8* dest_data, int16 size){
int8 *src_ptr, *dest_ptr;
for(src_ptr=src_data, dest_ptr=dest_data; size!=0; --size,++src_ptr,++dest_ptr)
*dest_ptr = *src_ptr;
}
void printhex(const int8* data, size_t size, int indent = 0)
{
for (size_t i = 0; i < size; ++i) {
if (i % 16 == 0) {
cout << "\n";
for (int j = 0; j < indent; ++j) cout << ' ';
}
cout << hex << setw(2) << setfill('0') << (int)data[i] << " ";
}
cout << dec << "\n";
}
}
#endif