/src/vlc/modules/codec/atsc_a65.c
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * atsc_a65.c : ATSC A65 decoding helpers |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2016 - VideoLAN Authors |
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 General Public License |
17 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | | *****************************************************************************/ |
19 | | |
20 | | #ifdef HAVE_CONFIG_H |
21 | | # include "config.h" |
22 | | #endif |
23 | | |
24 | | #include <vlc_common.h> |
25 | | #include <vlc_charset.h> |
26 | | |
27 | | #include "atsc_a65.h" |
28 | | |
29 | | enum |
30 | | { |
31 | | ATSC_A65_COMPRESSION_NONE = 0x00, |
32 | | ATSC_A65_COMPRESSION_HUFFMAN_C4C5 = 0x01, |
33 | | ATSC_A65_COMPRESSION_HUFFMAN_C6C7 = 0x02, |
34 | | ATSC_A65_COMPRESSION_RESERVED_FIRST = 0x03, |
35 | | ATSC_A65_COMPRESSION_RESERVED_LAST = 0xAF, |
36 | | ATSC_A65_COMPRESSION_OTHER_FIRST = 0xB0, |
37 | | ATSC_A65_COMPRESSION_OTHER_LAST = 0xFF, |
38 | | }; |
39 | | |
40 | | enum |
41 | | { |
42 | | ATSC_A65_MODE_UNICODE_RANGE_START = 0x00, /* See reserved ranges */ |
43 | | ATSC_A65_MODE_UNICODE_RANGE_END = 0x33, |
44 | | ATSC_A65_MODE_SCSU = 0x3E, |
45 | | ATSC_A65_MODE_UNICODE_UTF16 = 0x3F, |
46 | | ATSC_A65_MODE_TAIWAN_FIRST = 0x40, |
47 | | ATSC_A65_MODE_TAIWAN_LAST = 0x41, |
48 | | ATSC_A65_MODE_SOUTH_KOREA = 0x48, |
49 | | ATSC_A65_MODE_OTHER_FIRST = 0xE0, |
50 | | ATSC_A65_MODE_OTHER_LAST = 0xFE, |
51 | | ATSC_A65_MODE_NOT_APPLICABLE = 0xFF, |
52 | | }; |
53 | | |
54 | | const uint8_t ATSC_A65_MODE_RESERVED_RANGES[12] = { |
55 | | /* start, end */ |
56 | | 0x07, 0x08, |
57 | | 0x11, 0x1F, |
58 | | 0x28, 0x2F, |
59 | | 0x34, 0x3D, |
60 | | 0x42, 0x47, |
61 | | 0x49, 0xDF, |
62 | | }; |
63 | | |
64 | | struct atsc_a65_handle_t |
65 | | { |
66 | | char *psz_lang; |
67 | | vlc_iconv_t iconv_u16be; |
68 | | }; |
69 | | |
70 | | atsc_a65_handle_t *atsc_a65_handle_New( const char *psz_lang ) |
71 | 0 | { |
72 | 0 | atsc_a65_handle_t *p_handle = malloc( sizeof(*p_handle) ); |
73 | 0 | if( p_handle ) |
74 | 0 | { |
75 | 0 | if( psz_lang && strnlen(psz_lang, 2+1) > 2 ) |
76 | 0 | p_handle->psz_lang = strdup( psz_lang ); |
77 | 0 | else |
78 | 0 | p_handle->psz_lang = NULL; |
79 | |
|
80 | 0 | p_handle->iconv_u16be = NULL; |
81 | 0 | } |
82 | 0 | return p_handle; |
83 | 0 | } |
84 | | |
85 | | void atsc_a65_handle_Release( atsc_a65_handle_t *p_handle ) |
86 | 0 | { |
87 | 0 | if( p_handle->iconv_u16be ) |
88 | 0 | vlc_iconv_close( p_handle->iconv_u16be ); |
89 | 0 | free( p_handle->psz_lang ); |
90 | 0 | free( p_handle ); |
91 | 0 | } |
92 | | |
93 | | static char *enlarge_to16( const uint8_t *p_src, size_t i_src, uint8_t i_prefix ) |
94 | 0 | { |
95 | 0 | if( i_src == 0 ) |
96 | 0 | return NULL; |
97 | | |
98 | 0 | char *psz_new_allocated = malloc( i_src * 2 + 1 ); |
99 | 0 | char *psz_new = psz_new_allocated; |
100 | |
|
101 | 0 | if( psz_new ) |
102 | 0 | { |
103 | 0 | memset( psz_new, i_prefix, i_src * 2 ); |
104 | 0 | psz_new[ i_src * 2 ] = 0; |
105 | 0 | while( i_src-- ) |
106 | 0 | { |
107 | 0 | psz_new[1] = p_src[0]; |
108 | 0 | p_src++; |
109 | 0 | psz_new += 2; |
110 | 0 | } |
111 | 0 | } |
112 | 0 | return psz_new_allocated; |
113 | 0 | } |
114 | | |
115 | | static bool convert_encoding_set( atsc_a65_handle_t *p_handle, |
116 | | const uint8_t *p_src, size_t i_src, |
117 | | char **ppsz_merg, size_t *pi_mergmin1, |
118 | | uint8_t i_mode ) |
119 | 0 | { |
120 | 0 | char *psz_dest = *ppsz_merg; |
121 | 0 | size_t i_mergmin1 = *pi_mergmin1; |
122 | 0 | bool b_ret = true; |
123 | |
|
124 | 0 | if( i_src == 0 ) |
125 | 0 | return false; |
126 | | |
127 | | /* First exclude reserved ranges */ |
128 | 0 | for( unsigned i=0; i<12; i+=2 ) |
129 | 0 | { |
130 | 0 | if( i_mode >= ATSC_A65_MODE_RESERVED_RANGES[i] && |
131 | 0 | i_mode <= ATSC_A65_MODE_RESERVED_RANGES[i+1] ) |
132 | 0 | return false; |
133 | 0 | } |
134 | | |
135 | 0 | if( i_mode <= ATSC_A65_MODE_UNICODE_RANGE_END ) /* 8 range prefix + 8 */ |
136 | 0 | { |
137 | 0 | if( !p_handle->iconv_u16be ) |
138 | 0 | { |
139 | 0 | if ( !(p_handle->iconv_u16be = vlc_iconv_open("UTF-8", "UTF-16BE")) ) |
140 | 0 | return false; |
141 | 0 | } |
142 | 0 | else if ( VLC_ICONV_ERR == vlc_iconv( p_handle->iconv_u16be, NULL, NULL, NULL, NULL ) ) /* reset */ |
143 | 0 | { |
144 | 0 | return false; |
145 | 0 | } |
146 | | |
147 | 0 | char *psz16 = enlarge_to16( p_src, i_src, i_mode ); /* Maybe we can skip and feed iconv 2 by 2 */ |
148 | 0 | if( psz16 ) |
149 | 0 | { |
150 | 0 | char *psz_realloc = realloc( psz_dest, i_mergmin1 + (4 * i_src) + 1 ); |
151 | 0 | if( psz_realloc ) |
152 | 0 | { |
153 | 0 | const char *p_inbuf = psz16; |
154 | 0 | char *p_outbuf = &psz_realloc[i_mergmin1]; |
155 | 0 | const size_t i_outbuf_size = i_src * 4; |
156 | 0 | size_t i_inbuf_remain = i_src * 2; |
157 | 0 | size_t i_outbuf_remain = i_outbuf_size; |
158 | 0 | b_ret = ( VLC_ICONV_ERR != vlc_iconv( p_handle->iconv_u16be, &p_inbuf, &i_inbuf_remain, |
159 | 0 | &p_outbuf, &i_outbuf_remain ) ); |
160 | 0 | psz_dest = psz_realloc; |
161 | 0 | i_mergmin1 += (i_outbuf_size - i_outbuf_remain); |
162 | 0 | *p_outbuf = '\0'; |
163 | 0 | } |
164 | 0 | free( psz16 ); |
165 | 0 | } |
166 | 0 | else return false; |
167 | 0 | } |
168 | 0 | else |
169 | 0 | { |
170 | | /* Unsupported encodings */ |
171 | 0 | return false; |
172 | 0 | } |
173 | | |
174 | 0 | *ppsz_merg = psz_dest; |
175 | 0 | *pi_mergmin1 = i_mergmin1; |
176 | 0 | return b_ret; |
177 | 0 | } |
178 | | |
179 | 0 | #define BUF_ADVANCE(n) p_buffer += n; i_buffer -= n; |
180 | | |
181 | | char * atsc_a65_Decode_multiple_string( atsc_a65_handle_t *p_handle, const uint8_t *p_buffer, size_t i_buffer ) |
182 | 0 | { |
183 | 0 | char *psz_res = NULL; |
184 | 0 | size_t i_resmin1 = 0; |
185 | |
|
186 | 0 | if( i_buffer < 1 ) |
187 | 0 | return NULL; |
188 | | |
189 | 0 | uint8_t i_nb = p_buffer[0]; |
190 | 0 | BUF_ADVANCE(1); |
191 | |
|
192 | 0 | for( ; i_nb > 0; i_nb-- ) |
193 | 0 | { |
194 | 0 | if( i_buffer < 4 ) |
195 | 0 | goto error; |
196 | | |
197 | 0 | bool b_skip = ( p_handle->psz_lang && memcmp(p_buffer, p_handle->psz_lang, 3) ); |
198 | 0 | BUF_ADVANCE(3); |
199 | |
|
200 | 0 | uint8_t i_seg = p_buffer[0]; |
201 | 0 | BUF_ADVANCE(1); |
202 | 0 | for( ; i_seg > 0; i_seg-- ) |
203 | 0 | { |
204 | 0 | if( i_buffer < 3 ) |
205 | 0 | goto error; |
206 | | |
207 | 0 | const uint8_t i_compression = p_buffer[0]; |
208 | 0 | const uint8_t i_mode = p_buffer[1]; |
209 | 0 | const uint8_t i_bytes = p_buffer[2]; |
210 | 0 | BUF_ADVANCE(3); |
211 | |
|
212 | 0 | if( i_buffer < i_bytes ) |
213 | 0 | goto error; |
214 | | |
215 | 0 | if( i_compression != ATSC_A65_COMPRESSION_NONE ) // TBD |
216 | 0 | { |
217 | 0 | b_skip = true; |
218 | 0 | } |
219 | |
|
220 | 0 | if( !b_skip ) |
221 | 0 | { |
222 | 0 | (void) convert_encoding_set( p_handle, p_buffer, i_bytes, |
223 | 0 | &psz_res, &i_resmin1, i_mode ); |
224 | 0 | } |
225 | |
|
226 | 0 | BUF_ADVANCE(i_bytes); |
227 | 0 | } |
228 | 0 | } |
229 | | |
230 | 0 | return psz_res; |
231 | | |
232 | 0 | error: |
233 | 0 | free( psz_res ); |
234 | 0 | return NULL; |
235 | 0 | } |
236 | | |
237 | | #undef BUF_ADVANCE |
238 | | |
239 | | char * atsc_a65_Decode_simple_UTF16_string( atsc_a65_handle_t *p_handle, const uint8_t *p_buffer, size_t i_buffer ) |
240 | 0 | { |
241 | 0 | if( i_buffer < 1 ) |
242 | 0 | return NULL; |
243 | | |
244 | 0 | if( !p_handle->iconv_u16be ) |
245 | 0 | { |
246 | 0 | if ( !(p_handle->iconv_u16be = vlc_iconv_open("UTF-8", "UTF-16BE")) ) |
247 | 0 | return NULL; |
248 | 0 | } |
249 | 0 | else if ( VLC_ICONV_ERR == vlc_iconv( p_handle->iconv_u16be, NULL, NULL, NULL, NULL ) ) /* reset */ |
250 | 0 | { |
251 | 0 | return NULL; |
252 | 0 | } |
253 | | |
254 | 0 | const size_t i_target_buffer = i_buffer * 3 / 2; |
255 | 0 | size_t i_target_remaining = i_target_buffer; |
256 | 0 | const char *psz_toconvert = (const char *) p_buffer; |
257 | 0 | char *psz_converted_end; |
258 | 0 | char *psz_converted = psz_converted_end = malloc( i_target_buffer ); |
259 | |
|
260 | 0 | if( unlikely(!psz_converted) ) |
261 | 0 | return NULL; |
262 | | |
263 | 0 | if( VLC_ICONV_ERR == vlc_iconv( p_handle->iconv_u16be, &psz_toconvert, &i_buffer, |
264 | 0 | &psz_converted_end, &i_target_remaining ) ) |
265 | 0 | { |
266 | 0 | free( psz_converted ); |
267 | 0 | psz_converted = NULL; |
268 | 0 | } |
269 | 0 | else |
270 | 0 | psz_converted[ i_target_buffer - i_target_remaining - 1 ] = 0; |
271 | |
|
272 | 0 | return psz_converted; |
273 | 0 | } |