]> git.sesse.net Git - vlc/blob - modules/demux/nsc.c
Include vlc_plugin.h as needed
[vlc] / modules / demux / nsc.c
1 /*****************************************************************************
2  * nsc.c: NSC file demux and encoding decoder
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon@nanocrew.net>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
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.
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 General Public License for more details.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_playlist.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( _("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
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( **input ) == 0 )
81             return -1;
82
83         if( isdigit( **input ) == 0 )
84             *output = (toupper( **input ) - 7) * 16;
85         else
86             *output = **input * 16;
87
88         (*input)++;
89
90         if( isxdigit( **input ) == 0 )
91             return -1;
92
93         if( isdigit( **input ) == 0 )
94             *output |= toupper( **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
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     vlc_iconv_t conv;
152     size_t buf16_size;
153     unsigned char *buf16;
154     const char *p_buf16;
155     size_t buf8_size;
156     char *buf8;
157     char *p_buf8;
158
159     char *p_input = p_encoded;
160
161     if( strlen( p_input ) < 15 )
162     {
163         msg_Err( p_demux, "input string less than 15 characters" );
164         return NULL;
165     }
166
167     if( load_byte( 1, &encoding_type, &p_input, NULL, NULL ) )
168     {
169         msg_Err( p_demux, "unable to get NSC encoding type" );
170         return NULL;
171     }
172
173     if( encoding_type != 1 && encoding_type != 2 )
174     {
175         msg_Err( p_demux, "encoding type %d is not supported",
176                  encoding_type );
177         return NULL;
178     }
179
180     j = k = 0;
181
182     if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
183     {
184         msg_Err( p_demux, "load_byte failed" );
185         return NULL;
186     }
187
188     for( i = 0; i < 4; i++ )
189     {
190         if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
191         {
192             msg_Err( p_demux, "load_byte failed" );
193             return NULL;
194         }
195     }
196
197     length = 0;
198     for( i = 4; i; i-- )
199     {
200         if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
201         {
202             msg_Err( p_demux, "load_byte failed" );
203             return NULL;
204         }
205         length |= tmp << ((i - 1) * 8);
206     }
207
208     if( length == 0 )
209     {
210         msg_Err( p_demux, "Length is 0" );
211         return NULL;
212     }
213
214     buf16_size = length;
215     buf16 = malloc( buf16_size );
216     if( buf16 == NULL )
217     {
218         msg_Err( p_demux, "out of memory" );
219         return NULL;
220     }
221
222     for( i = 0; i < length; i++ )
223     {
224         if( load_byte( encoding_type, &buf16[ i ], &p_input, &j, &k ) )
225         {
226             msg_Err( p_demux, "load_byte failed" );
227             free( buf16 );
228             return NULL;
229         }
230     }
231
232     buf8_size = length;
233     buf8 = malloc( buf8_size + 1 );
234     if( buf8 == NULL )
235     {
236         msg_Err( p_demux, "out of memory" );
237         free( buf16 );
238         return NULL;
239     }
240
241     conv = vlc_iconv_open( "UTF-8", "UTF-16LE" );
242     if( conv == (vlc_iconv_t)(-1) )
243     {
244         msg_Err( p_demux, "iconv_open failed" );
245         free( buf16 );
246         free( buf8 );
247         return NULL;
248     }
249
250     p_buf8 = buf8;
251     p_buf16 = (const char *)buf16;
252
253     if( vlc_iconv( conv, &p_buf16, &buf16_size, &p_buf8, &buf8_size ) == (size_t)(-1) )
254     {
255         msg_Err( p_demux, "iconv failed" );
256         return NULL;
257     }
258     else
259     {
260         buf8[ length - buf8_size ] = '\0';
261     }
262
263     vlc_iconv_close( conv );
264
265     free( buf16 );
266     return buf8;
267 }
268
269 static int DemuxOpen( vlc_object_t * p_this )
270 {
271     demux_t *p_demux = (demux_t *)p_this;
272     const uint8_t *p_peek;
273     int i_size;
274
275     /* Lets check the content to see if this is a NSC file */
276     i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
277     i_size -= sizeof("NSC Format Version=") - 1;
278
279     if ( i_size > 0 )
280     {
281         while ( i_size && strncasecmp( (char *)p_peek, "NSC Format Version=",
282                                        (int) sizeof("NSC Format Version=") - 1 ) )
283         {
284             p_peek++;
285             i_size--;
286         }
287         if ( !strncasecmp( (char *)p_peek, "NSC Format Version=",
288                            (int) sizeof("NSC Format Version=") -1 ) )
289         {
290             p_demux->pf_demux = Demux;
291             p_demux->pf_control = Control;
292             return VLC_SUCCESS;
293         }
294     }
295     return VLC_EGENERIC;
296 }
297
298
299 /*****************************************************************************
300  * Deactivate: frees unused data
301  *****************************************************************************/
302 static void DemuxClose( vlc_object_t *p_this )
303 {
304     return;
305 }
306
307 static int ParseLine ( demux_t *p_demux, char *psz_line )
308 {
309     char        *psz_bol;
310     char        *psz_value;
311
312     psz_bol = psz_line;
313     /* Remove unnecessary tabs or spaces at the beginning of line */
314     while( *psz_bol == ' ' || *psz_bol == '\t' ||
315            *psz_bol == '\n' || *psz_bol == '\r' )
316     {
317         psz_bol++;
318     }
319     psz_value = strchr( psz_bol, '=' );
320     if( psz_value == NULL )
321     {
322         return 0; /* a [Address] or [Formats] line or something else we will ignore */
323     }
324     *psz_value = '\0';
325     psz_value++;
326  
327     if( !strncasecmp( psz_value, "0x", 2 ) )
328     {
329         int i_value;
330         sscanf( psz_value, "%x", &i_value );
331         msg_Dbg( p_demux, "%s = %d", psz_bol, i_value );
332     }
333     else if( !strncasecmp( psz_bol, "Format", 6 ) )
334     {
335         msg_Dbg( p_demux, "%s = asf header", psz_bol );
336     }
337     else
338     {
339         /* This should be NSC encoded strings in the values */
340         char *psz_out;
341         psz_out = nscdec( (vlc_object_t *)p_demux, psz_value );
342         if( psz_out )
343         {
344             msg_Dbg( p_demux, "%s = %s", psz_bol, psz_out );
345             free( psz_out );
346         }
347     }
348     return VLC_SUCCESS;
349 }
350
351 /*****************************************************************************
352  * Demux: reads and demuxes data packets
353  *****************************************************************************
354  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
355  *****************************************************************************/
356 static int Demux ( demux_t *p_demux )
357 {
358     char            *psz_line;
359
360     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
361     {
362         ParseLine( p_demux, psz_line );
363         free( psz_line );
364     }
365     return VLC_SUCCESS;
366 }
367
368 static int Control( demux_t *p_demux, int i_query, va_list args )
369 {
370     return VLC_EGENERIC;
371 }