-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse-pat.cpp
More file actions
88 lines (67 loc) · 1.98 KB
/
parse-pat.cpp
File metadata and controls
88 lines (67 loc) · 1.98 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
#include <stdio.h>
#include <iostream>
#include <arpa/inet.h>
uint32_t ntoh(uint32_t t) {
return ntohl(t);
}
uint16_t ntoh(uint16_t t) {
return ntohs(t);
}
uint8_t ntoh(uint8_t t) {
return t;
}
template<typename T>
struct ReadWrapper {
ReadWrapper(T& t) : t_(t) {}
T& t_;
};
template<typename T>
std::istream& operator >> (std::istream& is, ReadWrapper<T> const& r) {
is.read(reinterpret_cast<char*>(&r.t_), sizeof(r.t_));
r.t_ = ntoh(r.t_);
return is;
}
template<typename T>
ReadWrapper<T> read(T& t) {
return ReadWrapper<T>(t);
}
struct PatSectionHeader {
uint8_t tableid_;
uint16_t length_;
static const size_t header_length = 8;
uint16_t streamid_;
uint8_t version_;
bool current_next_;
uint8_t section_number_;
uint8_t last_section_number_;
};
std::istream& operator >> (std::istream& is, PatSectionHeader& h) {
uint16_t d;
uint8_t v;
is >> read(h.tableid_) >> read(d) >> read(h.streamid_) >> read(v) >> read(h.section_number_) >> read(h.last_section_number_);
h.length_ = d & ((1 << 12) - 1);
h.version_ = (v & ((1 << 5) -1)) >> 1;
h.current_next_ = v & 1;
return is;
}
int main(int argc, char* argv[]) {
PatSectionHeader hdr = {0};
if(std::cin >> hdr) {
std::cout << "{";
std::cout << "\"tableid\":" << (uint32_t)hdr.tableid_ << ",";
std::cout << "\"streamid\":" << hdr.streamid_ << ",";
std::cout << "\"version\":" << (uint32_t)hdr.version_ << ",";
std::cout << "\"number\":" << (uint32_t)hdr.section_number_ << ",";
std::cout << "\"lastnumber\":" << (uint32_t)hdr.last_section_number_ << ",";
std::cout << "\"programs\":[";
for(int i = 0; std::cin && hdr.length_ > PatSectionHeader::header_length && i < (hdr.length_ - PatSectionHeader::header_length)/4; ++i) {
uint16_t program, pid;
std::cin >> read(program) >> read(pid);
std::cout << ((i!=0) ? "," : "") << "{\"program\":" << program << ",\"pid\":" << (pid & ((1 << 12) -1)) << "}";
}
std::cout << "]";
uint32_t crc;
std::cin >> read(crc);
std::cout << "}" << std::endl;
}
}