packet.cpp
3.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
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
/* Modificado por: @Felipe Lacet
* Ultima Atualização: 08/10/2008
*
* Packet.cpp
*
*/
#include "bitutil_hal.h"
#include "packet.h"
//#include "dprintf.h"
#include <iostream>
int TS_PACKET_SIZE = 188;
//static int payloadIndex;
using namespace std;
Packet::Packet(unsigned char* packet) {
this->packet = packet;
}
Packet::~Packet() {
};
unsigned char* Packet::getPayloadBeginingAtUnitStarted() {
int payloadIndex = 4 + (hasAdaptationField() ? 1 : 0) + getAdaptationFieldLength();
return (&packet[payloadIndex]);
}
BOOL Packet::hasPointerField() {
return ( getBitAt(packet, 9));
}
BOOL Packet::hasPayload() {
unsigned int adaptationFieldControl = getByteAtBits(packet, 26, 2);
if ((adaptationFieldControl == 0x1) || (adaptationFieldControl == 0x3)) {
return (1);
} else {
return (0);
}
}
U8 Packet::getPayloadLength() {
if (!hasPayload()) {
return (0);
} else {
return (TS_PACKET_SIZE - (hasAdaptationField() ? 1 : 0) - getAdaptationFieldLength() - 4);
}
}
unsigned char* Packet::getPayloadCopy() {
int payloadIndex = 4 + (hasAdaptationField() ? 1 : 0) + getAdaptationFieldLength();
if (hasPayload()) {
int size = getPayloadLength();
/*unsigned char * payload =(unsigned char *) malloc(sizeof(unsigned char)*size);*/
return (&packet[payloadIndex]);
/*memcpy(payload, &packet[payloadIndex], size);*/
/*return payload;*/
/*return &packet[payloadIndex];*/
} else {
return (NULL);
}
}
U8 Packet::getPayloadBeginingAtUnitStartedLength() {
int payloadIndex = 4 + (hasAdaptationField() ? 1 : 0) + getAdaptationFieldLength();
return TS_PACKET_SIZE - payloadIndex - 1;
}
U8 Packet::getPointerField() {
int payloadIndex = 4 + (hasAdaptationField() ? 1 : 0) + getAdaptationFieldLength();
if (hasPointerField()) {
unsigned char pf = getByteAt(packet, payloadIndex);
//cout << "Poiter Field: " << pf << endl;
//printf("Poiter Field: %x\n", pf);
return ( getByteAt(packet, payloadIndex));
} else {
return (0);
}
}
U16 Packet::getPid() {
return ( getIntAtBits(packet, 11, 13));
}
U8 Packet::getAdaptationFieldLength() {
if (hasAdaptationField()) {
return ( getByteAt(packet, 4));
} else {
return (0);
}
}
unsigned int Packet::getContinuityCounter() {
return ( getIntAtBits(packet, 28, 4));
}
BOOL Packet::hasAdaptationField() {
unsigned int adaptationFieldControl = getByteAtBits(packet, 26, 2);
if ((adaptationFieldControl == 0x2) || (adaptationFieldControl == 0x3)) {
return TRUE;
} else {
return FALSE;
}
}
BOOL Packet::parser() {
// DPRINTF("PACOTE %X\n",packet[0]);
if (getByteAt(packet, 0) != 0x47) {
// DPRINTF("NAO PACOTE");
return FALSE;
}
if (getBitAt(packet, 8)) {
// DPRINTF("NAO PACOTE 2");
return FALSE;
}
// payloadIndex = 4 + (hasAdaptationField(packet) ? 1 : 0) + getAdaptationFieldLength(packet);
return TRUE;
}