Debian Codegraph @

lib/aviread/AsxReader.cpp

raw
1 #include "AsxReader.h"
2 #include "avm_output.h"
3
4 #include <ctype.h>
5 #include <string.h>
6 #include <stdio.h>
7
8 AVM_BEGIN_NAMESPACE;
9
10 enum asx_position
11 {
12 BEGIN,
13 OPEN_BRACKET,
14 CLOSE_BRACKET,
15 IN_COMMENT,
16 IN_QUOTED_COMMENT,
17 IN_TAGNAME,
18 AFTER_TAGNAME,
19 IN_ENDTAG,
20 BEFORE_TAGCLOSE,
21 IN_PROPNAME,
22 AFTER_PROPNAME,
23 PROP_EQUAL,
24 AFTER_PROPEQUAL,
25 BETWEEN_PROPS,
26 IN_PROPCONTENTS,
27 IN_QUOTED_PROPCONTENTS,
28 IN_QUOTED_CONTENTS,
29 IN_CONTENTS,
30 };
31
32 bool ASXReader::addURL(const char* url)
33 {
34 int i = 0;
35
36 while (*url && (!isprint(*url) || (*url == '"')))
37 url++;
38
39 while (isprint(url[i]) && (url[i] != '"'))
40 i++;
41
42 if (i > 0)
43 {
44 avm::string vurl = avm::string(url, i);
45
46 if (strncasecmp(vurl.c_str(), "mms://", 6) != 0
47 && strncasecmp(vurl.c_str(), "http://", 7) != 0)
48 {
49 char* h = new char[m_Server.size() * 2 + m_Filename.size()];
50 if (h)
51 {
52 int p = sprintf(h, "http://%s", m_Server.c_str());
53 if (vurl[0] != '/')
54 {
55 h[p++] = '/';
56 strcpy(h + p, m_Filename.c_str());
57 char* q = strchr(h + p, '?'); // strip all after '?'
58 if (q) *q = 0;
59 }
60 //printf("_____addUrl>%s<>%s<____\n", h, vurl.c_str());
61 vurl.insert(0, h);
62 }
63 }
64
65 //printf("URL:%s<\n", vurl.c_str());
66 m_Urls.push_back(vurl);
67 return true;
68 }
69 return false;
70 }
71
72 bool ASXReader::create(const char* data, size_t size)
73 {
74 int last_start = 0;
75 avm::string last_tag;
76 avm::string last_prop;
77 avm::string last_prop_val;
78 m_Urls.clear();
79
80 //printf("PARSE >%s< %d\n", data, size);
81 asx_position state = BEGIN;
82 for (unsigned i = 0; i < size; i++)
83 {
84 unsigned char c = data[i];
85 if (c < 6) break; // stop processing
86 //printf("i %d %c %d\n", i, c, state);
87 int is_alpha = isalpha(c) || (c >= 0x80);
88 int is_space = isspace(c);
89 switch(state)
90 {
91 case BEGIN:
92 if (c == '<')
93 state = OPEN_BRACKET;
94 else if (is_alpha)
95 {
96 // sometime only single one line redirection appears
97 if (!strncasecmp(data + i, "mms://", 6)
98 || !strncasecmp(data + i, "http://", 7))
99 return addURL(data + i);
100 }
101 continue;
102 case OPEN_BRACKET:
103 if (c == '!')
104 state = IN_COMMENT;
105 else if (c == '/')
106 {
107 state = IN_ENDTAG;
108 last_start = i + 1;
109 }
110 else if (is_alpha)
111 {
112 state = IN_TAGNAME;
113 last_start = i;
114 }
115 else if (!is_space)
116 return false;
117 continue;
118 case IN_COMMENT:
119 if (c == '"')
120 state = IN_QUOTED_COMMENT;
121 else if (c == '>')
122 state = CLOSE_BRACKET;
123 continue;
124 case IN_QUOTED_COMMENT:
125 if (c == '"')
126 state = IN_COMMENT;
127 continue;
128 case CLOSE_BRACKET:
129 if (c == '<')
130 state = OPEN_BRACKET;
131 else
132 state = IN_CONTENTS;
133 continue;
134 case IN_TAGNAME:
135 if (is_space)
136 {
137 last_tag = avm::string(&data[last_start], i-last_start);
138 //printf("TAG1: %s\n", last_tag.c_str());
139 state = AFTER_TAGNAME;
140 }
141 else if ((c == '/') || (c == '>'))
142 {
143 last_tag = avm::string(&data[last_start], i-last_start);
144 //printf("TAG2: %s\n", last_tag.c_str());
145 if (c == '/')
146 state = BEFORE_TAGCLOSE;
147 else
148 state = CLOSE_BRACKET;
149 }
150 continue;
151 case AFTER_TAGNAME:
152 if (c == '/')
153 state = BEFORE_TAGCLOSE;
154 else if (!is_space)
155 {
156 last_start = i;
157 state = IN_PROPNAME;
158 }
159 continue;
160 case IN_ENDTAG:
161 if (c == '>')
162 {
163 last_tag = avm::string(&data[last_start], i-last_start);
164 state = CLOSE_BRACKET;
165 AVM_WRITE("ASX reader", 1, " TAG: %s\n", last_tag.c_str());
166 if (last_tag == "asx")
167 return true;
168 }
169 continue;
170 case BEFORE_TAGCLOSE:
171 if (c == '>')
172 state = CLOSE_BRACKET;
173 else if (!is_space)
174 return false;
175 continue;
176 case IN_PROPNAME:
177 if (c == '=')
178 {
179 last_prop = avm::string(&data[last_start], i-last_start);
180 state = PROP_EQUAL;
181 }
182 else if (is_space)
183 {
184 last_prop = avm::string(&data[last_start], i-last_start);
185 state = AFTER_PROPNAME;
186 }
187 continue;
188 case AFTER_PROPNAME:
189 if (c == '=')
190 state = PROP_EQUAL;
191 continue;
192 case PROP_EQUAL:
193 if (is_space)
194 continue;
195 if (c == '"')
196 state = IN_QUOTED_PROPCONTENTS;
197 else
198 state = IN_PROPCONTENTS;
199 last_start = i;
200 continue;
201 case IN_PROPCONTENTS:
202 if (c == '"')
203 state = IN_QUOTED_PROPCONTENTS;
204 else if (is_space || (c == '/') || (c == '>'))
205 {
206 last_prop_val = avm::string(&data[last_start], i-last_start);
207
208 AVM_WRITE("ASX reader", 1, "VALUE: %s t:%s p:%s\n", last_prop_val.c_str(), last_tag.c_str(), last_prop.c_str());
209 if (last_tag.tolower() == "ref"
210 || last_tag == "entryref"
211 || last_tag == "a"
212 || last_tag == "location"
213 || last_tag == "embed") // part of some javascript
214 if (last_prop.tolower() == "href"
215 || last_prop == "src")
216 addURL(last_prop_val.c_str());
217 if (c == '/')
218 state = BEFORE_TAGCLOSE;
219 else if (c == '>')
220 state = CLOSE_BRACKET;
221 else
222 state = BETWEEN_PROPS;
223 }
224 continue;
225 case IN_QUOTED_PROPCONTENTS:
226 if (c == '"')
227 state = IN_PROPCONTENTS;
228 continue;
229 case BETWEEN_PROPS:
230 if (c == '/')
231 state = BEFORE_TAGCLOSE;
232 else if (c == '>')
233 state = CLOSE_BRACKET;
234 else if (is_alpha)
235 {
236 last_start = i;
237 state = IN_PROPNAME;
238 }
239 else if (!is_space)
240 return false;
241 continue;
242 case IN_CONTENTS:
243 if (c == '"')
244 state = IN_QUOTED_CONTENTS;
245 else if (c == '<')
246 state = OPEN_BRACKET;
247 continue;
248 case IN_QUOTED_CONTENTS:
249 if (c == '"')
250 state = IN_CONTENTS;
251 continue;
252 default: // after_propequal
253 AVM_WRITE("ASX reader", "FIXME ERROR after_propequal not handled\n");
254 break;
255 }
256 }
257 return (m_Urls.size() > 0);
258 }
259
260 bool ASXReader::getURLs(avm::vector<avm::string>& urls) const
261 {
262 //for (unsigned i = 0; i < m_Urls.size(); i++) printf("URLs: %s\n", m_Urls[i].c_str());
263 urls = m_Urls;
264 return true;
265 }
266
267 AVM_END_NAMESPACE;