asx.c 28.2 KB
Newer Older
hartman's avatar
hartman committed
1
2
3
/*****************************************************************************
 * asx.c : ASX playlist format import
 *****************************************************************************
4
 * Copyright (C) 2005-2006 the VideoLAN team
hartman's avatar
hartman committed
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
 * $Id$
 *
 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/* See also: http://msdn.microsoft.com/library/en-us/wmplay10/mmp_sdk/windowsmediametafilereference.asp
 */

/*****************************************************************************
 * Preamble
 *****************************************************************************/
30
31
32
33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include <vlc_common.h>
zorglub's avatar
zorglub committed
35
#include <vlc_demux.h>
hartman's avatar
hartman committed
36

37
#include <ctype.h>
zorglub's avatar
zorglub committed
38
#include <vlc_charset.h>
hartman's avatar
hartman committed
39
#include "playlist.h"
40
#include <vlc_meta.h>
hartman's avatar
hartman committed
41
42
43
44
45
46

struct demux_sys_t
{
    char    *psz_prefix;
    char    *psz_data;
    int64_t i_data_len;
47
48
    bool b_utf8;
    bool b_skip_ads;
hartman's avatar
hartman committed
49
50
51
52
53
54
55
56
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux( demux_t *p_demux);
static int Control( demux_t *p_demux, int i_query, va_list args );

57
58
59
static int StoreString( demux_t *p_demux, char **ppsz_string,
                        const char *psz_source_start,
                        const char *psz_source_end )
hartman's avatar
hartman committed
60
61
{
    demux_sys_t *p_sys = p_demux->p_sys;
62
    unsigned len = psz_source_end - psz_source_start;
hartman's avatar
hartman committed
63

64
    free( *ppsz_string );
65
66
67
68

    char *buf = *ppsz_string = malloc ((len * (1 + !p_sys->b_utf8)) + 1);
    if (buf == NULL)
        return VLC_ENOMEM;
hartman's avatar
hartman committed
69
70

    if( p_sys->b_utf8 )
71
72
73
74
75
    {
        memcpy (buf, psz_source_start, len);
        (*ppsz_string)[len] = '\0';
        EnsureUTF8 (*ppsz_string);
    }
hartman's avatar
hartman committed
76
77
    else
    {
78
79
        /* Latin-1 -> UTF-8 */
        for (unsigned i = 0; i < len; i++)
hartman's avatar
hartman committed
80
        {
81
82
83
84
85
86
87
88
89
90
91
            unsigned char c = psz_source_start[i];
            if (c & 0x80)
            {
                *buf++ = 0xc0 | (c >> 6);
                *buf++ = 0x80 | (c & 0x3f);
            }
            else
                *buf++ = c;
        }
        *buf++ = '\0';

92
93
94
        buf = realloc (*ppsz_string, buf - *ppsz_string);
        if( buf )
            *ppsz_string = buf;
hartman's avatar
hartman committed
95
96
97
98
    }
    return VLC_SUCCESS;
}

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
static char *SkipBlanks(char *s, size_t i_strlen )
{
    while( i_strlen > 0 ) {
        switch( *s )
        {
            case ' ':
            case '\t':
            case '\r':
            case '\n':
                --i_strlen;
                ++s;
                break;
            default:
                i_strlen = 0;
        }
    }
    return s;
}

static int ParseTime(char *s, size_t i_strlen)
{
    // need to parse hour:minutes:sec.fraction string
    int result = 0;
    int val;
    const char *end = s + i_strlen;
    // skip leading spaces if any
    s = SkipBlanks(s, i_strlen);

    val = 0;
    while( (s < end) && isdigit(*s) )
    {
        int newval = val*10 + (*s - '0');
        if( newval < val )
        {
            // overflow
            val = 0;
            break;
        }
        val = newval;
        ++s;
    }
    result = val;
    s = SkipBlanks(s, end-s);
    if( *s == ':' )
    {
        ++s;
        s = SkipBlanks(s, end-s);
        result = result * 60;
        val = 0;
        while( (s < end) && isdigit(*s) )
        {
            int newval = val*10 + (*s - '0');
            if( newval < val )
            {
                // overflow
                val = 0;
                break;
            }
            val = newval;
            ++s;
        }
        result += val;
        s = SkipBlanks(s, end-s);
        if( *s == ':' )
        {
            ++s;
            s = SkipBlanks(s, end-s);
            result = result * 60;
            val = 0;
            while( (s < end) && isdigit(*s) )
            {
                int newval = val*10 + (*s - '0');
                if( newval < val )
                {
                    // overflow
                    val = 0;
                    break;
                }
                val = newval;
                ++s;
            }
            result += val;
            // TODO: one day, we may need to parse fraction for sub-second resolution
        }
    }
    return result;
}

hartman's avatar
hartman committed
187
188
189
/*****************************************************************************
 * Import_ASX: main import function
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
190
int Import_ASX( vlc_object_t *p_this )
hartman's avatar
hartman committed
191
192
{
    demux_t *p_demux = (demux_t *)p_this;
193
    const uint8_t *p_peek;
194
    CHECK_PEEK( p_peek, 10 );
195

196
197
    // skip over possible leading empty lines and empty spaces
    p_peek = (uint8_t *)SkipBlanks((char *)p_peek, 6);
198

199
200
201
    if( POKE( p_peek, "<asx", 4 ) || demux_IsPathExtension( p_demux, ".asx" ) ||
        demux_IsPathExtension( p_demux, ".wax" ) || demux_IsPathExtension( p_demux, ".wvx" ) ||
        demux_IsForced( p_demux, "asx-open" ) )
hartman's avatar
hartman committed
202
203
204
205
206
    {
        ;
    }
    else
        return VLC_EGENERIC;
207

208
    STANDARD_DEMUX_INIT_MSG( "found valid ASX playlist" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
209
    p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
210
211
    p_demux->p_sys->psz_data = NULL;
    p_demux->p_sys->i_data_len = -1;
212
    p_demux->p_sys->b_utf8 = false;
213
    p_demux->p_sys->b_skip_ads = config_GetInt( p_demux, "playlist-skip-ads" );
214

hartman's avatar
hartman committed
215
216
217
218
219
220
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Deactivate: frees unused data
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
221
void Close_ASX( vlc_object_t *p_this )
hartman's avatar
hartman committed
222
223
224
225
{
    demux_t *p_demux = (demux_t *)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;

226
227
    free( p_sys->psz_prefix );
    free( p_sys->psz_data );
hartman's avatar
hartman committed
228
229
230
231
232
233
234
235
    free( p_sys );
}

static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    char        *psz_parse = NULL;
    char        *psz_backup = NULL;
236
    bool  b_entry = false;
237
    input_item_t *p_current_input = GetCurrentItem(p_demux);
hartman's avatar
hartman committed
238
239
240
241
242
243
244

    /* init txt */
    if( p_sys->i_data_len < 0 )
    {
        int64_t i_pos = 0;
        p_sys->i_data_len = stream_Size( p_demux->s ) +1; /* This is a cheat to prevent unnecessary realloc */
        if( p_sys->i_data_len <= 0 && p_sys->i_data_len < 16384 ) p_sys->i_data_len = 1024;
245
        p_sys->psz_data = malloc( p_sys->i_data_len +1);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
246

hartman's avatar
hartman committed
247
248
249
250
        /* load the complete file */
        for( ;; )
        {
            int i_read = stream_Read( p_demux->s, &p_sys->psz_data[i_pos], p_sys->i_data_len - i_pos );
251
            p_sys->psz_data[i_pos + i_read] = '\0';
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
252

hartman's avatar
hartman committed
253
            if( i_read < p_sys->i_data_len - i_pos ) break; /* Done */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
254

255
            /* XXX this looks fishy and inefficient */
hartman's avatar
hartman committed
256
257
258
259
            i_pos += i_read;
            p_sys->i_data_len += 1024;
            p_sys->psz_data = realloc( p_sys->psz_data, p_sys->i_data_len * sizeof( char * ) +1 );
        }
260
        if( p_sys->i_data_len <= 0 ) return -1;
hartman's avatar
hartman committed
261
262
263
264
265
266
267
268
269
270
271
272
    }

    psz_parse = p_sys->psz_data;
    /* Find first element */
    if( ( psz_parse = strcasestr( psz_parse, "<ASX" ) ) )
    {
        /* ASX element */
        char *psz_string = NULL;
        int i_strlen = 0;

        char *psz_base_asx = NULL;
        char *psz_title_asx = NULL;
zorglub's avatar
zorglub committed
273
        char *psz_artist_asx = NULL;
hartman's avatar
hartman committed
274
275
276
        char *psz_copyright_asx = NULL;
        char *psz_moreinfo_asx = NULL;
        char *psz_abstract_asx = NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
277

hartman's avatar
hartman committed
278
279
        char *psz_base_entry = NULL;
        char *psz_title_entry = NULL;
zorglub's avatar
zorglub committed
280
        char *psz_artist_entry = NULL;
hartman's avatar
hartman committed
281
282
283
284
        char *psz_copyright_entry = NULL;
        char *psz_moreinfo_entry = NULL;
        char *psz_abstract_entry = NULL;
        int i_entry_count = 0;
285
        bool b_skip_entry = false;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
286

287
288
289
290
        char *psz_href = NULL;
        int i_starttime = 0;
        int i_duration = 0;

hartman's avatar
hartman committed
291
292
        psz_parse = strcasestr( psz_parse, ">" );

293
        while( psz_parse && ( psz_parse = strcasestr( psz_parse, "<" ) ) )
hartman's avatar
hartman committed
294
295
296
297
298
299
300
301
302
303
        {
            if( !strncasecmp( psz_parse, "<!--", 4 ) )
            {
                /* this is a comment */
                if( ( psz_parse = strcasestr( psz_parse, "-->" ) ) )
                    psz_parse+=3;
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<PARAM ", 7 ) )
            {
304
                bool b_encoding_flag = false;
305
                psz_parse = SkipBlanks(psz_parse+7, (unsigned)-1);
hartman's avatar
hartman committed
306
307
308
309
310
311
312
313
314
315
                if( !strncasecmp( psz_parse, "name", 4 ) )
                {
                    if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                    {
                        psz_backup = ++psz_parse;
                        if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                        {
                            i_strlen = psz_parse-psz_backup;
                            if( i_strlen < 1 ) continue;
                            msg_Dbg( p_demux, "param name strlen: %d", i_strlen);
316
                            psz_string = malloc( i_strlen + 1);
hartman's avatar
hartman committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
                            memcpy( psz_string, psz_backup, i_strlen );
                            psz_string[i_strlen] = '\0';
                            msg_Dbg( p_demux, "param name: %s", psz_string);
                            b_encoding_flag = !strcasecmp( psz_string, "encoding" );
                            free( psz_string );
                        }
                        else continue;
                    }
                    else continue;
                }
                psz_parse++;
                if( !strncasecmp( psz_parse, "value", 5 ) )
                {
                    if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                    {
                        psz_backup = ++psz_parse;
                        if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                        {
                            i_strlen = psz_parse-psz_backup;
                            if( i_strlen < 1 ) continue;
                            msg_Dbg( p_demux, "param value strlen: %d", i_strlen);
338
                            psz_string = malloc( i_strlen +1);
hartman's avatar
hartman committed
339
340
341
                            memcpy( psz_string, psz_backup, i_strlen );
                            psz_string[i_strlen] = '\0';
                            msg_Dbg( p_demux, "param value: %s", psz_string);
342
                            if( b_encoding_flag && !strcasecmp( psz_string, "utf-8" ) ) p_sys->b_utf8 = true;
hartman's avatar
hartman committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
                            free( psz_string );
                        }
                        else continue;
                    }
                    else continue;
                }
                if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
                    psz_parse += 2;
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<BANNER", 7 ) )
            {
                /* We skip this element */
                if( ( psz_parse = strcasestr( psz_parse, "</BANNER>" ) ) )
                    psz_parse += 9;
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<PREVIEWDURATION", 16 ) ||
                     !strncasecmp( psz_parse, "<LOGURL", 7 ) ||
                     !strncasecmp( psz_parse, "<Skin", 5 ) )
            {
                /* We skip this element */
                if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
                    psz_parse += 2;
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<BASE ", 6 ) )
            {
371
                psz_parse = SkipBlanks(psz_parse+6, (unsigned)-1);
hartman's avatar
hartman committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
                if( !strncasecmp( psz_parse, "HREF", 4 ) )
                {
                    if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                    {
                        psz_backup = ++psz_parse;
                        if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                        {
                            StoreString( p_demux, (b_entry ? &psz_base_entry : &psz_base_asx), psz_backup, psz_parse );
                        }
                        else continue;
                    }
                    else continue;
                }
                if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
                    psz_parse += 2;
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<TITLE>", 7 ) )
            {
                psz_backup = psz_parse+=7;
                if( ( psz_parse = strcasestr( psz_parse, "</TITLE>" ) ) )
                {
                    StoreString( p_demux, (b_entry ? &psz_title_entry : &psz_title_asx), psz_backup, psz_parse );
                    psz_parse += 8;
                }
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<Author>", 8 ) )
            {
                psz_backup = psz_parse+=8;
                if( ( psz_parse = strcasestr( psz_parse, "</Author>" ) ) )
                {
zorglub's avatar
zorglub committed
404
                    StoreString( p_demux, (b_entry ? &psz_artist_entry : &psz_artist_asx), psz_backup, psz_parse );
hartman's avatar
hartman committed
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
                    psz_parse += 9;
                }
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<Copyright", 10 ) )
            {
                psz_backup = psz_parse+=11;
                if( ( psz_parse = strcasestr( psz_parse, "</Copyright>" ) ) )
                {
                    StoreString( p_demux, (b_entry ? &psz_copyright_entry : &psz_copyright_asx), psz_backup, psz_parse );
                    psz_parse += 12;
                }
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<MoreInfo ", 10 ) )
            {
421
                psz_parse = SkipBlanks(psz_parse+10, (unsigned)-1);
hartman's avatar
hartman committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
                if( !strncasecmp( psz_parse, "HREF", 4 ) )
                {
                    if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                    {
                        psz_backup = ++psz_parse;
                        if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                        {
                            StoreString( p_demux, (b_entry ? &psz_moreinfo_entry : &psz_moreinfo_asx), psz_backup, psz_parse );
                        }
                        else continue;
                    }
                    else continue;
                }
                if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
                    psz_parse += 2;
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<ABSTRACT>", 10 ) )
            {
                psz_backup = psz_parse+=10;
                if( ( psz_parse = strcasestr( psz_parse, "</ABSTRACT>" ) ) )
                {
                    StoreString( p_demux, (b_entry ? &psz_abstract_entry : &psz_abstract_asx), psz_backup, psz_parse );
                    psz_parse += 11;
                }
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<EntryRef ", 10 ) )
            {
451
                psz_parse = SkipBlanks(psz_parse+10, (unsigned)-1);
hartman's avatar
hartman committed
452
453
454
455
456
457
458
459
460
                if( !strncasecmp( psz_parse, "HREF", 4 ) )
                {
                    if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                    {
                        psz_backup = ++psz_parse;
                        if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                        {
                            i_strlen = psz_parse-psz_backup;
                            if( i_strlen < 1 ) continue;
461
                            psz_string = malloc( i_strlen +1);
hartman's avatar
hartman committed
462
463
                            memcpy( psz_string, psz_backup, i_strlen );
                            psz_string[i_strlen] = '\0';
464
                            input_item_t *p_input;
465
466
467
                            p_input = input_item_New( p_demux, psz_string, psz_title_asx );
                            input_item_CopyOptions( p_current_input, p_input );
                            input_item_AddSubItem( p_current_input, p_input );
468
                            vlc_gc_decref( p_input );
hartman's avatar
hartman committed
469
470
471
472
473
474
475
476
477
478
479
480
                            free( psz_string );
                        }
                        else continue;
                    }
                    else continue;
                }
                if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
                    psz_parse += 2;
                else continue;
            }
            else if( !strncasecmp( psz_parse, "</Entry>", 8 ) )
            {
481
482
483
484
485
486
                input_item_t *p_entry = NULL;
                char *psz_name = NULL;

                char * ppsz_options[2];
                int i_options = 0;

hartman's avatar
hartman committed
487
488
489
490
491
492
493
                /* add a new entry */
                psz_parse+=8;
                if( !b_entry )
                {
                    msg_Err( p_demux, "end of entry without start?" );
                    continue;
                }
494
495
496
497
498
499
500

                if( !psz_href )
                {
                    msg_Err( p_demux, "entry without href?" );
                    continue;
                }

501
                if( p_sys->b_skip_ads && b_skip_entry )
502
                {
503
504
                    char *psz_current_input_name = input_item_GetName( p_current_input );

505
                    msg_Dbg( p_demux, "skipped entry %d %s (%s)",
506
507
508
                             i_entry_count,
                             ( psz_title_entry ? psz_title_entry : psz_current_input_name ), psz_href );
                    free( psz_current_input_name );
509
                }
510
511
512
513
                else
                {
                    if( i_starttime || i_duration )
                    {
514
515
516
517
518
519
                        if( i_starttime )
                        {
                            if( asprintf(ppsz_options+i_options, ":start-time=%d", i_starttime) == -1 )
                                *(ppsz_options+i_options) = NULL;
                            else
                                ++i_options;
520
                        }
521
522
523
524
525
526
                        if( i_duration )
                        {
                            if( asprintf(ppsz_options+i_options, ":stop-time=%d", i_starttime + i_duration) == -1 )
                                *(ppsz_options+i_options) = NULL;
                            else
                                ++i_options;
527
528
                        }
                    }
529

530
                    /* create the new entry */
531
532
                    char *psz_current_input_name = input_item_GetName( p_current_input );
                    if( asprintf( &psz_name, "%d %s", i_entry_count, ( psz_title_entry ? psz_title_entry : psz_current_input_name ) ) != -1 )
533
                    {
534
535
                        const char *psz_mrl = ProcessMRL( psz_href, p_demux->p_sys->psz_prefix );
                        p_entry = input_item_NewExt( p_demux, psz_mrl, psz_name,
536
                                                     i_options, (const char * const *)ppsz_options, VLC_INPUT_OPTION_TRUSTED, -1 );
537
                        free( psz_name );
538
                        free( psz_mrl );
539
540
541
542
                        input_item_CopyOptions( p_current_input, p_entry );
                        while( i_options )
                        {
                            psz_name = ppsz_options[--i_options];
543
                            free( psz_name );
544
                        }
545
                        psz_name = NULL;
546

547
548
549
550
551
552
553
554
                        if( psz_title_entry ) input_item_SetTitle( p_entry, psz_title_entry );
                        if( psz_artist_entry ) input_item_SetArtist( p_entry, psz_artist_entry );
                        if( psz_copyright_entry ) input_item_SetCopyright( p_entry, psz_copyright_entry );
                        if( psz_moreinfo_entry ) input_item_SetURL( p_entry, psz_moreinfo_entry );
                        if( psz_abstract_entry ) input_item_SetDescription( p_entry, psz_abstract_entry );
                        input_item_AddSubItem( p_current_input, p_entry );
                        vlc_gc_decref( p_entry );
                    }
555
                    free( psz_current_input_name );
556
557
                }

558
559
560
561
562
563
564
565
                /* cleanup entry */;
                FREENULL( psz_href );
                FREENULL( psz_title_entry );
                FREENULL( psz_base_entry );
                FREENULL( psz_artist_entry );
                FREENULL( psz_copyright_entry );
                FREENULL( psz_moreinfo_entry );
                FREENULL( psz_abstract_entry );
566
                b_entry = false;
hartman's avatar
hartman committed
567
            }
568
            else if( !strncasecmp( psz_parse, "<Entry", 6 ) )
hartman's avatar
hartman committed
569
            {
570
                char *psz_clientskip;
571
                psz_parse+=6;
hartman's avatar
hartman committed
572
573
574
575
576
577
                if( b_entry )
                {
                    msg_Err( p_demux, "We already are in an entry section" );
                    continue;
                }
                i_entry_count += 1;
578
                b_entry = true;
579
                psz_clientskip = strcasestr( psz_parse, "clientskip=\"no\"" );
580
                psz_parse = strcasestr( psz_parse, ">" );
581

582
                /* If clientskip was enabled ... this is an ad */
583
                b_skip_entry = (NULL != psz_clientskip) && (psz_clientskip < psz_parse);
584

585
586
587
588
                // init entry details
                FREENULL(psz_href);
                i_starttime = 0;
                i_duration = 0;
hartman's avatar
hartman committed
589
590
591
            }
            else if( !strncasecmp( psz_parse, "<Ref ", 5 ) )
            {
592
                psz_parse = SkipBlanks(psz_parse+5, (unsigned)-1);
hartman's avatar
hartman committed
593
594
595
596
597
                if( !b_entry )
                {
                    msg_Err( p_demux, "A ref outside an entry section" );
                    continue;
                }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
598

hartman's avatar
hartman committed
599
600
601
602
603
                if( !strncasecmp( psz_parse, "HREF", 4 ) )
                {
                    if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                    {
                        psz_backup = ++psz_parse;
604
                        psz_backup = SkipBlanks(psz_backup, (unsigned)-1);
hartman's avatar
hartman committed
605
606
                        if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                        {
607
                            char *psz_tmp;
hartman's avatar
hartman committed
608
609
610
                            i_strlen = psz_parse-psz_backup;
                            if( i_strlen < 1 ) continue;

611
612
613
614
615
616
617
                            if( psz_href )
                            {
                                /* we have allready one href in this entry, lets make new input from it and
                                continue with new href, don't free meta/options*/
                                input_item_t *p_entry = NULL;
                                char *psz_name = input_item_GetName( p_current_input );

618
619
                                const char *psz_mrl = ProcessMRL( psz_href, p_demux->p_sys->psz_prefix );
                                p_entry = input_item_NewExt( p_demux, psz_mrl, psz_name,
620
                                                     0, NULL, VLC_INPUT_OPTION_TRUSTED, -1 );
621
                                free( psz_mrl );
622
623
624
625
626
627
628
629
630
631
                                input_item_CopyOptions( p_current_input, p_entry );
                                if( psz_title_entry ) input_item_SetTitle( p_entry, psz_title_entry );
                                if( psz_artist_entry ) input_item_SetArtist( p_entry, psz_artist_entry );
                                if( psz_copyright_entry ) input_item_SetCopyright( p_entry, psz_copyright_entry );
                                if( psz_moreinfo_entry ) input_item_SetURL( p_entry, psz_moreinfo_entry );
                                if( psz_abstract_entry ) input_item_SetDescription( p_entry, psz_abstract_entry );
                                input_item_AddSubItem( p_current_input, p_entry );
                                vlc_gc_decref( p_entry );
                            }

632
                            free( psz_href );
633
                            psz_href = malloc( i_strlen +1);
634
635
                            memcpy( psz_href, psz_backup, i_strlen );
                            psz_href[i_strlen] = '\0';
636
637
638
639
640
641
642
                            psz_tmp = psz_href + (i_strlen-1);
                            while( psz_tmp >= psz_href &&
                                 ( *psz_tmp == '\r' || *psz_tmp == '\n' ) )
                            {
                                *psz_tmp = '\0';
                                psz_tmp++;
                            }
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
                        }
                        else continue;
                    }
                    else continue;
                }
                if( ( psz_parse = strcasestr( psz_parse, ">" ) ) )
                    psz_parse++;
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<starttime ", 11 ) )
            {
                psz_parse = SkipBlanks(psz_parse+11, (unsigned)-1);
                if( !b_entry )
                {
                    msg_Err( p_demux, "starttime outside an entry section" );
                    continue;
                }

                if( !strncasecmp( psz_parse, "value", 5 ) )
                {
                    if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                    {
                        psz_backup = ++psz_parse;
                        if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                        {
                            i_strlen = psz_parse-psz_backup;
                            if( i_strlen < 1 ) continue;

                            i_starttime = ParseTime(psz_backup, i_strlen);
                        }
                        else continue;
                    }
                    else continue;
                }
                if( ( psz_parse = strcasestr( psz_parse, ">" ) ) )
                    psz_parse++;
                else continue;
            }
            else if( !strncasecmp( psz_parse, "<duration ", 11 ) )
            {
                psz_parse = SkipBlanks(psz_parse+5, (unsigned)-1);
                if( !b_entry )
                {
                    msg_Err( p_demux, "duration outside an entry section" );
                    continue;
                }

                if( !strncasecmp( psz_parse, "value", 5 ) )
                {
                    if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                    {
                        psz_backup = ++psz_parse;
                        if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
                        {
                            i_strlen = psz_parse-psz_backup;
                            if( i_strlen < 1 ) continue;

                            i_duration = ParseTime(psz_backup, i_strlen);
hartman's avatar
hartman committed
701
702
703
704
705
                        }
                        else continue;
                    }
                    else continue;
                }
hartman's avatar
hartman committed
706
707
                if( ( psz_parse = strcasestr( psz_parse, ">" ) ) )
                    psz_parse++;
hartman's avatar
hartman committed
708
709
710
711
                else continue;
            }
            else if( !strncasecmp( psz_parse, "</ASX", 5 ) )
            {
712
713
714
715
716
                if( psz_title_asx ) input_item_SetTitle( p_current_input, psz_title_asx );
                if( psz_artist_asx ) input_item_SetArtist( p_current_input, psz_artist_asx );
                if( psz_copyright_asx ) input_item_SetCopyright( p_current_input, psz_copyright_asx );
                if( psz_moreinfo_asx ) input_item_SetURL( p_current_input, psz_moreinfo_asx );
                if( psz_abstract_asx ) input_item_SetDescription( p_current_input, psz_abstract_asx );
zorglub's avatar
zorglub committed
717
718
                FREENULL( psz_base_asx );
                FREENULL( psz_title_asx );
zorglub's avatar
zorglub committed
719
                FREENULL( psz_artist_asx );
zorglub's avatar
zorglub committed
720
721
722
                FREENULL( psz_copyright_asx );
                FREENULL( psz_moreinfo_asx );
                FREENULL( psz_abstract_asx );
hartman's avatar
hartman committed
723
724
725
726
727
728
729
730
731
732
733
734
735
                psz_parse++;
            }
            else psz_parse++;
        }
#if 0
/* FIXME Unsupported elements */
            PARAM
            EVENT
            REPEAT
            ENDMARK
            STARTMARK
#endif
    }
736
    vlc_gc_decref(p_current_input);
737
    return 0; /* Needed for correct operation of go back */
hartman's avatar
hartman committed
738
739
740
741
}

static int Control( demux_t *p_demux, int i_query, va_list args )
{
742
    VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
hartman's avatar
hartman committed
743
    return VLC_EGENERIC;
744
}