-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReader.cpp
More file actions
95 lines (71 loc) · 2.03 KB
/
Reader.cpp
File metadata and controls
95 lines (71 loc) · 2.03 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
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
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include "Reader.h"
#include <netinet/in.h>
#include <algorithm>
#include "HexConverter.h"
#define HEADER 20
using std::string;
using std::endl;
using std::ios;
using std::ifstream;
using std::cout;
using std::vector;
Reader::Reader(const string &file, PacketContainer &pkts) :
filename(file), packets(pkts) {}
Reader::~Reader() {}
void Reader::run() {
create_packets();
}
int Reader::create_packets(){
ifstream file;
file.open(Reader::filename, ios::in | ios::binary);
HexConverter converter;
while (file.peek() != EOF){
short len;
short data_len;
short shorts[3];
// HexConverter converter;
file.seekg(2, ios::cur);
//lee length
file.read((char*) shorts, 2);
len = shorts[0];
len = ntohs(len);
data_len = len - HEADER;
// lee id
unsigned char id_buff[2];
file.read((char*) id_buff, 2);
string id = converter.convertNum(id_buff, 2);
// lee offset
unsigned char fo[2];
file.read((char*) fo, 2);
unsigned char flag = fo[0];
flag <<= 2;
flag >>= 7;
fo[0] <<= 3;
fo[0] >>= 3;
short* offset_ptr = (short*) fo;
short offset = *offset_ptr;
offset = ntohs(offset);
file.seekg(4, ios::cur);
unsigned char fo_buff[5];
fo_buff[4] = '\0';
file.read((char*)fo_buff, 4);
string src = converter.convertNum(fo_buff, 4);
file.read((char*)fo_buff, 4);
string dst = converter.convertNum(fo_buff, 4);
unsigned char *buffer = (unsigned char*) malloc(data_len);
file.read((char*)buffer, data_len);
string data = converter.convertChars(buffer, data_len);
free(buffer);
Packet p = Packet(id, src, dst, flag, offset, data_len);
p.setData(data);
this->packets.addPacket(p);
}
file.close();
return 0;
}