core, vo: modify OSD redraw architecture, support EOSD
[mplayer.git] / playtreeparser.c
blobaea0241ca4bb559a726627165762389e29d123e3
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /// \file
20 /// \ingroup PlaytreeParser
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <limits.h>
35 #include "talloc.h"
36 #include "asxparser.h"
37 #include "m_config.h"
38 #include "playtree.h"
39 #include "playtreeparser.h"
40 #include "stream/stream.h"
41 #include "libmpdemux/demuxer.h"
42 #include "mp_msg.h"
45 #define BUF_STEP 1024
47 #define WHITES " \n\r\t"
49 static void
50 strstrip(char* str) {
51 char* i;
53 if (str==NULL)
54 return;
55 for(i = str ; i[0] != '\0' && strchr(WHITES,i[0]) != NULL; i++)
56 /* NOTHING */;
57 if(i[0] != '\0') {
58 memmove(str,i,strlen(i) + 1);
59 for(i = str + strlen(str) - 1 ; strchr(WHITES,i[0]) != NULL; i--)
60 /* NOTHING */;
61 i[1] = '\0';
62 } else
63 str[0] = '\0';
66 static char*
67 play_tree_parser_get_line(play_tree_parser_t* p) {
68 char *end,*line_end;
69 int r,resize = 0;
71 if(p->buffer == NULL) {
72 p->buffer = malloc(BUF_STEP);
73 p->buffer_size = BUF_STEP;
74 p->buffer[0] = 0;
75 p->iter = p->buffer;
78 if(p->stream->eof && (p->buffer_end == 0 || p->iter[0] == '\0'))
79 return NULL;
81 assert(p->buffer_end < p->buffer_size);
82 assert(!p->buffer[p->buffer_end]);
83 while(1) {
85 if(resize) {
86 char *tmp;
87 r = p->iter - p->buffer;
88 end = p->buffer + p->buffer_end;
89 if (p->buffer_size > INT_MAX - BUF_STEP)
90 break;
91 tmp = realloc(p->buffer, p->buffer_size + BUF_STEP);
92 if (!tmp)
93 break;
94 p->buffer = tmp;
95 p->iter = p->buffer + r;
96 p->buffer_size += BUF_STEP;
97 resize = 0;
100 if(p->buffer_size - p->buffer_end > 1 && ! p->stream->eof) {
101 r = stream_read(p->stream,p->buffer + p->buffer_end,p->buffer_size - p->buffer_end - 1);
102 if(r > 0) {
103 p->buffer_end += r;
104 assert(p->buffer_end < p->buffer_size);
105 p->buffer[p->buffer_end] = '\0';
106 while(strlen(p->buffer + p->buffer_end - r) != r)
107 p->buffer[p->buffer_end - r + strlen(p->buffer + p->buffer_end - r)] = '\n';
109 assert(!p->buffer[p->buffer_end]);
112 end = strchr(p->iter,'\n');
113 if(!end) {
114 if(p->stream->eof) {
115 end = p->buffer + p->buffer_end;
116 break;
118 resize = 1;
119 continue;
121 break;
124 line_end = (end > p->iter && *(end-1) == '\r') ? end-1 : end;
125 if(line_end - p->iter >= 0)
126 p->line = realloc(p->line, line_end - p->iter + 1);
127 else
128 return NULL;
129 if(line_end - p->iter > 0)
130 strncpy(p->line,p->iter,line_end - p->iter);
131 p->line[line_end - p->iter] = '\0';
132 if(end[0] != '\0')
133 end++;
135 if(!p->keep) {
136 if(end[0] != '\0') {
137 p->buffer_end -= end-p->iter;
138 memmove(p->buffer,end,p->buffer_end);
139 } else
140 p->buffer_end = 0;
141 p->buffer[p->buffer_end] = '\0';
142 p->iter = p->buffer;
143 } else
144 p->iter = end;
146 return p->line;
149 static void
150 play_tree_parser_reset(play_tree_parser_t* p) {
151 p->iter = p->buffer;
154 static void
155 play_tree_parser_stop_keeping(play_tree_parser_t* p) {
156 p->keep = 0;
157 if(p->iter && p->iter != p->buffer) {
158 p->buffer_end -= p->iter -p->buffer;
159 if(p->buffer_end)
160 memmove(p->buffer,p->iter,p->buffer_end);
161 p->buffer[p->buffer_end] = 0;
162 p->iter = p->buffer;
167 static play_tree_t*
168 parse_asx(play_tree_parser_t* p) {
169 int comments = 0,get_line = 1;
170 char* line = NULL;
172 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n");
174 while(1) {
175 if(get_line) {
176 line = play_tree_parser_get_line(p);
177 if(!line)
178 return NULL;
179 strstrip(line);
180 if(line[0] == '\0')
181 continue;
183 if(!comments) {
184 if(line[0] != '<') {
185 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",line[0]);
186 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",p->buffer);
187 return NULL;
188 } else if(strncmp(line,"<!--",4) == 0) { // Comments
189 comments = 1;
190 line += 4;
191 if(line[0] != '\0' && strlen(line) > 0)
192 get_line = 0;
193 } else if(strncasecmp(line,"<ASX",4) == 0) // We got an asx element
194 break;
195 else // We don't get an asx
196 return NULL;
197 } else { // Comments
198 char* c;
199 c = strchr(line,'-');
200 if(c) {
201 if (strncmp(c,"--!>",4) == 0) { // End of comments
202 comments = 0;
203 line = c+4;
204 if(line[0] != '\0') // There is some more data on this line : keep it
205 get_line = 0;
207 } else {
208 line = c+1; // Jump the -
209 if(line[0] != '\0') // Some more data
210 get_line = 0;
211 else // End of line
212 get_line = 1;
214 } else // No - on this line (or rest of line) : get next one
215 get_line = 1;
219 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n");
221 // We have an asx : load it in memory and parse
223 while((line = play_tree_parser_get_line(p)) != NULL)
224 /* NOTHING */;
226 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Parsing asx file: [%s]\n",p->buffer);
227 return asx_parser_build_tree(p->mconfig, p->buffer,p->deep);
230 static char*
231 pls_entry_get_value(char* line) {
232 char* i;
234 i = strchr(line,'=');
235 if(!i || i[1] == '\0')
236 return NULL;
237 else
238 return i+1;
241 typedef struct pls_entry {
242 char* file;
243 char* title;
244 char* length;
245 } pls_entry_t;
247 static int
248 pls_read_entry(char* line,pls_entry_t** _e,int* _max_entry,char** val) {
249 int num,max_entry = (*_max_entry);
250 pls_entry_t* e = (*_e);
251 int limit = INT_MAX / sizeof(*e);
252 char* v;
254 v = pls_entry_get_value(line);
255 if(!v) {
256 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
257 return -1;
260 num = atoi(line);
261 if(num <= 0 || num > limit) {
262 if (max_entry >= limit) {
263 mp_msg(MSGT_PLAYTREE, MSGL_WARN, "Too many index entries\n");
264 return -1;
266 num = max_entry+1;
267 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"No or invalid entry index in entry %s\nAssuming %d\n",line,num);
269 if(num > max_entry) {
270 e = realloc(e, num * sizeof(pls_entry_t));
271 if (!e)
272 return -1;
273 memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
274 max_entry = num;
276 (*_e) = e;
277 (*_max_entry) = max_entry;
278 (*val) = v;
280 return num;
284 static play_tree_t*
285 parse_pls(play_tree_parser_t* p) {
286 char *line,*v;
287 pls_entry_t* entries = NULL;
288 int n_entries = 0,max_entry=0,num;
289 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
291 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying Winamp playlist...\n");
292 while((line = play_tree_parser_get_line(p))) {
293 strstrip(line);
294 if(strlen(line))
295 break;
297 if (!line)
298 return NULL;
299 if(strcasecmp(line,"[playlist]"))
300 return NULL;
301 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected Winamp playlist format\n");
302 play_tree_parser_stop_keeping(p);
303 line = play_tree_parser_get_line(p);
304 if(!line)
305 return NULL;
306 strstrip(line);
307 if(strncasecmp(line,"NumberOfEntries",15) == 0) {
308 v = pls_entry_get_value(line);
309 n_entries = atoi(v);
310 if(n_entries < 0)
311 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Invalid number of entries: very funny!!!\n");
312 else
313 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Playlist claims to have %d entries. Let's see.\n",n_entries);
314 line = play_tree_parser_get_line(p);
317 while(line) {
318 strstrip(line);
319 if(line[0] == '\0') {
320 line = play_tree_parser_get_line(p);
321 continue;
323 if(strncasecmp(line,"File",4) == 0) {
324 num = pls_read_entry(line+4,&entries,&max_entry,&v);
325 if(num < 0)
326 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
327 else
328 entries[num-1].file = strdup(v);
329 } else if(strncasecmp(line,"Title",5) == 0) {
330 num = pls_read_entry(line+5,&entries,&max_entry,&v);
331 if(num < 0)
332 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
333 else
334 entries[num-1].title = strdup(v);
335 } else if(strncasecmp(line,"Length",6) == 0) {
336 num = pls_read_entry(line+6,&entries,&max_entry,&v);
337 if(num < 0)
338 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
339 else {
340 char *end;
341 long val = strtol(v, &end, 10);
342 if (*end || (val <= 0 && val != -1))
343 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Invalid length value in entry %s\n",line);
344 else if (val > 0)
345 entries[num-1].length = strdup(v);
347 } else
348 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Unknown entry type %s\n",line);
349 line = play_tree_parser_get_line(p);
352 for(num = 0; num < max_entry ; num++) {
353 if(entries[num].file == NULL)
354 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Entry %d don't have a file !!!!\n",num+1);
355 else {
356 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",entries[num].file);
357 entry = play_tree_new();
358 play_tree_add_file(entry,entries[num].file);
359 if (entries[num].length)
360 play_tree_set_param(entry, bstr("endpos"), bstr(entries[num].length));
361 free(entries[num].file);
362 if(list)
363 play_tree_append_entry(last_entry,entry);
364 else
365 list = entry;
366 last_entry = entry;
368 // When we have info in playtree we add these info
369 free(entries[num].title);
370 free(entries[num].length);
373 free(entries);
375 if (!list)
376 return NULL;
378 entry = play_tree_new();
379 play_tree_set_child(entry,list);
380 return entry;
384 Reference Ini-Format: Each entry is assumed a reference
386 static play_tree_t*
387 parse_ref_ini(play_tree_parser_t* p) {
388 char *line,*v;
389 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
391 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying reference-ini playlist...\n");
392 if (!(line = play_tree_parser_get_line(p)))
393 return NULL;
394 strstrip(line);
395 if(strcasecmp(line,"[Reference]"))
396 return NULL;
397 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected reference-ini playlist format\n");
398 play_tree_parser_stop_keeping(p);
399 line = play_tree_parser_get_line(p);
400 if(!line)
401 return NULL;
402 while(line) {
403 strstrip(line);
404 if(strncasecmp(line,"Ref",3) == 0) {
405 v = pls_entry_get_value(line+3);
406 if(!v)
407 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
408 else
410 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",v);
411 entry = play_tree_new();
412 play_tree_add_file(entry,v);
413 if(list)
414 play_tree_append_entry(last_entry,entry);
415 else
416 list = entry;
417 last_entry = entry;
420 line = play_tree_parser_get_line(p);
423 if(!list) return NULL;
424 entry = play_tree_new();
425 play_tree_set_child(entry,list);
426 return entry;
429 static play_tree_t*
430 parse_m3u(play_tree_parser_t* p) {
431 char* line;
432 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
434 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying extended m3u playlist...\n");
435 if (!(line = play_tree_parser_get_line(p)))
436 return NULL;
437 strstrip(line);
438 if(strcasecmp(line,"#EXTM3U"))
439 return NULL;
440 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected extended m3u playlist format\n");
441 play_tree_parser_stop_keeping(p);
443 while((line = play_tree_parser_get_line(p)) != NULL) {
444 strstrip(line);
445 if(line[0] == '\0')
446 continue;
447 /* EXTM3U files contain such lines:
448 * #EXTINF:<seconds>, <title>
449 * followed by a line with the filename
450 * for now we have no place to put that
451 * so we just skip that extra-info ::atmos
453 if(line[0] == '#') {
454 #if 0 /* code functional */
455 if(strncasecmp(line,"#EXTINF:",8) == 0) {
456 mp_msg(MSGT_PLAYTREE,MSGL_INFO,"[M3U] Duration: %dsec Title: %s\n",
457 strtol(line+8,&line,10), line+2);
459 #endif
460 continue;
462 entry = play_tree_new();
463 play_tree_add_file(entry,line);
464 if(!list)
465 list = entry;
466 else
467 play_tree_append_entry(last_entry,entry);
468 last_entry = entry;
471 if(!list) return NULL;
472 entry = play_tree_new();
473 play_tree_set_child(entry,list);
474 return entry;
477 static play_tree_t*
478 parse_smil(play_tree_parser_t* p) {
479 int entrymode=0;
480 char* line,source[512],*pos,*s_start,*s_end,*src_line;
481 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
482 int is_rmsmil = 0;
483 unsigned int npkt, ttlpkt;
485 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying smil playlist...\n");
487 // Check if smil
488 while((line = play_tree_parser_get_line(p)) != NULL) {
489 strstrip(line);
490 if(line[0] == '\0') // Ignore empties
491 continue;
492 if (strncasecmp(line,"<?xml",5)==0) // smil in xml
493 continue;
494 if (strncasecmp(line,"<!DOCTYPE smil",13)==0) // smil in xml
495 continue;
496 if (strncasecmp(line,"<smil",5)==0 || strncasecmp(line,"<?wpl",5)==0 ||
497 strncasecmp(line,"(smil-document",14)==0)
498 break; // smil header found
499 else
500 return NULL; //line not smil exit
503 if (!line) return NULL;
504 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected smil playlist format\n");
505 play_tree_parser_stop_keeping(p);
507 if (strncasecmp(line,"(smil-document",14)==0) {
508 mp_msg(MSGT_PLAYTREE,MSGL_V,"Special smil-over-realrtsp playlist header\n");
509 is_rmsmil = 1;
510 if (sscanf(line, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt, &ttlpkt) != 2) {
511 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
512 npkt = ttlpkt = 1;
514 if (ttlpkt == 0 || npkt > ttlpkt) {
515 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
516 npkt, ttlpkt);
517 npkt = ttlpkt = 1;
521 //Get entries from smil
522 src_line = line;
523 line = NULL;
524 do {
525 strstrip(src_line);
526 free(line);
527 line = NULL;
528 /* If we're parsing smil over realrtsp and this is not the last packet and
529 * this is the last line in the packet (terminating with ") ) we must get
530 * the next line, strip the header, and concatenate it to the current line.
532 if (is_rmsmil && npkt != ttlpkt && strstr(src_line,"\")")) {
533 char *payload;
535 line = strdup(src_line);
536 if(!(src_line = play_tree_parser_get_line(p))) {
537 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt, ttlpkt);
538 break;
540 strstrip(src_line);
541 // Skip header, packet starts after "
542 if(!(payload = strchr(src_line,'\"'))) {
543 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
544 payload = src_line;
545 } else
546 payload++;
547 // Skip ") at the end of the last line from the current packet
548 line[strlen(line)-2] = 0;
549 line = realloc(line, strlen(line)+strlen(payload)+1);
550 strcat (line, payload);
551 npkt++;
552 } else
553 line = strdup(src_line);
554 /* Unescape \" to " for smil-over-rtsp */
555 if (is_rmsmil && line[0] != '\0') {
556 int i, j;
558 for (i = 0; i < strlen(line); i++)
559 if (line[i] == '\\' && line[i+1] == '"')
560 for (j = i; line[j]; j++)
561 line[j] = line[j+1];
563 pos = line;
564 while (pos) {
565 if (!entrymode) { // all entries filled so far
566 while ((pos=strchr(pos, '<'))) {
567 if (strncasecmp(pos,"<video",6)==0 || strncasecmp(pos,"<audio",6)==0 || strncasecmp(pos,"<media",6)==0) {
568 entrymode=1;
569 break; // Got a valid tag, exit '<' search loop
571 pos++;
574 if (entrymode) { //Entry found but not yet filled
575 pos = strstr(pos,"src="); // Is source present on this line
576 if (pos != NULL) {
577 entrymode=0;
578 if (pos[4] != '"' && pos[4] != '\'') {
579 mp_msg(MSGT_PLAYTREE,MSGL_V,"Unknown delimiter %c in source line %s\n", pos[4], line);
580 break;
582 s_start=pos+5;
583 s_end=strchr(s_start,pos[4]);
584 if (s_end == NULL) {
585 mp_msg(MSGT_PLAYTREE,MSGL_V,"Error parsing this source line %s\n",line);
586 break;
588 if (s_end-s_start> 511) {
589 mp_msg(MSGT_PLAYTREE,MSGL_V,"Cannot store such a large source %s\n",line);
590 break;
592 strncpy(source,s_start,s_end-s_start);
593 source[(s_end-s_start)]='\0'; // Null terminate
594 entry = play_tree_new();
595 play_tree_add_file(entry,source);
596 if(!list) //Insert new entry
597 list = entry;
598 else
599 play_tree_append_entry(last_entry,entry);
600 last_entry = entry;
601 pos = s_end;
605 } while((src_line = play_tree_parser_get_line(p)) != NULL);
607 free(line);
609 if(!list) return NULL; // Nothing found
611 entry = play_tree_new();
612 play_tree_set_child(entry,list);
613 return entry;
616 static play_tree_t*
617 embedded_playlist_parse(struct m_config *mconfig, char *line) {
618 int f=DEMUXER_TYPE_PLAYLIST;
619 stream_t* stream;
620 play_tree_parser_t* ptp;
621 play_tree_t* entry;
623 // Get stream opened to link
624 stream=open_stream(line,0,&f);
625 if(!stream) {
626 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Can't open playlist %s\n",line);
627 return NULL;
630 //add new playtree
631 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding playlist %s to element entryref\n",line);
633 ptp = play_tree_parser_new(stream, mconfig, 1);
634 entry = play_tree_parser_get_play_tree(ptp, 1);
635 play_tree_parser_free(ptp);
636 free_stream(stream);
638 return entry;
641 static play_tree_t*
642 parse_textplain(play_tree_parser_t* p) {
643 char* line;
644 char *c;
645 int embedded;
646 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
648 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext playlist...\n");
649 play_tree_parser_stop_keeping(p);
651 while((line = play_tree_parser_get_line(p)) != NULL) {
652 strstrip(line);
653 if(line[0] == '\0' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
654 continue;
656 //Special check for embedded smil or ram reference in file
657 embedded = 0;
658 if (strlen(line) > 5)
659 for(c = line; c[0]; c++ )
660 if ( ((c[0] == '.') && //start with . and next have smil with optional ? or &
661 (tolower(c[1]) == 's') && (tolower(c[2])== 'm') &&
662 (tolower(c[3]) == 'i') && (tolower(c[4]) == 'l') &&
663 (!c[5] || c[5] == '?' || c[5] == '&')) || // or
664 ((c[0] == '.') && // start with . and next have smi or ram with optional ? or &
665 ( ((tolower(c[1]) == 's') && (tolower(c[2])== 'm') && (tolower(c[3]) == 'i')) ||
666 ((tolower(c[1]) == 'r') && (tolower(c[2])== 'a') && (tolower(c[3]) == 'm')) )
667 && (!c[4] || c[4] == '?' || c[4] == '&')) ){
668 entry=embedded_playlist_parse(p->mconfig, line);
669 embedded = 1;
670 break;
673 if (!embedded) { //regular file link
674 entry = play_tree_new();
675 play_tree_add_file(entry,line);
678 if (entry != NULL) {
679 if(!list)
680 list = entry;
681 else
682 play_tree_append_entry(last_entry,entry);
683 last_entry = entry;
687 if(!list) return NULL;
688 entry = play_tree_new();
689 play_tree_set_child(entry,list);
690 return entry;
694 * \brief decode the base64 used in nsc files
695 * \param in input string, 0-terminated
696 * \param buf output buffer, must point to memory suitable for realloc,
697 * will be NULL on failure.
698 * \return decoded length in bytes
700 static int decode_nsc_base64(char *in, char **buf) {
701 int i, j, n;
702 if (in[0] != '0' || in[1] != '2')
703 goto err_out;
704 in += 2; // skip prefix
705 if (strlen(in) < 16) // error out if nothing to decode
706 goto err_out;
707 in += 12; // skip encoded string length
708 n = strlen(in) / 4;
709 *buf = realloc(*buf, n * 3);
710 for (i = 0; i < n; i++) {
711 uint8_t c[4];
712 for (j = 0; j < 4; j++) {
713 c[j] = in[4 * i + j];
714 if (c[j] >= '0' && c[j] <= '9') c[j] += 0 - '0';
715 else if (c[j] >= 'A' && c[j] <= 'Z') c[j] += 10 - 'A';
716 else if (c[j] >= 'a' && c[j] <= 'z') c[j] += 36 - 'a';
717 else if (c[j] == '{') c[j] = 62;
718 else if (c[j] == '}') c[j] = 63;
719 else {
720 mp_msg(MSGT_PLAYTREE, MSGL_ERR, "Invalid character %c (0x%02"PRIx8")\n", c[j], c[j]);
721 goto err_out;
724 (*buf)[3 * i] = (c[0] << 2) | (c[1] >> 4);
725 (*buf)[3 * i + 1] = (c[1] << 4) | (c[2] >> 2);
726 (*buf)[3 * i + 2] = (c[2] << 6) | c[3];
728 return 3 * n;
729 err_out:
730 free(*buf);
731 *buf = NULL;
732 return 0;
736 * \brief "converts" utf16 to ascii by just discarding every second byte
737 * \param buf buffer to convert
738 * \param len lenght of buffer, must be > 0
740 static void utf16_to_ascii(char *buf, int len) {
741 int i;
742 if (len <= 0) return;
743 for (i = 0; i < len / 2; i++)
744 buf[i] = buf[i * 2];
745 buf[i] = 0; // just in case
748 static play_tree_t *parse_nsc(play_tree_parser_t* p) {
749 char *line, *addr = NULL, *url, *unicast_url = NULL;
750 int port = 0;
751 play_tree_t *entry = NULL;
753 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying nsc playlist...\n");
754 while((line = play_tree_parser_get_line(p)) != NULL) {
755 strstrip(line);
756 if(!line[0]) // Ignore empties
757 continue;
758 if (strncasecmp(line,"[Address]", 9) == 0)
759 break; // nsc header found
760 else
761 return NULL;
763 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected nsc playlist format\n");
764 play_tree_parser_stop_keeping(p);
765 while ((line = play_tree_parser_get_line(p)) != NULL) {
766 strstrip(line);
767 if (!line[0])
768 continue;
769 if (strncasecmp(line, "Unicast URL=", 12) == 0) {
770 int len = decode_nsc_base64(&line[12], &unicast_url);
771 if (len <= 0)
772 mp_msg(MSGT_PLAYTREE, MSGL_WARN, "[nsc] Unsupported Unicast URL encoding\n");
773 else
774 utf16_to_ascii(unicast_url, len);
775 } else if (strncasecmp(line, "IP Address=", 11) == 0) {
776 int len = decode_nsc_base64(&line[11], &addr);
777 if (len <= 0)
778 mp_msg(MSGT_PLAYTREE, MSGL_WARN, "[nsc] Unsupported IP Address encoding\n");
779 else
780 utf16_to_ascii(addr, len);
781 } else if (strncasecmp(line, "IP Port=", 8) == 0) {
782 port = strtol(&line[8], NULL, 0);
786 if (unicast_url)
787 url = strdup(unicast_url);
788 else if (addr && port) {
789 url = malloc(strlen(addr) + 7 + 20 + 1);
790 sprintf(url, "http://%s:%i", addr, port);
791 } else
792 goto out;
794 entry = play_tree_new();
795 play_tree_add_file(entry, url);
796 free(url);
797 out:
798 free(addr);
799 free(unicast_url);
800 return entry;
803 play_tree_t*
804 parse_playtree(stream_t *stream, struct m_config *mconfig, int forced) {
805 play_tree_parser_t* p;
806 play_tree_t* ret;
808 assert(stream != NULL);
810 p = play_tree_parser_new(stream, mconfig, 0);
811 if(!p)
812 return NULL;
814 ret = play_tree_parser_get_play_tree(p, forced);
815 play_tree_parser_free(p);
817 return ret;
820 static void
821 play_tree_add_basepath(play_tree_t* pt, char* bp) {
822 int i,bl = strlen(bp),fl;
824 if(pt->child) {
825 play_tree_t* i;
826 for(i = pt->child ; i != NULL ; i = i->next)
827 play_tree_add_basepath(i,bp);
828 return;
831 if(!pt->files)
832 return;
834 for(i = 0 ; pt->files[i] != NULL ; i++) {
835 fl = strlen(pt->files[i]);
836 // if we find a full unix path, url:// or X:\ at the beginning,
837 // don't mangle it.
838 if(fl <= 0 || strstr(pt->files[i],"://") || (strstr(pt->files[i],":\\") == pt->files[i] + 1) || (pt->files[i][0] == '/') )
839 continue;
840 // if the path begins with \ then prepend drive letter to it.
841 if (pt->files[i][0] == '\\') {
842 if (pt->files[i][1] == '\\')
843 continue;
844 pt->files[i] = realloc(pt->files[i], 2 + fl + 1);
845 memmove(pt->files[i] + 2,pt->files[i],fl+1);
846 memcpy(pt->files[i],bp,2);
847 continue;
849 pt->files[i] = realloc(pt->files[i], bl + fl + 1);
850 memmove(pt->files[i] + bl,pt->files[i],fl+1);
851 memcpy(pt->files[i],bp,bl);
855 // Wrapper for play_tree_add_basepath (add base path from file)
856 void play_tree_add_bpf(play_tree_t *pt, struct bstr filename)
858 char *ls, *file;
860 if (pt)
862 file = bstrdup0(NULL, filename);
863 if (file)
865 ls = strrchr(file,'/');
866 if(!ls) ls = strrchr(file,'\\');
867 if(ls) {
868 ls[1] = '\0';
869 play_tree_add_basepath(pt,file);
871 talloc_free(file);
876 play_tree_t*
877 parse_playlist_file(struct m_config *mconfig, struct bstr file) {
878 stream_t *stream;
879 play_tree_t* ret;
880 int f=DEMUXER_TYPE_PLAYLIST;
882 char *file0 = bstrdup0(NULL, file);
883 stream = open_stream(file0, 0, &f);
884 talloc_free(file0);
886 if(!stream) {
887 mp_msg(MSGT_PLAYTREE,MSGL_ERR,
888 "Error while opening playlist file %.*s: %s\n",
889 BSTR_P(file), strerror(errno));
890 return NULL;
893 mp_msg(MSGT_PLAYTREE, MSGL_V,
894 "Parsing playlist file %.*s...\n", BSTR_P(file));
896 ret = parse_playtree(stream, mconfig, 1);
897 free_stream(stream);
899 play_tree_add_bpf(ret, file);
901 return ret;
906 play_tree_parser_t*
907 play_tree_parser_new(stream_t* stream, struct m_config *mconfig, int deep) {
908 play_tree_parser_t* p;
910 p = calloc(1,sizeof(play_tree_parser_t));
911 if(!p)
912 return NULL;
913 p->stream = stream;
914 p->mconfig = mconfig;
915 p->deep = deep;
916 p->keep = 1;
918 return p;
922 void
923 play_tree_parser_free(play_tree_parser_t* p) {
925 assert(p != NULL);
927 free(p->buffer);
928 free(p->line);
929 free(p);
932 play_tree_t*
933 play_tree_parser_get_play_tree(play_tree_parser_t* p, int forced) {
934 play_tree_t* tree = NULL;
936 assert(p != NULL);
938 while(play_tree_parser_get_line(p) != NULL) {
939 play_tree_parser_reset(p);
941 tree = parse_asx(p);
942 if(tree) break;
943 play_tree_parser_reset(p);
945 tree = parse_pls(p);
946 if(tree) break;
947 play_tree_parser_reset(p);
949 tree = parse_m3u(p);
950 if(tree) break;
951 play_tree_parser_reset(p);
953 tree = parse_ref_ini(p);
954 if(tree) break;
955 play_tree_parser_reset(p);
957 tree = parse_smil(p);
958 if(tree) break;
959 play_tree_parser_reset(p);
961 tree = parse_nsc(p);
962 if(tree) break;
963 play_tree_parser_reset(p);
965 // Here come the others formats ( textplain must stay the last one )
966 if (forced)
968 tree = parse_textplain(p);
969 if(tree) break;
971 break;
974 if(tree)
975 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist successfully parsed\n");
976 else
977 mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_ERR:MSGL_V),"Error while parsing playlist\n");
979 if(tree)
980 tree = play_tree_cleanup(tree);
982 if(!tree) mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_WARN:MSGL_V),"Warning: empty playlist\n");
984 return tree;