Coverage Report

Created: 2026-09-28 07:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/codec/subsdec.c
Line
Count
Source
1
/*****************************************************************************
2
 * subsdec.c : text subtitle decoder
3
 *****************************************************************************
4
 * Copyright (C) 2000-2006 VLC authors and VideoLAN
5
 *
6
 * Authors: Gildas Bazin <gbazin@videolan.org>
7
 *          Samuel Hocevar <sam@zoy.org>
8
 *          Derk-Jan Hartman <hartman at videolan dot org>
9
 *          Bernie Purcell <bitmap@videolan.org>
10
 *
11
 * This program is free software; you can redistribute it and/or modify it
12
 * under the terms of the GNU Lesser General Public License as published by
13
 * the Free Software Foundation; either version 2.1 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Lesser General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Lesser General Public License
22
 * along with this program; if not, write to the Free Software Foundation,
23
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24
 *****************************************************************************/
25
26
/*****************************************************************************
27
 * Preamble
28
 *****************************************************************************/
29
#ifdef HAVE_CONFIG_H
30
# include "config.h"
31
#endif
32
33
#include <limits.h>
34
#include <errno.h>
35
#include <ctype.h>
36
37
#include <vlc_common.h>
38
#include <vlc_plugin.h>
39
#include <vlc_codec.h>
40
#include <vlc_charset.h>
41
#include <vlc_xml.h>
42
43
#include "substext.h"
44
45
/*****************************************************************************
46
 * Module descriptor.
47
 *****************************************************************************/
48
static const char *const ppsz_encodings[] = {
49
    "",
50
    "system",
51
    "UTF-8",
52
    "UTF-16",
53
    "UTF-16BE",
54
    "UTF-16LE",
55
    "GB18030",
56
    "ISO-8859-15",
57
    "Windows-1252",
58
    "IBM850",
59
    "ISO-8859-2",
60
    "Windows-1250",
61
    "ISO-8859-3",
62
    "ISO-8859-10",
63
    "Windows-1251",
64
    "KOI8-R",
65
    "KOI8-U",
66
    "ISO-8859-6",
67
    "Windows-1256",
68
    "ISO-8859-7",
69
    "Windows-1253",
70
    "ISO-8859-8",
71
    "Windows-1255",
72
    "ISO-8859-9",
73
    "Windows-1254",
74
    "ISO-8859-11",
75
    "Windows-874",
76
    "ISO-8859-13",
77
    "Windows-1257",
78
    "ISO-8859-14",
79
    "ISO-8859-16",
80
    "ISO-2022-CN-EXT",
81
    "EUC-CN",
82
    "ISO-2022-JP-2",
83
    "EUC-JP",
84
    "Shift_JIS",
85
    "CP949",
86
    "ISO-2022-KR",
87
    "Big5",
88
    "ISO-2022-TW",
89
    "Big5-HKSCS",
90
    "VISCII",
91
    "Windows-1258",
92
};
93
94
static const char *const ppsz_encoding_names[] = {
95
    /* xgettext:
96
      The character encoding name in parenthesis corresponds to that used for
97
      the GetACP translation. "Windows-1252" applies to Western European
98
      languages using the Latin alphabet. */
99
    N_("Default (Windows-1252)"),
100
    N_("System codeset"),
101
    N_("Universal (UTF-8)"),
102
    N_("Universal (UTF-16)"),
103
    N_("Universal (big endian UTF-16)"),
104
    N_("Universal (little endian UTF-16)"),
105
    N_("Universal, Chinese (GB18030)"),
106
107
  /* ISO 8859 and the likes */
108
    /* 1 */
109
    N_("Western European (Latin-9)"), /* mostly superset of Latin-1 */
110
    N_("Western European (Windows-1252)"),
111
    N_("Western European (IBM 00850)"),
112
    /* 2 */
113
    N_("Eastern European (Latin-2)"),
114
    N_("Eastern European (Windows-1250)"),
115
    /* 3 */
116
    N_("Esperanto (Latin-3)"),
117
    /* 4 */
118
    N_("Nordic (Latin-6)"), /* Latin 6 supersedes Latin 4 */
119
    /* 5 */
120
    N_("Cyrillic (Windows-1251)"), /* ISO 8859-5 is not practically used */
121
    N_("Russian (KOI8-R)"),
122
    N_("Ukrainian (KOI8-U)"),
123
    /* 6 */
124
    N_("Arabic (ISO 8859-6)"),
125
    N_("Arabic (Windows-1256)"),
126
    /* 7 */
127
    N_("Greek (ISO 8859-7)"),
128
    N_("Greek (Windows-1253)"),
129
    /* 8 */
130
    N_("Hebrew (ISO 8859-8)"),
131
    N_("Hebrew (Windows-1255)"),
132
    /* 9 */
133
    N_("Turkish (ISO 8859-9)"),
134
    N_("Turkish (Windows-1254)"),
135
    /* 10 -> 4 */
136
    /* 11 */
137
    N_("Thai (TIS 620-2533/ISO 8859-11)"),
138
    N_("Thai (Windows-874)"),
139
    /* 13 */
140
    N_("Baltic (Latin-7)"),
141
    N_("Baltic (Windows-1257)"),
142
    /* 12 -> /dev/null */
143
    /* 14 */
144
    N_("Celtic (Latin-8)"),
145
    /* 15 -> 1 */
146
    /* 16 */
147
    N_("South-Eastern European (Latin-10)"),
148
  /* CJK families */
149
    N_("Simplified Chinese (ISO-2022-CN-EXT)"),
150
    N_("Simplified Chinese Unix (EUC-CN)"),
151
    N_("Japanese (7-bits JIS/ISO-2022-JP-2)"),
152
    N_("Japanese Unix (EUC-JP)"),
153
    N_("Japanese (Shift JIS)"),
154
    N_("Korean (EUC-KR/CP949)"),
155
    N_("Korean (ISO-2022-KR)"),
156
    N_("Traditional Chinese (Big5)"),
157
    N_("Traditional Chinese Unix (EUC-TW)"),
158
    N_("Hong-Kong Supplementary (HKSCS)"),
159
  /* Other */
160
    N_("Vietnamese (VISCII)"),
161
    N_("Vietnamese (Windows-1258)"),
162
};
163
164
static const int  pi_justification[] = { -1, 0, SUBPICTURE_ALIGN_LEFT, SUBPICTURE_ALIGN_RIGHT };
165
static const char *const ppsz_justification_text[] = {
166
    N_("Auto"),N_("Center"),N_("Left"),N_("Right")
167
};
168
169
#define ENCODING_TEXT N_("Subtitle text encoding")
170
#define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
171
#define ALIGN_TEXT N_("Subtitle justification")
172
#define ALIGN_LONGTEXT N_("Set the justification of subtitles")
173
#define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitle autodetection")
174
#define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
175
            "UTF-8 encoding within subtitle files.")
176
177
static int  OpenDecoder   ( vlc_object_t * );
178
static void CloseDecoder  ( vlc_object_t * );
179
180
174
vlc_module_begin ()
181
87
    set_shortname( N_("Subtitles"))
182
87
    set_description( N_("Text subtitle decoder") )
183
87
    set_capability( "spu decoder", 50 )
184
174
    set_callbacks( OpenDecoder, CloseDecoder )
185
87
    set_subcategory( SUBCAT_INPUT_SCODEC )
186
187
87
    add_integer( "subsdec-align", -1, ALIGN_TEXT, ALIGN_LONGTEXT )
188
87
        change_integer_list( pi_justification, ppsz_justification_text )
189
87
    add_string( "subsdec-encoding", "",
190
87
                ENCODING_TEXT, ENCODING_LONGTEXT )
191
87
        change_string_list( ppsz_encodings, ppsz_encoding_names )
192
87
    add_bool( "subsdec-autodetect-utf8", true,
193
87
              AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT )
194
87
vlc_module_end ()
195
196
/*****************************************************************************
197
 * Local prototypes
198
 *****************************************************************************/
199
#define NO_BREAKING_SPACE  "&#160;"
200
201
typedef struct
202
{
203
    int                 i_align;          /* Subtitles alignment on the vout */
204
205
    vlc_iconv_t         iconv_handle;            /* handle to iconv instance */
206
    bool                b_autodetect_utf8;
207
} decoder_sys_t;
208
209
210
static int             DecodeBlock   ( decoder_t *, block_t * );
211
static subpicture_t   *ParseText     ( decoder_t *, block_t * );
212
static text_segment_t *ParseSubtitles(int *pi_align, const char * );
213
214
/*****************************************************************************
215
 * OpenDecoder: probe the decoder and return score
216
 *****************************************************************************
217
 * Tries to launch a decoder and return score so that the interface is able
218
 * to chose.
219
 *****************************************************************************/
220
static int OpenDecoder( vlc_object_t *p_this )
221
16.9k
{
222
16.9k
    decoder_t     *p_dec = (decoder_t*)p_this;
223
16.9k
    decoder_sys_t *p_sys;
224
225
16.9k
    switch( p_dec->fmt_in->i_codec )
226
16.9k
    {
227
6.26k
        case VLC_CODEC_SUBT:
228
6.26k
        case VLC_CODEC_ITU_T140:
229
6.26k
            break;
230
10.7k
        default:
231
10.7k
            return VLC_EGENERIC;
232
16.9k
    }
233
234
    /* Allocate the memory needed to store the decoder's structure */
235
6.26k
    p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
236
6.26k
    if( p_sys == NULL )
237
0
        return VLC_ENOMEM;
238
239
6.26k
    p_dec->pf_decode = DecodeBlock;
240
6.26k
    p_dec->fmt_out.i_codec = 0;
241
242
    /* init of p_sys */
243
6.26k
    p_sys->i_align = -1;
244
6.26k
    p_sys->iconv_handle = (vlc_iconv_t)-1;
245
6.26k
    p_sys->b_autodetect_utf8 = false;
246
247
6.26k
    const char *encoding;
248
6.26k
    char *var = NULL;
249
250
    /* First try demux-specified encoding */
251
6.26k
    if( p_dec->fmt_in->i_codec == VLC_CODEC_ITU_T140 )
252
0
        encoding = "UTF-8"; /* IUT T.140 is always using UTF-8 */
253
6.26k
    else
254
6.26k
    if( p_dec->fmt_in->subs.psz_encoding && *p_dec->fmt_in->subs.psz_encoding )
255
66
    {
256
66
        encoding = p_dec->fmt_in->subs.psz_encoding;
257
66
        msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
258
66
                 encoding);
259
66
    }
260
6.20k
    else
261
6.20k
    {
262
        /* Second, try configured encoding */
263
6.20k
        if ((var = var_InheritString (p_dec, "subsdec-encoding")) != NULL)
264
0
        {
265
0
            msg_Dbg (p_dec, "trying configured character encoding: %s", var);
266
0
            if (!strcmp (var, "system"))
267
0
            {
268
0
                free (var);
269
0
                var = NULL;
270
0
                encoding = "";
271
                /* ^ iconv() treats "" as nl_langinfo(CODESET) */
272
0
            }
273
0
            else
274
0
                encoding = var;
275
0
        }
276
6.20k
        else
277
        /* Third, try "local" encoding */
278
6.20k
        {
279
        /* xgettext:
280
           The Windows ANSI code page most commonly used for this language.
281
           VLC uses this as a guess of the subtitle files character set
282
           (if UTF-8 and UTF-16 autodetection fails).
283
           Western European languages normally use "CP1252", which is a
284
           Microsoft-variant of ISO 8859-1. That suits the Latin alphabet.
285
           Other scripts use other code pages.
286
287
           This MUST be a valid iconv character set. If unsure, please refer
288
           the VideoLAN translators mailing list. */
289
6.20k
            encoding = vlc_pgettext("GetACP", "CP1252");
290
6.20k
            msg_Dbg (p_dec, "trying default character encoding: %s", encoding);
291
6.20k
        }
292
293
        /* Check UTF-8 autodetection */
294
6.20k
        if (var_InheritBool (p_dec, "subsdec-autodetect-utf8"))
295
6.20k
        {
296
6.20k
            msg_Dbg (p_dec, "using automatic UTF-8 detection");
297
6.20k
            p_sys->b_autodetect_utf8 = true;
298
6.20k
        }
299
6.20k
    }
300
301
6.26k
    if (strcasecmp (encoding, "UTF-8") && strcasecmp (encoding, "utf8"))
302
6.20k
    {
303
6.20k
        p_sys->iconv_handle = vlc_iconv_open ("UTF-8", encoding);
304
6.20k
        if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
305
6.20k
            msg_Err (p_dec, "cannot convert from %s: %s", encoding,
306
6.20k
                     vlc_strerror_c(errno));
307
6.20k
    }
308
6.26k
    free (var);
309
310
6.26k
    p_sys->i_align = var_InheritInteger( p_dec, "subsdec-align" );
311
312
6.26k
    return VLC_SUCCESS;
313
6.26k
}
314
315
/****************************************************************************
316
 * DecodeBlock: the whole thing
317
 ****************************************************************************
318
 * This function must be fed with complete subtitles units.
319
 ****************************************************************************/
320
static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
321
1.45M
{
322
1.45M
    subpicture_t *p_spu;
323
324
1.45M
    if( p_block == NULL ) /* No Drain */
325
734k
        return VLCDEC_SUCCESS;
326
327
723k
    if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
328
0
    {
329
0
        block_Release( p_block );
330
0
        return VLCDEC_SUCCESS;
331
0
    }
332
333
723k
    p_spu = ParseText( p_dec, p_block );
334
335
723k
    block_Release( p_block );
336
723k
    if( p_spu != NULL )
337
702k
        decoder_QueueSub( p_dec, p_spu );
338
723k
    return VLCDEC_SUCCESS;
339
723k
}
340
341
/*****************************************************************************
342
 * CloseDecoder: clean up the decoder
343
 *****************************************************************************/
344
static void CloseDecoder( vlc_object_t *p_this )
345
6.26k
{
346
6.26k
    decoder_t *p_dec = (decoder_t *)p_this;
347
6.26k
    decoder_sys_t *p_sys = p_dec->p_sys;
348
349
6.26k
    if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
350
6.20k
        vlc_iconv_close( p_sys->iconv_handle );
351
352
6.26k
    free( p_sys );
353
6.26k
}
354
355
/*****************************************************************************
356
 * ParseText: parse an text subtitle packet and send it to the video output
357
 *****************************************************************************/
358
static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
359
723k
{
360
723k
    decoder_sys_t *p_sys = p_dec->p_sys;
361
723k
    subpicture_t *p_spu = NULL;
362
363
723k
    if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
364
0
        return NULL;
365
366
    /* We cannot display a subpicture with no date */
367
723k
    if( p_block->i_pts == VLC_TICK_INVALID )
368
177
    {
369
177
        msg_Warn( p_dec, "subtitle without a date" );
370
177
        return NULL;
371
177
    }
372
373
    /* Check validity of packet data */
374
    /* An "empty" line containing only \0 can be used to force
375
       and ephemer picture from the screen */
376
722k
    if( p_block->i_buffer < 1 )
377
11
    {
378
11
        msg_Warn( p_dec, "no subtitle data" );
379
11
        return NULL;
380
11
    }
381
382
722k
    char *psz_subtitle = NULL;
383
384
    /* Should be resiliant against bad subtitles */
385
722k
    if( p_sys->iconv_handle == (vlc_iconv_t)-1 ||
386
722k
        p_sys->b_autodetect_utf8 )
387
14.4k
    {
388
14.4k
        psz_subtitle = malloc( p_block->i_buffer + 1 );
389
14.4k
        if( psz_subtitle == NULL )
390
0
            return NULL;
391
14.4k
        memcpy( psz_subtitle, p_block->p_buffer, p_block->i_buffer );
392
14.4k
        psz_subtitle[p_block->i_buffer] = '\0';
393
14.4k
    }
394
395
722k
    if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
396
336
    {
397
336
        if (EnsureUTF8( psz_subtitle ) == NULL)
398
193
        {
399
193
            msg_Err( p_dec, "failed to convert subtitle encoding.\n"
400
193
                     "Try manually setting a character-encoding "
401
193
                     "before you open the file." );
402
193
        }
403
336
    }
404
722k
    else
405
722k
    {
406
722k
        if( p_sys->b_autodetect_utf8 )
407
14.0k
        {
408
14.0k
            if( IsUTF8( psz_subtitle ) == NULL )
409
1.72k
            {
410
1.72k
                msg_Dbg( p_dec, "invalid UTF-8 sequence: "
411
1.72k
                         "disabling UTF-8 subtitles autodetection" );
412
1.72k
                p_sys->b_autodetect_utf8 = false;
413
1.72k
            }
414
14.0k
        }
415
416
722k
        if( !p_sys->b_autodetect_utf8 )
417
710k
        {
418
710k
            size_t inbytes_left = p_block->i_buffer;
419
710k
            size_t outbytes_left = 6 * inbytes_left;
420
710k
            char *psz_new_subtitle = xmalloc( outbytes_left + 1 );
421
710k
            char *psz_convert_buffer_out = psz_new_subtitle;
422
710k
            const char *psz_convert_buffer_in =
423
710k
                    psz_subtitle ? psz_subtitle : (char *)p_block->p_buffer;
424
425
710k
            size_t ret = vlc_iconv( p_sys->iconv_handle,
426
710k
                                    &psz_convert_buffer_in, &inbytes_left,
427
710k
                                    &psz_convert_buffer_out, &outbytes_left );
428
429
710k
            *psz_convert_buffer_out++ = '\0';
430
710k
            free( psz_subtitle );
431
432
710k
            if( ( ret == (size_t)(-1) ) || inbytes_left )
433
20.6k
            {
434
20.6k
                free( psz_new_subtitle );
435
20.6k
                msg_Err( p_dec, "failed to convert subtitle encoding.\n"
436
20.6k
                        "Try manually setting a character-encoding "
437
20.6k
                                "before you open the file." );
438
20.6k
                return NULL;
439
20.6k
            }
440
441
689k
            psz_subtitle = realloc( psz_new_subtitle,
442
689k
                                    psz_convert_buffer_out - psz_new_subtitle );
443
689k
            if( !psz_subtitle )
444
0
                psz_subtitle = psz_new_subtitle;
445
689k
        }
446
722k
    }
447
448
    /* Create the subpicture unit */
449
702k
    p_spu = decoder_NewSubpictureText( p_dec );
450
702k
    if( !p_spu )
451
0
    {
452
0
        free( psz_subtitle );
453
0
        return NULL;
454
0
    }
455
702k
    p_spu->i_start    = p_block->i_pts;
456
702k
    p_spu->i_stop     = p_block->i_pts + p_block->i_length;
457
702k
    p_spu->b_ephemer  = (p_block->i_length == 0);
458
459
702k
    subtext_updater_sys_t *p_spu_sys = p_spu->updater.sys;
460
461
702k
    int i_inline_align = -1;
462
702k
    p_spu_sys->region.p_segments = ParseSubtitles( &i_inline_align, psz_subtitle );
463
702k
    free( psz_subtitle );
464
702k
    p_spu_sys->region.b_absolute = false;
465
702k
    p_spu_sys->region.b_in_window = true;
466
702k
    if( p_sys->i_align >= 0 ) /* bottom ; left, right or centered */
467
0
    {
468
0
        p_spu_sys->region.align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
469
0
        p_spu_sys->region.inner_align = p_sys->i_align;
470
0
    }
471
702k
    else if( i_inline_align >= 0 )
472
267
    {
473
267
        p_spu_sys->region.align = i_inline_align;
474
267
        p_spu_sys->region.inner_align = i_inline_align;
475
267
    }
476
702k
    else /* default, bottom ; centered */
477
702k
    {
478
702k
        p_spu_sys->region.align = SUBPICTURE_ALIGN_BOTTOM;
479
702k
        p_spu_sys->region.inner_align = 0;
480
702k
    }
481
482
702k
    return p_spu;
483
702k
}
484
485
static bool AppendCharacter( text_segment_t* p_segment, char c )
486
3.91M
{
487
3.91M
    char* tmp;
488
3.91M
    if ( asprintf( &tmp, "%s%c", p_segment->psz_text ? p_segment->psz_text : "", c ) < 0 )
489
0
        return false;
490
3.91M
    free( p_segment->psz_text );
491
3.91M
    p_segment->psz_text = tmp;
492
3.91M
    return true;
493
3.91M
}
494
495
static bool AppendString( text_segment_t* p_segment, const char* psz_str )
496
56.5k
{
497
56.5k
    char* tmp;
498
56.5k
    if ( asprintf( &tmp, "%s%s", p_segment->psz_text ? p_segment->psz_text : "", psz_str ) < 0 )
499
0
        return false;
500
56.5k
    free( p_segment->psz_text );
501
56.5k
    p_segment->psz_text = tmp;
502
56.5k
    return true;
503
56.5k
}
504
505
static char* ConsumeAttribute( const char** ppsz_subtitle, char** ppsz_attribute_value )
506
16.9k
{
507
16.9k
    const char* psz_subtitle = *ppsz_subtitle;
508
16.9k
    char* psz_attribute_name;
509
16.9k
    *ppsz_attribute_value = NULL;
510
511
29.3k
    while (*psz_subtitle == ' ')
512
12.4k
        psz_subtitle++;
513
514
16.9k
    size_t attr_len = 0;
515
16.9k
    char delimiter;
516
517
74.3k
    while ( *psz_subtitle && isalpha( *psz_subtitle ) )
518
57.4k
    {
519
57.4k
        psz_subtitle++;
520
57.4k
        attr_len++;
521
57.4k
    }
522
16.9k
    if ( !*psz_subtitle || attr_len == 0 )
523
4.52k
        return NULL;
524
12.4k
    psz_attribute_name = malloc( attr_len + 1 );
525
12.4k
    if ( unlikely( !psz_attribute_name ) )
526
0
        return NULL;
527
12.4k
    strncpy( psz_attribute_name, psz_subtitle - attr_len, attr_len );
528
12.4k
    psz_attribute_name[attr_len] = 0;
529
530
    // Skip over to the attribute value
531
48.7k
    while ( *psz_subtitle && *psz_subtitle != '=' )
532
36.2k
        psz_subtitle++;
533
12.4k
    if ( !*psz_subtitle )
534
294
    {
535
294
        *ppsz_subtitle = psz_subtitle;
536
294
        return psz_attribute_name;
537
294
    }
538
    // Skip the '=' sign
539
12.1k
    psz_subtitle++;
540
541
    // Aknoledge the delimiter if any
542
12.5k
    while ( *psz_subtitle && isspace( *psz_subtitle) )
543
379
        psz_subtitle++;
544
545
12.1k
    if ( *psz_subtitle == '\'' || *psz_subtitle == '"' )
546
3.15k
    {
547
        // Save the delimiter and skip it
548
3.15k
        delimiter = *psz_subtitle;
549
3.15k
        psz_subtitle++;
550
3.15k
    }
551
8.98k
    else
552
8.98k
        delimiter = 0;
553
554
    // Skip spaces, just in case
555
12.6k
    while ( *psz_subtitle && isspace( *psz_subtitle ) )
556
491
        psz_subtitle++;
557
558
12.1k
    attr_len = 0;
559
232k
    while ( *psz_subtitle && ( ( delimiter != 0 && *psz_subtitle != delimiter ) ||
560
90.1k
                               ( delimiter == 0 && ( !isspace(*psz_subtitle) && *psz_subtitle != '>' ) ) ) )
561
220k
    {
562
220k
        psz_subtitle++;
563
220k
        attr_len++;
564
220k
    }
565
12.1k
    if ( attr_len == 0 )
566
477
    {
567
477
        *ppsz_subtitle = psz_subtitle;
568
477
        return psz_attribute_name;
569
477
    }
570
11.6k
    if ( unlikely( !( *ppsz_attribute_value = malloc( attr_len + 1 ) ) ) )
571
0
    {
572
0
        free( psz_attribute_name );
573
0
        return NULL;
574
0
    }
575
11.6k
    strncpy( *ppsz_attribute_value, psz_subtitle - attr_len, attr_len );
576
11.6k
    (*ppsz_attribute_value)[attr_len] = 0;
577
    // Finally, skip over the final delimiter
578
11.6k
    if (delimiter != 0 && *psz_subtitle)
579
2.59k
        psz_subtitle++;
580
11.6k
    *ppsz_subtitle = psz_subtitle;
581
11.6k
    return psz_attribute_name;
582
11.6k
}
583
584
// Returns the next tag and consume the string up to after the tag name, or
585
// returns NULL and doesn't advance if the angle bracket was not a tag opening
586
// For instance, if psz_subtitle == "<some_tag attribute=value>"
587
// GetTag will return "some_tag", and will advance up to the first 'a' in "attribute"
588
// The returned value must be freed.
589
static char* GetTag( const char** ppsz_subtitle, bool b_closing )
590
176k
{
591
176k
    const char* psz_subtitle = *ppsz_subtitle;
592
176k
    if ( *psz_subtitle != '<' )
593
0
        return NULL;
594
    // Skip the '<'
595
176k
    psz_subtitle++;
596
176k
    if ( b_closing && *psz_subtitle == '/' )
597
57.3k
        psz_subtitle++;
598
    // Skip potential spaces
599
177k
    while ( *psz_subtitle == ' ' )
600
1.29k
        psz_subtitle++;
601
    // Now we need to verify if what comes next is a valid tag:
602
176k
    if ( !isalpha( *psz_subtitle ) )
603
106k
        return NULL;
604
70.6k
    size_t tag_size = 1;
605
800k
    while ( isalnum( psz_subtitle[tag_size] ) || psz_subtitle[tag_size] == '_' )
606
730k
        tag_size++;
607
70.6k
    char* psz_tagname = vlc_alloc( tag_size + 1, sizeof( *psz_tagname ) );
608
70.6k
    if ( unlikely( !psz_tagname ) )
609
0
        return NULL;
610
70.6k
    strncpy( psz_tagname, psz_subtitle, tag_size );
611
70.6k
    psz_tagname[tag_size] = 0;
612
70.6k
    psz_subtitle += tag_size;
613
70.6k
    *ppsz_subtitle = psz_subtitle;
614
70.6k
    return psz_tagname;
615
70.6k
}
616
617
static bool IsClosed( const char* psz_subtitle, const char* psz_tagname )
618
17.5k
{
619
17.5k
    const char* psz_tagpos = strcasestr( psz_subtitle, psz_tagname );
620
17.5k
    if ( !psz_tagpos )
621
4.05k
        return false;
622
    // Search for '</' and '>' immediately before & after (minding the potential spaces)
623
13.5k
    const char* psz_endtag = psz_tagpos + strlen( psz_tagname );
624
13.9k
    while ( *psz_endtag == ' ' )
625
426
        psz_endtag++;
626
13.5k
    if ( *psz_endtag != '>' )
627
1.48k
        return false;
628
    // Skip back before the tag itself
629
12.0k
    psz_tagpos--;
630
12.5k
    while ( *psz_tagpos == ' ' && psz_tagpos > psz_subtitle )
631
537
        psz_tagpos--;
632
12.0k
    if ( *psz_tagpos-- != '/' )
633
10.0k
        return false;
634
1.99k
    if ( *psz_tagpos != '<' )
635
256
        return false;
636
1.73k
    return true;
637
1.99k
}
638
639
typedef struct tag_stack tag_stack_t;
640
struct tag_stack
641
{
642
    char* psz_tagname;
643
    tag_stack_t *p_next;
644
};
645
646
static void AppendTag( tag_stack_t **pp_stack, char* psz_tagname )
647
1.73k
{
648
1.73k
    tag_stack_t* p_elem = malloc( sizeof( *p_elem ) );
649
1.73k
    if ( unlikely( !p_elem ) )
650
0
        return;
651
1.73k
    p_elem->p_next = *pp_stack;
652
1.73k
    p_elem->psz_tagname = psz_tagname;
653
1.73k
    *pp_stack = p_elem;
654
1.73k
}
655
656
static bool HasTag( tag_stack_t **pp_stack, const char* psz_tagname )
657
4.06k
{
658
4.06k
    tag_stack_t *p_prev = NULL;
659
12.9k
    for ( tag_stack_t* p_current = *pp_stack; p_current; p_current = p_current->p_next )
660
9.65k
    {
661
9.65k
        if ( !strcasecmp( psz_tagname, p_current->psz_tagname ) )
662
820
        {
663
820
            if ( p_current == *pp_stack )
664
496
            {
665
496
                *pp_stack = p_current->p_next;
666
496
            }
667
324
            else
668
324
            {
669
324
                p_prev->p_next = p_current->p_next;
670
324
            }
671
820
            free( p_current->psz_tagname );
672
820
            free( p_current );
673
820
            return true;
674
820
        }
675
8.83k
        p_prev = p_current;
676
8.83k
    }
677
3.24k
    return false;
678
4.06k
}
679
680
/*
681
 * mini style stack implementation
682
 */
683
typedef struct style_stack style_stack_t;
684
struct  style_stack
685
{
686
    text_style_t* p_style;
687
    style_stack_t* p_next;
688
};
689
690
static text_style_t* DuplicateAndPushStyle(style_stack_t** pp_stack)
691
29.9k
{
692
29.9k
    text_style_t* p_dup = ( *pp_stack ) ? text_style_Duplicate( (*pp_stack)->p_style ) : text_style_Create( STYLE_NO_DEFAULTS );
693
29.9k
    if ( unlikely( !p_dup ) )
694
0
        return NULL;
695
29.9k
    style_stack_t* p_entry = malloc( sizeof( *p_entry ) );
696
29.9k
    if ( unlikely( !p_entry ) )
697
0
    {
698
0
        text_style_Delete( p_dup );
699
0
        return NULL;
700
0
    }
701
    // Give the style ownership to the segment.
702
29.9k
    p_entry->p_style = p_dup;
703
29.9k
    p_entry->p_next = *pp_stack;
704
29.9k
    *pp_stack = p_entry;
705
29.9k
    return p_dup;
706
29.9k
}
707
708
static void PopStyle(style_stack_t** pp_stack)
709
48.3k
{
710
48.3k
    style_stack_t* p_old = *pp_stack;
711
48.3k
    if ( !p_old )
712
18.3k
        return;
713
29.9k
    *pp_stack = p_old->p_next;
714
    // Don't free the style, it is now owned by the text_segment_t
715
29.9k
    free( p_old );
716
29.9k
}
717
718
static text_segment_t* NewTextSegmentPushStyle( text_segment_t* p_segment, style_stack_t** pp_stack )
719
29.9k
{
720
29.9k
    text_segment_t* p_new = text_segment_New( NULL );
721
29.9k
    if ( unlikely( p_new == NULL ) )
722
0
        return NULL;
723
29.9k
    text_style_t* p_style = DuplicateAndPushStyle( pp_stack );
724
29.9k
    p_new->style = p_style;
725
29.9k
    p_segment->p_next = p_new;
726
29.9k
    return p_new;
727
29.9k
}
728
729
static text_segment_t* NewTextSegmentPopStyle( text_segment_t* p_segment, style_stack_t** pp_stack )
730
19.8k
{
731
19.8k
    text_segment_t* p_new = text_segment_New( NULL );
732
19.8k
    if ( unlikely( p_new == NULL ) )
733
0
        return NULL;
734
    // We shouldn't have an empty stack since this happens when closing a tag,
735
    // but better be safe than sorry if (/when) we encounter a broken subtitle file.
736
19.8k
    PopStyle( pp_stack );
737
19.8k
    text_style_t* p_dup = ( *pp_stack ) ? text_style_Duplicate( (*pp_stack)->p_style ) : text_style_Create( STYLE_NO_DEFAULTS );
738
19.8k
    p_new->style = p_dup;
739
19.8k
    p_segment->p_next = p_new;
740
19.8k
    return p_new;
741
19.8k
}
742
743
static text_segment_t* ParseSubtitles( int *pi_align, const char *psz_subtitle )
744
702k
{
745
702k
    text_segment_t* p_segment;
746
702k
    text_segment_t* p_first_segment;
747
702k
    style_stack_t* p_stack = NULL;
748
702k
    tag_stack_t* p_tag_stack = NULL;
749
750
    //FIXME: Remove initial allocation? Might make the below code more complicated
751
702k
    p_first_segment = p_segment = text_segment_New( "" );
752
753
702k
    *pi_align = -1;
754
755
    /* */
756
4.69M
    while( *psz_subtitle )
757
3.99M
    {
758
        /* HTML extensions */
759
3.99M
        if( *psz_subtitle == '<' )
760
119k
        {
761
119k
            char *psz_tagname = GetTag( &psz_subtitle, false );
762
119k
            if ( psz_tagname != NULL )
763
46.7k
            {
764
46.7k
                if( !strcasecmp( psz_tagname, "br" ) )
765
1.20k
                {
766
1.20k
                    if ( !AppendCharacter( p_segment, '\n' ) )
767
0
                    {
768
0
                        free( psz_tagname );
769
0
                        goto fail;
770
0
                    }
771
1.20k
                }
772
45.4k
                else if( !strcasecmp( psz_tagname, "b" ) )
773
19.3k
                {
774
19.3k
                    p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
775
19.3k
                    p_segment->style->i_style_flags |= STYLE_BOLD;
776
19.3k
                    p_segment->style->i_features |= STYLE_HAS_FLAGS;
777
19.3k
                }
778
26.1k
                else if( !strcasecmp( psz_tagname, "i" ) )
779
2.53k
                {
780
2.53k
                    p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
781
2.53k
                    p_segment->style->i_style_flags |= STYLE_ITALIC;
782
2.53k
                    p_segment->style->i_features |= STYLE_HAS_FLAGS;
783
2.53k
                }
784
23.6k
                else if( !strcasecmp( psz_tagname, "u" ) )
785
399
                {
786
399
                    p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
787
399
                    p_segment->style->i_style_flags |= STYLE_UNDERLINE;
788
399
                    p_segment->style->i_features |= STYLE_HAS_FLAGS;
789
399
                }
790
23.2k
                else if( !strcasecmp( psz_tagname, "s" ) )
791
1.11k
                {
792
1.11k
                    p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
793
1.11k
                    p_segment->style->i_style_flags |= STYLE_STRIKEOUT;
794
1.11k
                    p_segment->style->i_features |= STYLE_HAS_FLAGS;
795
1.11k
                }
796
22.1k
                else if( !strcasecmp( psz_tagname, "font" ) )
797
4.52k
                {
798
4.52k
                    p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
799
800
4.52k
                    char* psz_attribute_name;
801
4.52k
                    char* psz_attribute_value;
802
803
16.9k
                    while( ( psz_attribute_name = ConsumeAttribute( &psz_subtitle, &psz_attribute_value ) ) )
804
12.4k
                    {
805
12.4k
                        if ( !psz_attribute_value )
806
771
                        {
807
771
                            free( psz_attribute_name );
808
771
                            continue;
809
771
                        }
810
11.6k
                        if ( !strcasecmp( psz_attribute_name, "face" ) )
811
242
                        {
812
242
                            free(p_segment->style->psz_fontname);
813
242
                            p_segment->style->psz_fontname = psz_attribute_value;
814
                            // We don't want to free the attribute value since it has become our fontname
815
242
                            psz_attribute_value = NULL;
816
242
                        }
817
11.4k
                        else if ( !strcasecmp( psz_attribute_name, "family" ) )
818
348
                        {
819
348
                            free(p_segment->style->psz_monofontname);
820
348
                            p_segment->style->psz_monofontname = psz_attribute_value;
821
348
                            psz_attribute_value = NULL;
822
348
                        }
823
11.0k
                        else if ( !strcasecmp( psz_attribute_name, "size" ) )
824
471
                        {
825
471
                            int size = atoi( psz_attribute_value );
826
471
                            if( size )
827
246
                            {
828
246
                                p_segment->style->i_font_size = size;
829
246
                                p_segment->style->f_font_relsize = STYLE_DEFAULT_REL_FONT_SIZE *
830
246
                                        STYLE_DEFAULT_FONT_SIZE / p_segment->style->i_font_size;
831
246
                            }
832
471
                        }
833
10.5k
                        else if ( !strcasecmp( psz_attribute_name, "color" ) )
834
8.31k
                        {
835
8.31k
                            p_segment->style->i_font_color = vlc_html_color( psz_attribute_value, NULL );
836
8.31k
                            p_segment->style->i_features |= STYLE_HAS_FONT_COLOR;
837
8.31k
                        }
838
2.28k
                        else if ( !strcasecmp( psz_attribute_name, "outline-color" ) )
839
0
                        {
840
0
                            p_segment->style->i_outline_color = vlc_html_color( psz_attribute_value, NULL );
841
0
                            p_segment->style->i_features |= STYLE_HAS_OUTLINE_COLOR;
842
0
                        }
843
2.28k
                        else if ( !strcasecmp( psz_attribute_name, "shadow-color" ) )
844
0
                        {
845
0
                            p_segment->style->i_shadow_color = vlc_html_color( psz_attribute_value, NULL );
846
0
                            p_segment->style->i_features |= STYLE_HAS_SHADOW_COLOR;
847
0
                        }
848
2.28k
                        else if ( !strcasecmp( psz_attribute_name, "outline-level" ) )
849
0
                        {
850
0
                            p_segment->style->i_outline_width = atoi( psz_attribute_value );
851
0
                        }
852
2.28k
                        else if ( !strcasecmp( psz_attribute_name, "shadow-level" ) )
853
0
                        {
854
0
                            p_segment->style->i_shadow_width = atoi( psz_attribute_value );
855
0
                        }
856
2.28k
                        else if ( !strcasecmp( psz_attribute_name, "back-color" ) )
857
0
                        {
858
0
                            p_segment->style->i_background_color = vlc_html_color( psz_attribute_value, NULL );
859
0
                            p_segment->style->i_features |= STYLE_HAS_BACKGROUND_COLOR;
860
0
                        }
861
2.28k
                        else if ( !strcasecmp( psz_attribute_name, "alpha" ) )
862
209
                        {
863
209
                            p_segment->style->i_font_alpha = atoi( psz_attribute_value );
864
209
                            p_segment->style->i_features |= STYLE_HAS_FONT_ALPHA;
865
209
                        }
866
867
11.6k
                        free( psz_attribute_name );
868
11.6k
                        free( psz_attribute_value );
869
11.6k
                    }
870
4.52k
                }
871
17.5k
                else
872
17.5k
                {
873
                    // This is an unknown tag. We need to hide it if it's properly closed, and display it otherwise
874
17.5k
                    if ( !IsClosed( psz_subtitle, psz_tagname ) )
875
15.8k
                    {
876
15.8k
                        AppendCharacter( p_segment, '<' );
877
15.8k
                        AppendString( p_segment, psz_tagname );
878
15.8k
                        AppendCharacter( p_segment, '>' );
879
15.8k
                    }
880
1.73k
                    else
881
1.73k
                    {
882
1.73k
                        AppendTag( &p_tag_stack, psz_tagname );
883
                        // We don't want to free the tagname now, it will be freed when the tag
884
                        // gets poped from the stack.
885
1.73k
                        psz_tagname = NULL;
886
1.73k
                    }
887
                    // In any case, fall through and skip to the closing tag.
888
17.5k
                }
889
                // Skip potential spaces & end tag
890
354k
                while ( *psz_subtitle && *psz_subtitle != '>' )
891
308k
                    psz_subtitle++;
892
46.7k
                if ( *psz_subtitle == '>' )
893
40.9k
                    psz_subtitle++;
894
895
46.7k
                free( psz_tagname );
896
46.7k
            }
897
72.5k
            else if( !strncmp( psz_subtitle, "</", 2 ))
898
57.3k
            {
899
57.3k
                char* psz_closetagname = GetTag( &psz_subtitle, true );
900
57.3k
                if ( psz_closetagname != NULL )
901
23.9k
                {
902
23.9k
                    if ( !strcasecmp( psz_closetagname, "b" ) ||
903
6.95k
                         !strcasecmp( psz_closetagname, "i" ) ||
904
6.00k
                         !strcasecmp( psz_closetagname, "u" ) ||
905
5.65k
                         !strcasecmp( psz_closetagname, "s" ) ||
906
5.23k
                         !strcasecmp( psz_closetagname, "font" ) )
907
19.8k
                    {
908
                        // A closing tag for one of the tags we handle, meaning
909
                        // we pushed a style onto the stack earlier
910
19.8k
                        p_segment = NewTextSegmentPopStyle( p_segment, &p_stack );
911
19.8k
                    }
912
4.06k
                    else
913
4.06k
                    {
914
                        // Unknown closing tag. If it is closing an unknown tag, ignore it. Otherwise, display it
915
4.06k
                        if ( !HasTag( &p_tag_stack, psz_closetagname ) )
916
3.24k
                        {
917
3.24k
                            AppendString( p_segment, "</" );
918
3.24k
                            AppendString( p_segment, psz_closetagname );
919
3.24k
                            AppendCharacter( p_segment, '>' );
920
3.24k
                        }
921
4.06k
                    }
922
25.0k
                    while ( *psz_subtitle == ' ' )
923
1.09k
                        psz_subtitle++;
924
23.9k
                    if ( *psz_subtitle == '>' )
925
19.9k
                        psz_subtitle++;
926
23.9k
                    free( psz_closetagname );
927
23.9k
                }
928
33.4k
                else
929
33.4k
                {
930
                    /**
931
                      * This doesn't appear to be a valid tag closing syntax.
932
                      * Simply append the text
933
                      */
934
33.4k
                    AppendString( p_segment, "</" );
935
33.4k
                    psz_subtitle += 2;
936
33.4k
                }
937
57.3k
            }
938
15.2k
            else
939
15.2k
            {
940
                /* We have an unknown tag, just append it, and move on.
941
                 * The rest of the string won't be recognized as a tag, and
942
                 * we will ignore unknown closing tag
943
                 */
944
15.2k
                AppendCharacter( p_segment, '<' );
945
15.2k
                psz_subtitle++;
946
15.2k
            }
947
119k
        }
948
        /* SSA extensions */
949
3.87M
        else if( psz_subtitle[0] == '{' && psz_subtitle[1] == '\\' &&
950
2.32k
                 strchr( psz_subtitle, '}' ) )
951
2.03k
        {
952
            /* Check for forced alignment */
953
2.03k
            if( *pi_align < 0 &&
954
1.83k
                !strncmp( psz_subtitle, "{\\an", 4 ) && psz_subtitle[4] >= '1' && psz_subtitle[4] <= '9' && psz_subtitle[5] == '}' )
955
267
            {
956
267
                static const int pi_vertical[3] = { SUBPICTURE_ALIGN_BOTTOM, 0, SUBPICTURE_ALIGN_TOP };
957
267
                static const int pi_horizontal[3] = { SUBPICTURE_ALIGN_LEFT, 0, SUBPICTURE_ALIGN_RIGHT };
958
267
                const int i_id = psz_subtitle[4] - '1';
959
960
267
                *pi_align = pi_vertical[i_id/3] | pi_horizontal[i_id%3];
961
267
            }
962
            /* TODO fr -> rotation */
963
964
            /* Hide {\stupidity} */
965
2.03k
            psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
966
2.03k
        }
967
        /* MicroDVD extensions */
968
        /* FIXME:
969
         *  - Currently, we don't do difference between X and x, and we should:
970
         *    Capital Letters applies to the whole text and not one line
971
         *  - We don't support Position and Coordinates
972
         *  - We don't support the DEFAULT flag (HEADER)
973
         */
974
975
3.87M
        else if( psz_subtitle[0] == '{' && psz_subtitle[1] != 0 &&
976
9.94k
                 psz_subtitle[2] == ':' && strchr( &psz_subtitle[2], '}' ) )
977
3.64k
        {
978
3.64k
            const char *psz_tag_end = strchr( &psz_subtitle[2], '}' );
979
3.64k
            size_t i_len = psz_tag_end - &psz_subtitle[3];
980
981
3.64k
            if( psz_subtitle[1] == 'Y' || psz_subtitle[1] == 'y' )
982
1.41k
            {
983
1.41k
                if( psz_subtitle[3] == 'i' )
984
216
                {
985
216
                    p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
986
216
                    p_segment->style->i_style_flags |= STYLE_ITALIC;
987
216
                    p_segment->style->i_features |= STYLE_HAS_FLAGS;
988
216
                    psz_subtitle++;
989
216
                }
990
1.41k
                if( psz_subtitle[3] == 'b' )
991
357
                {
992
357
                    p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
993
357
                    p_segment->style->i_style_flags |= STYLE_BOLD;
994
357
                    p_segment->style->i_features |= STYLE_HAS_FLAGS;
995
357
                    psz_subtitle++;
996
357
                }
997
1.41k
                if( psz_subtitle[3] == 'u' )
998
210
                {
999
210
                    p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
1000
210
                    p_segment->style->i_style_flags |= STYLE_UNDERLINE;
1001
210
                    p_segment->style->i_features |= STYLE_HAS_FLAGS;
1002
210
                    psz_subtitle++;
1003
210
                }
1004
1.41k
            }
1005
2.22k
            else if( (psz_subtitle[1] == 'C' || psz_subtitle[1] == 'c' )
1006
1.14k
                    && psz_subtitle[3] == '$' && i_len >= 7 )
1007
614
            {
1008
                /* Yes, they use BBGGRR, instead of RRGGBB */
1009
614
                char psz_color[7];
1010
614
                psz_color[0] = psz_subtitle[8]; psz_color[1] = psz_subtitle[9];
1011
614
                psz_color[2] = psz_subtitle[6]; psz_color[3] = psz_subtitle[7];
1012
614
                psz_color[4] = psz_subtitle[4]; psz_color[5] = psz_subtitle[5];
1013
614
                psz_color[6] = '\0';
1014
614
                p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
1015
614
                p_segment->style->i_font_color = vlc_html_color( psz_color, NULL );
1016
614
                p_segment->style->i_features |= STYLE_HAS_FONT_COLOR;
1017
614
            }
1018
1.61k
            else if( psz_subtitle[1] == 'F' || psz_subtitle[1] == 'f' )
1019
440
            {
1020
440
                p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
1021
440
                free(p_segment->style->psz_fontname);
1022
440
                p_segment->style->psz_fontname = strndup( &psz_subtitle[3], i_len );
1023
440
            }
1024
1.17k
            else if( psz_subtitle[1] == 'S' || psz_subtitle[1] == 's' )
1025
542
            {
1026
542
                int size = atoi( &psz_subtitle[3] );
1027
542
                if( size )
1028
253
                {
1029
253
                    p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
1030
253
                    p_segment->style->i_font_size = size;
1031
253
                    p_segment->style->f_font_relsize = STYLE_DEFAULT_REL_FONT_SIZE *
1032
253
                                STYLE_DEFAULT_FONT_SIZE / p_segment->style->i_font_size;
1033
1034
253
                }
1035
542
            }
1036
            /* Currently unsupported since we don't have access to the i_align flag here
1037
            else if( psz_subtitle[1] == 'P' )
1038
            {
1039
                if( psz_subtitle[3] == "1" )
1040
                    i_align = SUBPICTURE_ALIGN_TOP;
1041
                else if( psz_subtitle[3] == "0" )
1042
                    i_align = SUBPICTURE_ALIGN_BOTTOM;
1043
            } */
1044
            // Hide other {x:y} atrocities, notably {o:x}
1045
3.64k
            psz_subtitle = psz_tag_end + 1;
1046
3.64k
        }
1047
3.86M
        else
1048
3.86M
        {
1049
3.86M
            if( *psz_subtitle == '\n' || !strncasecmp( psz_subtitle, "\\n", 2 ) )
1050
19.3k
            {
1051
19.3k
                if ( !AppendCharacter( p_segment, '\n' ) )
1052
0
                    goto fail;
1053
19.3k
                if ( *psz_subtitle == '\n' )
1054
18.9k
                    psz_subtitle++;
1055
366
                else
1056
366
                    psz_subtitle += 2;
1057
19.3k
            }
1058
3.84M
            else if( !strncasecmp( psz_subtitle, "\\h", 2 ) )
1059
773
            {
1060
773
                if ( !AppendString( p_segment, "\xC2\xA0" ) )
1061
0
                    goto fail;
1062
773
                psz_subtitle += 2;
1063
773
            }
1064
3.84M
            else
1065
3.84M
            {
1066
                //FIXME: Highly inneficient
1067
3.84M
                AppendCharacter( p_segment, *psz_subtitle );
1068
3.84M
                psz_subtitle++;
1069
3.84M
            }
1070
3.86M
        }
1071
3.99M
    }
1072
730k
    while ( p_stack )
1073
28.5k
        PopStyle( &p_stack );
1074
703k
    while ( p_tag_stack )
1075
919
    {
1076
919
        tag_stack_t *p_tag = p_tag_stack;
1077
919
        p_tag_stack = p_tag_stack->p_next;
1078
919
        free( p_tag->psz_tagname );
1079
919
        free( p_tag );
1080
919
    }
1081
1082
702k
    return p_first_segment;
1083
1084
0
fail:
1085
0
    text_segment_ChainDelete( p_first_segment );
1086
    return NULL;
1087
702k
}