Qt: do not show open options in both normal and advanced UI
[vlc.git] / modules / demux / nsc.c
blobc7b4ed6049a2bd37918080bb88a0e67764e8908f
1 /*****************************************************************************
2 * nsc.c: NSC file demux and encoding decoder
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
5 * $Id$
7 * Authors: Jon Lech Johansen <jon@nanocrew.net>
8 * Derk-Jan Hartman <hartman at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_charset.h>
37 #include <ctype.h>
38 #define MAX_LINE 16024
40 /*****************************************************************************
41 * Module descriptor
42 *****************************************************************************/
43 static int DemuxOpen ( vlc_object_t * );
44 static void DemuxClose ( vlc_object_t * );
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 ()
54 static int Demux ( demux_t *p_demux );
55 static int Control( demux_t *p_demux, int i_query, va_list args );
57 static const unsigned char inverse[ 128 ] =
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
72 static int load_byte( unsigned char encoding_type,
73 unsigned char *output, char **input,
74 unsigned char *j, unsigned char *k )
76 *output = 0;
78 if( encoding_type == 1 )
80 if( isxdigit( (unsigned char)**input ) == 0 )
81 return -1;
83 if( isdigit( (unsigned char)**input ) == 0 )
84 *output = (toupper( (unsigned char)**input ) - 7) * 16;
85 else
86 *output = **input * 16;
88 (*input)++;
90 if( isxdigit( (unsigned char)**input ) == 0 )
91 return -1;
93 if( isdigit( (unsigned char)**input ) == 0 )
94 *output |= toupper( (unsigned char)**input ) - 0x37;
95 else
96 *output |= **input - 0x30;
98 (*input)++;
100 else if( encoding_type == 2 )
102 unsigned char **uinput = (unsigned char **)input;
104 if( **uinput > 127 || inverse[ **uinput ] == 0xFF )
105 return -1;
107 if( *k == 0 )
109 if( (*uinput)[ 1 ] > 127 || inverse[ (*uinput)[ 1 ] ] == 0xFF )
110 return -1;
112 *output = (inverse[ (*uinput)[ 0 ] ] * 4) |
113 (inverse[ (*uinput)[ 1 ] ] / 16);
115 *j = inverse[ (*uinput)[ 1 ] ] * 16;
116 *k = 4;
118 (*uinput) += 2;
120 else if( *k == 2 )
122 *output = *j | inverse[ **uinput ];
124 *j = 0;
125 *k = 0;
127 (*uinput)++;
129 else if( *k == 4 )
131 *output = (inverse[ **uinput ] / 4) | *j;
133 *j = inverse[ **uinput ] * 64;
134 *k = 2;
136 (*uinput)++;
140 return 0;
143 static char *nscdec( vlc_object_t *p_demux, char* p_encoded )
145 unsigned int i;
146 unsigned char tmp;
147 unsigned char j, k;
148 unsigned int length;
149 unsigned char encoding_type;
151 unsigned char *buf16;
152 char *buf8;
154 char *p_input = p_encoded;
156 if( strlen( p_input ) < 15 )
158 msg_Err( p_demux, "input string less than 15 characters" );
159 return NULL;
162 if( load_byte( 1, &encoding_type, &p_input, NULL, NULL ) )
164 msg_Err( p_demux, "unable to get NSC encoding type" );
165 return NULL;
168 if( encoding_type != 1 && encoding_type != 2 )
170 msg_Err( p_demux, "encoding type %d is not supported",
171 encoding_type );
172 return NULL;
175 j = k = 0;
177 if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
179 msg_Err( p_demux, "load_byte failed" );
180 return NULL;
183 for( i = 0; i < 4; i++ )
185 if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
187 msg_Err( p_demux, "load_byte failed" );
188 return NULL;
192 length = 0;
193 for( i = 4; i; i-- )
195 if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
197 msg_Err( p_demux, "load_byte failed" );
198 return NULL;
200 length |= tmp << ((i - 1) * 8);
203 if( length == 0 )
205 msg_Err( p_demux, "Length is 0" );
206 return NULL;
209 buf16 = malloc( length );
210 if( buf16 == NULL )
211 return NULL;
213 for( i = 0; i < length; i++ )
215 if( load_byte( encoding_type, &buf16[ i ], &p_input, &j, &k ) )
217 msg_Err( p_demux, "load_byte failed" );
218 free( buf16 );
219 return NULL;
223 buf8 = FromCharset( "UTF-16LE", buf16, length );
224 free( buf16 );
225 if( buf8 == NULL )
227 msg_Err( p_demux, "iconv failed" );
228 return NULL;
230 return buf8;
233 static int DemuxOpen( vlc_object_t * p_this )
235 demux_t *p_demux = (demux_t *)p_this;
236 const uint8_t *p_peek;
237 int i_size;
239 /* Lets check the content to see if this is a NSC file */
240 i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
241 i_size -= sizeof("NSC Format Version=") - 1;
243 if ( i_size > 0 )
245 while ( i_size && strncasecmp( (char *)p_peek, "NSC Format Version=",
246 (int) sizeof("NSC Format Version=") - 1 ) )
248 p_peek++;
249 i_size--;
251 if ( !strncasecmp( (char *)p_peek, "NSC Format Version=",
252 (int) sizeof("NSC Format Version=") -1 ) )
254 p_demux->pf_demux = Demux;
255 p_demux->pf_control = Control;
256 return VLC_SUCCESS;
259 return VLC_EGENERIC;
263 /*****************************************************************************
264 * Deactivate: frees unused data
265 *****************************************************************************/
266 static void DemuxClose( vlc_object_t *p_this )
268 VLC_UNUSED( p_this );
269 return;
272 static int ParseLine ( demux_t *p_demux, char *psz_line )
274 char *psz_bol;
275 char *psz_value;
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' )
282 psz_bol++;
284 psz_value = strchr( psz_bol, '=' );
285 if( psz_value == NULL )
287 return 0; /* a [Address] or [Formats] line or something else we will ignore */
289 *psz_value = '\0';
290 psz_value++;
292 if( !strncasecmp( psz_value, "0x", 2 ) )
294 int i_value;
295 sscanf( psz_value, "%x", &i_value );
296 msg_Dbg( p_demux, "%s = %d", psz_bol, i_value );
298 else if( !strncasecmp( psz_bol, "Format", 6 ) )
300 msg_Dbg( p_demux, "%s = asf header", psz_bol );
302 else
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 )
309 msg_Dbg( p_demux, "%s = %s", psz_bol, psz_out );
310 free( psz_out );
313 return VLC_SUCCESS;
316 /*****************************************************************************
317 * Demux: reads and demuxes data packets
318 *****************************************************************************
319 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
320 *****************************************************************************/
321 static int Demux ( demux_t *p_demux )
323 char *psz_line;
325 while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
327 ParseLine( p_demux, psz_line );
328 free( psz_line );
330 return VLC_SUCCESS;
333 static int Control( demux_t *p_demux, int i_query, va_list args )
335 VLC_UNUSED( p_demux ); VLC_UNUSED( i_query ); VLC_UNUSED( args );
336 //FIXME
337 return VLC_EGENERIC;