1 /*****************************************************************************
2 * nsc.c: NSC file demux and encoding decoder
3 *****************************************************************************
4 * Copyright (C) 2005 VLC authors and VideoLAN
5 * $Id: ba144816b34cdd0d9a62ad1378c12fb416d94599 $
6 *
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8 * based on work from Jon Lech Johansen <jon@nanocrew.net>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
24
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_charset.h>
36
37 #include <ctype.h>
38 #define MAX_LINE 16024
39
40 /*****************************************************************************
41 * Module descriptor
42 *****************************************************************************/
43 static int DemuxOpen ( vlc_object_t * );
44 static void DemuxClose ( vlc_object_t * );
45
46 vlc_module_begin ()
47 set_description( N_("Windows Media NSC metademux") )
48 set_category( CAT_INPUT )
49 set_subcategory( SUBCAT_INPUT_DEMUX )
50 set_capability( "demux", 3 )
51 set_callbacks( DemuxOpen, DemuxClose )
52 vlc_module_end ()
53
54 static int Demux ( demux_t *p_demux );
55 static int Control( demux_t *p_demux, int i_query, va_list args );
56
57 static const unsigned char inverse[ 128 ] =
58 {
59 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
60 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
61 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
62 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
63 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF,
64 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
65 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
66 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
67 0xFF, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,
68 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
69 0x3B, 0x3C, 0x3D, 0x3E, 0xFF, 0x3F, 0xFF, 0xFF
70 };
71
load_byte(unsigned char encoding_type,unsigned char * output,char ** input,unsigned char * j,unsigned char * k)72 static int load_byte( unsigned char encoding_type,
73 unsigned char *output, char **input,
74 unsigned char *j, unsigned char *k )
75 {
76 *output = 0;
77
78 if( encoding_type == 1 )
79 {
80 if( isxdigit( (unsigned char)**input ) == 0 )
81 return -1;
82
83 if( isdigit( (unsigned char)**input ) == 0 )
84 *output = (toupper( (unsigned char)**input ) - 7) * 16;
85 else
86 *output = **input * 16;
87
88 (*input)++;
89
90 if( isxdigit( (unsigned char)**input ) == 0 )
91 return -1;
92
93 if( isdigit( (unsigned char)**input ) == 0 )
94 *output |= toupper( (unsigned char)**input ) - 0x37;
95 else
96 *output |= **input - 0x30;
97
98 (*input)++;
99 }
100 else if( encoding_type == 2 )
101 {
102 unsigned char **uinput = (unsigned char **)input;
103
104 if( **uinput > 127 || inverse[ **uinput ] == 0xFF )
105 return -1;
106
107 if( *k == 0 )
108 {
109 if( (*uinput)[ 1 ] > 127 || inverse[ (*uinput)[ 1 ] ] == 0xFF )
110 return -1;
111
112 *output = (inverse[ (*uinput)[ 0 ] ] * 4) |
113 (inverse[ (*uinput)[ 1 ] ] / 16);
114
115 *j = inverse[ (*uinput)[ 1 ] ] * 16;
116 *k = 4;
117
118 (*uinput) += 2;
119 }
120 else if( *k == 2 )
121 {
122 *output = *j | inverse[ **uinput ];
123
124 *j = 0;
125 *k = 0;
126
127 (*uinput)++;
128 }
129 else if( *k == 4 )
130 {
131 *output = (inverse[ **uinput ] / 4) | *j;
132
133 *j = inverse[ **uinput ] * 64;
134 *k = 2;
135
136 (*uinput)++;
137 }
138 }
139
140 return 0;
141 }
142
nscdec(vlc_object_t * p_demux,char * p_encoded)143 static char *nscdec( vlc_object_t *p_demux, char* p_encoded )
144 {
145 unsigned int i;
146 unsigned char tmp;
147 unsigned char j, k;
148 unsigned int length;
149 unsigned char encoding_type;
150
151 unsigned char *buf16;
152 char *buf8;
153
154 char *p_input = p_encoded;
155
156 if( strlen( p_input ) < 15 )
157 {
158 msg_Err( p_demux, "input string less than 15 characters" );
159 return NULL;
160 }
161
162 if( load_byte( 1, &encoding_type, &p_input, NULL, NULL ) )
163 {
164 msg_Err( p_demux, "unable to get NSC encoding type" );
165 return NULL;
166 }
167
168 if( encoding_type != 1 && encoding_type != 2 )
169 {
170 msg_Err( p_demux, "encoding type %d is not supported",
171 encoding_type );
172 return NULL;
173 }
174
175 j = k = 0;
176
177 if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
178 {
179 msg_Err( p_demux, "load_byte failed" );
180 return NULL;
181 }
182
183 for( i = 0; i < 4; i++ )
184 {
185 if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
186 {
187 msg_Err( p_demux, "load_byte failed" );
188 return NULL;
189 }
190 }
191
192 length = 0;
193 for( i = 4; i; i-- )
194 {
195 if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
196 {
197 msg_Err( p_demux, "load_byte failed" );
198 return NULL;
199 }
200 length |= (unsigned int)tmp << ((i - 1) * 8);
201 }
202
203 if( length == 0 )
204 {
205 msg_Err( p_demux, "Length is 0" );
206 return NULL;
207 }
208
209 buf16 = malloc( length );
210 if( buf16 == NULL )
211 return NULL;
212
213 for( i = 0; i < length; i++ )
214 {
215 if( load_byte( encoding_type, &buf16[ i ], &p_input, &j, &k ) )
216 {
217 msg_Err( p_demux, "load_byte failed" );
218 free( buf16 );
219 return NULL;
220 }
221 }
222
223 buf8 = FromCharset( "UTF-16LE", buf16, length );
224 free( buf16 );
225 if( buf8 == NULL )
226 {
227 msg_Err( p_demux, "iconv failed" );
228 return NULL;
229 }
230 return buf8;
231 }
232
DemuxOpen(vlc_object_t * p_this)233 static int DemuxOpen( vlc_object_t * p_this )
234 {
235 demux_t *p_demux = (demux_t *)p_this;
236 const uint8_t *p_peek;
237 int i_size;
238
239 /* Lets check the content to see if this is a NSC file */
240 i_size = vlc_stream_Peek( p_demux->s, &p_peek, MAX_LINE );
241 i_size -= sizeof("NSC Format Version=") - 1;
242
243 if ( i_size > 0 )
244 {
245 while ( i_size && strncasecmp( (char *)p_peek, "NSC Format Version=",
246 (int) sizeof("NSC Format Version=") - 1 ) )
247 {
248 p_peek++;
249 i_size--;
250 }
251 if ( !strncasecmp( (char *)p_peek, "NSC Format Version=",
252 (int) sizeof("NSC Format Version=") -1 ) )
253 {
254 p_demux->pf_demux = Demux;
255 p_demux->pf_control = Control;
256 return VLC_SUCCESS;
257 }
258 }
259 return VLC_EGENERIC;
260 }
261
262
263 /*****************************************************************************
264 * Deactivate: frees unused data
265 *****************************************************************************/
DemuxClose(vlc_object_t * p_this)266 static void DemuxClose( vlc_object_t *p_this )
267 {
268 VLC_UNUSED( p_this );
269 return;
270 }
271
ParseLine(demux_t * p_demux,char * psz_line)272 static int ParseLine ( demux_t *p_demux, char *psz_line )
273 {
274 char *psz_bol;
275 char *psz_value;
276
277 psz_bol = psz_line;
278 /* Remove unnecessary tabs or spaces at the beginning of line */
279 while( *psz_bol == ' ' || *psz_bol == '\t' ||
280 *psz_bol == '\n' || *psz_bol == '\r' )
281 {
282 psz_bol++;
283 }
284 psz_value = strchr( psz_bol, '=' );
285 if( psz_value == NULL )
286 {
287 return 0; /* a [Address] or [Formats] line or something else we will ignore */
288 }
289 *psz_value = '\0';
290 psz_value++;
291
292 if( !strncasecmp( psz_value, "0x", 2 ) )
293 {
294 int i_value;
295 sscanf( psz_value, "%x", &i_value );
296 msg_Dbg( p_demux, "%s = %d", psz_bol, i_value );
297 }
298 else if( !strncasecmp( psz_bol, "Format", 6 ) )
299 {
300 msg_Dbg( p_demux, "%s = asf header", psz_bol );
301 }
302 else
303 {
304 /* This should be NSC encoded strings in the values */
305 char *psz_out;
306 psz_out = nscdec( (vlc_object_t *)p_demux, psz_value );
307 if( psz_out )
308 {
309 msg_Dbg( p_demux, "%s = %s", psz_bol, psz_out );
310 free( psz_out );
311 }
312 }
313 return VLC_SUCCESS;
314 }
315
316 /*****************************************************************************
317 * Demux: reads and demuxes data packets
318 *****************************************************************************
319 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
320 *****************************************************************************/
Demux(demux_t * p_demux)321 static int Demux ( demux_t *p_demux )
322 {
323 char *psz_line;
324
325 while( ( psz_line = vlc_stream_ReadLine( p_demux->s ) ) )
326 {
327 ParseLine( p_demux, psz_line );
328 free( psz_line );
329 }
330 return VLC_SUCCESS;
331 }
332
Control(demux_t * p_demux,int i_query,va_list args)333 static int Control( demux_t *p_demux, int i_query, va_list args )
334 {
335 VLC_UNUSED( p_demux ); VLC_UNUSED( i_query ); VLC_UNUSED( args );
336 //FIXME
337 return VLC_EGENERIC;
338 }
339