]> git.sesse.net Git - vlc/blob - modules/demux/nsc.c
Most of demux/
[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 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <ctype.h>
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32 #include <vlc_playlist.h>
33
34 #define MAX_LINE 16024
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  DemuxOpen  ( vlc_object_t * );
40 static void DemuxClose ( vlc_object_t * );
41
42 vlc_module_begin();
43     set_description( _("Windows Media NSC metademux") );
44     set_category( CAT_INPUT );
45     set_subcategory( SUBCAT_INPUT_DEMUX );
46     set_capability( "demux2", 2 );
47     set_callbacks( DemuxOpen, DemuxClose );
48 vlc_module_end();
49
50 static int Demux ( demux_t *p_demux );
51 static int Control( demux_t *p_demux, int i_query, va_list args );
52
53 static const unsigned char inverse[ 128 ] =
54 {
55     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
56     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
57     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
58     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
59     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF,
60     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
61     0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
62     0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
63     0xFF, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,
64     0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
65     0x3B, 0x3C, 0x3D, 0x3E, 0xFF, 0x3F, 0xFF, 0xFF
66 };
67
68 static int load_byte( unsigned char encoding_type,
69                       unsigned char *output, char **input,
70                       unsigned char *j, unsigned char *k )
71 {
72     *output = 0;
73
74     if( encoding_type == 1 )
75     {
76         if( isxdigit( **input ) == 0 )
77             return -1;
78
79         if( isdigit( **input ) == 0 )
80             *output = (toupper( **input ) - 7) * 16;
81         else
82             *output = **input * 16;
83
84         (*input)++;
85
86         if( isxdigit( **input ) == 0 )
87             return -1;
88
89         if( isdigit( **input ) == 0 )
90             *output |= toupper( **input ) - 0x37;
91         else
92             *output |= **input - 0x30;
93
94         (*input)++;
95     }
96     else if( encoding_type == 2 )
97     {
98         unsigned char **uinput = (unsigned char **)input;
99
100         if( **uinput > 127 || inverse[ **uinput ] == 0xFF )
101             return -1;
102
103         if( *k == 0 )
104         {
105             if( (*uinput)[ 1 ] > 127 || inverse[ (*uinput)[ 1 ] ] == 0xFF )
106                 return -1;
107
108             *output = (inverse[ (*uinput)[ 0 ] ] * 4) |
109                         (inverse[ (*uinput)[ 1 ] ] / 16);
110
111             *j = inverse[ (*uinput)[ 1 ] ] * 16;
112             *k = 4;
113
114             (*uinput) += 2;
115         }
116         else if( *k == 2 )
117         {
118             *output = *j | inverse[ **uinput ];
119
120             *j = 0;
121             *k = 0;
122
123             (*uinput)++;
124         }
125         else if( *k == 4 )
126         {
127             *output = (inverse[ **uinput ] / 4) | *j;
128
129             *j = inverse[ **uinput ] * 64;
130             *k = 2;
131
132             (*uinput)++;
133         }
134     }
135
136     return 0;
137 }
138
139 char *nscdec( vlc_object_t *p_demux, char* p_encoded )
140 {
141     unsigned int i;
142     unsigned char tmp;
143     unsigned char j, k;
144     unsigned int length;
145     unsigned char encoding_type;
146
147     vlc_iconv_t conv;
148     size_t buf16_size;
149     unsigned char *buf16;
150     const char *p_buf16;
151     size_t buf8_size;
152     char *buf8;
153     char *p_buf8;
154
155     char *p_input = p_encoded;
156
157     if( strlen( p_input ) < 15 )
158     {
159         msg_Err( p_demux, "input string less than 15 characters" );
160         return NULL;
161     }
162
163     if( load_byte( 1, &encoding_type, &p_input, NULL, NULL ) )
164     {
165         msg_Err( p_demux, "unable to get NSC encoding type" );
166         return NULL;
167     }
168
169     if( encoding_type != 1 && encoding_type != 2 )
170     {
171         msg_Err( p_demux, "encoding type %d is not supported",
172                  encoding_type );
173         return NULL;
174     }
175
176     j = k = 0;
177
178     if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
179     {
180         msg_Err( p_demux, "load_byte failed" );
181         return NULL;
182     }
183
184     for( i = 0; i < 4; i++ )
185     {
186         if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
187         {
188             msg_Err( p_demux, "load_byte failed" );
189             return NULL;
190         }
191     }
192
193     length = 0;
194     for( i = 4; i; i-- )
195     {
196         if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
197         {
198             msg_Err( p_demux, "load_byte failed" );
199             return NULL;
200         }
201         length |= tmp << ((i - 1) * 8);
202     }
203
204     if( length == 0 )
205     {
206         msg_Err( p_demux, "Length is 0" );
207         return NULL;
208     }
209
210     buf16_size = length;
211     buf16 = malloc( buf16_size );
212     if( buf16 == NULL )
213     {
214         msg_Err( p_demux, "out of memory" );
215         return NULL;
216     }
217
218     for( i = 0; i < length; i++ )
219     {
220         if( load_byte( encoding_type, &buf16[ i ], &p_input, &j, &k ) )
221         {
222             msg_Err( p_demux, "load_byte failed" );
223             free( buf16 );
224             return NULL;
225         }
226     }
227
228     buf8_size = length;
229     buf8 = malloc( buf8_size + 1 );
230     if( buf8 == NULL )
231     {
232         msg_Err( p_demux, "out of memory" );
233         free( buf16 );
234         return NULL;
235     }
236
237     conv = vlc_iconv_open( "UTF-8", "UTF-16LE" );
238     if( conv == (vlc_iconv_t)-1 )
239     {
240         msg_Err( p_demux, "iconv_open failed" );
241         free( buf16 );
242         free( buf8 );
243         return NULL;
244     }
245
246     p_buf8 = buf8;
247     p_buf16 = (const char *)buf16;
248
249     if( vlc_iconv( conv, &p_buf16, &buf16_size, &p_buf8, &buf8_size ) < 0 )
250     {
251         msg_Err( p_demux, "iconv failed" );
252         return NULL;
253     }
254     else
255     {
256         buf8[ length - buf8_size ] = '\0';
257     }
258
259     vlc_iconv_close( conv );
260
261     free( buf16 );
262     return buf8;
263 }
264
265 static int DemuxOpen( vlc_object_t * p_this )
266 {
267     demux_t *p_demux = (demux_t *)p_this;
268     byte_t *p_peek;
269     int i_size;
270
271     /* Lets check the content to see if this is a NSC file */
272     i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
273     i_size -= sizeof("NSC Format Version=") - 1;
274
275     if ( i_size > 0 )
276     {
277         while ( i_size && strncasecmp( (char *)p_peek, "NSC Format Version=",
278                                        (int) sizeof("NSC Format Version=") - 1 ) )
279         {
280             p_peek++;
281             i_size--;
282         }
283         if ( !strncasecmp( (char *)p_peek, "NSC Format Version=",
284                            (int) sizeof("NSC Format Version=") -1 ) )
285         {
286             p_demux->pf_demux = Demux;
287             p_demux->pf_control = Control;
288             return VLC_SUCCESS;
289         }
290     }
291     return VLC_EGENERIC;
292 }
293
294
295 /*****************************************************************************
296  * Deactivate: frees unused data
297  *****************************************************************************/
298 static void DemuxClose( vlc_object_t *p_this )
299 {
300     return;
301 }
302
303 static int ParseLine ( demux_t *p_demux, char *psz_line )
304 {
305     char        *psz_bol;
306     char        *psz_value;
307
308     psz_bol = psz_line;
309     /* Remove unnecessary tabs or spaces at the beginning of line */
310     while( *psz_bol == ' ' || *psz_bol == '\t' ||
311            *psz_bol == '\n' || *psz_bol == '\r' )
312     {
313         psz_bol++;
314     }
315     psz_value = strchr( psz_bol, '=' );
316     if( psz_value == NULL )
317     {
318         return 0; /* a [Address] or [Formats] line or something else we will ignore */
319     }
320     *psz_value = '\0';
321     psz_value++;
322     
323     if( !strncasecmp( psz_value, "0x", 2 ) )
324     {
325         int i_value;
326         sscanf( psz_value, "%x", &i_value );
327         msg_Dbg( p_demux, "%s = %d", psz_bol, i_value );
328     }
329     else if( !strncasecmp( psz_bol, "Format", 6 ) )
330     {
331         msg_Dbg( p_demux, "%s = asf header", psz_bol );
332     }
333     else
334     {
335         /* This should be NSC encoded strings in the values */
336         char *psz_out;
337         psz_out = nscdec( (vlc_object_t *)p_demux, psz_value );
338         if( psz_out )
339         {
340             msg_Dbg( p_demux, "%s = %s", psz_bol, psz_out );
341             if( psz_out) free( psz_out );
342         }
343     }
344     return VLC_SUCCESS;
345 }
346
347 /*****************************************************************************
348  * Demux: reads and demuxes data packets
349  *****************************************************************************
350  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
351  *****************************************************************************/
352 static int Demux ( demux_t *p_demux )
353 {
354     char            *psz_line;
355
356     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
357     {
358         ParseLine( p_demux, psz_line );
359         if( psz_line ) free( psz_line );
360     }
361     return VLC_SUCCESS;
362 }
363
364 static int Control( demux_t *p_demux, int i_query, va_list args )
365 {
366     return VLC_EGENERIC;
367 }