-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmpls.cpp
165 lines (119 loc) · 3.2 KB
/
mpls.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "mpls.h"
namespace mpls
{
inline u_int32_t get_ui32(unsigned char* p)
{
u_int32_t rc=((u_int32_t)p[0])<<24;
rc+=((u_int32_t)p[1])<<16;
rc+=((u_int32_t)p[2])<<8;
rc+=p[3];
return rc;
}
inline u_int16_t get_ui16(unsigned char* p)
{
u_int32_t rc=((u_int32_t)p[0])<<8;
rc+=p[1];
return rc;
}
}
int mpls::parse(const char* filename,std::list<int>& playlist,std::map<int,std::string>& datetime,int verb)
{
FILE* fp=fopen(filename,"rb");
if(!fp)
return -1;
struct stat st;
if(fstat(fileno(fp),&st)==-1)
{
fclose(fp);
return -1;
}
unsigned int len=st.st_size;
std::auto_ptr<unsigned char> p(new unsigned char[len]);
unsigned char* ptr=p.get();
if(!ptr)
{
fclose(fp);
return -1;
}
if(fread(ptr,1,len,fp)!=len)
{
fclose(fp);
return -1;
}
fclose(fp);
if(memcmp(ptr,"MPLS0",5))
return -1;
std::vector<int> chapters;
chapters.reserve(512);
u_int32_t playlist_offset=get_ui32(ptr+8);
u_int32_t playlist_mark_offset=get_ui32(ptr+12);
u_int32_t playlist_ext_offset=get_ui32(ptr+16);
if(playlist_offset>len || playlist_mark_offset>len || playlist_ext_offset>len)
return -1;
if(verb)
fprintf(stderr,"=== clips from playlist %s ===\n",filename);
if(playlist_offset)
{
unsigned char* p=ptr+playlist_offset;
u_int32_t l=get_ui32(p);
p+=4;
unsigned char* p2=p+l;
p+=2; // reserved
u_int16_t n=get_ui16(p);
p+=4;
for(u_int16_t i=0;i<n;i++)
{
if(p>p2)
return -1;
u_int16_t item_len=get_ui16(p);
p+=2;
int clip=0;
for(int j=0;j<5;j++)
clip=clip*10+(p[j]-48);
chapters.push_back(clip);
playlist.push_back(clip);
p+=item_len;
}
}
if(playlist_mark_offset)
{
// skip section
}
if(playlist_ext_offset)
{
unsigned char* p=ptr+playlist_ext_offset;
u_int32_t l=get_ui32(p);
p+=4;
unsigned char* p2=p+l;
if(p+4<=p2 && !memcmp(p+20,"PLEX",4))
{
p+=348;
if(p+2<=p2)
{
u_int16_t n=get_ui16(p);
p+=2;
for(u_int16_t i=0;i<n;i++)
{
if(p+66>p2)
break;
// CA or DA
if((p[44] == 'C' || p[44] == 'D') && p[45] == 'A')
{
char tmp[64];
sprintf(tmp,"20%.2x-%.2x-%.2x %.2x:%.2x:%.2x",p[12],p[13],p[14],p[15],p[16],p[17]);
datetime[chapters[i]]=tmp;
if(verb)
fprintf(stderr,"* %.5i: %s\n",chapters[i],tmp);
}
p+=66;
}
}
}
}else
{
if(verb)
for(int i=0;i<chapters.size();i++)
fprintf(stderr,"* %.5i\n",chapters[i]);
}
return 0;
}