1 /*****************************************************************************
2 * strings.c: String related functions
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
5 * Copyright (C) 2008-2009 Rémi Denis-Courmont
6 * $Id: a15541afbab7213a1528e57eb309b89f5bdcfc58 $
7 *
8 * Authors: Antoine Cellerier <dionoea at videolan dot org>
9 * Daniel Stranger <vlc at schmaller dot de>
10 * Rémi Denis-Courmont
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
26
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <assert.h>
36
37 /* Needed by vlc_strftime */
38 #include <time.h>
39 #include <limits.h>
40 #include <math.h>
41 #include <string.h>
42 #ifndef HAVE_STRCOLL
43 # define strcoll strcasecmp
44 #endif
45
46 /* Needed by vlc_strfinput */
47 #include <vlc_input.h>
48 #include <vlc_meta.h>
49 #include <vlc_aout.h>
50 #include <vlc_memstream.h>
51
52 #include <vlc_strings.h>
53 #include <vlc_charset.h>
54 #include <vlc_arrays.h>
55 #include <libvlc.h>
56 #include <errno.h>
57
58 static const struct xml_entity_s
59 {
60 char psz_entity[8];
61 char psz_char[4];
62 } xml_entities[] = {
63 /* Important: this list has to be in alphabetical order (psz_entity-wise) */
64 { "AElig;", "Æ" },
65 { "Aacute;", "Á" },
66 { "Acirc;", "Â" },
67 { "Agrave;", "À" },
68 { "Aring;", "Å" },
69 { "Atilde;", "Ã" },
70 { "Auml;", "Ä" },
71 { "Ccedil;", "Ç" },
72 { "Dagger;", "‡" },
73 { "ETH;", "Ð" },
74 { "Eacute;", "É" },
75 { "Ecirc;", "Ê" },
76 { "Egrave;", "È" },
77 { "Euml;", "Ë" },
78 { "Iacute;", "Í" },
79 { "Icirc;", "Î" },
80 { "Igrave;", "Ì" },
81 { "Iuml;", "Ï" },
82 { "Ntilde;", "Ñ" },
83 { "OElig;", "Œ" },
84 { "Oacute;", "Ó" },
85 { "Ocirc;", "Ô" },
86 { "Ograve;", "Ò" },
87 { "Oslash;", "Ø" },
88 { "Otilde;", "Õ" },
89 { "Ouml;", "Ö" },
90 { "Scaron;", "Š" },
91 { "THORN;", "Þ" },
92 { "Uacute;", "Ú" },
93 { "Ucirc;", "Û" },
94 { "Ugrave;", "Ù" },
95 { "Uuml;", "Ü" },
96 { "Yacute;", "Ý" },
97 { "Yuml;", "Ÿ" },
98 { "aacute;", "á" },
99 { "acirc;", "â" },
100 { "acute;", "´" },
101 { "aelig;", "æ" },
102 { "agrave;", "à" },
103 { "amp;", "&" },
104 { "apos;", "'" },
105 { "aring;", "å" },
106 { "atilde;", "ã" },
107 { "auml;", "ä" },
108 { "bdquo;", "„" },
109 { "brvbar;", "¦" },
110 { "ccedil;", "ç" },
111 { "cedil;", "¸" },
112 { "cent;", "¢" },
113 { "circ;", "ˆ" },
114 { "copy;", "©" },
115 { "curren;", "¤" },
116 { "dagger;", "†" },
117 { "deg;", "°" },
118 { "divide;", "÷" },
119 { "eacute;", "é" },
120 { "ecirc;", "ê" },
121 { "egrave;", "è" },
122 { "eth;", "ð" },
123 { "euml;", "ë" },
124 { "euro;", "€" },
125 { "frac12;", "½" },
126 { "frac14;", "¼" },
127 { "frac34;", "¾" },
128 { "gt;", ">" },
129 { "hellip;", "…" },
130 { "iacute;", "í" },
131 { "icirc;", "î" },
132 { "iexcl;", "¡" },
133 { "igrave;", "ì" },
134 { "iquest;", "¿" },
135 { "iuml;", "ï" },
136 { "laquo;", "«" },
137 { "ldquo;", "“" },
138 { "lsaquo;", "‹" },
139 { "lsquo;", "‘" },
140 { "lt;", "<" },
141 { "macr;", "¯" },
142 { "mdash;", "—" },
143 { "micro;", "µ" },
144 { "middot;", "·" },
145 { "nbsp;", "\xc2\xa0" },
146 { "ndash;", "–" },
147 { "not;", "¬" },
148 { "ntilde;", "ñ" },
149 { "oacute;", "ó" },
150 { "ocirc;", "ô" },
151 { "oelig;", "œ" },
152 { "ograve;", "ò" },
153 { "ordf;", "ª" },
154 { "ordm;", "º" },
155 { "oslash;", "ø" },
156 { "otilde;", "õ" },
157 { "ouml;", "ö" },
158 { "para;", "¶" },
159 { "permil;", "‰" },
160 { "plusmn;", "±" },
161 { "pound;", "£" },
162 { "quot;", "\"" },
163 { "raquo;", "»" },
164 { "rdquo;", "”" },
165 { "reg;", "®" },
166 { "rsaquo;", "›" },
167 { "rsquo;", "’" },
168 { "sbquo;", "‚" },
169 { "scaron;", "š" },
170 { "sect;", "§" },
171 { "shy;", "" },
172 { "sup1;", "¹" },
173 { "sup2;", "²" },
174 { "sup3;", "³" },
175 { "szlig;", "ß" },
176 { "thorn;", "þ" },
177 { "tilde;", "˜" },
178 { "times;", "×" },
179 { "trade;", "™" },
180 { "uacute;", "ú" },
181 { "ucirc;", "û" },
182 { "ugrave;", "ù" },
183 { "uml;", "¨" },
184 { "uuml;", "ü" },
185 { "yacute;", "ý" },
186 { "yen;", "¥" },
187 { "yuml;", "ÿ" },
188 };
189
cmp_entity(const void * key,const void * elem)190 static int cmp_entity (const void *key, const void *elem)
191 {
192 const struct xml_entity_s *ent = elem;
193 const char *name = key;
194
195 return strncmp (name, ent->psz_entity, strlen (ent->psz_entity));
196 }
197
vlc_xml_decode(char * psz_value)198 void vlc_xml_decode( char *psz_value )
199 {
200 char *p_pos = psz_value;
201
202 while ( *psz_value )
203 {
204 if( *psz_value == '&' )
205 {
206 if( psz_value[1] == '#' )
207 { /* &#DDD; or &#xHHHH; Unicode code point */
208 char *psz_end;
209 unsigned long cp;
210
211 if( psz_value[2] == 'x' ) /* The x must be lower-case. */
212 cp = strtoul( psz_value + 3, &psz_end, 16 );
213 else
214 cp = strtoul( psz_value + 2, &psz_end, 10 );
215
216 if( *psz_end == ';' )
217 {
218 psz_value = psz_end + 1;
219 if( cp == 0 )
220 (void)0; /* skip nulls */
221 else
222 if( cp <= 0x7F )
223 {
224 *p_pos = cp;
225 }
226 else
227 /* Unicode code point outside ASCII.
228 * &#xxx; representation is longer than UTF-8 :) */
229 if( cp <= 0x7FF )
230 {
231 *p_pos++ = 0xC0 | (cp >> 6);
232 *p_pos = 0x80 | (cp & 0x3F);
233 }
234 else
235 if( cp <= 0xFFFF )
236 {
237 *p_pos++ = 0xE0 | (cp >> 12);
238 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
239 *p_pos = 0x80 | (cp & 0x3F);
240 }
241 else
242 if( cp <= 0x1FFFFF ) /* Outside the BMP */
243 { /* Unicode stops at 10FFFF, but who cares? */
244 *p_pos++ = 0xF0 | (cp >> 18);
245 *p_pos++ = 0x80 | ((cp >> 12) & 0x3F);
246 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
247 *p_pos = 0x80 | (cp & 0x3F);
248 }
249 }
250 else
251 {
252 /* Invalid entity number */
253 *p_pos = *psz_value;
254 psz_value++;
255 }
256 }
257 else
258 { /* Well-known XML entity */
259 const struct xml_entity_s *ent;
260
261 ent = bsearch (psz_value + 1, xml_entities,
262 ARRAY_SIZE (xml_entities),
263 sizeof (*ent), cmp_entity);
264 if (ent != NULL)
265 {
266 size_t olen = strlen (ent->psz_char);
267 memcpy (p_pos, ent->psz_char, olen);
268 p_pos += olen - 1;
269 psz_value += strlen (ent->psz_entity) + 1;
270 }
271 else
272 { /* No match */
273 *p_pos = *psz_value;
274 psz_value++;
275 }
276 }
277 }
278 else
279 {
280 *p_pos = *psz_value;
281 psz_value++;
282 }
283
284 p_pos++;
285 }
286
287 *p_pos = '\0';
288 }
289
vlc_xml_encode(const char * str)290 char *vlc_xml_encode (const char *str)
291 {
292 struct vlc_memstream stream;
293 size_t n;
294 uint32_t cp;
295
296 assert(str != NULL);
297 vlc_memstream_open(&stream);
298
299 while ((n = vlc_towc (str, &cp)) != 0)
300 {
301 if (unlikely(n == (size_t)-1))
302 {
303 if (vlc_memstream_close(&stream) == 0)
304 free(stream.ptr);
305 errno = EILSEQ;
306 return NULL;
307 }
308
309 switch (cp)
310 {
311 case '\"':
312 vlc_memstream_puts(&stream, """);
313 break;
314 case '&':
315 vlc_memstream_puts(&stream, "&");
316 break;
317 case '\'':
318 vlc_memstream_puts(&stream, "'");
319 break;
320 case '<':
321 vlc_memstream_puts(&stream, "<");
322 break;
323 case '>':
324 vlc_memstream_puts(&stream, ">");
325 break;
326 default:
327 if (cp < 32) /* C0 code not allowed (except 9, 10 and 13) */
328 break;
329 if (cp >= 128 && cp < 160) /* C1 code encoded (except 133) */
330 {
331 vlc_memstream_printf(&stream, "&#%"PRIu32";", cp);
332 break;
333 }
334 /* fall through */
335 case 9:
336 case 10:
337 case 13:
338 case 133:
339 vlc_memstream_write(&stream, str, n);
340 break;
341 }
342 str += n;
343 }
344
345 if (vlc_memstream_close(&stream))
346 return NULL;
347 return stream.ptr;
348 }
349
350 /* Base64 encoding */
vlc_b64_encode_binary(const uint8_t * src,size_t i_src)351 char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src )
352 {
353 static const char b64[] =
354 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
355
356 char *ret = malloc( ( i_src + 4 ) * 4 / 3 );
357 char *dst = ret;
358
359 if( dst == NULL )
360 return NULL;
361
362 while( i_src > 0 )
363 {
364 /* pops (up to) 3 bytes of input, push 4 bytes */
365 uint32_t v;
366
367 /* 1/3 -> 1/4 */
368 v = ((unsigned)*src++) << 24;
369 *dst++ = b64[v >> 26];
370 v = v << 6;
371
372 /* 2/3 -> 2/4 */
373 if( i_src >= 2 )
374 v |= *src++ << 22;
375 *dst++ = b64[v >> 26];
376 v = v << 6;
377
378 /* 3/3 -> 3/4 */
379 if( i_src >= 3 )
380 v |= *src++ << 20; // 3/3
381 *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4
382 v = v << 6;
383
384 /* -> 4/4 */
385 *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4
386
387 if( i_src <= 3 )
388 break;
389 i_src -= 3;
390 }
391
392 *dst = '\0';
393
394 return ret;
395 }
396
vlc_b64_encode(const char * src)397 char *vlc_b64_encode( const char *src )
398 {
399 if( src )
400 return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) );
401 else
402 return vlc_b64_encode_binary( (const uint8_t*)"", 0 );
403 }
404
405 /* Base64 decoding */
vlc_b64_decode_binary_to_buffer(uint8_t * p_dst,size_t i_dst,const char * p_src)406 size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src )
407 {
408 static const int b64[256] = {
409 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
410 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
411 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
412 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
413 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
414 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
415 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
416 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
417 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
418 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
419 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
420 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
421 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
422 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
423 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
424 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
425 };
426 uint8_t *p_start = p_dst;
427 uint8_t *p = (uint8_t *)p_src;
428
429 int i_level;
430 int i_last;
431
432 for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
433 {
434 const int c = b64[(unsigned int)*p];
435 if( c == -1 )
436 break;
437
438 switch( i_level )
439 {
440 case 0:
441 i_level++;
442 break;
443 case 1:
444 *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 );
445 i_level++;
446 break;
447 case 2:
448 *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
449 i_level++;
450 break;
451 case 3:
452 *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c;
453 i_level = 0;
454 }
455 i_last = c;
456 }
457
458 return p_dst - p_start;
459 }
vlc_b64_decode_binary(uint8_t ** pp_dst,const char * psz_src)460 size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src )
461 {
462 const int i_src = strlen( psz_src );
463 uint8_t *p_dst;
464
465 *pp_dst = p_dst = malloc( i_src );
466 if( !p_dst )
467 return 0;
468 return vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src );
469 }
vlc_b64_decode(const char * psz_src)470 char *vlc_b64_decode( const char *psz_src )
471 {
472 const int i_src = strlen( psz_src );
473 char *p_dst = malloc( i_src + 1 );
474 size_t i_dst;
475 if( !p_dst )
476 return NULL;
477
478 i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src );
479 p_dst[i_dst] = '\0';
480
481 return p_dst;
482 }
483
vlc_strftime(const char * tformat)484 char *vlc_strftime( const char *tformat )
485 {
486 time_t curtime;
487 struct tm loctime;
488
489 if (strcmp (tformat, "") == 0)
490 return strdup (""); /* corner case w.r.t. strftime() return value */
491
492 /* Get the current time. */
493 time( &curtime );
494
495 /* Convert it to local time representation. */
496 localtime_r( &curtime, &loctime );
497 for (size_t buflen = strlen (tformat) + 32;; buflen += 32)
498 {
499 char *str = malloc (buflen);
500 if (str == NULL)
501 return NULL;
502
503 size_t len = strftime (str, buflen, tformat, &loctime);
504 if (len > 0)
505 {
506 char *ret = realloc (str, len + 1);
507 return ret ? ret : str; /* <- this cannot fail */
508 }
509 free (str);
510 }
511 vlc_assert_unreachable ();
512 }
513
write_duration(struct vlc_memstream * stream,int64_t duration)514 static void write_duration(struct vlc_memstream *stream, int64_t duration)
515 {
516 lldiv_t d;
517 long long sec;
518
519 duration /= CLOCK_FREQ;
520 d = lldiv(duration, 60);
521 sec = d.rem;
522 d = lldiv(d.quot, 60);
523 vlc_memstream_printf(stream, "%02lld:%02lld:%02lld", d.quot, d.rem, sec);
524 }
525
write_meta(struct vlc_memstream * stream,input_item_t * item,vlc_meta_type_t type)526 static int write_meta(struct vlc_memstream *stream, input_item_t *item,
527 vlc_meta_type_t type)
528 {
529 if (item == NULL)
530 return EOF;
531
532 char *value = input_item_GetMeta(item, type);
533 if (value == NULL)
534 return EOF;
535
536 vlc_memstream_puts(stream, value);
537 free(value);
538 return 0;
539 }
540
vlc_strfinput(input_thread_t * input,const char * s)541 char *vlc_strfinput(input_thread_t *input, const char *s)
542 {
543 struct vlc_memstream stream[1];
544
545 input_item_t *item = (input != NULL) ? input_GetItem(input) : NULL;
546
547 char c;
548 bool b_is_format = false;
549 bool b_empty_if_na = false;
550
551 assert(s != NULL);
552
553 vlc_memstream_open(stream);
554
555 while ((c = *s) != '\0')
556 {
557 s++;
558
559 if (!b_is_format)
560 {
561 if (c == '$')
562 {
563 b_is_format = true;
564 b_empty_if_na = false;
565 continue;
566 }
567
568 vlc_memstream_putc(stream, c);
569 continue;
570 }
571
572 b_is_format = false;
573
574 switch (c)
575 {
576 case 'a':
577 write_meta(stream, item, vlc_meta_Artist);
578 break;
579 case 'b':
580 write_meta(stream, item, vlc_meta_Album);
581 break;
582 case 'c':
583 write_meta(stream, item, vlc_meta_Copyright);
584 break;
585 case 'd':
586 write_meta(stream, item, vlc_meta_Description);
587 break;
588 case 'e':
589 write_meta(stream, item, vlc_meta_EncodedBy);
590 break;
591 case 'f':
592 if (item != NULL)
593 {
594 vlc_mutex_lock(&item->lock);
595 if (item->p_stats != NULL)
596 {
597 vlc_mutex_lock(&item->p_stats->lock);
598 vlc_memstream_printf(stream, "%"PRIi64,
599 item->p_stats->i_displayed_pictures);
600 vlc_mutex_unlock(&item->p_stats->lock);
601 }
602 else if (!b_empty_if_na)
603 vlc_memstream_putc(stream, '-');
604 vlc_mutex_unlock(&item->lock);
605 }
606 else if (!b_empty_if_na)
607 vlc_memstream_putc(stream, '-');
608 break;
609 case 'g':
610 write_meta(stream, item, vlc_meta_Genre);
611 break;
612 case 'l':
613 write_meta(stream, item, vlc_meta_Language);
614 break;
615 case 'n':
616 write_meta(stream, item, vlc_meta_TrackNumber);
617 break;
618 case 'o':
619 write_meta(stream, item, vlc_meta_TrackTotal);
620 break;
621 case 'p':
622 if (item == NULL)
623 break;
624 {
625 char *value = input_item_GetNowPlayingFb(item);
626 if (value == NULL)
627 break;
628
629 vlc_memstream_puts(stream, value);
630 free(value);
631 }
632 break;
633 case 'r':
634 write_meta(stream, item, vlc_meta_Rating);
635 break;
636 case 's':
637 {
638 char *lang = NULL;
639
640 if (input != NULL)
641 lang = var_GetNonEmptyString(input, "sub-language");
642 if (lang != NULL)
643 {
644 vlc_memstream_puts(stream, lang);
645 free(lang);
646 }
647 else if (!b_empty_if_na)
648 vlc_memstream_putc(stream, '-');
649 break;
650 }
651 case 't':
652 write_meta(stream, item, vlc_meta_Title);
653 break;
654 case 'u':
655 write_meta(stream, item, vlc_meta_URL);
656 break;
657 case 'A':
658 write_meta(stream, item, vlc_meta_Date);
659 break;
660 case 'B':
661 if (input != NULL)
662 vlc_memstream_printf(stream, "%"PRId64,
663 var_GetInteger(input, "bit-rate") / 1000);
664 else if (!b_empty_if_na)
665 vlc_memstream_putc(stream, '-');
666 break;
667 case 'C':
668 if (input != NULL)
669 vlc_memstream_printf(stream, "%"PRId64,
670 var_GetInteger(input, "chapter"));
671 else if (!b_empty_if_na)
672 vlc_memstream_putc(stream, '-');
673 break;
674 case 'D':
675 if (item != NULL)
676 write_duration(stream, input_item_GetDuration(item));
677 else if (!b_empty_if_na)
678 vlc_memstream_puts(stream, "--:--:--");
679 break;
680 case 'F':
681 if (item != NULL)
682 {
683 char *uri = input_item_GetURI(item);
684 if (uri != NULL)
685 {
686 vlc_memstream_puts(stream, uri);
687 free(uri);
688 }
689 }
690 break;
691 case 'I':
692 if (input != NULL)
693 vlc_memstream_printf(stream, "%"PRId64,
694 var_GetInteger(input, "title"));
695 else if (!b_empty_if_na)
696 vlc_memstream_putc(stream, '-');
697 break;
698 case 'L':
699 if (item != NULL)
700 {
701 assert(input != NULL);
702 write_duration(stream, input_item_GetDuration(item)
703 - var_GetInteger(input, "time"));
704 }
705 else if (!b_empty_if_na)
706 vlc_memstream_puts(stream, "--:--:--");
707 break;
708 case 'N':
709 if (item != NULL)
710 {
711 char *name = input_item_GetName(item);
712 if (name != NULL)
713 {
714 vlc_memstream_puts(stream, name);
715 free(name);
716 }
717 }
718 break;
719 case 'O':
720 {
721 char *lang = NULL;
722
723 if (input != NULL)
724 lang = var_GetNonEmptyString(input, "audio-language");
725 if (lang != NULL)
726 {
727 vlc_memstream_puts(stream, lang);
728 free(lang);
729 }
730 else if (!b_empty_if_na)
731 vlc_memstream_putc(stream, '-');
732 break;
733 }
734 case 'P':
735 if (input != NULL)
736 vlc_memstream_printf(stream, "%2.1f",
737 var_GetFloat(input, "position") * 100.f);
738 else if (!b_empty_if_na)
739 vlc_memstream_puts(stream, "--.-%");
740 break;
741 case 'R':
742 if (input != NULL)
743 vlc_memstream_printf(stream, "%.3f",
744 var_GetFloat(input, "rate"));
745 else if (!b_empty_if_na)
746 vlc_memstream_putc(stream, '-');
747 break;
748 case 'S':
749 if (input != NULL)
750 {
751 int rate = var_GetInteger(input, "sample-rate");
752 div_t dr = div((rate + 50) / 100, 10);
753
754 vlc_memstream_printf(stream, "%d.%01d", dr.quot, dr.rem);
755 }
756 else if (!b_empty_if_na)
757 vlc_memstream_putc(stream, '-');
758 break;
759 case 'T':
760 if (input != NULL)
761 write_duration(stream, var_GetInteger(input, "time"));
762 else if (!b_empty_if_na)
763 vlc_memstream_puts(stream, "--:--:--");
764 break;
765 case 'U':
766 write_meta(stream, item, vlc_meta_Publisher);
767 break;
768 case 'V':
769 {
770 float vol = 0.f;
771
772 if (input != NULL)
773 {
774 audio_output_t *aout = input_GetAout(input);
775 if (aout != NULL)
776 {
777 vol = aout_VolumeGet(aout);
778 vlc_object_release(aout);
779 }
780 }
781 if (vol >= 0.f)
782 vlc_memstream_printf(stream, "%ld", lroundf(vol * 256.f));
783 else if (!b_empty_if_na)
784 vlc_memstream_puts(stream, "---");
785 break;
786 }
787 case '_':
788 vlc_memstream_putc(stream, '\n');
789 break;
790 case 'Z':
791 if (item == NULL)
792 break;
793 {
794 char *value = input_item_GetNowPlayingFb(item);
795 if (value != NULL)
796 {
797 vlc_memstream_puts(stream, value);
798 free(value);
799 }
800 else
801 {
802 char *title = input_item_GetTitleFbName(item);
803
804 if (write_meta(stream, item, vlc_meta_Artist) >= 0
805 && title != NULL)
806 vlc_memstream_puts(stream, " - ");
807
808 if (title != NULL)
809 {
810 vlc_memstream_puts(stream, title);
811 free(title);
812 }
813 }
814 }
815 break;
816 case ' ':
817 b_empty_if_na = true;
818 b_is_format = true;
819 break;
820 default:
821 vlc_memstream_putc(stream, c);
822 break;
823 }
824 }
825
826 if (vlc_memstream_close(stream))
827 return NULL;
828 return stream->ptr;
829 }
830
vlc_filenamecmp(const char * a,const char * b)831 int vlc_filenamecmp(const char *a, const char *b)
832 {
833 size_t i;
834 char ca, cb;
835
836 /* Attempt to guess if the sorting algorithm should be alphabetic
837 * (i.e. collation) or numeric:
838 * - If the first mismatching characters are not both digits,
839 * then collation is the only option.
840 * - If one of the first mismatching characters is 0 and the other is also
841 * a digit, the comparands are probably left-padded numerical values.
842 * It does not matter which algorithm is used: the zero will be smaller
843 * than non-zero either way.
844 * - Otherwise, the comparands are numerical values, and might not be
845 * aligned (i.e. not same order of magnitude). If so, collation would
846 * fail. So numerical comparison is performed. */
847 for (i = 0; (ca = a[i]) == (cb = b[i]); i++)
848 if (ca == '\0')
849 return 0; /* strings are exactly identical */
850
851 if ((unsigned)(ca - '0') > 9 || (unsigned)(cb - '0') > 9)
852 return strcoll(a, b);
853
854 unsigned long long ua = strtoull(a + i, NULL, 10);
855 unsigned long long ub = strtoull(b + i, NULL, 10);
856
857 /* The number may be identical in two cases:
858 * - leading zero (e.g. "012" and "12")
859 * - overflow on both sides (#ULLONG_MAX) */
860 if (ua == ub)
861 return strcoll(a, b);
862
863 return (ua > ub) ? +1 : -1;
864 }
865
866 /**
867 * Sanitize a file name.
868 *
869 * Remove forbidden, potentially forbidden and otherwise evil characters from
870 * file names. That includes slashes, and popular characters like colon
871 * (on Unix anyway).
872 *
873 * \warning This function should only be used for automatically generated
874 * file names. Do not use this on full paths, only single file names without
875 * any directory separator!
876 */
filename_sanitize(char * str)877 void filename_sanitize( char *str )
878 {
879 unsigned char c;
880
881 /* Special file names, not allowed */
882 if( !strcmp( str, "." ) || !strcmp( str, ".." ) )
883 {
884 while( *str )
885 *(str++) = '_';
886 return;
887 }
888
889 /* On platforms not using UTF-8, VLC cannot access non-Unicode paths.
890 * Also, some file systems require Unicode file names.
891 * NOTE: This may inserts '?' thus is done replacing '?' with '_'. */
892 EnsureUTF8( str );
893
894 /* Avoid leading spaces to please Windows. */
895 while( (c = *str) != '\0' )
896 {
897 if( c != ' ' )
898 break;
899 *(str++) = '_';
900 }
901
902 char *start = str;
903
904 while( (c = *str) != '\0' )
905 {
906 /* Non-printable characters are not a good idea */
907 if( c < 32 )
908 *str = '_';
909 /* This is the list of characters not allowed by Microsoft.
910 * We also black-list them on Unix as they may be confusing, and are
911 * not supported by some file system types (notably CIFS). */
912 else if( strchr( "/:\\*\"?|<>", c ) != NULL )
913 *str = '_';
914 str++;
915 }
916
917 /* Avoid trailing spaces also to please Windows. */
918 while( str > start )
919 {
920 if( *(--str) != ' ' )
921 break;
922 *str = '_';
923 }
924 }
925