/src/vlc/modules/meta_engine/ID3Text.h
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * ID3Text.h : ID3v2 Text Helper |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2016 VLC authors and VideoLAN |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation; either version 2.1 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with this program; if not, write to the Free Software Foundation, |
18 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
19 | | *****************************************************************************/ |
20 | | #ifndef ID3TEXT_H |
21 | | #define ID3TEXT_H |
22 | | |
23 | | #include <vlc_charset.h> |
24 | | |
25 | | /* Text encoding byte of the ID3v2 frames */ |
26 | | enum |
27 | | { |
28 | | ID3_ENCODING_ISO_8859_1 = 0x00, // ISO-8859-1 |
29 | | ID3_ENCODING_UTF16 = 0x01, // UTF-16 with BOM |
30 | | ID3_ENCODING_UTF16BE = 0x02, // UTF-16BE without BOM, ID3v2.4 |
31 | | ID3_ENCODING_UTF8 = 0x03, // UTF-8, ID3v2.4 |
32 | | }; |
33 | | |
34 | | static const char * ID3TextConv( const uint8_t *p_buf, size_t i_buf, |
35 | | uint8_t i_charset, char **ppsz_allocated ) |
36 | 9 | { |
37 | 9 | char *p_alloc = NULL; |
38 | 9 | const char *psz = p_alloc; |
39 | 9 | if( i_buf > 0 && i_charset <= ID3_ENCODING_UTF8 ) |
40 | 9 | { |
41 | 9 | switch( i_charset ) |
42 | 9 | { |
43 | 0 | case ID3_ENCODING_ISO_8859_1: |
44 | 0 | psz = p_alloc = FromCharset( "ISO_8859-1", p_buf, i_buf ); |
45 | 0 | break; |
46 | 0 | case ID3_ENCODING_UTF16: |
47 | 0 | psz = p_alloc = FromCharset( "UTF-16", p_buf, i_buf ); |
48 | 0 | break; |
49 | 0 | case ID3_ENCODING_UTF16BE: |
50 | 0 | psz = p_alloc = FromCharset( "UTF-16BE", p_buf, i_buf ); |
51 | 0 | break; |
52 | 0 | default: |
53 | 9 | case ID3_ENCODING_UTF8: |
54 | 9 | if( p_buf[ i_buf - 1 ] != 0x00 ) |
55 | 4 | { |
56 | 4 | psz = p_alloc = (char *) malloc( i_buf + 1 ); |
57 | 4 | if( p_alloc ) |
58 | 4 | { |
59 | 4 | memcpy( p_alloc, p_buf, i_buf ); |
60 | 4 | p_alloc[i_buf] = '\0'; |
61 | 4 | } |
62 | 4 | } |
63 | 5 | else |
64 | 5 | { |
65 | 5 | psz = (const char *) p_buf; |
66 | 5 | } |
67 | 9 | break; |
68 | 9 | } |
69 | 9 | } |
70 | 9 | *ppsz_allocated = p_alloc; |
71 | 9 | return psz; |
72 | 9 | } Unexecuted instantiation: es.c:ID3TextConv Unexecuted instantiation: meta.c:ID3TextConv ts_metadata.c:ID3TextConv Line | Count | Source | 36 | 9 | { | 37 | 9 | char *p_alloc = NULL; | 38 | 9 | const char *psz = p_alloc; | 39 | 9 | if( i_buf > 0 && i_charset <= ID3_ENCODING_UTF8 ) | 40 | 9 | { | 41 | 9 | switch( i_charset ) | 42 | 9 | { | 43 | 0 | case ID3_ENCODING_ISO_8859_1: | 44 | 0 | psz = p_alloc = FromCharset( "ISO_8859-1", p_buf, i_buf ); | 45 | 0 | break; | 46 | 0 | case ID3_ENCODING_UTF16: | 47 | 0 | psz = p_alloc = FromCharset( "UTF-16", p_buf, i_buf ); | 48 | 0 | break; | 49 | 0 | case ID3_ENCODING_UTF16BE: | 50 | 0 | psz = p_alloc = FromCharset( "UTF-16BE", p_buf, i_buf ); | 51 | 0 | break; | 52 | 0 | default: | 53 | 9 | case ID3_ENCODING_UTF8: | 54 | 9 | if( p_buf[ i_buf - 1 ] != 0x00 ) | 55 | 4 | { | 56 | 4 | psz = p_alloc = (char *) malloc( i_buf + 1 ); | 57 | 4 | if( p_alloc ) | 58 | 4 | { | 59 | 4 | memcpy( p_alloc, p_buf, i_buf ); | 60 | 4 | p_alloc[i_buf] = '\0'; | 61 | 4 | } | 62 | 4 | } | 63 | 5 | else | 64 | 5 | { | 65 | 5 | psz = (const char *) p_buf; | 66 | 5 | } | 67 | 9 | break; | 68 | 9 | } | 69 | 9 | } | 70 | 9 | *ppsz_allocated = p_alloc; | 71 | 9 | return psz; | 72 | 9 | } |
|
73 | | |
74 | | static inline const char * ID3TextConvert( const uint8_t *p_buf, size_t i_buf, |
75 | | char **ppsz_allocated ) |
76 | 9 | { |
77 | 9 | if( i_buf == 0 ) |
78 | 0 | { |
79 | 0 | *ppsz_allocated = NULL; |
80 | 0 | return NULL; |
81 | 0 | } |
82 | 9 | return ID3TextConv( &p_buf[1], i_buf - 1, p_buf[0], ppsz_allocated ); |
83 | 9 | } Unexecuted instantiation: es.c:ID3TextConvert Unexecuted instantiation: meta.c:ID3TextConvert ts_metadata.c:ID3TextConvert Line | Count | Source | 76 | 9 | { | 77 | 9 | if( i_buf == 0 ) | 78 | 0 | { | 79 | 0 | *ppsz_allocated = NULL; | 80 | 0 | return NULL; | 81 | 0 | } | 82 | 9 | return ID3TextConv( &p_buf[1], i_buf - 1, p_buf[0], ppsz_allocated ); | 83 | 9 | } |
|
84 | | |
85 | | /* Size in bytes of the terminator of a string: UTF-16 strings end with a |
86 | | * null code unit */ |
87 | | static inline size_t ID3TextTerminatorSize( uint8_t i_charset ) |
88 | 0 | { |
89 | 0 | return ( i_charset == ID3_ENCODING_UTF16 || |
90 | 0 | i_charset == ID3_ENCODING_UTF16BE ) ? 2 : 1; |
91 | 0 | } Unexecuted instantiation: es.c:ID3TextTerminatorSize Unexecuted instantiation: meta.c:ID3TextTerminatorSize Unexecuted instantiation: ts_metadata.c:ID3TextTerminatorSize |
92 | | |
93 | | /* Length in bytes of the first string of the buffer, terminator excluded, |
94 | | * or -1 if it is not terminated */ |
95 | | static inline ssize_t ID3TextLength( const uint8_t *p_buf, size_t i_buf, |
96 | | uint8_t i_charset ) |
97 | 0 | { |
98 | 0 | if( ID3TextTerminatorSize( i_charset ) == 2 ) |
99 | 0 | { |
100 | 0 | for( size_t i = 0; i + 1 < i_buf; i += 2 ) |
101 | 0 | if( GetWBE( &p_buf[i] ) == 0 ) |
102 | 0 | return i; |
103 | 0 | return -1; |
104 | 0 | } |
105 | | |
106 | 0 | const size_t i_len = strnlen( (const char *) p_buf, i_buf ); |
107 | 0 | return i_len < i_buf ? (ssize_t) i_len : -1; |
108 | 0 | } Unexecuted instantiation: es.c:ID3TextLength Unexecuted instantiation: meta.c:ID3TextLength Unexecuted instantiation: ts_metadata.c:ID3TextLength |
109 | | |
110 | | #endif |