summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/asxparser.c571
-rw-r--r--common/asxparser.h28
-rw-r--r--common/playlist.c41
-rw-r--r--common/playlist.h3
-rw-r--r--common/playlist_parser.c552
-rw-r--r--common/playlist_parser.h27
6 files changed, 44 insertions, 1178 deletions
diff --git a/common/asxparser.c b/common/asxparser.c
deleted file mode 100644
index 031fdb7572..0000000000
--- a/common/asxparser.c
+++ /dev/null
@@ -1,571 +0,0 @@
-/*
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include "config.h"
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdarg.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "playlist.h"
-#include "playlist_parser.h"
-#include "stream/stream.h"
-#include "asxparser.h"
-#include "common/msg.h"
-
-
-typedef struct ASX_Parser_t ASX_Parser_t;
-
-typedef struct {
- char* buffer;
- int line;
-} ASX_LineSave_t;
-
-struct ASX_Parser_t {
- int line; // Curent line
- ASX_LineSave_t *ret_stack;
- int ret_stack_size;
- char* last_body;
- int deep;
- struct playlist *pl;
- struct mp_log *log;
-};
-
-ASX_Parser_t *asx_parser_new(struct playlist *pl);
-
-void
-asx_parser_free(ASX_Parser_t* parser);
-
-/*
- * Return -1 on error, 0 when nothing is found, 1 on sucess
- */
-int
-asx_get_element(ASX_Parser_t* parser,char** _buffer,
- char** _element,char** _body,char*** _attribs);
-
-int
-asx_parse_attribs(ASX_Parser_t* parser,char* buffer,char*** _attribs);
-
-/////// Attribs utils
-
-char*
-asx_get_attrib(const char* attrib,char** attribs);
-
-#define asx_free_attribs(a) asx_list_free(&a,free)
-
-////// List utils
-
-typedef void (*ASX_FreeFunc)(void* arg);
-
-void
-asx_list_free(void* list_ptr,ASX_FreeFunc free_func);
-
-
-////// List utils
-
-void
-asx_list_free(void* list_ptr,ASX_FreeFunc free_func) {
- void** ptr = *(void***)list_ptr;
- if(ptr == NULL) return;
- if(free_func != NULL) {
- for( ; *ptr != NULL ; ptr++)
- free_func(*ptr);
- }
- free(*(void**)list_ptr);
- *(void**)list_ptr = NULL;
-}
-
-/////// Attribs utils
-
-char*
-asx_get_attrib(const char* attrib,char** attribs) {
- char** ptr;
-
- if(attrib == NULL || attribs == NULL) return NULL;
- for(ptr = attribs; ptr[0] != NULL; ptr += 2){
- if(strcasecmp(ptr[0],attrib) == 0)
- return strdup(ptr[1]);
- }
- return NULL;
-}
-
-#define asx_warning_attrib_required(p,e,a) MP_WARN(parser, "At line %d : element %s don't have the required attribute %s",p->line,e,a)
-#define asx_warning_body_parse_error(p,e) MP_WARN(parser, "At line %d : error while parsing %s body",p->line,e)
-
-ASX_Parser_t *asx_parser_new(struct playlist *pl)
-{
- ASX_Parser_t* parser = calloc(1,sizeof(ASX_Parser_t));
- parser->pl = pl;
- return parser;
-}
-
-void
-asx_parser_free(ASX_Parser_t* parser) {
- if(!parser) return;
- free(parser->ret_stack);
- free(parser);
-
-}
-
-#define LETTER "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
-#define SPACE " \n\t\r"
-
-int
-asx_parse_attribs(ASX_Parser_t* parser,char* buffer,char*** _attribs) {
- char *ptr1, *ptr2, *ptr3;
- int n_attrib = 0;
- char **attribs = NULL;
- char *attrib, *val;
-
- ptr1 = buffer;
- while(1) {
- for( ; strchr(SPACE,*ptr1) != NULL; ptr1++) { // Skip space
- if(*ptr1 == '\0') break;
- }
- ptr3 = strchr(ptr1,'=');
- if(ptr3 == NULL) break;
- for(ptr2 = ptr3-1; strchr(SPACE,*ptr2) != NULL; ptr2--) {
- if (ptr2 == ptr1) {
- MP_ERR(parser, "At line %d : this should never append, back to attribute begin while skipping end space",parser->line);
- break;
- }
- }
- attrib = malloc(ptr2-ptr1+2);
- strncpy(attrib,ptr1,ptr2-ptr1+1);
- attrib[ptr2-ptr1+1] = '\0';
-
- ptr1 = strchr(ptr3,'"');
- if(ptr1 == NULL || ptr1[1] == '\0') ptr1 = strchr(ptr3,'\'');
- if(ptr1 == NULL || ptr1[1] == '\0') {
- MP_WARN(parser, "At line %d : can't find attribute %s value",parser->line,attrib);
- free(attrib);
- break;
- }
- ptr2 = strchr(ptr1+1,ptr1[0]);
- if (ptr2 == NULL) {
- MP_WARN(parser, "At line %d : value of attribute %s isn't finished",parser->line,attrib);
- free(attrib);
- break;
- }
- ptr1++;
- val = malloc(ptr2-ptr1+1);
- strncpy(val,ptr1,ptr2-ptr1);
- val[ptr2-ptr1] = '\0';
- n_attrib++;
-
- attribs = realloc(attribs, (2 * n_attrib + 1) * sizeof(char*));
- attribs[n_attrib*2-2] = attrib;
- attribs[n_attrib*2-1] = val;
-
- ptr1 = ptr2+1;
- }
-
- if(n_attrib > 0)
- attribs[n_attrib*2] = NULL;
-
- *_attribs = attribs;
-
- return n_attrib;
-}
-
-/*
- * Return -1 on error, 0 when nothing is found, 1 on sucess
- */
-int
-asx_get_element(ASX_Parser_t* parser,char** _buffer,
- char** _element,char** _body,char*** _attribs) {
- char *ptr1,*ptr2, *ptr3, *ptr4;
- char *attribs = NULL;
- char *element = NULL, *body = NULL, *ret = NULL, *buffer;
- int n_attrib = 0;
- int body_line = 0,attrib_line,ret_line,in = 0;
- int quotes = 0;
-
- if(_buffer == NULL || _element == NULL || _body == NULL || _attribs == NULL) {
- MP_ERR(parser, "At line %d : asx_get_element called with invalid value",parser->line);
- return -1;
- }
-
- *_body = *_element = NULL;
- *_attribs = NULL;
- buffer = *_buffer;
-
- if(buffer == NULL) return 0;
-
- if(parser->ret_stack && /*parser->last_body && */buffer != parser->last_body) {
- ASX_LineSave_t* ls = parser->ret_stack;
- int i;
- for(i = 0 ; i < parser->ret_stack_size ; i++) {
- if(buffer == ls[i].buffer) {
- parser->line = ls[i].line;
- break;
- }
-
- }
- if( i < parser->ret_stack_size) {
- i++;
- if( i < parser->ret_stack_size)
- memmove(parser->ret_stack,parser->ret_stack+i, (parser->ret_stack_size - i)*sizeof(ASX_LineSave_t));
- parser->ret_stack_size -= i;
- if(parser->ret_stack_size > 0)
- parser->ret_stack = realloc(parser->ret_stack,parser->ret_stack_size*sizeof(ASX_LineSave_t));
- else {
- free(parser->ret_stack);
- parser->ret_stack = NULL;
- }
- }
- }
-
- ptr1 = buffer;
- while(1) {
- for( ; ptr1[0] != '<' ; ptr1++) {
- if(ptr1[0] == '\0') {
- ptr1 = NULL;
- break;
- }
- if(ptr1[0] == '\n') parser->line++;
- }
- //ptr1 = strchr(ptr1,'<');
- if(!ptr1 || ptr1[1] == '\0') return 0; // Nothing found
-
- if(strncmp(ptr1,"<!--",4) == 0) { // Comments
- for( ; strncmp(ptr1,"-->",3) != 0 ; ptr1++) {
- if(ptr1[0] == '\0') {
- ptr1 = NULL;
- break;
- }
- if(ptr1[0] == '\n') parser->line++;
- }
- //ptr1 = strstr(ptr1,"-->");
- if(!ptr1) {
- MP_ERR(parser, "At line %d : unfinished comment",parser->line);
- return -1;
- }
- } else {
- break;
- }
- }
-
- // Is this space skip very useful ??
- for(ptr1++; strchr(SPACE,ptr1[0]) != NULL; ptr1++) { // Skip space
- if(ptr1[0] == '\0') {
- MP_ERR(parser, "At line %d : EOB reached while parsing element start",parser->line);
- return -1;
- }
- if(ptr1[0] == '\n') parser->line++;
- }
-
- for(ptr2 = ptr1; strchr(LETTER,*ptr2) != NULL;ptr2++) { // Go to end of name
- if(*ptr2 == '\0'){
- MP_ERR(parser, "At line %d : EOB reached while parsing element start",parser->line);
- return -1;
- }
- if(ptr2[0] == '\n') parser->line++;
- }
-
- element = malloc(ptr2-ptr1+1);
- strncpy(element,ptr1,ptr2-ptr1);
- element[ptr2-ptr1] = '\0';
-
- for( ; strchr(SPACE,*ptr2) != NULL; ptr2++) { // Skip space
- if(ptr2[0] == '\0') {
- MP_ERR(parser, "At line %d : EOB reached while parsing element start",parser->line);
- free(element);
- return -1;
- }
- if(ptr2[0] == '\n') parser->line++;
- }
- attrib_line = parser->line;
-
-
-
- for(ptr3 = ptr2; ptr3[0] != '\0'; ptr3++) { // Go to element end
- if(ptr3[0] == '"') quotes ^= 1;
- if(!quotes && (ptr3[0] == '>' || strncmp(ptr3,"/>",2) == 0))
- break;
- if(ptr3[0] == '\n') parser->line++;
- }
- if(ptr3[0] == '\0' || ptr3[1] == '\0') { // End of file
- MP_ERR(parser, "At line %d : EOB reached while parsing element start",parser->line);
- free(element);
- return -1;
- }
-
- // Save attribs string
- if(ptr3-ptr2 > 0) {
- attribs = malloc(ptr3-ptr2+1);
- strncpy(attribs,ptr2,ptr3-ptr2);
- attribs[ptr3-ptr2] = '\0';
- }
- //bs_line = parser->line;
- if(ptr3[0] != '/') { // Not Self closed element
- ptr3++;
- for( ; strchr(SPACE,*ptr3) != NULL; ptr3++) { // Skip space on body begin
- if(*ptr3 == '\0') {
- MP_ERR(parser, "At line %d : EOB reached while parsing %s element body",parser->line,element);
- free(element);
- free(attribs);
- return -1;
- }
- if(ptr3[0] == '\n') parser->line++;
- }
- ptr4 = ptr3;
- body_line = parser->line;
- while(1) { // Find closing element
- for( ; ptr4[0] != '<' ; ptr4++) {
- if(ptr4[0] == '\0') {
- ptr4 = NULL;
- break;
- }
- if(ptr4[0] == '\n') parser->line++;
- }
- if(ptr4 && strncmp(ptr4,"<!--",4) == 0) { // Comments
- for( ; strncmp(ptr4,"-->",3) != 0 ; ptr4++) {
- if(ptr4[0] == '\0') {
- ptr4 = NULL;
- break;
- }
- if(ptr1[0] == '\n') parser->line++;
- }
- continue;
- }
- if(ptr4 == NULL || ptr4[1] == '\0') {
- MP_ERR(parser, "At line %d : EOB reached while parsing %s element body",parser->line,element);
- free(element);
- free(attribs);
- return -1;
- }
- if(ptr4[1] != '/' && strncasecmp(element,ptr4+1,strlen(element)) == 0) {
- in++;
- ptr4+=2;
- continue;
- } else if(strncasecmp(element,ptr4+2,strlen(element)) == 0) { // Extract body
- if(in > 0) {
- in--;
- ptr4 += 2+strlen(element);
- continue;
- }
- ret = ptr4+strlen(element)+3;
- if(ptr4 != ptr3) {
- ptr4--;
- for( ; ptr4 != ptr3 && strchr(SPACE,*ptr4) != NULL; ptr4--) ;// Skip space on body end
- // if(ptr4[0] == '\0') parser->line--;
- //}
- ptr4++;
- body = malloc(ptr4-ptr3+1);
- strncpy(body,ptr3,ptr4-ptr3);
- body[ptr4-ptr3] = '\0';
- }
- break;
- } else {
- ptr4 += 2;
- }
- }
- } else {
- ret = ptr3 + 2; // 2 is for />
- }
-
- for( ; ret[0] != '\0' && strchr(SPACE,ret[0]) != NULL; ret++) { // Skip space
- if(ret[0] == '\n') parser->line++;
- }
-
- ret_line = parser->line;
-
- if(attribs) {
- parser->line = attrib_line;
- n_attrib = asx_parse_attribs(parser,attribs,_attribs);
- free(attribs);
- if(n_attrib < 0) {
- MP_WARN(parser, "At line %d : error while parsing element %s attributes",parser->line,element);
- free(element);
- free(body);
- return -1;
- }
- } else
- *_attribs = NULL;
-
- *_element = element;
- *_body = body;
-
- parser->last_body = body;
- parser->ret_stack_size++;
- parser->ret_stack = realloc(parser->ret_stack,parser->ret_stack_size*sizeof(ASX_LineSave_t));
- if(parser->ret_stack_size > 1)
- memmove(parser->ret_stack+1,parser->ret_stack,(parser->ret_stack_size-1)*sizeof(ASX_LineSave_t));
- parser->ret_stack[0].buffer = ret;
- parser->ret_stack[0].line = ret_line;
- parser->line = body ? body_line : ret_line;
-
- *_buffer = ret;
- return 1;
-
-}
-
-static void
-asx_parse_ref(ASX_Parser_t* parser, char** attribs) {
- char *href;
-
- href = asx_get_attrib("HREF",attribs);
- if(href == NULL) {
- asx_warning_attrib_required(parser,"REF" ,"HREF" );
- return;
- }
-
- playlist_add_file(parser->pl, href);
-
- MP_VERBOSE(parser, "Adding file %s to element entry\n",href);
-
- free(href);
-
-}
-
-static void asx_parse_entryref(ASX_Parser_t* parser,char* buffer,char** _attribs) {
- char *href;
-
- if(parser->deep > 0)
- return;
-
- href = asx_get_attrib("HREF",_attribs);
- if(href == NULL) {
- asx_warning_attrib_required(parser,"ENTRYREF" ,"HREF" );
- return;
- }
- MP_ERR(parser, "Recursive playlist %s\n", href);
- playlist_add_file(parser->pl, href);
- free(href);
- //MP_INFO(parser, "Need to implement entryref\n");
-}
-
-static void asx_parse_entry(ASX_Parser_t* parser,char* buffer,char** _attribs) {
- char *element,*body,**attribs;
- int r;
-
- while(buffer && buffer[0] != '\0') {
- r = asx_get_element(parser,&buffer,&element,&body,&attribs);
- if(r < 0) {
- asx_warning_body_parse_error(parser,"ENTRY");
- return;
- } else if (r == 0) { // No more element
- break;
- }
- if(strcasecmp(element,"REF") == 0) {
- asx_parse_ref(parser,attribs);
- MP_DBG(parser, "Adding element %s to entry\n",element);
- } else
- MP_DBG(parser, "Ignoring element %s\n",element);
- free(body);
- asx_free_attribs(attribs);
- }
-
-}
-
-
-static void asx_parse_repeat(ASX_Parser_t* parser,char* buffer,char** _attribs) {
- char *element,*body,**attribs;
- int r;
-
- asx_get_attrib("COUNT",_attribs);
- MP_ERR(parser, "Ignoring repeated playlist entries\n");
-
- while(buffer && buffer[0] != '\0') {
- r = asx_get_element(parser,&buffer,&element,&body,&attribs);
- if(r < 0) {
- asx_warning_body_parse_error(parser,"REPEAT");
- return;
- } else if (r == 0) { // No more element
- break;
- }
- if(strcasecmp(element,"ENTRY") == 0) {
- asx_parse_entry(parser,body,attribs);
- } else if(strcasecmp(element,"ENTRYREF") == 0) {
- asx_parse_entryref(parser,body,attribs);
- } else if(strcasecmp(element,"REPEAT") == 0) {
- asx_parse_repeat(parser,body,attribs);
- } else
- MP_DBG(parser, "Ignoring element %s\n",element);
- free(body);
- asx_free_attribs(attribs);
- }
-
-}
-
-
-bool asx_parse(char* buffer, struct playlist *pl, struct mp_log *log)
-{
- char *element,*asx_body,**asx_attribs,*body = NULL, **attribs;
- int r;
- ASX_Parser_t* parser = asx_parser_new(pl);
- parser->log = log;
-
- parser->line = 1;
- parser->deep = 0;
-
- r = asx_get_element(parser,&buffer,&element,&asx_body,&asx_attribs);
- if(r < 0) {
- MP_ERR(parser, "At line %d : Syntax error ???",parser->line);
- asx_parser_free(parser);
- return false;
- } else if(r == 0) { // No contents
- MP_ERR(parser, "empty asx element");
- asx_parser_free(parser);
- return false;
- }
-
- if(strcasecmp(element,"ASX") != 0) {
- MP_ERR(parser, "first element isn't ASX, it's %s\n",element);
- asx_free_attribs(asx_attribs);
- asx_parser_free(parser);
- return false;
- }
-
- if(!asx_body) {
- MP_ERR(parser, "ASX element is empty");
- asx_free_attribs(asx_attribs);
- asx_parser_free(parser);
- return false;
- }
-
- buffer = asx_body;
- while(buffer && buffer[0] != '\0') {
- r = asx_get_element(parser,&buffer,&element,&body,&attribs);
- if(r < 0) {
- asx_warning_body_parse_error(parser,"ASX");
- asx_parser_free(parser);
- return false;
- } else if (r == 0) { // No more element
- break;
- }
- if(strcasecmp(element,"ENTRY") == 0) {
- asx_parse_entry(parser,body,attribs);
- } else if(strcasecmp(element,"ENTRYREF") == 0) {
- asx_parse_entryref(parser,body,attribs);
- } else if(strcasecmp(element,"REPEAT") == 0) {
- asx_parse_repeat(parser,body,attribs);
- } else
- MP_DBG(parser, "Ignoring element %s\n",element);
- free(body);
- asx_free_attribs(attribs);
- }
-
- free(asx_body);
- asx_free_attribs(asx_attribs);
- asx_parser_free(parser);
- return true;
-}
diff --git a/common/asxparser.h b/common/asxparser.h
deleted file mode 100644
index 87e0f759c8..0000000000
--- a/common/asxparser.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#ifndef MPLAYER_ASXPARSER_H
-#define MPLAYER_ASXPARSER_H
-
-#include <stdbool.h>
-
-struct playlist;
-struct mp_log;
-bool asx_parse(char* buffer, struct playlist *pl, struct mp_log *log);
-
-#endif /* MPLAYER_ASXPARSER_H */
diff --git a/common/playlist.c b/common/playlist.c
index 297cb4d379..9080a27ae9 100644
--- a/common/playlist.c
+++ b/common/playlist.c
@@ -19,9 +19,14 @@
#include "config.h"
#include "playlist.h"
#include "common/common.h"
+#include "common/global.h"
+#include "common/msg.h"
#include "talloc.h"
#include "options/path.h"
+#include "demux/demux.h"
+#include "stream/stream.h"
+
struct playlist_entry *playlist_entry_new(const char *filename)
{
struct playlist_entry *e = talloc_zero(NULL, struct playlist_entry);
@@ -239,3 +244,39 @@ struct playlist_entry *playlist_entry_from_index(struct playlist *pl, int index)
}
}
+struct playlist *playlist_parse_file(const char *file, struct mpv_global *global)
+{
+ struct mp_log *log = mp_log_new(NULL, global->log, "!playlist_parser");
+ mp_verbose(log, "Parsing playlist file %s...\n", file);
+
+ struct playlist *ret = NULL;
+ stream_t *stream = stream_open(file, global);
+ if(!stream) {
+ mp_err(log, "Error while opening playlist file %s\n", file);
+ talloc_free(log);
+ return NULL;
+ }
+
+ struct demuxer *pl_demux = demux_open(stream, "playlist", NULL, global);
+ if (pl_demux && pl_demux->playlist) {
+ ret = talloc_zero(NULL, struct playlist);
+ playlist_transfer_entries(ret, pl_demux->playlist);
+ }
+ free_demuxer(pl_demux);
+ free_stream(stream);
+
+ if (ret) {
+ mp_verbose(log, "Playlist successfully parsed\n");
+ } else {
+ mp_err(log, "Error while parsing playlist\n");
+ }
+
+ if (ret && !ret->first)
+ mp_warn(log, "Warning: empty playlist\n");
+
+ if (ret)
+ playlist_add_base_path(ret, mp_dirname(file));
+
+ talloc_free(log);
+ return ret;
+}
diff --git a/common/playlist.h b/common/playlist.h
index f383a85fea..c55a3a21c8 100644
--- a/common/playlist.h
+++ b/common/playlist.h
@@ -79,4 +79,7 @@ int playlist_entry_to_index(struct playlist *pl, struct playlist_entry *e);
int playlist_entry_count(struct playlist *pl);
struct playlist_entry *playlist_entry_from_index(struct playlist *pl, int index);
+struct mpv_global;
+struct playlist *playlist_parse_file(const char *file, struct mpv_global *global);
+
#endif
diff --git a/common/playlist_parser.c b/common/playlist_parser.c
deleted file mode 100644
index b9c40ea2a1..0000000000
--- a/common/playlist_parser.c
+++ /dev/null
@@ -1,552 +0,0 @@
-/*
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-/*
- * Warning: this is outdated, crappy code. It is used only for --playlist.
- * New or cleaned up code should be added to demux_playlist.c instead.
- */
-
-#include "config.h"
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <assert.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <ctype.h>
-#include <limits.h>
-
-#include "talloc.h"
-#include "asxparser.h"
-#include "playlist.h"
-#include "playlist_parser.h"
-#include "stream/stream.h"
-#include "demux/demux.h"
-#include "common/global.h"
-#include "common/msg.h"
-#include "options/path.h"
-
-
-#define BUF_STEP 1024
-
-#define WHITES " \n\r\t"
-
-typedef struct play_tree_parser {
- struct stream *stream;
- char *buffer,*iter,*line;
- int buffer_size , buffer_end;
- int keep;
- struct playlist *pl;
- struct mp_log *log;
-} play_tree_parser_t;
-
-static void
-strstrip(char* str) {
- char* i;
-
- if (str==NULL)
- return;
- for(i = str ; i[0] != '\0' && strchr(WHITES,i[0]) != NULL; i++)
- /* NOTHING */;
- if(i[0] != '\0') {
- memmove(str,i,strlen(i) + 1);
- for(i = str + strlen(str) - 1 ; strchr(WHITES,i[0]) != NULL; i--)
- /* NOTHING */;
- i[1] = '\0';
- } else
- str[0] = '\0';
-}
-
-static char*
-play_tree_parser_get_line(play_tree_parser_t* p) {
- char *end,*line_end;
- int r,resize = 0;
-
- if(p->buffer == NULL) {
- p->buffer = malloc(BUF_STEP);
- p->buffer_size = BUF_STEP;
- p->buffer[0] = 0;
- p->iter = p->buffer;
- }
-
- if(p->stream->eof && (p->buffer_end == 0 || p->iter[0] == '\0'))
- return NULL;
-
- assert(p->buffer_end < p->buffer_size);
- assert(!p->buffer[p->buffer_end]);
- while(1) {
-
- if(resize) {
- char *tmp;
- r = p->iter - p->buffer;
- end = p->buffer + p->buffer_end;
- if (p->buffer_size > INT_MAX - BUF_STEP)
- break;
- tmp = realloc(p->buffer, p->buffer_size + BUF_STEP);
- if (!tmp)
- break;
- p->buffer = tmp;
- p->iter = p->buffer + r;
- p->buffer_size += BUF_STEP;
- resize = 0;
- }
-
- if(p->buffer_size - p->buffer_end > 1 && ! p->stream->eof) {
- r = stream_read(p->stream,p->buffer + p->buffer_end,p->buffer_size - p->buffer_end - 1);
- if(r > 0) {
- p->buffer_end += r;
- assert(p->buffer_end < p->buffer_size);
- p->buffer[p->buffer_end] = '\0';
- while(strlen(p->buffer + p->buffer_end - r) != r)
- p->buffer[p->buffer_end - r + strlen(p->buffer + p->buffer_end - r)] = '\n';
- }
- assert(!p->buffer[p->buffer_end]);
- }
-
- end = strchr(p->iter,'\n');
- if(!end) {
- if(p->stream->eof) {
- end = p->buffer + p->buffer_end;
- break;
- }
- resize = 1;
- continue;
- }
- break;
- }
-
- line_end = (end > p->iter && *(end-1) == '\r') ? end-1 : end;
- if(line_end - p->iter >= 0)
- p->line = realloc(p->line, line_end - p->iter + 1);
- else
- return NULL;
- if(line_end - p->iter > 0)
- strncpy(p->line,p->iter,line_end - p->iter);
- p->line[line_end - p->iter] = '\0';
- if(end[0] != '\0')
- end++;
-
- if(!p->keep) {
- if(end[0] != '\0') {
- p->buffer_end -= end-p->iter;
- memmove(p->buffer,end,p->buffer_end);
- } else
- p->buffer_end = 0;
- p->buffer[p->buffer_end] = '\0';
- p->iter = p->buffer;
- } else
- p->iter = end;
-
- return p->line;
-}
-
-static void
-play_tree_parser_reset(play_tree_parser_t* p) {
- p->iter = p->buffer;
-}
-
-static void
-play_tree_parser_stop_keeping(play_tree_parser_t* p) {
- p->keep = 0;
- if(p->iter && p->iter != p->buffer) {
- p->buffer_end -= p->iter -p->buffer;
- if(p->buffer_end)
- memmove(p->buffer,p->iter,p->buffer_end);
- p->buffer[p->buffer_end] = 0;
- p->iter = p->buffer;
- }
-}
-
-
-static bool parse_asx(play_tree_parser_t* p) {
- int comments = 0,get_line = 1;
- char* line = NULL;
-
- MP_VERBOSE(p, "Trying asx...\n");
-
- while(1) {
- if(get_line) {
- line = play_tree_parser_get_line(p);
- if(!line)
- return false;
- strstrip(line);
- if(line[0] == '\0')
- continue;
- }
- if(!comments) {
- if(line[0] != '<') {
- MP_DBG(p, "First char isn't '<' but '%c'\n",line[0]);
- MP_TRACE(p, "Buffer = [%s]\n",p->buffer);
- return false;
- } else if(strncmp(line,"<!--",4) == 0) { // Comments
- comments = 1;
- line += 4;
- if(line[0] != '\0' && strlen(line) > 0)
- get_line = 0;
- } else if(strncasecmp(line,"<ASX",4) == 0) // We got an asx element
- break;
- else // We don't get an asx
- return false;
- } else { // Comments
- char* c;
- c = strchr(line,'-');
- if(c) {
- if (strncmp(c,"--!>",4) == 0) { // End of comments
- comments = 0;
- line = c+4;
- if(line[0] != '\0') // There is some more data on this line : keep it
- get_line = 0;
-
- } else {
- line = c+1; // Jump the -
- if(line[0] != '\0') // Some more data
- get_line = 0;
- else // End of line
- get_line = 1;
- }
- } else // No - on this line (or rest of line) : get next one
- get_line = 1;
- }
- }
-
- MP_VERBOSE(p, "Detected asx format\n");
-
- // We have an asx : load it in memory and parse
-
- while((line = play_tree_parser_get_line(p)) != NULL)
- /* NOTHING */;
-
- MP_TRACE(p, "Parsing asx file: [%s]\n",p->buffer);
- return asx_parse(p->buffer,p->pl,p->log);
-}
-
-static bool parse_smil(play_tree_parser_t* p) {
- int entrymode=0;
- char* line,source[512],*pos,*s_start,*s_end,*src_line;
- int is_rmsmil = 0;
- unsigned int npkt, ttlpkt;
-
- MP_VERBOSE(p, "Trying smil playlist...\n");
-
- // Check if smil
- while((line = play_tree_parser_get_line(p)) != NULL) {
- strstrip(line);
- if(line[0] == '\0') // Ignore empties
- continue;
- if (strncasecmp(line,"<?xml",5)==0) // smil in xml
- continue;
- if (strncasecmp(line,"<!DOCTYPE smil",13)==0) // smil in xml
- continue;
- if (strncasecmp(line,"<smil",5)==0 || strncasecmp(line,"<?wpl",5)==0 ||
- strncasecmp(line,"(smil-document",14)==0)
- break; // smil header found
- else
- return NULL; //line not smil exit
- }
-
- if (!line) return NULL;
- MP_VERBOSE(p, "Detected smi l playlist format\n");
- play_tree_parser_stop_keeping(p);
-
- if (strncasecmp(line,"(smil-document",14)==0) {
- MP_VERBOSE(p, "Special smil-over-realrtsp playlist header\n");
- is_rmsmil = 1;
- if (sscanf(line, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt, &ttlpkt) != 2) {
- MP_WARN(p, "smil-over-realrtsp: header parsing failure, assuming single packet.\n");
- npkt = ttlpkt = 1;
- }
- if (ttlpkt == 0 || npkt > ttlpkt) {
- MP_WARN(p, "smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
- npkt, ttlpkt);
- npkt = ttlpkt = 1;
- }
- }
-
- //Get entries from smil
- src_line = line;
- line = NULL;
- do {
- strstrip(src_line);
- free(line);
- line = NULL;
- /* If we're parsing smil over realrtsp and this is not the last packet and
- * this is the last line in the packet (terminating with ") ) we must get
- * the next line, strip the header, and concatenate it to the current line.
- */
- if (is_rmsmil && npkt != ttlpkt && strstr(src_line,"\")")) {
- char *payload;
-
- line = strdup(src_line);
- if(!(src_line = play_tree_parser_get_line(p))) {
- MP_WARN(p, "smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt, ttlpkt);
- break;
- }
- strstrip(src_line);
- // Skip header, packet starts after "
- if(!(payload = strchr(src_line,'\"'))) {
- MP_WARN(p, "smil-over-realrtsp: can't find start of packet, using complete line.\n");
- payload = src_line;
- } else
- payload++;
- // Skip ") at the end of the last line from the current packet
- line[strlen(line)-2] = 0;
- line = realloc(line, strlen(line)+strlen(payload)+1);
- strcat (line, payload);
- npkt++;
- } else
- line = strdup(src_line);
- /* Unescape \" to " for smil-over-rtsp */
- if (is_rmsmil && line[0] != '\0') {
- int i, j;
-
- for (i = 0; i < strlen(line); i++)
- if (line[i] == '\\' && line[i+1] == '"')
- for (j = i; line[j]; j++)
- line[j] = line[j+1];
- }
- pos = line;
- while (pos) {
- if (!entrymode) { // all entries filled so far
- while ((pos=strchr(pos, '<'))) {
- if (strncasecmp(pos,"<video",6)==0 || strncasecmp(pos,"<audio",6)==0 || strncasecmp(pos,"<media",6)==0) {
- entrymode=1;
- break; // Got a valid tag, exit '<' search loop
- }
- pos++;
- }
- }
- if (entrymode) { //Entry found but not yet filled
- pos = strstr(pos,"src="); // Is source present on this line
- if (pos != NULL) {
- entrymode=0;
- if (pos[4] != '"' && pos[4] != '\'') {
- MP_VERBOSE(p, "Unknown delimiter %c in source line %s\n", pos[4], line);
- break;
- }
- s_start=pos+5;
- s_end=strchr(s_start,pos[4]);
- if (s_end == NULL) {
- MP_VERBOSE(p, "Error parsing this source line %s\n",line);
- break;
- }
- if (s_end-s_start> 511) {
- MP_VERBOSE(p, "Cannot store such a large source %s\n",line);
- break;
- }
- strncpy(source,s_start,s_end-s_start);
- source[(s_end-s_start)]='\0'; // Null terminate
- playlist_add_file(p->pl, source);
- pos = s_end;
- }
- }
- }
- } while((src_line = play_tree_parser_get_line(p)) != NULL);
-
- free(line);
- return true;
-}
-
-/**
- * \brief decode the base64 used in nsc files
- * \param in input string, 0-terminated
- * \param buf output buffer, must point to memory suitable for realloc,
- * will be NULL on failure.
- * \return decoded length in bytes
- */
-static int decode_nsc_base64(struct mp_log *log, char *in, char **buf) {
- int i, j, n;
- if (in[0] != '0' || in[1] != '2')
- goto err_out;
- in += 2; // skip prefix
- if (strlen(in) < 16) // error out if nothing to decode
- goto err_out;
- in += 12; // skip encoded string length
- n = strlen(in) / 4;
- *buf = realloc(*buf, n * 3);
- for (i = 0; i < n; i++) {
- uint8_t c[4];
- for (j = 0; j < 4; j++) {
- c[j] = in[4 * i + j];
- if (c[j] >= '0' && c[j] <= '9') c[j] += 0 - '0';
- else if (c[j] >= 'A' && c[j] <= 'Z') c[j] += 10 - 'A';
- else if (c[j] >= 'a' && c[j] <= 'z') c[j] += 36 - 'a';
- else if (c[j] == '{') c[j] = 62;
- else if (c[j] == '}') c[j] = 63;
- else {
- mp_err(log, "Invalid character %c (0x%02"PRIx8")\n", c[j], c[j]);
- goto err_out;
- }
- }
- (*buf)[3 * i] = (c[0] << 2) | (c[1] >> 4);
- (*buf)[3 * i + 1] = (c[1] << 4) | (c[2] >> 2);
- (*buf)[3 * i + 2] = (c[2] << 6) | c[3];
- }
- return 3 * n;
-err_out:
- free(*buf);
- *buf = NULL;
- return 0;
-}
-
-/**
- * \brief "converts" utf16 to ascii by just discarding every second byte
- * \param buf buffer to convert
- * \param len lenght of buffer, must be > 0
- */
-static void utf16_to_ascii(char *buf, int len) {
- int i;
- if (len <= 0) return;
- for (i = 0; i < len / 2; i++)
- buf[i] = buf[i * 2];
- buf[i] = 0; // just in case
-}
-
-static bool parse_nsc(play_tree_parser_t* p) {
- char *line, *addr = NULL, *url, *unicast_url = NULL;
- int port = 0;
-
- MP_VERBOSE(p, "Trying nsc playlist...\n");
- while((line = play_tree_parser_get_line(p)) != NULL) {
- strstrip(line);
- if(!line[0]) // Ignore empties
- continue;
- if (strncasecmp(line,"[Address]", 9) == 0)
- break; // nsc header found
- else
- return false;
- }
- MP_VERBOSE(p, "Detected nsc playlist format\n");
- play_tree_parser_stop_keeping(p);
- while ((line = play_tree_parser_get_line(p)) != NULL) {
- strstrip(line);
- if (!line[0])
- continue;
- if (strncasecmp(line, "Unicast URL=", 12) == 0) {
- int len = decode_nsc_base64(p->log, &line[12], &unicast_url);
- if (len <= 0)
- MP_WARN(p, "[nsc] Unsupported Unicast URL encoding\n");
- else
- utf16_to_ascii(unicast_url, len);
- } else if (strncasecmp(line, "IP Address=", 11) == 0) {
- int len = decode_nsc_base64(p->log, &line[11], &addr);
- if (len <= 0)
- MP_WARN(p, "[nsc] Unsupported IP Address encoding\n");
- else
- utf16_to_ascii(addr, len);
- } else if (strncasecmp(line, "IP Port=", 8) == 0) {
- port = strtol(&line[8], NULL, 0);
- }
- }
-
- bool success = false;
-
- if (unicast_url)
- url = strdup(unicast_url);
- else if (addr && port) {
- url = malloc(strlen(addr) + 7 + 20 + 1);
- sprintf(url, "http://%s:%i", addr, port);
- } else
- goto err_out;
-
- playlist_add_file(p->pl, url);
- free(url);
- success = true;
-err_out:
- free(addr);
- free(unicast_url);
- return success;
-}
-
-static struct playlist *do_parse(struct stream* stream, bool forced,
- struct mp_log *log, struct mpv_global *global);
-
-struct playlist *playlist_parse_file(const char *file, struct mpv_global *global)
-{
- struct mp_log *log = mp_log_new(NULL, global->log, "!playlist_parser");
- struct playlist *ret = NULL;
- stream_t *stream = stream_open(file, global);
- if(!stream) {
- mp_err(log, "Error while opening playlist file %s: %s\n",
- file, strerror(errno));
- goto done;
- }
-
- mp_verbose(log, "Parsing playlist file %s...\n", file);
-
- ret = do_parse(stream, true, log, global);
- free_stream(stream);
-
- if (ret)
- playlist_add_base_path(ret, mp_dirname(file));
-
-done:
- talloc_free(log);
- return ret;
-}
-
-typedef bool (*parser_fn)(play_tree_parser_t *);
-static const parser_fn pl_parsers[] = {
- parse_asx,
- parse_smil,
- parse_nsc,
-};
-
-
-static struct playlist *do_parse(struct stream* stream, bool forced,
- struct mp_log *log, struct mpv_global *global)
-{
- play_tree_parser_t p = {
- .stream = stream,
- .pl = talloc_zero(NULL, struct playlist),
- .keep = 1,
- .log = log,
- };
-
- bool success = false;
- if (play_tree_parser_get_line(&p) != NULL) {
- for (int n = 0; n < sizeof(pl_parsers) / sizeof(pl_parsers[0]); n++) {
- play_tree_parser_reset(&p);
- if (pl_parsers[n](&p)) {
- success = true;
- break;
- }
- }
- }
- struct demuxer *pl_demux = demux_open(stream, "playlist", NULL, global);
- if (!success && pl_demux && pl_demux->playlist) {
- playlist_transfer_entries(p.pl, pl_demux->playlist);
- success = true;
- }
- free_demuxer(pl_demux);
-
- if(success)
- mp_verbose(log, "Playlist successfully parsed\n");
- else {
- mp_msg(log,((forced==1)?MSGL_ERR:MSGL_V),"Error while parsing playlist\n");
- talloc_free(p.pl);
- p.pl = NULL;
- }
-
- if (p.pl && !p.pl->first)
- mp_msg(log, ((forced==1)?MSGL_WARN:MSGL_V),"Warning: empty playlist\n");
-
- return p.pl;
-}
diff --git a/common/playlist_parser.h b/common/playlist_parser.h
deleted file mode 100644
index 9d139d90f3..0000000000
--- a/common/playlist_parser.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#ifndef MPLAYER_PLAYLISTPARSER_H
-#define MPLAYER_PLAYLISTPARSER_H
-
-struct mpv_global;
-struct playlist;
-
-struct playlist *playlist_parse_file(const char *file, struct mpv_global *global);
-
-#endif