@@ -106,6 +106,27 @@ static const struct {
106
106
107
107
#define SUBRIP_MAX_STACKED_FONT_TAGS 16
108
108
109
+ /* Read the attribute value starting at *s, and skip *s past the value.
110
+ * Set out_value to the parsed value, with possible '"' stripped.
111
+ * Return whether the attribute is well formed. */
112
+ static bool read_value (char * * s , struct bstr * out_value )
113
+ {
114
+ char term = 0 ;
115
+ if (* * s == '"' ) {
116
+ term = '"' ;
117
+ (* s )++ ;
118
+ }
119
+ out_value -> start = * s ;
120
+ out_value -> len = 0 ;
121
+ unsigned char * start = * s ;
122
+ unsigned char * end = term ? strchr (start , term ) : strpbrk (start , " >" );
123
+ if (!end )
124
+ return false;
125
+ out_value -> len = end - out_value -> start ;
126
+ * s = end + (term ? 1 : 0 );
127
+ return true;
128
+ }
129
+
109
130
void subassconvert_subrip (const char * orig , char * dest , int dest_buffer_size )
110
131
{
111
132
/* line is not const to avoid warnings with strtol, etc.
@@ -174,34 +195,35 @@ void subassconvert_subrip(const char *orig, char *dest, int dest_buffer_size)
174
195
line += 6 ;
175
196
176
197
while (* line && * line != '>' ) {
177
- if (strncmp (line , "size=\"" , 6 ) == 0 ) {
178
- line += 6 ;
179
- tag -> size = strtol (line , & line , 10 );
180
- if (* line != '"' )
198
+ if (strncmp (line , "size=" , 5 ) == 0 ) {
199
+ line += 5 ;
200
+ struct bstr val ;
201
+ if (!read_value (& line , & val ))
202
+ break ;
203
+ tag -> size = bstrtoll (val , & val , 10 );
204
+ if (val .len )
181
205
break ;
182
206
append_text (& new_line , "{\\fs%d}" , tag -> size );
183
207
tag -> has_size = true;
184
208
has_valid_attr = true;
185
- } else if (strncmp (line , "color=\"" , 7 ) == 0 ) {
186
- line += 7 ;
187
- if (* line == '#' ) {
209
+ } else if (strncmp (line , "color=" , 6 ) == 0 ) {
210
+ line += 6 ;
211
+ struct bstr val ;
212
+ if (!read_value (& line , & val ))
213
+ break ;
214
+ if (bstr_eatstart (& val , bstr ("#" ))) {
188
215
// #RRGGBB format
189
- line ++ ;
190
- tag -> color = strtol (line , & line , 16 ) & 0x00ffffff ;
191
- if (* line != '"' )
216
+ tag -> color = bstrtoll (val , & val , 16 ) & 0x00ffffff ;
217
+ if (val .len )
192
218
break ;
193
219
tag -> color = ((tag -> color & 0xff ) << 16 )
194
220
| (tag -> color & 0xff00 )
195
221
| ((tag -> color & 0xff0000 ) >> 16 );
196
222
} else {
197
223
// Standard web colors
198
- int len = indexof (line , '"' );
199
- if (len <= 0 )
200
- break ;
201
224
for (int i = 0 ; i < FF_ARRAY_ELEMS (subrip_web_colors ); i ++ ) {
202
225
char * color = subrip_web_colors [i ].s ;
203
- if (strlen (color ) == len
204
- && strncasecmp (line , color , len ) == 0 ) {
226
+ if (bstrcasecmp (val , bstr (color )) == 0 ) {
205
227
tag -> color = subrip_web_colors [i ].v ;
206
228
goto foundcolor ;
207
229
}
@@ -211,29 +233,25 @@ void subassconvert_subrip(const char *orig, char *dest, int dest_buffer_size)
211
233
mp_tmsg (MSGT_SUBREADER , MSGL_WARN ,
212
234
"SubRip: unknown font color in subtitle: %s\n" , orig );
213
235
append_text (& new_line , "{\\c}" );
214
- line += len + 1 ;
215
236
continue ;
216
237
217
- foundcolor :
218
- line += len ;
238
+ foundcolor : ;
219
239
}
220
240
append_text (& new_line , "{\\c&H%06X&}" , tag -> color );
221
241
tag -> has_color = true;
222
242
has_valid_attr = true;
223
- } else if (strncmp (line , "face=\"" , 6 ) == 0 ) {
243
+ } else if (strncmp (line , "face=" , 5 ) == 0 ) {
224
244
/* Font face attribute */
225
- line += 6 ;
226
- int len = indexof ( line , '"' ) ;
227
- if (len <= 0 )
245
+ line += 5 ;
246
+ struct bstr val ;
247
+ if (! read_value ( & line , & val ) )
228
248
break ;
229
- tag -> face .start = line ;
230
- tag -> face .len = len ;
231
- line += len ;
249
+ tag -> face = val ;
232
250
append_text (& new_line , "{\\fn%.*s}" , BSTR_P (tag -> face ));
233
251
tag -> has_face = true;
234
252
has_valid_attr = true;
235
- }
236
- line ++ ;
253
+ } else
254
+ line ++ ;
237
255
}
238
256
239
257
if (!has_valid_attr || * line != '>' ) { /* Not valid font tag */
0 commit comments